Visual LISP, AutoLISP and General Customization

Reply
Contributor
Draftsman13
Posts: 16
Registered: ‎12-09-2008
Message 1 of 9 (272 Views)
Accepted Solution

How to insert block on layer that is frozen

272 Views, 8 Replies
02-19-2013 03:16 PM

I am hoping some one can help.  I am still a beginner at writing lisp routines, but trying to learn.  We are using autocad 2010 and I have written a routine that will insert several blocks on to several layers.  I thought it worked flawlessly, until I used it.  Our drawings use layer states.  When one state is in use, all the other layers are frozen.  We want to insert a block, at a specific point chosen by the user, and the lisp routine will insert a couple of other blocks at that same point, but on a layer that is frozen.  Three blocks installed in all, at the same point, on two layers. The routine works if all layers are thawed, but not if the layers are frozen.  I have been beating my head on the desk trying to figure out my failure.  Below is the coding.  Sorry for the small novel.

 

(defun c:lP1 () ; this starts the routine

(setq temperr *error*) ; saves *error*
(setq *error* rerr) ; sets the new error variable
(command "_.Undo" "_mark") ; start of "undo" command

(graphscr)
(setvar "cmdecho" 0)
(setq oldos (getvar "osmode")) ; saves the current osnap settings
(setvar "osmode" 191) ; sets new osnap settings
(setq oldor (getvar "orthomode")) ; saves the current ortho setting
(setvar "orthomode" 1) ; turns the ortho on
(setq oldlayer (getvar "clayer")) ; saves the current layer

(command "_.-layer" "_make" "1st FL DESIGN NOTES" "_color" "9"
"" "_ltype" "continuous" "" "") ; updates the layer settings if newly created

(setq p1 (getpoint "\nSelect a point load! ")) ; asks user to select a point
(command "_.-insert" "1st Load" p1 "" "" "" "") ; inserts a block and asks user for input

(command "_.-layer" "_make" "found_notes" "_color" "1"
"" "_ltype" "continuous" "" "") ; updates the layer settings if newly created

(command "_.-insert" "fndn pt load" p1 "" "" "") : inserts a block on a different layer
(command "_.-insert" "fndn load nt" p1 "" "" "" "") : inserts a block on a different layer


(setvar "osmode" oldos) ; sets the osnaps to previous settings
(setvar "orthomode" oldor) ; restores the ortho to previous setting
(setvar "clayer" oldlayer) ; sets the current layer to previous setting
(setq *error* rerr) ; restores original error

(princ)
)

;;;----------------------------ERROR-----------------------------------------------------------------------------

(defun rerr (errmsg) ; defines error

(command "_.undo" "_back") ; removes any work done under this command
(setvar "osmode" oldos) ; sets the osnaps to previous settings
(setvar "orthomode" oldor) ; restores the ortho to previous setting
(setvar "clayer" oldlayer) ; sets the current layer to previous setting
(command "_.snapbase" "0,0") ; restores the snapbase to the original position
(setq *error* rerr) ; restores original error
(prompt "\nNow Restarting Your Computer ") ; prompts user

(princ)
) ; end error

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 2 of 9 (259 Views)

Re: How to insert block on layer that is frozen

02-19-2013 03:57 PM in reply to: Draftsman13

Draftsman13,
perhaps something like

 

(if (and (setq lay (tblsearch "LAYER" "1st FL DESIGN NOTES"));; if layer exists
    (= (logand 1 (cdr (assoc 70 lay))) 1));; and is frozen
    (command "_.LAYER" "_T" "1st FL DESIGN NOTES" "_set" "1st FL DESIGN NOTES" "_color" "9" "" "_ltype" "continuous" "" "")
    (command "_.-layer" "_make" "1st FL DESIGN NOTES" "_color" "_color" "9" "" "_ltype" "continuous" "" "")
    )
(setq p1 (getpoint "\nSelect a point load! "))
(command "_.-insert" "1st Load" p1 "" "" "" "")
(if (and (setq lay (tblsearch "LAYER" "found_notes"));; if layer exists
    (= (logand 1 (cdr (assoc 70 lay))) 1));; and is frozen
    (command "_.LAYER" "_T" "found_notes" "_set" "found_notes" "_color" "1""" "_ltype" "continuous" "" "")
    (command "_.-layer" "_make" "found_notes" "_color" "1" "" "_ltype" "continuous" "" "")
    )
