PDA

View Full Version : Adding a layer description through command line


LanceMcHatton
2005-06-01, 07:05 PM
Does anyone know if it's possible to add a layer description thought the command line (2005)? I have lisps that create layers per our company standard but they get created via the command line and it doesn't look like there's a description option listed there.

It would be real nice to be able to just hit a button and have all the right layers there with all the right descriptions. One outta two ain't bad, I guess. :)

Perhaps there's another way to do it with a different code?

msretenovic
2005-06-01, 09:57 PM
Does anyone know if it's possible to add a layer description thought the command line (2005)? I have lisps that create layers per our company standard but they get created via the command line and it doesn't look like there's a description option listed there.

It would be real nice to be able to just hit a button and have all the right layers there with all the right descriptions. One outta two ain't bad, I guess. :)

Perhaps there's another way to do it with a different code?
Here is an example of how to get the layer description of each layer (comments in bold red):

(defun c:MKSxPrintLayerDesc(/ lyrCol lyr lyrDesc doc)
(setq doc (vla-get-activeDocument (vlax-get-acad-object)) ;;get active drawing
lyrCol (vla-get-layers doc) ;;get layer collection
)
(princ "\n\n")
(vlax-for lyr lyrCol ;;iterate through each layer object in the collection
(setq lyrDesc (vla-get-description lyr) ;;get the layer description of the current layer object
lyrName (vla-get-name lyr) ;;get the layer name of the current layer object
)
(princ (strcat lyrName ": " lyrDesc "\n")) ;;print the layer name and layer description
)
(princ "\n\n ")
(princ)
)


To change the layer description, you will need to use (vla-put-description <layer object> <string description>).

If you want to use ActiveX methods to create a layer, check out (vla-add <layer collection> <new layer name>). It will return the new layer object, from here you can set any properties it has using the vla- methods.

You can use (vlax-dump-object <object>) to see what properties an object has available.

HTH,

peter
2005-06-02, 02:19 PM
Here are a couple generalized functions for manipulating the layer description

Peter Jamtgaard


; Written By: Peter Jamtgaard copr 2005
; Set the layer description of a specified layer
; Syntax (setlayerdescription "layername" "layerdescription")
; Function returns T if successful nil if not
(defun SetLayerDescription (strLayer strDescription)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
(= (type strDescription) 'STR)
)
(progn
(vla-put-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
strDescription
)
T
)
)
)
; Written By: Peter Jamtgaard copr 2005
; Get the layer description of a specified layer
; Syntax (Getlayerdescription "layername")
; Function Returns Description if successful nil if not
(defun GetLayerDescription (strLayer)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
)
(vla-get-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
)
)
)

LanceMcHatton
2005-06-02, 06:30 PM
To change the layer description, you will need to use (vla-put-description <layer object> <string description>).

If you want to use ActiveX methods to create a layer, check out (vla-add <layer collection> <new layer name>). It will return the new layer object, from here you can set any properties it has using the vla- methods.

You can use (vlax-dump-object <object>) to see what properties an object has available.
Oh, great...! Now you're making me THINK? :shock: I don't know anything about VLA programming but I'll give it a shot. If I can make people think I can write LISP, I can do the same with VLA, right? Right! ;-)

msretenovic
2005-06-02, 06:39 PM
Oh, great...! Now you're making me THINK? :shock: I don't know anything about VLA programming but I'll give it a shot. If I can make people think I can write LISP, I can do the same with VLA, right? Right! ;-)There's nothing wrong with a little thinkin'. And, you can always come back with more questions. There are more than enough of us around to help you. ;)

jpduhon
2006-03-21, 12:28 AM
Here are a couple generalized functions for manipulating the layer description

Peter Jamtgaard OK, I have a question. There is this nifty little lisp routine available in the exchange named layers2.lsp. The lisp allows you to populate a drawing with layers compiled in a CSV file and allows you to set name, color, plottable, on/off, frozed/thawed. I've been trying to figure out how to add the vla-put-description argument into the routine so that the template layer descriptions don't get lost when repopulating layers.

Can't figure it out. I'm sure it has something to do with defining the string in the vla argument to read from the last column of each line in the CSV file but that seems beyond me and I'm about ready to give it up.

Is there anyone out there that knows how to do this?

Any help is IMMENSELY appreciated. The "layermake" portion of the routine is below.







;; Layers2.lsp by Jeff Mishler, April 2003

