Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Writing LISP to load layers with Description

7 REPLIES 7
Reply
Message 1 of 8
perkins76
1453 Views, 7 Replies

Writing LISP to load layers with Description

I need some help with my LISP.

 

I am trying to load all of our standard layers to include the layer name, color, linetype and description.  The routine works the first time it is run, but if someone re-runs it (i.e. they delete a layer, etc.) the promts change to add in the despription... after the 1st time it asks if you want to replace the current description.  I cannot figure out how to make this work both the first time (w/o the extra promt) and the 2nd time (w/ the additional prompt.Any help would be appreciated.  

 

Here is a sample of the code.

 

(defun c:FPlayers ()

(command "CMDECHO" "1")(terpri) (prompt "Setting layers")(princ)

(command "layer" "unlock" "*" "thaw" "*" "on" "*" "")

(command "layer" "m" "0" "c" "7" "" "l" "Continuous" "" "")

(command "layer" "m" "A-COLS" "c" "yellow" "" "l" "Continuous" "" "")

(command "layer" "d" "Columns" "A-COLS" "")

(command "layer" "m" "A-COLS-GRID" "c" "40" "" "l" "CENTER2" "" "")

(command "layer" "d" "Column Grid" "A-COLS-GRID" "") 

(command "layer" "s" "0" "")
(command "CMDECHO" "1")
(prompt "All layers are set")
(princ)
);end defun

7 REPLIES 7
Message 2 of 8
rkmcswain
in reply to: perkins76

IMO, this is exactly the type of situation where the sysvar EXPERT should be honored, but I guess today's current "-layer" programmers didn't know it existed or chose to ignore it for some reason.  You can use this function instead.

(defun foo (layer desc)
  (vla-put-description
    (vlax-ename->vla-object
      (tblobjname "Layer" layer)
    )
 desc)
)

;;; use like this

(foo "0" "My Desc")

 

 



R.K. McSwain     | CADpanacea | on twitter
Message 3 of 8
perkins76
in reply to: rkmcswain

 

Message 4 of 8
rkmcswain
in reply to: perkins76

Include the function (defun foo.....) inside your lisp file.
You can rename the "foo" to something else if you like.

At the point in your other code where you want to set the description, then call the "foo" function like this:

(foo "layer_name" "description")

"layer_name" is the layer you want to modify
"description" is the description you want to use

Both arguments must be strings

Lastly, there is no error checking so if you pass this function a layer that does not exist, or a description that is not a string, etc., it will fail.

R.K. McSwain     | CADpanacea | on twitter
Message 5 of 8
Kent1Cooper
in reply to: perkins76

I'm not at a station with a new-enough version to have the Description option in the Layer command, but when it asks whether you want to replace the Description, is the default option "Yes"?  If so, I can suggest another way to do what you're after.

Kent Cooper, AIA
Message 6 of 8
rkmcswain
in reply to: Kent1Cooper

The default is "N" on 2015.

 

Command: -LAYER
Current layer:  "0"
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: d
Enter layer description: test
Enter name list of layer(s) to apply description or <select objects>:  <*>: 0
Replace existing description(s)? [Yes/No]:? <N> y
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: 
Command:

 

 

R.K. McSwain     | CADpanacea | on twitter
Message 7 of 8
Kent1Cooper
in reply to: perkins76

By the way, ignoring the replace-the-description problem, you are apparently unaware of the fact that you can do a whole batch of Layer option operations within one Layer command, so you don't need to end it and get back into it all those times:

 

(command "layer"

  "unlock" "*" "thaw" "*" "on" "*"

  "m" "0" "c" "7" "" "l" "Continuous" ""

  "m" "A-COLS" "c" "yellow" "" "l" "Continuous" ""

  "d" "Columns" "A-COLS"

  "m" "A-COLS-GRID" "c" "40" "" "l" "CENTER2" ""

  "d" "Column Grid" "A-COLS-GRID"

  "s" "0

  "" ; terminate the one collective Layer command

)

 

If the default on replacing a Description is Yes, the other approach I will suggest will require breaking that into a few pieces, but most of it can remain in one Layer command.

Kent Cooper, AIA
Message 8 of 8
Kent1Cooper
in reply to: rkmcswain


@rkmcswain wrote:

The default is "N" on 2015.

 

....Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
....

In that case, here's a really cheap trick you can try:  Since Yes is not an option in the "base" Layer sub-prompt, you can supply that as an answer to the mere possibility that it might ask that question.  In the individual-Layer-commands approach:

 

(command "layer" "d" "Columns" "A-COLS" "_yes" "")

 

If there's no description for it to ask whether you want to replace, an "Invalid option keyword" message will go by, but it will then just go back to the base prompt and carry on, in the above case to the Enter that closes the Layer command.

 

But that also has the advantage over the other suggestion I was going to make [if the default was Yes], that it ought to work in an overall-combined-into-one-Layer-command approach:

 

(command "layer"

  "unlock" "*" "thaw" "*" "on" "*"

  "m" "0" "c" "7" "" "l" "Continuous" ""

  "m" "A-COLS" "c" "yellow" "" "l" "Continuous" ""

  "d" "Columns" "A-COLS" "_yes"

  "m" "A-COLS-GRID" "c" "40" "" "l" "CENTER2" ""

  "d" "Column Grid" "A-COLS-GRID" "_yes"

  "s" "0

  "" ; terminate the one collective Layer command

)

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost