edit lisp for check layer exists, if not, create layer

edit lisp for check layer exists, if not, create layer

Anonymous
Not applicable
3,750 Views
13 Replies
Message 1 of 14

edit lisp for check layer exists, if not, create layer

Anonymous
Not applicable

This has been looked at many times but I have searched the forums and can not find a specific solution that will work.

 

Testing on AutoCAD 2016

 

 

Premise:

 

  Load Linetype from Linetype file,

  Check for layer,

  if found updated properties,

  if not found create..

 

Here's what we've got so far.  Needs QC checked because it doesn't function correctly.

 

(defun c:NGD ()
(setq echo (getvar "cmdecho")
(setvar "cmdecho" 0)
)
;if linetype exist, reload it
;(if (tblsearch "ltype" "NATURAL_GAS(DEMO)")
(command "-linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" "")

(if (tblsearch "layer" "C_1UDG_NGAS_PIPE_DEMO")
(command "clayer" "C_1UDG_NGAS_PIPE_DEMO" "")
)

(if (not (tblsearch "layer" "C_1UDG_NGAS_PIPE_DEMO"))
(command "_.LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "C_1UDG_NGAS_PIPE_DEMO" "")
)

(command "clayer" "C_1UDG_NGAS_PIPE_DEMO" "")
(command "graphscr")
(command "undo" "end")
(setvar "cmdecho" 1)
)

0 Likes
3,751 Views
13 Replies
Replies (13)
Message 2 of 14

Jonathan.Trostad
Advocate
Advocate

Hi,

 

So Just at first glance, you appear to be using the wrong "Layer" instead of "_.Layer", you need "-Layer" without knowing exactly what your error is, that will likely fix part of it.

 

Also, the IF function lets you pursue both Condition met and Condition not met. So part of your code could maybe look like this:

 

(if (tblsearch "layer" "C_1UDG_NGAS_PIPE_DEMO")
(setvar "clayer" "C_1UDG_NGAS_PIPE_DEMO")
(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "C_1UDG_NGAS_PIPE_DEMO" "") );end if

Hope That Helps!

 

Message 3 of 14

Anonymous
Not applicable

Thanks for the help!  Almost there...?...

 

(defun c:NGASD ()
(setq echo (getvar "cmdecho")
(setvar "cmdecho" 0)
)

 

;if linetype exist, reload it
(if (tblsearch "ltype" "NATURAL_GAS(DEMO)")
(command "-linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" "")
);end if

 

;if layer exist = make current, if not = make it
(if (tblsearch "layer" "C_1UDG_NGAS_PIPE_DEMO")
(setvar "clayer" "C_1UDG_NGAS_PIPE_DEMO")
(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "C_1UDG_NGAS_PIPE_DEMO" "")
);end if

 

(command "graphscr")
(command "undo" "end")
(setvar "cmdecho" 1)
)

0 Likes
Message 4 of 14

Kent1Cooper
Consultant
Consultant

I'm curious -- does your definition of that linetype change often, so that you would need to reload it every time  you go for that Layer?  If not, and reloading isn't really necessary every time, then all you need out of all that code is just this one (command) function:

 

(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "C_1UDG_NGAS_PIPE_DEMO" "")

 

The Make option makes it current, so you don't need to otherwise, and it doesn't care if the Layer already exists, so there's no need to check for that -- just Make it regardless.  And there's no need to load the linetype if it's not in the drawing yet, provided it's in a findable location -- when done in a Layer command, it will find it [unlike, for instance, if you make a Layer with (entmake), which does  require it to be already loaded].

 

Also, I'm at my pretty-old-AutoCAD location at the moment, which has Layer Descriptions in the dialog-box Layer Manager but not as an option in the command-line version, so I can't test this, but if assigning a Description to a Layer gives it to the current Layer by default, as it does with Color and Linetype and so on, then presumably you should need only this much:

 

(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "" "")

Kent Cooper, AIA
Message 5 of 14

Anonymous
Not applicable

Like most shops we're constantly updating our standards.  Linetypes are one of those things we seem to update and add to legacy maps, and by reloading a network .lin type helps get the look we're going for.

 

another syntax error...

 

(defun c:NGASD ()
(setq echo (getvar "cmdecho")
(setvar "cmdecho" 0)
)

 

;if linetype exist, reload it
(if (tblsearch "ltype" "NATURAL_GAS(DEMO)")
(command "-linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" "")
);end if

 

;if layer exist = make current, if not = make it
(if (tblsearch "layer" "C_1UDG_NGAS_PIPE_DEMO")
(setvar "clayer" "C_1UDG_NGAS_PIPE_DEMO")
(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "c" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "" "")
);end if

 

(command "graphscr")
(command "undo" "end")
(setvar "cmdecho" 1)
)

0 Likes
Message 6 of 14

Jonathan.Trostad
Advocate
Advocate

can you post the error? I don't have the droids...erm, linetype you are looking for, and it's hard to diagnose an issue without knowing what broke.

0 Likes
Message 7 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

another syntax error...

 

(defun c:NGASD ()
(setq echo (getvar "cmdecho")

....


That last quoted line is missing the right parenthesis at the end to finish the (setq) function.

Kent Cooper, AIA
Message 8 of 14

john.uhden
Mentor
Mentor

If the layer "X" exists, but it is frozen then neither

 

(setvar "clayer" "X")

nor

(command "-layer" "M" "X")

will work.

 

Rather

(command "-layer" "T" "X" "S" "X" "")

 

Actually, maybe it's off, so

(command "-layer" "T" "X" "On" "X" "S" "X" "")

John F. Uhden

Message 9 of 14

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

 

....

(command "-layer" "T" "X" "S" "X" "")

 

Actually, maybe it's off, so

(command "-layer" "T" "X" "On" "X" "S" "X" "")


Actually, that's another area where the command-line  version of Layer has an advantage over either the dialog box or (setvar 'clayer ...).  The Set option [and also the Make option] will turn it on, if it's off, so you don't need to; the first code line above is enough.

 

And by the way, you don't need the hyphen prefix when in an AutoLisp (command) function -- there, the default operation is command-line, so you don't need that to suppress the dialog box, as you would in a command macro.

 

And again, you don't need to check whether the Layer exists, but in case it does and might be Frozen, you can cover all bases with:

(command "_.layer" "T" "X" "M" "X" "")

 

[and color and linetype options, as desired].  It won't cause any trouble if the Layer doesn't exist yet when you ask it to Thaw it.  A message will go by that it didn't find such a Layer, but it will continue without error, and Make [and/or thereby Set] it.

Kent Cooper, AIA
Message 10 of 14

john.uhden
Mentor
Mentor

Thank you for providing a correcter answer.

John F. Uhden

0 Likes
Message 11 of 14

Anonymous
Not applicable

 

Rewrote it per your suggestions, it loads the linetype just fine (so i didn't attach it), creates the layer, assigns the properties, but it keeps hanging on the description the second time around asking if it should be replaced...

 

 

(defun NGASD ()
;
(setvar "cmdecho" 0)
)

;
(command "-linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" "")
;
(command "-LAYER" "T" "C_1UDG_NGAS_PIPE_DEMO" "")

;
(command "-LAYER" "On" "C_1UDG_NGAS_PIPE_DEMO" "")
;
(command "-LAYER" "M" "C_1UDG_NGAS_PIPE_DEMO" "C" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "" "")
;
(setvar "cmdecho" 1)

(princ)
)

0 Likes
Message 12 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

 

Rewrote it per your suggestions, it loads the linetype just fine (so i didn't attach it), creates the layer, assigns the properties, but it keeps hanging on the description the second time around asking if it should be replaced...

....


[Not quite all of my suggestions...]

 

I'm now at a newer version with Layer Descriptions among the options in the command-line version.  Since you can assign the same Description to more than one Layer, it doesn't default to the current one, as several other options do, and it needs the Layer name spelled out again.  If there already is a Description for that Layer, the default on whether to replace it is no.  A workaround would be to have that the last Layer option, and build in the "Yes" answer, and if the question is not asked [first time], a message will go by that Yes is an invalid option [which you won't see anyway, with command echoing suppressed], but it won't cause any error -- it will just continue to the final Enter "" that ends the Layer command:

 

....

(command ; can put all commands together into one (command) function

   "_.linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" ""
   "_.LAYER" ; can put all Layer operations together into one Layer command

     "T" "C_1UDG_NGAS_PIPE_DEMO"

     "M" "C_1UDG_NGAS_PIPE_DEMO" ; will set it current and  turn it On if it's Off

     "C" "79" ""

     "LT" "NATURAL_GAS(DEMO)" ""

     "D" "Natural Gas Pipe Existing" "NATURAL_GAS(DEMO)" "_yes"

     "" ; finish Layer command

); end command function

....

Kent Cooper, AIA
Message 13 of 14

Anonymous
Not applicable

Turns out it's erroring on the description portion first time through, requiring an additional "" (enter) to get past.  Second time through the "yes" works fine.

 

Anyway around this hiccup without removing the description portion all together?

 

(defun c:NGASD ()
(setvar "cmdecho" 0)
(command "_.linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin" "" "" "_.LAYER" "T" "C_1UDG_NGAS_PIPE_DEMO" "M" "C_1UDG_NGAS_PIPE_DEMO" "C" "79" "" "LT" "NATURAL_GAS(DEMO)" "" "D" "Natural Gas Pipe Existing" "C_1UDG_NGAS_PIPE_DEMO" "_yes" "" "")
(setvar "cmdecho" 1)
(princ))

 

0 Likes
Message 14 of 14

Kent1Cooper
Consultant
Consultant

I think it's actually not a Layer-command problem, but that you have too many Enters at the end of the Linetype-loading command for the first run.  It doesn't ask  whether to reload the linetype if it's not already in the drawing, but you have an answer to that question-that-may-not-arise built in.  If that's the issue, since reloading it [Yes] is the default  answer to that question if it does come up, it should be handleable this way [untested]:

 

(command "_.linetype" "load" "NATURAL_GAS(DEMO)" "X:/Line-Types/acad.lin"); leave you in the command

(while (> (getvar 'cmdactive) 0) (command "")); will give it Enters, however many are needed, until the command is done, and then  go on to:

(command "_.LAYER" ....

Kent Cooper, AIA
0 Likes