;; Laymake will load a list layers contained in the file "Layers.csv" or "Layers.txt".
;; The file(s) must exist in the ACAD search path.
;; Portions of this code are either as directly posted or modifications
;; of posted code to the acad customization newsgroup by John Uhden and
;; Jason Piercey, my thanks go to them for the inspiration for this lisp file.


(defun c:laymake (/ layers obj linecnt *file lyrfile *line lines)
(vl-load-com)
(or *acad* (setq *acad* (vlax-get-acad-object)))
(or *doc* (setq *doc* (vla-get-activedocument *acad*)))
(setq layers (vla-get-layers *doc*))
(if (setq *file (if (findfile "layers.csv");added so that Excel may be used to edit and
(findfile "layers.csv") ;save as comma delimited file
(findfile "AIA.txt")
)
)
(progn
(setq lyrfile (open *file "r"))
(repeat 6 (setq *line (read-line lyrfile)))
(while (/= *line nil)
(setq x (str2list *line ","))
(setq obj (vla-add layers (nth 0 x)))
(if (or (< 0 (atoi (nth 1 x)))(> 256 (atoi (nth 1 x))))
(vla-put-color obj (atoi (nth 1 x)))
(vla-put-color obj 7)
)
(if (not (tblsearch "ltype" (nth 2 x)))
(command "-linetype" "l" (nth 2 x) "" "" )
)
(if (not (tblsearch "ltype" (nth 2 x)))
(progn
(vla-put-linetype obj "continuous");force linetype if it wasn't found in acad.lin
(setq linecnt (cons (nth 2 x) linecnt))
)
(vla-put-linetype obj (nth 2 x))
)
(vla-put-plottable obj (if (= "0" (nth 3 x)) :vlax-true :vlax-false))
(vla-put-layeron obj (if (= "0" (nth 4 x)) :vlax-true :vlax-false))
(if (/= (nth 0 x) (getvar "clayer"))
(vla-put-freeze obj (if (= "0" (nth 5 x)) :vlax-false :vlax-true))
)
(vla-put-lineweight obj (atoi (nth 6 x)))
(setq *line (read-line lyrfile))
);while
(close lyrfile)
(setq lines "")
(if (= linecnt nil)(setq lines "<none> ")
(foreach x linecnt (setq lines (strcat lines x ", ")))
)
(princ (strcat "\nLaymake done.\n The linetypes " lines
" were not found in acad.lin and were forced to continuous."))
);progn
(princ "\n Layers.txt or Layers.csv not found, use Laysave to create....")
);if
(princ)
);defun

;str2list taken from discussion group, written by John Uhden
(defun str2list (str pat / pos lst)
(while (setq pos (vl-string-search pat str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ (strlen pat) (1+ pos)))
)
)
(reverse (cons str lst))
)

jpduhon
2006-03-21, 01:32 AM
Now I feel silly. In case anyone was wondering this is what I ended up with and what I was doing wrong:


I was adding the vla-put-description argument in and pointing it to read from the last column but I was trying to add a function.


(vla-put-description obj (atoi (nth 6 x)))
I didn't know what string function to add so I tried just taking the function out:

(vla-put-lineweight obj (nth 6 x))
That did it.


Peter, thanks for your prior post on this subject. It helped lead the way.

MikeM4OSU
2006-04-27, 05:06 PM
Thanks Peter, here is what I have done so far. I just do not understand how your layer description lisp works. Help me please, I have tried, but I am not sure where to insert the layer description into my layer creation lisp. Do these need to be two different routines or can they run as one? Please take a look at what I have here and guide me in the proper direction. For now I am trying to create a layer "M-ANNO-KEYN" with the color green (3) and a layer description of "Mechanical Annotation: Keynotes, text & leaders", of course I have dozens of layers to create this is why I am working on a lisp routine. As you can probably tell I have limited experience with lisp. Thanks again for lending your knowledge out to those of us that are less fortunate

(defun c:MLayer ()
(command ".undo" "begin")
(setvar "cmdecho" 0)
(command "-layer" "n" "M-ANNO-KEYN" "c" "3" "M-ANNO-KEYN" "")
(command ".undo" "end")
(princ)
(princ)
(prompt "Mechanical layers have been created")
(princ)
)


