Jump to content

Changing layers by selection


crgonzo

Recommended Posts

I have layer names that I'm trying to modify by selection.  
I-EQPT-21, for instance, will change to XI-EQPT-81 representing a change 
from new to existing.  In other cases, the opposite is true.  The numbers 
represent the layer color and the X represents "existing".  I was able to 
modify some code written by Lee Mac for RubberDinero to make the change 
from I-EQPT-21 to XI-EQPT-81 by selection but can't figure out how to go 
in reverse.  I can tell that the ENTMOD code removes the last 3 characters 
of the layer name when switching back.  Obviously, I don't need that to happen.  
Is it possible to easily do what I need without completely rewriting the code?  
Thanks a bunch for any tips.  The original code used (thanks LeeMac) is below.

 

 

(defun c:lay2pr ( / enx idx lay sel )
   (if (setq sel (ssget "_:L" '((8 . "~*-PR"))))
       (repeat (setq idx (sslength sel))
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
           )
           (entmod (subst (cons 8 (matchlayer (cdr lay) (strcat (cdr lay) "-PR"))) lay enx))
       )
   )
   (princ)
)
(defun c:pr2lay ( / enx idx lay sel )
   (if (setq sel (ssget "_:L" '((8 . "*?-PR"))))
       (repeat (setq idx (sslength sel))
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
           )
           (entmod (subst (cons 8 (matchlayer (cdr lay) (substr (cdr lay) 1 (- (strlen (cdr lay)) 3)))) lay enx))
       )
   )
   (princ)
)
(defun matchlayer ( src new )
   (or (tblsearch "layer" new)
       (entmake
           (subst
               (cons 2 new)
               (cons 2 src)
               (vl-remove-if '(lambda ( x ) (member (car x) '(-1 5 102 360))) (entget (tblobjname "layer" src)))
           )
       )
   )
   new
)
(princ)

 

 

Link to comment
Share on other sites

When posting code.

 

Need to answer some questions

 

Do the layers follow a pattern?

I-EQPT-##

 

Is their a pattern to the renaming the layers?

I-EQPT-21 > XI-EQPT-81 (add x and new color?)

 

By Reverse you want to select something on layer

XI-EQPT-## and create a layer named I-EQPT-##

 

Its the weekend now so most people don't post *looks at @Steven P*  probably won't get a response til Monday.

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

Maybe this will help you along a bit? MHUPP will no doubt come up with something nicer - he usually does.

 

Just making assumptions from MHUPPS questions,

The pattern doesn't mater so much here

Adding an X to start with

Adding just the colour code to finish (1 for red, ->254 for grey) - removing -any- suffix number and '-'

 

Reversed to remove -any- X at the start of the layer and -any- number at the end replaced by'21'

 

Done this so that newlayname shows how I was building up the new layer name.

Commented out original lines where not needed and additional lines are not indented.

 

 

 

(defun c:lay2x ( / enx idx lay sel col newlayname)
   (if (setq sel (ssget "_:L" '((8 . "~X*")))) ;; Select objects if layer doesn't begin with X
       (repeat (setq idx (sslength sel))       ;; Loop through selection set
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
col (rtos (cdr (assoc 62 enx))) ; get colour as a string
           ) ; end setq

(setq newlayname (strcat "X" (cdr lay) ) )                        ; add X to start
(setq newlayname (vl-string-right-trim "-0123456789" newlayname)) ; trim away suffix numbers and suffix '-'
(setq newlayname (strcat newlayname "-" col))                     ; add in colour suffix.

;;           (entmod (subst (cons 8 (matchlayer (cdr lay) (strcat (cdr lay) "-PR"))) lay enx))
(entmod (subst (cons 8 (matchlayer (cdr lay) newlayname)) lay enx)) ; update entity definition
       ) ; end repeat
   )     ; end if
   (princ)
)

(defun c:x2lay ( / enx idx lay sel newlaynbame)
   (if (setq sel (ssget "_:L" '((8 . "X*")))) ;; Select layers beginning with X
       (repeat (setq idx (sslength sel))      ;; Loop thrpugh selection set
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
           ) ; end setq

(setq newlayname (vl-string-left-trim "X" (cdr lay)))             ; trim away X prefix
(setq newlayname (vl-string-right-trim "-0123456789" newlayname)) ; trim away suffix numbers and suffix '-'
(setq newlayname (strcat newlayname "-21"))                       ; add in '21' suffix as a string.


;;           (entmod (subst (cons 8 (matchlayer (cdr lay) (substr (cdr lay) 1 (- (strlen (cdr lay)) 3)))) lay enx))
(entmod (subst (cons 8 (matchlayer (cdr lay) newlayname)) lay enx)) ; update entity definition
       )     ; end repeat
   )         ; end if
   (princ)
)

(defun matchlayer ( src new )
   (or (tblsearch "layer" new)
       (entmake
           (subst
               (cons 2 new)
               (cons 2 src)
               (vl-remove-if '(lambda ( x ) (member (car x) '(-1 5 102 360))) (entget (tblobjname "layer" src)))
           )
       )
   )
   new
)

 

 

 

