Create layer with brackets in the name

Create layer with brackets in the name

dumitraa
Explorer Explorer
868 Views
12 Replies
Message 1 of 13

Create layer with brackets in the name

dumitraa
Explorer
Explorer

Hello guys! So I want to create a command where when I input a number, it creates several layers with the input followed by other values, with their own lineweight and colors.

 

Example: - If I input "123", AutoCAD should create 3 layers "[123]_1", [123]_2", [123]_3" with their own layer colors and other characteristics. However, it doesn't seem to work. It works without the brackets, but when I try it with brackets, it doesn't change any characteristics even if it does create the layers.

 

I'm a complete beginner in this language so I used ChatGPT, sorry haha! I do know some other languages better (JavaScript, Python) but overall I'm still at the beginning.

 

Here's what my code kind of looks like (simplified version):

 

(defun CreateLayer (layerName color lineweight)
(if (not (tblsearch "LAYER" layerName))
(progn
(command "_.-layer" "M" layerName "C" color "" "LT" "CONTINUOUS" "" "LW" (rtos lineweight) "" "")
)
)
)


(defun C:CL (/ input)
(setq input (getstring "\nWrite a number: "))
(CreateLayer (strcat "[" input "]" "_1") 1 0.5)
(CreateLayer (strcat "[" input "]" "_2") 3 0.4)
(CreateLayer (strcat "[" input "]" "_3") 7 0.25)
(princ "\nSucces!")
(princ)
)

 

Thank you in advance!

0 Likes
Accepted solutions (1)
869 Views
12 Replies
Replies (12)
Message 2 of 13

pendean
Community Legend
Community Legend

Hmm, seemed to work here 

pendean_0-1683745498680.png

 

0 Likes
Message 3 of 13

dumitraa
Explorer
Explorer

The layers do get created but the specified colors and lineweights don't get chosen. They are just the default ones. Did it work for you?

0 Likes
Message 4 of 13

Kent1Cooper
Consultant
Consultant

May not fix your problem, but....

 

There's no need to assign Continuous as a linetype -- it's the default.  And the lineweight could be fed in as a raw number, rather than converted to a string:

 

(command "_.-layer" "M" layerName "C" color "" "LW" lineweight "" "")

 

Also, the (strcat) functions can be abbreviated a little:

 

(CreateLayer (strcat "[" input "]_1") 1 0.5)

Kent Cooper, AIA
0 Likes
Message 5 of 13

dmfrazier
Advisor
Advisor

I don't know why (yet), but when I try to do what the LISP does on the command line, this happens:

 

Enter name for new layer (becomes the current layer) <0>: [123]_1
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile/Xref]: C

New color [Truecolor/COlorbook] : 1

Enter name list of layer(s) for color 1 (red) <[123]_1>: [123]_1

No matching layer names found.

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

Enter lineweight (0.0mm - 2.11mm): 0.5

Enter name list of layers(s) for lineweight 0.50mm <[123]_1>: [123]_1

No matching layer names found.

 

Although the layer does get created, the Layer command does not seem to be able to find it.

 

If I repeat the test substituting parentheses for brackets, it works.

0 Likes
Message 6 of 13

CADaSchtroumpf
Advisor
Advisor
Accepted solution

Try with the modified function "CreateLayer", seem to work's.

(defun CreateLayer (layerName color lineweight)
  (if (not (tblsearch "LAYER" layerName))
    (entmake
      (list
        (cons 0 "LAYER")
        (cons 100 "AcDbSymbolTableRecord")
        (cons 100 "AcDbLayerTableRecord")
        (cons 2 layerName)
        (cons 70 0)
        (cons 62 color)
        (cons 6 "Continuous")
        (cons 290 1)
        (cons 370 (fix (* 100 lineweight)))
      )
    )
  )
)
0 Likes
Message 7 of 13

pendean
Community Legend
Community Legend

@dumitraa wrote:

