Jump to content
Solitechcadsolutions

code that select all object and convert in to mentioned layer

Recommended Posts

Solitechcadsolutions

Hello every one i need help on completing below code.

 

 

Basically i need code that select objects and after selecting objects convert layer "obj2" into "obj1" layer and "dim" into "0" layer

 

 

(defun c: autolayerchange ()

(if (setq ss (ssget))

(prong (setq i (1- (sslength ss)))

(while (

(setq e (ssname ss i)

x (cdr (assoc 8 (entget e)))

i (1- i)

) ; it is returning object layer name

(foreach x "obj2" convert to layer "obj1")

(foreach x "dim" convert to layer "0")

))))

i am stuck at red marked text. thank you for your help

Share this post


Link to post
Share on other sites
satishrajdev

Something like this?

 

(defun c:test ( / e i l ss)
 (if (setq ss (ssget))                       [color="red"]; <- add your ssget filter here[/color]
   (repeat (setq i (sslength ss))
     (setq e (entget (ssname ss (setq i (1- i)))))
     (setq l (cdr (assoc 8 e)))
     (cond ((eq l "obj2") (entmod (subst (cons 8 "obj1") (assoc 8 e) e)))
    ((eq l "dim") (entmod (subst (cons 8 "0") (assoc 8 e) e)))
     )
   )
 )
 (princ)
)

Share this post


Link to post
Share on other sites
ziele_o2k

Another:

(defun c:autolayerchange ( / _in _ss _obj)
 (if
   (setq _ss (ssget (list (cons 8 "obj2,dim"))));; <- change your ssget filter
   (repeat (setq _in (sslength _ss))
     (setq _obj (vlax-ename->vla-object (ssname _ss (setq _in (1- _in))))); get 
     (if (= (vla-get-layer _obj) "obj2")
       (vla-put-layer _obj "obj1")
       (vla-put-layer _obj "0")
     )
   )
 )
)

Share this post


Link to post
Share on other sites
Solitechcadsolutions

i added layer selection filter but its not working...

Share this post


Link to post
Share on other sites
Grrr

Another:

(defun C:AutoLayerChange ( / L _LayersList _Recapitalise lyrs SS i enx lyr tmp )
 
 (setq L ; Note: layer names are case insensitive - assoc list of: (<old layer> . <new layer>)
   '( ; Place your inputs here:
     ("obj2" . "obj1")
     ("dim" . "0")
   ); list
 ); setq L
 
 (defun _LayersList ( / d L )
   (while (setq d (tblnext "LAYER" (not d)))
     (setq L (cons (cdr (assoc 2 d)) L))
   )
 )
 
 (defun _Recapitalise ( itm L )
   (vl-some (function (lambda (x) (if (= (strcase itm) (strcase x)) x))) L)
 )
 
 (and
   (setq lyrs (_LayersList))
   (or
     (setq L (apply (function append) (mapcar (function (lambda (x / tmp) (if (setq tmp (_Recapitalise (car x) lyrs)) (list (cons tmp (cdr x)))))) L)))
     (prompt "\nNo valid layers were found.")
   ); or
   (or
     (prompt "\nSelect objects to change layers: ")
     (setq SS (ssget "_:L-I" (list (cons 8 (setq tmp (substr (apply (function strcat) (mapcar (function (lambda (x) (strcat "," (car x)))) L)) 2))))))
     (prompt (strcat "\nNo objects were found on \"" tmp "\"  layers."))
   ); or
   (repeat (setq i (sslength SS))
     (setq enx (entget (ssname SS (setq i (1- i)))))
     (setq lyr (assoc 8 enx))
     (entmod (subst (cons 8 (cond ( (cdr (assoc (setq tmp (cdr lyr)) L)) ) ( tmp ) )) lyr enx))
   ); repeat
 ); and
 (princ) 
); defun C:AutoLayerChange

Share this post


