Create A New Layer

Create A New Layer

Anonymous
Not applicable
1,745 Views
10 Replies
Message 1 of 11

Create A New Layer

Anonymous
Not applicable

Hey everyone,

 

I am looking to create a LISP routine to create a new layer based on what the user inputs for a name.

 

I believe my logic flows as follows:

  • “Nothing selected” if ran and, well, nothing is selected
  • Prompts user for layer name
  • Takes user input and “concatenates” it with predetermined text that is before and after the input (ex: user inputs “HVAC”; a layer gets created called “Rev0_HVAC_Line_0” – gibberish text for an example and creates the new layer with static settings (color, linetype, lineweight, etc)
  • Takes the objects that were selected and places them on the new layer
  • Sets the newly created layer as the current layer
  • If layer already exists, prompt user that it does and ask if they would like selected objects to be placed on it - place on layer if yes; if not do nothing

Basically, I can create the new layer (through layers manager), but the user still must manually select the objects, go through the layers and find the correct one, or create one, and hopefully enter the correct name (if the layer name always has the same “prefix” and “suffix”, then why open this to potential error when it can be automated).

 

I have this code that I have used to make a new layer:

(defun c:UA ()
    (command "_.Layer" "_Make" "Rev0_HVAC_Line_0" "_Color" "10" "" "LType" "PHANTOM2" "" "")
    (princ)
)

I am finding getting user inputs and automation of my task to be quite difficult:

 

I feel that my process can be automated and save a ton of time for me.

 

Any help/input on this matter is greatly appreciated.

 

Thanks! 

 

 

 

 

 

0 Likes
Accepted solutions (1)
1,746 Views
10 Replies
Replies (10)
Message 2 of 11

Kent1Cooper
Consultant
Consultant

Using the Make option sets the Layer current in the process, so you shouldn't need the Set part.  And it won't care if the Layer already exists -- if it does, the only difference it will make is that if it has different color/linetype/etc. settings than your standards, those will be corrected.  If it's to standard, the result will be exactly the same as simply Setting it current.

 

I'm wondering whether you really need to ask the User -- whether or not the Layer already exists, presumably they want to put the selected objects on it [i.e., under what circumstances would anyone answer "No"?], and I would think it's just a question of creating it if it doesn't exist yet.  If that's a reasonable assumption, try something like this [untested]:

 

(defun C:YourCommandName (/ LaySS LaySubName)

  (setq

    LaySS (ssget "_I"); will return nil if there are no pre-selected objects

    LaySubName (getstring "\nDiscpline portion of Layer name: "); User puts in something like "HVAC"

  ); setq

  (if (/= LaySubName ""); they didn't just hit Enter

    (progn ; then

      (command "_.layer' "_make" (strcat “Rev0_" LaySubName "_Line_0”) "_color" "10" "" "ltype" "PHANTOM2" "" "")

      (if LaySS ; if there were any pre-selected objects [do nothing if there weren't]

        (command "_.chprop" LaySS "" "_layer" (getvar 'clayer) "")

      ); if

    ); progn

  ); if

  (princ)

); defun

 

Presumably, you would have assorted command names combined with assorted different parts to add around it, but even parts of those could be optional, for example, it could ask for a number to use in place of the 0 here, so you wouldn't need separate commands for every Revision.

 

It could even be enhanced to limit the "discipline" part to only specific choices if you don't want it limitless, offering them in the prompt, which would not only ensure standard wordings, but could also be set up so that a User would need to type in only H [or h] to get "HVAC".

 

 

Kent Cooper, AIA
0 Likes
Message 3 of 11

john.uhden
Mentor
Mentor

I like your take on this.  But once again I am wondering if a long (and ...) would be better.  If LaySS is nil, does he want to make the new layer anyway?

John F. Uhden

0 Likes
Message 4 of 11

Anonymous
Not applicable

Yes @Kent1Cooper, I overlooked that logical point - good catch. If the user is attempting to create a new layer and put objects onto that layer, then it would stand to reason that if the layer already existed they would want to simply put the objects onto the layer and not be prompted. So basically if it exists, move selected objects to it, and if it doesn't exist, create it and move selected objects to it. 

Your 2 statements following your code has me intrigued. I never thought about doing that. I would think I would have to do some sort of "getkword" from the user for the disciple. The Rev would just be user input. I would, although highly inefficient code, use an if and repeat your code throughout the routine. What would you recommend here?

 

Also, I quickly ran your code and it does not run. It hangs as follows: (((("_>  Am I overlooking a missing bracket or something else small? 

 

@john.uhden I guess we could create the new layer, regardless if something is selected or not. My logic for having something selected was to not have layers created that didn't have anything on them and were unnecessary.

0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... I quickly ran your code and it does not run. It hangs as follows: (((("_>  Am I overlooking a missing bracket or something else small? ....


My mistake:

....

(command "_.layer" "_make" ....

Kent Cooper, AIA
0 Likes
Message 6 of 11

john.uhden
Mentor
Mentor

@Anonymous wrote,"I guess we could create the new layer, regardless if something is selected or not. My logic for having something selected was to not have layers created that didn't have anything on them and were unnecessary."

 

Um, that sounds a little contradictory to me.  If nothing is selected, and you create a new layer, and nothing is changed to that layer, then that new layer is in fact unnecessary (except for perhaps the future).

 

 

John F. Uhden

0 Likes
Message 7 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... I guess we could create the new layer, regardless if something is selected or not. My logic for having something selected was to not have layers created that didn't have anything on them and were unnecessary.


I had assumed, from the description in Post 1 of the steps, that the Layer was to be created anyway.  But maybe a worthwhile approach would be, if a User starts the command without selecting anything first, to ask them to select objects to put on that Layer, with the option to exit if not selecting anything [presumably from having mistakenly starting the command].

Kent Cooper, AIA
0 Likes
Message 8 of 11

Anonymous
Not applicable

@john.uhden A little contradictory, yes, but at the end of the day when the file is ready to be delivered, I simply run my "clean drawing" LISP and all unneeded entities are removed, like unused layers. The purpose here was either to create a new layer and put objects on it or add objects to an already existing layer without the user having to use the layers manager or scroll to find the appropriate layer to place the objects. Without having to do this now, the numbers of layers created does not matter as time is not wasted scrolling and looking as the LISP will do the "looking" and adding for you. 

0 Likes
Message 9 of 11

Anonymous
Not applicable

@Kent1Cooper I do not think, based on thinking about this further, that it would really matter that the layer is created or not. @john.uhden mentioned this and made me think, so I further explained that it wouldn't really make any difference if the new layer is used or not as the drawing will be "cleaned" before being sent to the client and all unused layers deleted by the process. 

 

After running the corrected code (thanks for picking up on missing quotation) it does not work. After I type in the "discipline" through the LISP prompt, I get "; error: bad argument type: stringp nil" and "Enter name for new layer (becomes the current layer) <0>:" with "Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:" that follows. What @Kent1Cooper has written is way above my comprehension at current time. More help on this matter is greatly appreciated. 

 

FYI: I am running AutoCAD 2016

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

After running the corrected code (thanks for picking up on missing quotation) it does not work. After I type in the "discipline" through the LISP prompt, I get "; error: bad argument type: stringp nil"....


On closer examination, it looks like this part ended up with some "smart quotes" [the slanted ones]:

(strcat Rev0_" LaySubName "_Line_0)

 

I don't know how [normally this only happens in word processors -- I hope the website didn't cause it, but it may have, because I think I typed the whole thing within the Reply window].  AutoLisp isn't going to like those.  Try changing them to "dumb" quotes [upright ones -- that's what you should get editing your .lsp file in Notepad], and see whether it works:

 

(strcat "Rev0_" LaySubName "_Line_0")

Kent Cooper, AIA
Message 11 of 11

Anonymous
Not applicable

Amazing. @Kent1Cooper Thanks for all the help! 

0 Likes