...Did it work for you?


"... However, it doesn't seem to work. It works without the brackets, but when I try it with brackets, it doesn't change any characteristics even if it does create the layers...." Ah, I guess that is what you meant by "characteristics" instead of spelling it out LOL

Message 8 of 13

ronjonp
Advisor
Advisor

@dumitraa  There is also the VL route:

(defun createlayer (layername color lineweight description / lo)
  (if (not (tblsearch "LAYER" layername))
    (progn
      (setq lo (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layername))
      (vla-put-color lo color)
      (vl-catch-all-apply 'vla-put-lineweight (list lo lineweight))
      (vla-put-description lo description)
    )
  )
)
(defun c:cl (/ input)
  (setq input (getstring "\nWrite a number: "))
  (createlayer (strcat "[" input "]" "_1") 1 5 "")
  (createlayer (strcat "[" input "]" "_2") 3 4 "4 LINEWEIGHT ERRORS FOR ME")
  (createlayer (strcat "[" input "]" "_3") 7 25 "I'M THE BEST LAYER")
  (princ "\nSucces!")
  (princ)
)
Message 9 of 13

Kent1Cooper
Consultant
Consultant

@dmfrazier wrote:

I don't know why (yet), but....

New color [Truecolor/COlorbook] : 1

Enter name list of layer(s) for color 1 (red) <[123]_1>: [123]_1

No matching layer names found.

....

I know why.  It's because brackets are a wildcard option, meaning anything among the characters between them works.  If you make a Layer called 2_1, and go [in a command-line -LAYER command, and apparently also in (command "_.layer" ...)] to change its color, you can give it [123]_1 and it will assign the color to Layer 2_1, because 2 is among the characters within the brackets.

 

Typed in, you can give Layer [123]_1 a color [or whatever property] by "escaping" the bracket characters so they are taken literally instead of as a wildcard wrapper:
`[123`]_1

 

The thing I find most curious is that the same issue occurs even if what you tell it for assigning a property is to give it to the current Layer, with just Enter.

 

An argument against using square brackets in Layer names [or other wildcard characters -- see Help for the (wcmatch) function].

Kent Cooper, AIA
Message 10 of 13

ronjonp
Advisor
Advisor

@Kent1Cooper This goes for blocks names etc. Think about SSGET\WCMATCH filters. Lee has a function on his site to account for this. HERE's some more banter and early on functions about this if you care to read. *missing the clinking beers icon*

 

Another reason to stay away from command calls *pokes with stick* 😋

0 Likes
Message 11 of 13

komondormrex
Mentor
Mentor

for commandos

(defun CreateLayer (layerName suffix color lineweight)
	(if (not (tblsearch "LAYER" layerName))
		(vla-sendcommand  (vla-get-activedocument (vlax-get-acad-object))
				  (strcat "_.-layer _m " (strcat "[" layerName "]_" (itoa suffix)) "\n"
				  	  "_c " (itoa color) "\n" (strcat "`[" layerName "`]_" (itoa suffix)) "\n"
				  	  "_LT CONTINUOUS\n" (strcat "`[" layerName "`]_" (itoa suffix)) "\n"
				  	  "_LW " (rtos lineweight) "\n" (strcat "`[" layerName "`]_" (itoa suffix)) "\n\n"
				  )
		)  
	)
)
(defun C:CL (/ input)
	(setq input (getstring "\nWrite a number: "))
	(CreateLayer input 1 1 0.5)
	(CreateLayer input 2 3 0.4)
	(CreateLayer input 3 7 0.25)
	(princ "\nSucces!")
	(princ)
)
Message 12 of 13

dmfrazier
Advisor
Advisor

"I know why."

 

I knew you would.

0 Likes
Message 13 of 13

dumitraa
Explorer
Explorer

@Anonymous thanks everyone for your help!! I'm glad I made this post and I'm happy you guys were helpful~ thank you!!

0 Likes