Restoring System Variable

Restoring System Variable

jesu.williams
Contributor Contributor
850 Views
6 Replies
Message 1 of 7

Restoring System Variable

jesu.williams
Contributor
Contributor

Hi,

 

Full disclosure I am a complete novice with writing LISP and I have probably made this harder than it needs to be. I have a simple routine that changes the layer, inserts a block and rotates it. What I want to do is store the ANGBASE before insertion and change it when I insert the block and then reset the ANGBASE to the original value at the end. For the life of me I cant get it to work as it doesnt reset the stored ANGBASE.

 

Any help would be greatly appreciated, here is what I have:

 

(defun c:iblock ()
(setq Old_ANGBASE (getvar "ANGBASE"))
(setvar "ANGBASE" 0)

(command "layer" "s" "L_Block" "")
(command "insert" "B_Block" pause "" "")
(setq pt (cdr (assoc 10 (entget (entlast)))))
(command "rotate" "L" "" pt )

(setvar "ANGBASE" Old_ANGBASE)
(princ)
)

0 Likes
851 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor

@jesu.williams wrote:

What I want to do is store the ANGBASE before insertion and change it when I insert the block and then reset the ANGBASE to the original value at the end. For the life of me I cant get it to work as it doesnt reset the stored ANGBASE.

 

Any help would be greatly appreciated, here is what I have:


If any part of the command is not executed properl, the codewill stop. and it will not continue and set the varaible back again. 

IT could be any of this:

 

(command "layer" "s" "L_Block" "")

 

If the layer does not exist it will error, better use "m"

 

(command "layer" "m" "L_Block" "")

 

and this

 

(command "insert" "B_Block" pause "" "")

 

if the block being inserted requires more than just the basepoint and rotation, it will show an error simialr to this

 

Specify rotation angle <0>: rotate
Requires valid numeric angle or second point.
; error: Function cancelled

 

basically "killing" the lisp

Try this ( regardless of the block is uniformed scaled or not)

(defun c:iblock	(/ )
  (setq Old_ANGBASE (getvar "ANGBASE"))
  (setvar "ANGBASE" 0)
  (command "layer" "m" "L_Block" "")
  (command "insert" "B_Block" pause )
  (while (> (getvar "CMDACTIVE") 0)
  	(command ""))
  (setq pt (cdr (assoc 10 (entget (entlast)))))
  (command "rotate" "L" "" pt)
    (setvar "ANGBASE" Old_ANGBASE)  
  (princ)
)

HTH

 

0 Likes
Message 3 of 7

jesu.williams
Contributor
Contributor

Thanks for the quick reply @pbejse but that didnt fix my problem. I probably explained it poorly to begin with, what im trying to resolve is some people in my company prefer to have their Base Angle set to East 0d0' and others North 270d0'. My routine of inserting the block on the right layer works when its set to East, it doesnt work properly when its set to North as the block is rotated by 90d. What I was trying to do was read in the Base Angle and store it and then change it to East for the block insertion and then revert back to the original Base Angle.

 

So when I tried your code on a drawing that was set to North, it inserted the block 90d, so its as if it didnt change it to East before inserting it. Hope that makes sense.

 

0 Likes
Message 4 of 7

Sea-Haven
Mentor
Mentor

This is possibly wrong also as I found out in another post needs to wait for a response either a drag or a value.

 

(command "rotate" "L" "" pt pause)

 

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor

@jesu.williams wrote:

So when I tried your code on a drawing that was set to North, it inserted the block 90d, so its as if it didnt change it to East before inserting it. Hope that makes sense.


Not sure what you are refering to but it restored the value of Angbase here on my end, post a sample drawing and let me see for myself.

 

 

 

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

@Sea-Haven wrote:

This is possibly wrong also as I found out in another post needs to wait for a response either a drag or a value.

 

(command "rotate" "L" "" pt pause)

 


There you go @jesu.williams Maybe that explains it. 

0 Likes
Message 7 of 7

ВeekeeCZ
Consultant
Consultant

You should always add the *error* function when changing the system variables. That will restore them in case of error or ESC ending. Note that *error* MUST be localized.

 

(defun c:iblock ( / *error* old_angbase old_clayer)  ; *error* must be localized!, the rest should be
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if old_angbase (setvar 'angbase old_angbase))
    (if old_clayer (setvar 'clayer old_clayer))
    (princ))
  
  ; -------------------------------------------------
  
  (if (tblsearch "block" "B_Block")

    (progn
      (setq old_angbase (getvar 'angbase)
	    old_clayer (getvar 'clayer))
      (setvar 'angbase 0)
      
      (command "_.layer" "_m" "L_Block" "")
      (command-s "_.insert" "B_Block" "_s" 1 "_r" 0 pause)
      (command-s "_.rotate" "_l" "" (getvar 'lastpoint))
      
      (setvar 'angbase old_angbase)
      (setvar 'clayer  old_clayer)
      )
    (princ "\nError: Block 'B_Block' not found in the drawing!"))
  
  (princ)
  )

 

0 Likes