Lisp to create layers into filter group

Lisp to create layers into filter group

S_S_SS
Advocate Advocate
538 Views
7 Replies
Message 1 of 8

Lisp to create layers into filter group

S_S_SS
Advocate
Advocate

Hello every one 
I've made a lisp code to create layers with some conditions 
now I need to add something to it is to put these created layers in group filter ( ALT + G ) it's existed and say it's name is "new"
how to make this ??
thanks in advance 

(defun createLayerFromDialogValues (prefix text color)
  ;; Ensure prefix and text are valid (not nil) before proceeding
  (if (and prefix text)
    (progn
      (setq layerName (strcat prefix text))  ;; Concatenate prefix and text to form layer name
      
      ;; Display the layer name being created for debugging
      (prompt (strcat "\nAttempting to create layer: \"" layerName "\""))
      
      ;; Check if the layer already exists
      (if (tblsearch "layer" layerName)
        (prompt (strcat "\nLayer \"" layerName "\" already exists."))
        
        (progn
          ;; Get layer collection
          (setq layerCollection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
          
          ;; Attempt to create new layer with error handling
          (setq result (vl-catch-all-apply 'vla-add (list layerCollection layerName)))
          
          ;; Check for success or error
          (if (not (vl-catch-all-error-p result))
            (progn
              ;; Layer created successfully, set color
              (setq layerObj (vla-item layerCollection layerName))
              (vla-put-color layerObj color)
              (prompt (strcat "\nLayer \"" layerName "\" created with color " (itoa color) "."))

              ;; Now, handle group filter
              (setq acadDoc (vla-get-activedocument (vlax-get-acad-object)))
              (setq groups (vla-get-Groups acadDoc))
              
              ;; Check if a group named "new" exists
              (setq newGroup (vl-catch-all-apply 'vla-item (list groups "new")))
              
              ;; If "new" group doesn't exist, create it
              (if (vl-catch-all-error-p newGroup)
                (progn
                  (setq newGroup (vla-add groups "new"))
                  (prompt "\n'new' group created.")
                )
                (prompt "\n'new' group found.")
              )
              
              ;; Add the new layer to the "new" group
              (vla-AppendItem newGroup layerObj)
              (prompt (strcat "\nLayer \"" layerName "\" added to the 'new' group filter."))
            )
            ;; Handle error in layer creation
            (prompt (strcat "\nError creating layer: " (vl-catch-all-error-message result)))
          )
        )
      )
    )
    
    ;; Error handling if prefix or text is nil
    (prompt "\nError: prefix or text is nil; cannot create layer name.")
  )
)


say the syntax required is  

(createLayerFromDialogValues "g"   "f"  1) 
0 Likes
Accepted solutions (1)
539 Views
7 Replies
Replies (7)
Message 2 of 8

komondormrex
Mentor
Mentor

hey,

you cannot group layers into group filter this way. a group is a saved selection set consisting of objects (entities) to treat as a one unit. 

Message 3 of 8

Moshe-A
Mentor
Mentor

@S_S_SS  hi,

 

Layer property\group filter is stored some where under the layers table extended dictionaries pretty complicated. instead use (command "layer" "filter" "new" "group" ..........)

 

Moshe

 

Message 4 of 8

S_S_SS
Advocate
Advocate
thanks this works
(command "_.LAYER" "_FILTER" "_new" "_Group" "All" layerName "new" )

but in my case i already have the filter group "new" and i just need to add the layer in this filter group

i replaced the "_new" with "_edit" and it gives error to me

how to make this ??

notice that the filter group is existing and also have some layers in it so we can't replace the old with the new
0 Likes
Message 5 of 8

Moshe-A
Mentor
Mentor
Accepted solution

@S_S_SS ,

 

(command "._layer" "_filter" "_edit" "<enter your filter name here>" "add/delete" ........)

run layer command manually use the your options try it and the go to translate it to a code

 

moshe

 

 

 

Message 6 of 8

S_S_SS
Advocate
Advocate
thanks
it works correctly
0 Likes
Message 7 of 8

S_S_SS
Advocate
Advocate

hello every one 
now I've another problem 
it works correctly but in some text of the layer name it doesn't work correctly 

here in this code 
 if the syntax of it is 

(createLayerFromDialogValues "R"  "30 cm H 7.00 m {+7.00} [2 x3.0 m] LE5   plate" 1)

it doesn't put the new layer inside the filter group 
also sometimes it makes error in making the layer 
it's something about handling with spaces and strange symbols like the "[" and "{" in autolisp 
but it's necessary in my total code 
how to  fix these issues 

thanks in advance 

(defun createLayerFromDialogValues (prefix text color)
  ;; Ensure prefix and text are valid (not nil) before proceeding
  (if (and prefix text)
    (progn
      (setq layerName (strcat prefix text))  ;; Concatenate prefix and text to form layer name
      
      ;; Display the layer name being created for debugging
      (prompt (strcat "\nAttempting to create layer: \"" layerName "\""))
      
      ;; Check if the layer already exists
      (if (tblsearch "layer" layerName)
        (prompt (strcat "\nLayer \"" layerName "\" already exists."))
        
        (progn
          ;; Get layer collection
          (setq layerCollection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
          
          ;; Attempt to create new layer with error handling
          (setq result (vl-catch-all-apply 'vla-add (list layerCollection layerName)))
          
          ;; Check for success or error
          (if (not (vl-catch-all-error-p result))
            (progn
              ;; Layer created successfully, set color
              (setq layerObj (vla-item layerCollection layerName))
              (vla-put-color layerObj color)
              (prompt (strcat "\nLayer \"" layerName "\" created with color " (itoa color) "."))
              
              ;; Adding layer to filter group "Formwork"
              
             (command "_.LAYER" "_FILTER" "_edit" "Formwork" "add"  layerName )

                (if (> (getvar 'CMDACTIVE) 0)
                 (command "")
                 )

              (prompt (strcat "\nLayer \"" layerName "\" added to filter group \"Formwork\"."))
            )
            ;; Handle error in layer creation
            (prompt (strcat "\nError creating layer: " (vl-catch-all-error-message result)))
          )
        )
      )
    )
    
    ;; Error handling if prefix or text is nil
    (prompt "\nError: prefix or text is nil; cannot create layer name.")
  )
)

 

0 Likes
Message 8 of 8

Moshe-A
Mentor
Mentor

@S_S_SS ,

 

two problems:

1. to set\reset Layer color, you need have\create AcCmColor object to set it to AutoCAD index color.

2. turn out you can create a layer contain '[]' characters but it wont be 'send' to group filter (maybe bug or it is not allowed?!)  also note that group filter name it is case sensitive.

 

Moshe

 

 

             (setq layerObj (vla-item layerCollection layerName))
	     ; (vla-put-color layerObj color)
	      (setq AcCmColor (vlax-create-object (strcat "AutoCAD.AcCmColor." (substr (getvar "ACADVER") 1 2))))
	      (vla-put-colorIndex AcCmColor color)
              (vla-put-trueColor layerObj AcCmColor)

 

 

0 Likes