ps. this wasn't me, I wasn't here.

 

Edited by Steven P
  • Like 1
Link to comment
Share on other sites

MHUPP, answer to your queries is yes except for the last item.  I would also like to move the selected items to the newly created layer I-EQPT-XX.  I should also note that this is not the only layer name I have to deal with, if that matters.  There are TEXT, GROUNDING, SYMBOLS, etc.  

 

STEVEN P, thanks for the code.  I'll give it a try today.  Looking forward to seeing results.  This has been a challenge.

Link to comment
Share on other sites

Steven P, I tried your code (lay2x) and the following is the result.  I ran it line by line, selected 2 symbols and 2 signal lines.

 

(if (setq sel (ssget "_:L" '((8 . "~X*"))))
(_> (repeat (setq idx (sslength sel))
((_> (setq enx (entget (ssname sel (setq idx (1- idx))))
(((_> lay (assoc 8 enx)
(((_> col (rtos (cdr (assoc 62 enx)))
(((_> )
((_> (setq newlayname (strcat "X" (cdr lay) ) )
((_> (setq newlayname (vl-string-right-trim "-0123456789" newlayname))
((_> (setq newlayname (strcat newlayname "-" col))
((_> (entmod (subst (cons 8 (matchlayer (cdr lay) newlayname)) lay enx))
((_> ))
Select objects: 1 found
Select objects: 1 found, 2 total
Select objects: 1 found, 3 total
Select objects: 1 found, 4 total
Select objects:
bad argument type: numberp: nil
 

Link to comment
Share on other sites

bad argument type: numberp: nil - the LISP is wanting a number and isn't getting one, nil means it is getting a 'nil' - nothing at all.

 

The only part I deal with number are in the colour codes so that's where the error is.... and the one check I didn't do must be the problem.... if the objects are 'bylayer' colour then the colour code returned is a nil.

 

Try this:

Set the line (setq col "21") to be whatever you want, keeping the suffix as a string (enclosed in quotes)

 

(defun c:lay2x ( / enx idx lay sel col newlayname)
   (if (setq sel (ssget "_:L" '((8 . "~X*")))) ;; Select objects if layer doesn't begin with X
       (repeat (setq idx (sslength sel))       ;; Loop through selection set
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
                 col (assoc 62 enx)
           ) ; end setq
          (if (= nil col)
            (setq col "21")
            (setq col (rtos (cdr (assoc 62 enx))))
          )
         (setq newlayname (strcat "X" (cdr lay) ) )                        ; add X to start
         (setq newlayname (vl-string-right-trim "-0123456789" newlayname)) ; trim away suffix numbers and suffix '-'
         (setq newlayname (strcat newlayname "-" col))                     ; add in colour suffix.
         (entmod (subst (cons 8 (matchlayer (cdr lay) newlayname)) lay enx)) ; update entity definition
       ) ; end repeat
   )     ; end if
   (princ)
)

 

 

  • Agree 1
Link to comment
Share on other sites

StevenP,  I tried your code change.  It partially works.  It creates the new layers and moves the selected items to their appropriate layers.  It doesn't set the new layer color, though.  The color of the original entities remains since the color of the new layer has been kept like the old.  The entities are set to "bylayer" for color.  Is there some way to use entget and entmod to force the color change?   I've been doing some "light" reading on the subject but don't think I'm quite ready to tear apart what you've accomplished.  Thanks a lot for getting this far.  It's not really a big issue to go into layermanager and make the changes, if that becomes necessary.

Link to comment
Share on other sites

Yes, that's possible, in the code I get the entities colour (the variable I use is 'col' - but as a string, easy to turn back to a number as required)

 

You can add another entmod line in:

         (entmod (subst (cons 8 (matchlayer (cdr lay) newlayname)) lay enx)) ; update entity definition

to be something like

         (entmod (subst (cons 62 0) (assoc 62 enx) enx)) ; update entity definition

 

to go bylayer for the entities

 

and do a similar thing to the layer to use the colour, col. You could also change the layer colour with (command "layer" ..... ) following the prompts in the command line to fill it in, layer name will be newlayname - as you would usually do if you were manually changing the layer name

 

 

 

 

you might tell, CAD is off just now so no testing, just a hint of what to look for - will come back to this tomorrow (given a decent workload of course)

 

Link to comment
Share on other sites

I was thinking of adding some simple lines to change the layer properties.  Even thought of making sure all the required layers are available then merging as needed.  So many way to skin the cat.  It's been a busy day so no time to mess with the code, really.  Tomorrow will hopefully provide a few minutes to do some goofing.

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