Jump to content

code that select all object and convert in to mentioned layer


Solitechcadsolutions

Recommended Posts

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

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • Solitechcadsolutions

    8

  • Tharwat

    3

  • satishrajdev

    3

  • Grrr

    3

Top Posters In This Topic

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)
)

Link to comment
Share on other sites

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")
     )
   )
 )
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

(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)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...