; Written By: Peter Jamtgaard copr 2005
; Set the layer description of a specified layer
; Syntax (setlayerdescription "layername" "layerdescription")
; Function returns T if successful nil if not
(defun SetLayerDescription (strLayer strDescription)
(if (and (= (M-ANNO-KEYN) 'STR)
(tblsearch "M-ANNO-KEYN" strLayer)
(= (Mechanical Annotation: Keynotes, text & leaders) 'STR)
)
(progn
(vla-put-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
strDescription
)
T
)
)
)

[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]
Here are a couple generalized functions for manipulating the layer description

Peter Jamtgaard


; Written By: Peter Jamtgaard copr 2005
; Set the layer description of a specified layer
; Syntax (setlayerdescription "layername" "layerdescription")
; Function returns T if successful nil if not
(defun SetLayerDescription (strLayer strDescription)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
(= (type strDescription) 'STR)
)
(progn
(vla-put-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
strDescription
)
T
)
)
)
; Written By: Peter Jamtgaard copr 2005
; Get the layer description of a specified layer
; Syntax (Getlayerdescription "layername")
; Function Returns Description if successful nil if not
(defun GetLayerDescription (strLayer)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
)
(vla-get-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
)
)
)

Opie
2006-04-27, 05:13 PM
...
I just do not understand how your layer description lisp works.
...
First and no offense, check out how to add [CODE] tags... (http://forums.augi.com/misc.php?do=bbcode#code)

Next to add the description you would call Peter's routine as follows:

(SetLayerDescription "M-ANNO-KEYN" "Mechanical Annotation: Keynotes, text & leaders")

You will need to add the above code after you create the layer:
(defun C:MLAYER ()
(command ".undo" "begin")
(setvar "cmdecho" 0)
(command "-layer" "n" "M-ANNO-KEYN" "c" "3" "M-ANNO-KEYN" "")
(SetLayerDescription "M-ANNO-KEYN" "Mechanical Annotation: Keynotes, text & leaders")
(command ".undo" "end")
(princ)
(princ)
(prompt "Mechanical layers have been created")
(princ)
)

mpeterson79
2007-08-08, 10:56 PM
Okay, I cannot get my LISP routine to work. I'm trying to eventually create a button that will create or repair the current layers in a drawing. As part of that, I'd like to also make sure the layer descriptions are correct.

Can someone please tell me what I'm doing wrong - I haven't messed with LISP in years, and I'm pretty sure I've forgotten more than I ever knew.


; mlayer.lsp
; Written by Mark Peterson, August 08, 2007
;
;DESCRIPTION
; This program was written to automate the creation & repair of
; standard layer scheme in AutoCAD
;--------------------------------------------------------------------------------------------
;
;ERROR HANDLER
(defun err (s)
(if (/= s "Function cancelled")
(princ (strcat "\nError: " s))
)
(if ech (setvar "cmdecho" ech))
(setq *error* olderr)
(princ)
)
;---------------------------------------------------------------------------------------------
; Written By: Peter Jamtgaard copr 2005
; Set the layer description of a specified layer
; Syntax (setlayerdescription "layername" "layerdescription")
; Function returns T if successful nil if not
(defun SetLayerDescription (strLayer strDescription)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
(= (type strDescription) 'STR)
)
(progn
(vla-put-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
strDescription
)
T
)
)
)
; Written By: Peter Jamtgaard copr 2005
; Get the layer description of a specified layer
; Syntax (Getlayerdescription "layername")
; Function Returns Description if successful nil if not
(defun GetLayerDescription (strLayer)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
)
(vla-get-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
)
)
)
;----------------------------------------------------------------------------------------------
;MAKE LAYER ROUTINE
;
;
(defun C:MLAYER ()
(command ".undo" "begin")
(setvar "cmdecho" 0)
(setq ccl (getvar clayer))
;
(command "-layer" "m" "BASEPLATE" "c" "WHITE" "BASEPLATE" "")
(SetLayerDescription "BASEPLATE" "Baseplate Outline and Mounting Holes")
;
(command "-layer" "m" "BLDGS" "c" "WHITE" "BLDGS" "")
(SetLayerDescription "BLDGS" "Non-Test Buildings (foam)")
;
(command "-layer" "m" "BLDGS DATUM" "c" "51" "BLDGS DATUM" "")
(SetLayerDescription "BLDGS DATUM" "Baseplate Outline and Mounting Holes")
;
(setvar clayer ccl)
(command ".undo" "end")
(princ)
(princ)
)


When I run this in AutoCAD, it returns:
; error: bad argument type: (or stringp symbolp): nil