Link to post
Share on other sites
satishrajdev

What objects are you trying to select? Post your code here so that we can check it

Share this post


Link to post
Share on other sites
Solitechcadsolutions

(defun c:test ( / e i l ss)

(if (setq ss (ssget (list (cons 8 "dim,obj2")))) ;

(repeat (setq i (sslength ss))

(setq e (entget (ssname ss (setq i (1- i)))))

(setq l (cdr (assoc 8 e)))

(cond ((eq l "obj2") (entmod (subst (cons 8 "obj1") (assoc 8 e) e)))

((eq l "dim") (entmod (subst (cons 8 "0") (assoc 8 e) e)))

)

)

)

(princ)

)

 

 

it is not working ...

 

 

and code which is shared by ziele is working but it is converting both layer to "0" layer it should converted (obj2 to obj1 and dim to 0)

Share this post


Link to post
Share on other sites
Solitechcadsolutions
Another:

(defun C:AutoLayerChange ( / L _LayersList _Recapitalise lyrs SS i enx lyr tmp )
 
 (setq L ; Note: layer names are case insensitive - assoc list of: (<old layer> . <new layer>)
   '( ; Place your inputs here:
     ("obj2" . "obj1")
     ("dim" . "0")
   ); list
 ); setq L
 
 (defun _LayersList ( / d L )
   (while (setq d (tblnext "LAYER" (not d)))
     (setq L (cons (cdr (assoc 2 d)) L))
   )
 )
 
 (defun _Recapitalise ( itm L )
   (vl-some (function (lambda (x) (if (= (strcase itm) (strcase x)) x))) L)
 )
 
 (and
   (setq lyrs (_LayersList))
   (or
     (setq L (apply (function append) (mapcar (function (lambda (x / tmp) (if (setq tmp (_Recapitalise (car x) lyrs)) (list (cons tmp (cdr x)))))) L)))
     (prompt "\nNo valid layers were found.")
   ); or
   (or
     (prompt "\nSelect objects to change layers: ")
     (setq SS (ssget "_:L-I" (list (cons 8 (setq tmp (substr (apply (function strcat) (mapcar (function (lambda (x) (strcat "," (car x)))) L)) 2))))))
     (prompt (strcat "\nNo objects were found on \"" tmp "\"  layers."))
   ); or
   (repeat (setq i (sslength SS))
     (setq enx (entget (ssname SS (setq i (1- i)))))
     (setq lyr (assoc 8 enx))
     (entmod (subst (cons 8 (cond ( (cdr (assoc (setq tmp (cdr lyr)) L)) ) ( tmp ) )) lyr enx))
   ); repeat
 ); and
 (princ) 
); defun C:AutoLayerChange

 

 

working for me thank you... but need to understand your code :)

Share this post


Link to post
Share on other sites
satishrajdev
(defun c:test ( / e i l ss)

(if (setq ss (ssget (list (cons 8 "dim,obj2")))) ;

(repeat (setq i (sslength ss))

(setq e (entget (ssname ss (setq i (1- i)))))

(setq l (cdr (assoc 8 e)))

(cond ((eq l "obj2") (entmod (subst (cons 8 "obj1") (assoc 8 e) e)))

((eq l "dim") (entmod (subst (cons 8 "0") (assoc 8 e) e)))

)

)

)

(princ)

)

 

 

it is not working ...

 

 

and code which is shared by ziele is working but it is converting both layer to "0" layer it should converted (obj2 to obj1 and dim to 0)

 

This code working fine at my end. Please check properly.

 

I checked zilele's code also that also works fine. something is wrong are your end

Share this post


Link to post
Share on other sites
Solitechcadsolutions
This code working fine at my end. Please check properly.

 

I checked zilele's code also that also works fine. something is wrong are your end

 

 

 

Well sathish I am sorry but its not working on my pc .. I tried many times... if you could help me on this ...

Share this post


Link to post
Share on other sites
Tharwat

