Jump to content

Trouble with saving a variable to the registry and reading it to change block scale


Recommended Posts

Posted (edited)

I'm having an issue with the regScale variable in the code.

 

If the default is chosen by pressing return at this prompt there's an error:

<< Error: bad DXF group: (41 . "1") >>

 

But if I type a value it works.

 

The scale DXF group for 41, 42, and 43 require a decimal it seems so how can I fix that?

 

I'm missing something simple here (tried atof) so any help is greatly appreciated as always.

 

;; Convert points to a block
;; Mod by 3dwannab 2022.03.27

;; Issue with the regScale variable.
;; When the default is used it throws this error:
;; << Error: bad DXF group: (41 . "1") >>

;; I've tried 'atoi' but to no avail.

;; Original code here to convert points to blocks: https://www.3dcadforums.com/threads/lisp-to-convert-points-to-blocks.393/

(vl-load-com)

(defun c:Point_To_Block ( / *error* acDoc
 var_cmdecho
 bname
 elist
 ent
 entBlk
 i
 regScale
 ss
 )

(defun *error* (errmsg)
  (and acDoc (vla-EndUndoMark acDoc))
  (and errmsg
    (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
    (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
  (setvar 'cmdecho var_cmdecho)
  )

(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

(setq var_cmdecho (getvar "cmdecho"))
(setvar 'cmdecho 0)

;; Get the Block to replace the point with
(while (not (setq entBlk (entsel "\nSelect block to replace points: "))))
(setq bname (vla-get-effectivename (vlax-ename->vla-object (car entBlk))))

;; Get saved scale value from the registry or default to 1
(setq regScale (cond ((getenv "Point_To_Block")) (1)))

;; Prompt for integer value to scale to
(setq regScale (cond ((getint (strcat "\nEnter the scale of the new block :\nCurrent value <" (vl-princ-to-string regScale) ">: ")))
  (regScale)
  )
)

;; Set the registry value to the variable
(setenv "Point_To_Block" (vl-princ-to-string regScale))

;; There's a problem with the regScale variable when using the default
(princ "\n")
(princ regScale)
(princ "\n")

(if (and (tblobjname "BLOCK" bname)
  (setq ss (ssget '((0 . "POINT"))))
  )
(progn
  (setq i -1)
  (while (setq ent (ssname ss (setq i (1+ i))))
    (setq elist (entget ent))
    (entmake (list (cons 0 "INSERT")
      (cons 2 bname)
      (assoc 10 elist)
      (assoc 8 elist)
      (cons 41 regScale)
      (cons 42 regScale)
      (cons 43 regScale)
      (cons 50 0)
      )
    )
    (entdel ent)
    )
  )
) ;; end if

(*error* nil) (princ)

)

 

Edited by 3dwannab
  • 3dwannab changed the title to Trouble with saving a variable to the registry and reading it to change block scale
Posted (edited)

I've changed my prompt to use getreal.

 

This simple prompt works.

(initget 6)
(setq regScale (cond ((getreal "\nEnter scale factor:<1.0> "))
 (1.0)
 )
)

 

This one where I'm trying to save the value to the registry doesn't for some reason.

;; Get saved scale value from the registry or default to 1.0
(setq regScale (cond ((getenv "Point_To_Block")) (1.0)))

;; Prompt for value to scale to
(setq regScale (cond ((getreal (strcat "\nEnter the scale of the new block :\nCurrent value <" (vl-princ-to-string regScale) ">: ")))
  (regScale)
  )
)

;; Set the registry value to the variable
(setenv "Point_To_Block" (vl-princ-to-string regScale))

 

Edited by 3dwannab
Posted

Try this .. you need to save the getreal input to another variable:

;; Get saved scale value from the registry or default to 1.0
(setq regscale (cond ((getenv "Point_To_Block"))
		     ("1.0")
	       )
)
;; Prompt for value to scale to
(setq
  regscale (cond ((setq	scl (getreal (strcat "\nEnter the scale of the new block :\nCurrent value <"
					     regscale
					     ">: "
				     )
			    )
		  )
		  ;; Set the registry value to the variable
		  (setenv "Point_To_Block" (vl-princ-to-string scl))
		 )
		 (regscale)
	   )
)

 

Posted

Like ronjonp setenv uses string no matter, wether its a number or not. Its up to you to convert when required a set or get atof, atoi or rtos 

Posted

Thanks but still having problems.

 

Here's the full code and it doesn't work using ronjonp's method.

 

I've added (princ (type regScale)) in there to see what the regScale variable is and in both cases where I manually enter the value or if I chose the default it outputs STR but it always fails when I use the default value with the error in my first post.

 

;; Convert points to a block
;; Mod by 3dwannab 2022.03.27

;; Issue with the regScale variable.
;; When the default is used it throws this error:
;; << Error: bad DXF group: (41 . "1") >>

;; I've tried 'atoi' but to no avail.

;; Original code here to convert points to blocks: https://www.3dcadforums.com/threads/lisp-to-convert-points-to-blocks.393/

(vl-load-com)

(defun c:Point_To_Block ( / *error* acDoc
 var_cmdecho
 bname
 elist
 ent
 entBlk
 i
 regScale
 scl
 ss
 )

(defun *error* (errmsg)
  (and acDoc (vla-EndUndoMark acDoc))
  (and errmsg
    (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
    (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
  (setvar 'cmdecho var_cmdecho)
  )

(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

(setq var_cmdecho (getvar "cmdecho"))
(setvar 'cmdecho 0)

;; Get the Block to replace the point with
(while (not (setq entBlk (entsel "\nSelect block to replace points: "))))
(setq bname (vla-get-effectivename (vlax-ename->vla-object (car entBlk))))

;; There's a problem with the regScale variable when using the default
;; There's a problem with the regScale variable when using the default
;; There's a problem with the regScale variable when using the default

;; Get saved scale value from the registry or default to 1.0
(setq regScale (cond ((getenv "Point_To_Block"))
 ("1.0")
 )
)
;; Prompt for value to scale to
(setq
  regScale (cond ((setq scl (getreal (strcat "\nEnter the scale of the new block :\nCurrent value <"
   regScale
   ">: "
   )
  )
  )
  ;; Set the registry value to the variable
  (setenv "Point_To_Block" (vl-princ-to-string scl))
  )
  (regScale)
  )
  )

(princ "\n")
(princ (type regScale))
(princ "\n")

;; ;; Get saved scale value from the registry or default to 1.0
;; (setq regScale (cond ((getenv "Point_To_Block")) (1.0)))

;; ;; Prompt for value to scale to
;; (setq regScale (cond ((getreal (strcat "\nEnter the scale of the new block :\nCurrent value <" (vl-princ-to-string regScale) ">: ")))
;;   (regScale)
;;   )
;; )

;; ;; Set the registry value to the variable
;; (setenv "Point_To_Block" (vl-princ-to-string regScale))

;; (initget 6)
;; (setq regScale (cond ((getreal "\nEnter scale factor:<0.5> "))
;;  (0.5)
;;  )
;; )

(if (and (tblobjname "BLOCK" bname)
  (setq ss (ssget '((0 . "POINT"))))
  )
(progn
  (setq i -1)
  (while (setq ent (ssname ss (setq i (1+ i))))
    (setq elist (entget ent))
    (entmake (list (cons 0 "INSERT")
      (cons 2 bname)
      (assoc 10 elist)
      (assoc 8 elist)
      (cons 41 regScale)
      (cons 42 regScale)
      (cons 43 regScale)
      (cons 50 0)
      )
    )
    (entdel ent)
    )
  )
) ;; end if

(*error* nil) (princ)

)

 

Posted (edited)

This fixed it. Silly me again!! 🤦‍♂️

 

Changing:

(cons 41 regScale)
(cons 42 regScale)
(cons 43 regScale)

  

To:

(cons 41 (atof regScale))
(cons 42 (atof regScale))
(cons 43 (atof regScale))

 

Full working code:

;;
;; Convert points to a block
;; Mod by 3dwannab 2022.03.27
;;
;; Original code here to convert points to blocks: https://www.3dcadforums.com/threads/lisp-to-convert-points-to-blocks.393/

(vl-load-com)

(defun c:Point_To_Block ( / *error* acDoc
 var_cmdecho
 bname
 elist
 ent
 entBlk
 i
 regScale
 scl
 ss
 )

(defun *error* (errmsg)
  (and acDoc (vla-EndUndoMark acDoc))
  (and errmsg
    (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
    (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
  (setvar 'cmdecho var_cmdecho)
  )

(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

(setq var_cmdecho (getvar "cmdecho"))
(setvar 'cmdecho 0)

;; Get the Block to replace the point with
(while (not (setq entBlk (entsel "\nSelect block to replace points: "))))
(setq bname (vla-get-effectivename (vlax-ename->vla-object (car entBlk))))

;; Get saved scale value from the registry or default to 1.0
;; Help with this here: https://www.cadtutor.net/forum/topic/74687-trouble-with-saving-a-variable-to-the-registry-and-reading-it-to-change-block-scale/?do=findComment&comment=591582
(setq regScale (cond ((getenv "Point_To_Block"))
 ("1.0")
 )
)
;; Prompt for value to scale to
(setq
  regScale (cond ((setq scl (getreal (strcat "\nEnter the scale of the new block :\nCurrent value <"
   regScale
   ">: "
   )
  )
  )
  ;; Set the registry value to the variable
  (setenv "Point_To_Block" (vl-princ-to-string scl))
  )
  (regScale)
  )
  )

(if (and (tblobjname "BLOCK" bname)
  (setq ss (ssget '((0 . "POINT"))))
  )
(progn
  (setq i -1)
  (while (setq ent (ssname ss (setq i (1+ i))))
    (setq elist (entget ent))
    (entmake (list (cons 0 "INSERT")
      (cons 2 bname)
      (assoc 10 elist)
      (assoc 8 elist)
      (cons 41 (atof regScale))
      (cons 42 (atof regScale))
      (cons 43 (atof regScale))
      (cons 50 0)
      )
    )
    (entdel ent)
    )
  )
) ;; end if

(*error* nil) (princ)

)

 

Edited by 3dwannab
Posted (edited)

You could also convert it here too:

(setq
  regscale (atof
	     (cond ((setq scl (getreal (strcat "\nEnter the scale of the new block :\nCurrent value <"
					       regscale
					       ">: "
				       )
			      )
		    )
		    ;; Set the registry value to the variable
		    (setenv "Point_To_Block" (vl-princ-to-string scl))
		   )
		   (regscale)
	     )
	   )
)

 

Are you using the VLIDE to troubleshoot your problems?

Edited by ronjonp
  • Thanks 1
Posted
1 hour ago, ronjonp said:

Are you using the VLIDE to troubleshoot your problems?

 

Thanks for that. 

 

No, TBH I'm not too fond of it as an IDE.

 

I use Sublime text and drag and drop the file to AutoCAD and test that way.

 

I then use the up key in AutoCAD to go back to the load command to reload the file after any mods and run the command again.

Posted

The VLIDE is old school but the debugging it has is worth it. I bet it would save you some time troubleshooting.

  • Like 1
Posted

What i use  for  registry variables.

 

(or (setq *rad (getenv "FilletRadius")) (setq *rad "0.0625"))
(if (setq rad (getdist (strcat "\nFillet Radius [" *rad "]: ")))
  (progn
    (setenv "FilletRadius" (rtos rad 2))
    (setvar 'filletrad rad)
  )
  (setvar 'filletrad (setq rad (atof *rad)))
)

 

 

  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...