Using selection sets to manipulate object layer

Using selection sets to manipulate object layer

Anonymous
Not applicable
2,178 Views
4 Replies
Message 1 of 5

Using selection sets to manipulate object layer

Anonymous
Not applicable

Hello - I have seen several posts about using selection sets to change line color but I have not been able to locate any posts that discuss how to change the layer of selected entities. 

 

I tried to modify some code that I found that changed an entities color but I was not able run the code. I keep getting "error: bad argument type: numperp: nil" which I know means that I am passing an empty argument. I am just new to Autolisp so I don't know which argument is empty.

 

 

defun selectionlisp1 ( / sset check)

;load the visual lisp extensions
(vl-load-com)
(setq adoc (vla-get-activedocument
(vlax-get-acad-object)))
(vlax-for a (vla-get-activeselectionset adoc)
;check for selection
(while

;get the entity and entity name
(setq sset (car (entsel)))

;convert to vl object
(setq sset (vlax-ename->vla-object sset))

;check if the entity has a color property
;and it can be updated
(progn

;change it's layer
(vlax-put-layer subent "0")
)
);if

);while

(princ)

);defun

 

Any additional insight on using the ssget function would also be helpful. There are a lot of tutorials about passing an selection set function but very few about modifying the selected entities (that I have found). 

 

Thanks in advanced for your time!

0 Likes
Accepted solutions (2)
2,179 Views
4 Replies
Replies (4)
Message 2 of 5

Moshe-A
Mentor
Mentor
Accepted solution

@Anonymous hi,

 

in this forum (and others) there is tones of sample codes to manipulating entities \ objects to change any property you want.

 

here is another one specially for you:

usage: 

(chg-layer-color "LayerName" 1) ; change entity layer and color red

 

enjoy

moshe

 

(defun chg-layer-color (layer color / ss i ename elist)
 (if (setq ss (ssget))
  (progn
   (setq i -1)
   (repeat (sslength ss)
    (setq i (1+ i) ename (ssname ss i) elist (entget ename))
    (setq elist (subst (cons  '8 layer) (assoc  '8 elist) elist)) ; change layer 
    (setq elist (subst (cons '62 color) (assoc '62 elist) elist)) ; change color
    (entmod elist)
   ); repeat
  ); progn
 ); if
); chg-layer-color

 

Message 3 of 5

dlanorh
Advisor
Advisor
Accepted solution

Here is your amended lisp to work with either an entity selection (entsel) or a selection set selection (ssget).

I've commented what each line does. The code uses Autolisp and visual lisp.

 

You will need to un-comment the appropriate line (remove the leading 😉  in the first (setq). Only un-comment one line, otherwise you will receive two selection prompt and only the latter one will apply.

 

(defun c:selectionlisp1 ( / adoc sset a)
;load the visual lisp extensions
  (vl-load-com)
 
  (setq adoc (vla-get-activedocument (vlax-get-acad-object))
;        sset (car (entsel "\nSelect entity : ")) ;This allows a single selection only and returns an entity
;        sset (ssget)                             ;This allows multiple selections (can be a single item) and returns a selection set (pickset)
  );end_setq    

  (cond ( (= (type sset) 'ENAME)                    ;If sset is an entity
          (setq a (vlax-ename->vla-object sset))    ;covert to an object to use visual lisp object properties
          (if (vlax-property-available-p a 'layer T);If the object has a layer property and it is writable
            (vlax-put-property a 'layer "0")        ;Then set its layer to 0 
          );end_if
        );end_sub_cond_ENAME
        ( (= (type sset) 'PICKSET)                      ;If sset is a selection set
          (vlax-for a (vla-get-activeselectionset adoc) ;get the selection set as objects and loop through one object at a time
            (if (vlax-property-available-p a 'layer T)  ;If the object has a layer property and it is writable
              (vlax-put-property a 'layer "0")          ;Then set its layer to 0 
            );end_if
          );end_for
          (setq sset nil)                               ;Empty the selection set
        );end_sub_cond_PICKSET
  );end_cond      
  (princ)
);defun
(princ)

I am not one of the robots you're looking for

Message 4 of 5

john.uhden
Mentor
Mentor

Good job!

But, um, there's no need to

(setq sset nil)

because sset was declared as a local variable 

John F. Uhden

Message 5 of 5

dlanorh
Advisor
Advisor

@john.uhden wrote:

Good job!

But, um, there's no need to

(setq sset nil)

because sset was declared as a local variable 


old habits die hard, especially where selection sets are concerned Robot tongue

I am not one of the robots you're looking for

0 Likes