(command "_.-insert" "fndn pt load" p1 "" "" "")
(command "_.-insert" "fndn load nt" p1 "" "" "" "")

 

hope that helps

Henrique

*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 3 of 9 (253 Views)

Re: How to insert block on layer that is frozen

02-19-2013 04:10 PM in reply to: Draftsman13

Either the Set or Make option in Layer will return an error if the Layer is frozen, because it can't be made current.  You have two choices, that I can think of:

 

A.  Save the Layer State, add a Thaw option applied to each Layer name before the Make option [it won't care if the Layer doesn't exist yet, but it will prevent the Make option from causing an error], Insert the Blocks, and restore the Layer State to re-freeze any of those Layers that was frozen when you started.  [You could do a more complicated similar thing, by checking each Layer for whether it's frozen, remember that, if so thaw it, Insert the Block, and freeze the Layer again if it was before.  But since you have Layer States defined anyway, you may as well use them.]

 

B.  Use (entmake) to put the Blocks in, rather than Insert in a (command) function.  I'm not in a place where I can confirm it at the moment, but I think it will be able to put a Block on a frozen Layer, without thawing it.

Kent Cooper
*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 4 of 9 (249 Views)

Re: How to insert block on layer that is frozen

02-19-2013 04:19 PM in reply to: hmsilva

For making and/or thawing a Layer using (command), it's not necessary to check whether it exists or is frozen.  You can just do:

 

(command "_.layer" "_thaw" "LayerName" "_make" "LayerName" "_color" ;; .... etc. ....)

 

It will not be troubled if the Layer doesn't yet exist.  A message will go by that it didn't find a Layer like that to thaw, but it will then just go back to the Layer sub=prompt and carry on.

 

However, in this kind of approach in this particular case, I think it would be necessary to first check whether it's frozen, which essentially does mean checking in the process whether it exists.  And the result of  that check needs to be saved, so that if the Layer was frozen initially, it can be re-frozen afterwards.

Kent Cooper
*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 5 of 9 (238 Views)

Re: How to insert block on layer that is frozen

02-19-2013 04:38 PM in reply to: Kent1Cooper

Kent1Cooper wrote:

...

However, in this kind of approach in this particular case, I think it would be necessary to first check whether it's frozen, which essentially does mean checking in the process whether it exists.  And the result of  that check needs to be saved, so that if the Layer was frozen initially, it can be re-frozen afterwards

 

I fully agree, is an improvement to be considered, as in the original code, was not a concern to restore the previous layer state, I did not think in that solution.

 

Henrique

Contributor
Draftsman13
Posts: 16
Registered: ‎12-09-2008
Message 6 of 9 (199 Views)

Re: How to insert block on layer that is frozen

02-21-2013 07:34 AM in reply to: Kent1Cooper

Kent1Cooper wrote:

 

However, in this kind of approach in this particular case, I think it would be necessary to first check whether it's frozen, which essentially does mean checking in the process whether it exists.  And the result of  that check needs to be saved, so that if the Layer was frozen initially, it can be re-frozen afterwards.


We use a drawing template, so usually these layers exist in the drawing.  Therefore I do not need to check if the layer exists, but it would be advisable to check if the layer is frozen.  Question is how to save the result?  I would need to write a statement like

"if layer is frozen, then setvar X (get layer and thaw)" ?

 

My codes utilize a lot of commands because I test the result in autocad as I am writing the code.  It's the only way I know to achieve my results.

*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 7 of 9 (191 Views)

Re: How to insert block on layer that is frozen

02-21-2013 10:13 AM in reply to: Draftsman13

Draftsman13 wrote:
....

We use a drawing template, so usually these layers exist in the drawing.  Therefore I do not need to check if the layer exists, but it would be advisable to check if the layer is frozen.  Question is how to save the result?  I would need to write a statement like

"if layer is frozen, then setvar X (get layer and thaw)" ?

....


You can save something into a variable about whether the Layer is frozen [there are several ways to find out].  You could use that result to decide whether to thaw it and then re-freeze it later, but I have a simpler suggestion that you can do by using the "Freeze" property of the Layer as a VLA object.  Read that value, thaw the Layer regardless, do your thing, and then re-assign the original value, whether or not it was changed by thawing:

 

(setq

  LayObj (vlax-ename->vla-object (tblobjname "layer" "YourLayerName")); Layer as VLA object

  FT (vlax-get layobj 'Freeze); = Freeze/Thaw state value [0 = thawed, -1 = frozen]

); setq

(vlax-put LayObj 'Freeze 0); thaws Layer if it's frozen, or leaves thawed if not

; .... do what you want to do ....

(vlax-put LayObj 'Freeze FT); re-freezes if it was frozen, or leaves thawed if not

 

No (if) or (command) functions involved, and it's simpler than using (logand) on the (assoc 70) value of the Layer's entity data to test whether it's frozen.

Kent Cooper
Mentor
coopdetat
Posts: 195
Registered: ‎03-25-2011
Message 8 of 9 (169 Views)

Re: How to insert block on layer that is frozen

02-22-2013 07:05 AM in reply to: Kent1Cooper

Kent,

Entmake can create the block on a frozen layer; however, I believe it will give an alert that the current layer is frozen.  I have a routine that does it but I don't remember offhand whether layer manipulation within the routine throws the alert or the block creation does.  In any case ENTMAKE can successfully create a block (an INSERT) on a frozen layer.

---------------------------------------------Signature--------------------------------------------
Civil Design Professional Since 1983 (Intergraph), AutoCAD since 1989

It is said that a fool's voice is known by multitude of words... I'm
just writing this stuff instead of saying it outloud... that's my loophole.

Windows 7 Professional 64-bit - Service Pack 1
Intel﴾R﴿ Core﴾TM﴿ i7-3820 CPU 3.60GHz; 16 GB DDR3 Dual Channel RAM
nVidia Quadro 4000; AutoCAD Civil 3D 2013, sp1
Contributor
Draftsman13
Posts: 16
Registered: ‎12-09-2008
Message 9 of 9 (120 Views)

Re: How to insert block on layer that is frozen

03-19-2013 10:17 AM in reply to: Kent1Cooper

Kent1Cooper wrote:

 

A.  Save the Layer State, add a Thaw option applied to each Layer name before the Make option [it won't care if the Layer doesn't exist yet, but it will prevent the Make option from causing an error], Insert the Blocks, and restore the Layer State to re-freeze any of those Layers that was frozen when you started.  

 

Kent,

I went with option A.  I decided I did not know near enough to go with entmake or the vla stuff.  I also modified my code to not be so generic.  For my purposes, I know that the layers in question are in the file and frozen.  This allowed me to utilize the "set" function and get rid of the "make" function, thereby reducing the amount of coding.  Below is my final code.  I have already run tests to ensure it functions properly and will begin having my drafters test it as well.  Thank you for pointing me in the right direction.  I hope i can call on you in the future with more questions.

 

(defun c:lP1 () ; this starts the routine

(setq temperr *error*) ; saves *error*
(setq *error* rerr) ; sets the new error variable
(command "_.Undo" "_mark") ; start of "undo" command

(graphscr)
(setvar "cmdecho" 0)
(setq oldos (getvar "osmode")) ; saves the current osnap settings
(setvar "osmode" 0) ; sets new osnap settings
(setq oldor (getvar "orthomode")) ; saves the current ortho setting
(setvar "orthomode" 1) ; turns the ortho on
(setq oldlayer (getvar "clayer")) ; saves the current layer

(command "_.-layer" "_set" "1ST FL DESIGN NOTES" "") ; updates the current layer

(setq p1 (getpoint "\nSelect a point load! ")) ; asks user to select a point
(command "_.-insert" "1st Load" p1 "" "" "" "") ; inserts a block and asks user for input

(command "_.-layer" "thaw" "FOUND_NOTES" "_set" "FOUND_NOTES" "") ; updates the current layer

(command "_.-insert" "fndn pt load" p1 "" "" "") ; inserts a block on a different layer
(command "_.-insert" "fndn load nt" p1 "" "" "" "") ; inserts a block on a different layer

(setvar "osmode" oldos) ; sets the osnaps to previous settings
(setvar "orthomode" oldor) ; restores the ortho to previous setting
(setvar "clayer" oldlayer) ; sets the current layer to previous setting

(command "_.-layer" "freeze" "FOUND_NOTES" "" ) ; freezes last used layer

(setq *error* rerr) ; restores original error

(princ)
) ; ends the command

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community