Hi Grrr,

I think you need to exclude the VIEWPORT object from the selection set since you have used the entmod function besides that, you don't need to search if the target layer is found or existed since the cons function could create that target layer name with default values.

Share this post


Link to post
Share on other sites
SLW210

Please read the Code Posting Guidelines and have your Code to be included in Code Tags.[NOPARSE]

Your Code Here[/NOPARSE]

=

Your Code Here

Share this post


Link to post
Share on other sites
Grrr
Hi Grrr,

I think you need to exclude the VIEWPORT object from the selection set since you have used the entmod function besides that, you don't need to search if the target layer is found or existed since the cons function could create that target layer name with default values.

 

Hi Tharwat,

Thanks for the advices.. I have one question regarding the VIEWPORT:

Wheres a case for the user to select a VIEWPORT object? When working in paperspace (my guess) ?

I always work in modelspace, so I never regarded the possibility for this issue. :geek:

 

BTW for the second remark, I actually don't look if the targed layer already exists - just making sure if OP provides list of dotted pairs:

'( ; The routine will skip entmod'ing "Layer1"
 ("obj2" . "obj1")
 ("dim" . "0")
 ("Layer1")
)

But I agree its a bit inefficient to entmod with the same layer.

Share this post


Link to post
Share on other sites
Solitechcadsolutions

will take care of this next time thank you for your reminder.

Share this post


Link to post
Share on other sites
Tharwat
Hi Tharwat,

Thanks for the advices.. I have one question regarding the VIEWPORT:

Wheres a case for the user to select a VIEWPORT object? When working in paperspace (my guess) ?

I always work in modelspace, so I never regarded the possibility for this issue. :geek:

 

We can't guess that all users work from / in Model space.

 

 

BTW for the second remark, I actually don't look if the targed layer already exists - just making sure if OP provides list of dotted pairs:

Agree with that but that would ignore moving objects to targeted layer if its not existed and that is the point.

 

Hope that my comment did not make any confusion.

Share this post


Link to post
Share on other sites
Solitechcadsolutions
Hi Grrr,

I think you need to exclude the VIEWPORT object from the selection set since you have used the entmod function besides that, you don't need to search if the target layer is found or existed since the cons function could create that target layer name with default values.

 

 

 

tharwat if you could help on sathish's code why it is not working on my system ? any thoughts ?

 

 

test

Share this post


Link to post
Share on other sites
Tharwat

Solitechcadsolutions, you need to describe your goal of the program very clearly to all members whom wanting to help you and write the correct codes for that purpose.

 

For instance;

What are the objects that you are trying to move them to that new layer name?

Would you like to select certain objects or let the program select them all?

Do you have the objects on unlocked layers?

 

And so on.

Share this post


Link to post
Share on other sites
Lee Mac

I agree with the use of an association list, but the code could be greatly simplified, for example -

(defun c:laymap ( / enx idx lay map sel )
   (setq map
       (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x)))
          '(
               ("obj2" . "obj1")
               ("dim"  . "0")
           )
       )
   )
   (if (setq sel (ssget "_:L" (append '((-4 . "<OR")) (mapcar '(lambda ( x ) (cons 8 (car x))) map) '((-4 . "OR>")))))
       (repeat (setq idx (sslength sel))
           (if (setq idx (1- idx)
                     enx (entget (ssname sel idx))
                     lay (cdr (assoc (strcase (cdr (assoc 8 enx))) map))
               )
               (entmod (subst (cons 8 lay) (assoc 8 enx) enx))
           )
       )
   )
   (princ)
)

Share this post


Link to post
Share on other sites
Grrr

Nice one Lee,

I didn't realise you could filter the layers like that, with ignoring the case of their names! :thumbsup:

Share this post


Link to post
Share on other sites
Solitechcadsolutions

thank you lee and grr that codes are working nicely for me .... :)

 

 

Great

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×