Thanks for any help you can provide!

-Mark

Mike_R
2007-08-09, 03:14 PM
Your problem may be here...


;----------------------------------------------------------------------------------------------
;MAKE LAYER ROUTINE
...
(setq ccl (getvar clayer))

You should have clayer in quotes, like this.

(setq ccl (getvar "clayer"))

Try that out, if you still have a problem be sure to let us know. ;)

mpeterson79
2007-08-09, 05:10 PM
Your problem may be here...
You should have clayer in quotes, like this.

(setq ccl (getvar "clayer"))

Try that out, if you still have a problem be sure to let us know. ;)

Okay - fixed that, and still no go. I keep getting the following error:

; error: bad argument type: (or stringp symbolp): nil

I tried running (setlayerdescription "0" "TEST 01") at the command line, and it returns:; error: no function definition: VLAX-GET-ACAD-OBJECT

Now I'm completely lost - have I not setup/used Peter's code correctly?

Opie
2007-08-09, 05:20 PM
Add the following code somewhere prior to executing vlisp functions (vl*-* ...
(vl-load-com)

Mike_R
2007-08-09, 05:46 PM
The error "bad argument type: (or stringp symbolp): nil" is usually a setvar/getvar error. When you see that one, check your code and make sure that all your (setvar) and (getvar) lines have the variable in quotation marks...

I see when you set "Clayer" back to the global ccl variable, it's also not quoted... Should be:


(setvar "CLAYER" ccl)


P.S., you should always declare the variables you want used in only one routine as local, with a "/" in your defun line... For your program, it should look like this.

(defun C:MLAYER (/ ccl)

mpeterson79
2007-08-09, 07:15 PM
Opie - the vl-load-com seems to do the trick! Thanks!

And thanks Mike as well - for catching that quoting error, as well as helping me out with locally defining my variables!

My final code, in case anyone else is interested:


; -mlayer.lsp
; Written by Mark Peterson, August 08, 2007
;
;DESCRIPTION
; This program was written to automate the creation & repair of
; standard layer scheme in AutoCAD
;--------------------------------------------------------------------------------------------
;
;ERROR HANDLER
(defun err (s)
(if (/= s "Function cancelled")
(princ (strcat "\nError: " s))
)
(if ech (setvar "cmdecho" ech))
(setq *error* olderr)
(princ)
)
;---------------------------------------------------------------------------------------------
; Written By: Peter Jamtgaard copr 2005
; Set the layer description of a specified layer
; Syntax (setlayerdescription "layername" "layerdescription")
; Function returns T if successful nil if not
(defun SetLayerDescription (strLayer strDescription)
(vl-load-com)
(if (and (= (type strLayer) 'STR)
(tblsearch "layer" strLayer)
(= (type strDescription) 'STR)
)
(progn
(vla-put-description
(vla-item
(vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object)
)
)
strLayer
)
strDescription
)
T
)
)
)
;----------------------------------------------------------------------------------------------
;MAKE LAYER ROUTINE
;
;
(defun C:MLAYER ()
(command ".undo" "begin")
(setvar "cmdecho" 0)
(setq ccl (getvar "clayer"))
;
(command "-layer" "m" "BASEPLATE" "c" "WHITE" "BASEPLATE" "")
(SetLayerDescription "BASEPLATE" "Baseplate Outline and Mounting Holes")
;
(command "-layer" "m" "BLDGS" "c" "WHITE" "BLDGS" "")
(SetLayerDescription "BLDGS" "Non-Test Buildings (foam)")
;
(command "-layer" "m" "BLDGS DATUM" "c" "51" "BLDGS DATUM" "")
(SetLayerDescription "BLDGS DATUM" "Baseplate Outline and Mounting Holes")
;
(setvar "clayer" ccl)
(command ".undo" "end")
(princ)
(princ)
)

Liamnacuac
2009-06-23, 01:27 AM
This is working better than I expected, but I figured I could write a line as;

(command "-layer" "M" "Sample Layer" "C" "3" "Sample Layer" "L" "Hidden")

To add the line type. I think I'm losing track of the quotations?

I'm going to try:


(command "-layer" "M" "Sample Layer" "C" "3" "Sample Layer" "" "L" "Hidden" "")

Liamnacuac
2009-06-23, 01:38 AM
OK, that didn't work either.

I'm going to try:


(command "-layer" "M" "Sample Layer" "C" "3" "Sample Layer" "" "L" "Hidden" "")

RobertB
2009-06-23, 09:04 PM
Type -Layer at the command line and note what options you need to use.

Liamnacuac
2009-06-24, 01:06 AM
I don't quite understand why in this particular line;

(command "-layer" "m" "BLDGS DATUM" "c" "51" "BLDGS DATUM" "")
(SetLayerDescription "BLDGS DATUM" "Baseplate Outline and Mounting Holes")

We have the layer name twice? I see; M ake to create the layer named BLDGS DATUM, then C olor, to make the layer color # 51. What option is the second layer name for? Why doesn't L, and then the line type work? And what are the blank quotations at the end for?

Also, is the whole "SetLayerDescription" obsolete in 2010 as there is now a layer option for Description?



Type -Layer at the command line and note what options you need to use.

Opie
2009-06-24, 03:47 AM
Using the command line version of the layer command requires the layer name to be duplicated for each option you want to change.

The SetLayerDescription is a custom VLisp routine, which requires the layer name and desired description (both in string format) to work.

RobertB
2009-06-24, 05:35 PM
I don't quite understand why in this particular line;

(command "-layer" "m" "BLDGS DATUM" "c" "51" "BLDGS DATUM" "")
(SetLayerDescription "BLDGS DATUM" "Baseplate Outline and Mounting Holes")

We have the layer name twice? I see; M ake to create the layer named BLDGS DATUM, then C olor, to make the layer color # 51. What option is the second layer name for? Why doesn't L, and then the line type work? And what are the blank quotations at the end for?Did you run the -Layer command at the command prompt? That should make it very evident to you the answers to your questions. Remember that the (command) function is just like you typing at the command prompt.

Command: ._-Layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _Make
Enter name for new layer (becomes the current layer) <0>: Test
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _Color
New color [Truecolor/COlorbook] : 1
Enter name list of layer(s) for color 1 (red) <Test>:
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _Ltype
Enter loaded linetype name or [?] <Continuous>: Hidden
Enter name list of layer(s) for linetype "HIDDEN" <Test>:
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _Description
Enter layer description: My Test Layer
Enter name list of layer(s) to apply description or <select objects>: <*>: Test
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:


Also, is the whole "SetLayerDescription" obsolete in 2010 as there is now a layer option for Description?Yes, it is obsolete.

Liamnacuac
2009-06-25, 07:29 PM
Super! It's working. even though there's a description command, I'll leave in this code-if it ain't broke don't fix it...

I did notice a couple other problems with my routine, I call for line types, and if they aren't loaded, my routine will crash. So, I think I'll try to add loading lines types from two .lin files. Something like;

"-lt" "l" "*" "../acad.lin"

I then noticed that I need a text style called by some of these line types, but I haven't thoroughly thought that thru yet (workin on somethin else,,).

Opie
2009-06-25, 08:17 PM
Super! It's working. even though there's a description command, I'll leave in this code-if it ain't broke don't fix it...

I did notice a couple other problems with my routine, I call for line types, and if they aren't loaded, my routine will crash. So, I think I'll try to add loading lines types from two .lin files. Something like;

"-lt" "l" "*" "../acad.lin"

I then noticed that I need a text style called by some of these line types, but I haven't thoroughly thought that thru yet (workin on somethin else,,).
You could always create a template drawing with the correct settings (layers, linetypes, text styles) for later insertion into your existing drawings.

Liamnacuac
2009-06-30, 06:45 PM
Oh yeah, it's there already, but there's always people who work the same way they did five or six years ago (before I was responsible for stuff like this), and they use this lisp routine, even though I have updated the standards manual , updated the templates, preloaded designcenter on their workspaces, etc.
I want to make everyone as cost effective as possible, not necessarily AutoCAD Gurus. Only when they are doing something that is completely obsolete-such as when I found people were exploding blocks because they hadn't learned about annotative block settings.


You could always create a template drawing with the correct settings (layers, linetypes, text styles) for later insertion into your existing drawings.

peter
2009-07-01, 02:11 PM
Here is an abbreviated version of the description functions

Peter


(defun SetLayerDescription (strLayer strDescription)
(vla-put-description (vlax-ename->vla-object
(tblobjname "layer" strLayer))
strDescription)
)
(defun GetLayerDescription (strLayer)
(vla-get-description (vlax-ename->vla-object
(tblobjname "layer" strLayer)))
)
(vl-load-com)