Solitechcadsolutions Posted July 9, 2018 Share Posted July 9, 2018 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 Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted July 9, 2018 Share Posted July 9, 2018 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) ) Quote Link to comment Share on other sites More sharing options...
ziele_o2k Posted July 9, 2018 Share Posted July 9, 2018 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") ) ) ) ) Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 i added layer selection filter but its not working... Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 9, 2018 Share Posted July 9, 2018 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 Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted July 9, 2018 Share Posted July 9, 2018 What objects are you trying to select? Post your code here so that we can check it Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 (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) Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 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 Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted July 9, 2018 Share Posted July 9, 2018 (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 Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 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 ... Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 9, 2018 Share Posted July 9, 2018 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. Quote Link to comment Share on other sites More sharing options...
SLW210 Posted July 9, 2018 Share Posted July 9, 2018 Please read the Code Posting Guidelines and have your Code to be included in Code Tags.[NOPARSE] Your Code Here[/NOPARSE] = Your Code Here Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 9, 2018 Share Posted July 9, 2018 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. 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. Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 will take care of this next time thank you for your reminder. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 9, 2018 Share Posted July 9, 2018 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. 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. Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 9, 2018 Author Share Posted July 9, 2018 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 Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 9, 2018 Share Posted July 9, 2018 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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 9, 2018 Share Posted July 9, 2018 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) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 10, 2018 Share Posted July 10, 2018 Nice one Lee, I didn't realise you could filter the layers like that, with ignoring the case of their names! Quote Link to comment Share on other sites More sharing options...
Solitechcadsolutions Posted July 11, 2018 Author Share Posted July 11, 2018 thank you lee and grr that codes are working nicely for me .... Great Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.