(Update) Is there a lisp routine to switch layers back and forth?

(Update) Is there a lisp routine to switch layers back and forth?

Anonymous
Not applicable
1,167 Views
3 Replies
Message 1 of 4

(Update) Is there a lisp routine to switch layers back and forth?

Anonymous
Not applicable

Hi everybody,

 

Im back. So I did a lot of research trying to figure out how to get this program to run. Switching one layer (S-BEAM-STEEL) to an existing layer with a different color (E-BEAM-STEEL).  I tried messing a bit with the laymrg command but wasn't getting any luck. after hours of looking I found this one forum with a code that could work for me. I tried manipulating it again and again to get to do what want but didnt get to far. So far the code creates the existing layer but i cant figure out how to revert back to the original with the same command. Would i essentially have to repeat this whole code just exclude the EXT part?

 

Other things im trying to figure out

Is there a way where instead of naming the existing layer "S-ANNO-BREAK-EXST", it is named "E-ANNO-BREAK"?

 

Also, instead of selecting one line/object at a time is there a way to select multiple and change them at the same time? The code below was posted on the original forum but i couldnt get it to work. I'll admit i didnt spend to much on this part. I was focused to much on the other issues.

 

 

AA:ExlEntityToChange (entsel "\nSelect the entity to move to existing:")

(princ "\n"\nSelect the entity to move to existing:")
AA:ExlEntityToChange (ssget)

(princ "\n"\nSelect the entity to move to existing:")
AA:ExlEntityToChange (ssget '((0 . "INSERT")) )

 

 

Im a newbie and getting better at reading code but for the life of me i cant figure out whats going on in PART 2 of the code.

 

Any help is welcomed and appreciated. Thanks in advance.

 

 

Link to the original

http://forums.augi.com/showthread.php?167577-Existing-layer-lisp

 

 

;**********************************************************PART-1************************************************************************

(defun c:TEST ( / AA:ExlClayer AA:ExlEntityToChange AA:ExlEntityName AA:ExlEntityLayer AA:ExlNewLayerName AA:ExlNewLayerColor)
  
  (setq AA:ExlClayer (getvar "clayer")
	AA:ExlEntityToChange (entsel "\nSelect the entity to move to existing:")
	)
  (while (or (null AA:ExlEntityToChange))
    (princ "\nPlease try again.")
    (setq AA:ExlEntityToChange (entsel "\nSelect the entity to move to existing:"))
    )

;**********************************************************PART-2************************************************************************
  
(setq AA:ExlEntityName (car AA:ExlEntityToChange)
	AA:ExlEntityLayer (cdr (assoc 8 (entget AA:ExlEntityName)))
	)
  (if (wcmatch AA:ExlEntityLayer "*-EXST,0,Defpoints,InfoStamp,MHatch,EHatch,Alvine,MCalc,ETXT,Ttxt,Wipeout,Building")
    (progn (princ "\nThe entity is already on an existing layer!\nPlease try again.\n") (AA:ExlClearVariables) (exit) (princ))

;**********************************************************PART-4************************************************************************
 
(setq AA:ExlNewLayerName (strcat AA:ExlEntityLayer "-EXST"))
	)
  (LayerColorMatrix)
  (setvar "cmdecho" 0)
  (if (not (tblsearch "Layer" AA:ExlNewLayerName))
    (command "-layer" "make" AA:ExlNewLayerName "color" AA:ExlNewLayerColor "" "lt" AA:ExlLineType "" "")
    )
  (command ".chprop" AA:ExlEntityToChange "" "layer" AA:ExlNewLayerName "")
  (setvar "clayer" AA:ExlClayer)
  (setvar "cmdecho" 1)
  (AA:ExlClearVariables)
  (princ)
)
;**********************************************************PART-5************************************************************************ 
(defun LayerColorMatrix ( / AA:ExlLayerList)
  (setq AA:ExlLayerList (list 
"S-ANNO-BREAK-EXST"
"S-ANNO-GRID-EXST"
)
	)
  (if (not (member AA:ExlNewLayerName AA:ExlLayerList))
    (progn (setq AA:ExlNewLayerColor "216" AA:ExlLineType "Continuous"
		 AA:ExlLayerReport "No color definition has been found for this layer.\n\nA layer called:  ")
      (setq AA:ExlLayerReport (strcat AA:ExlLayerReport AA:ExlNewLayerName " has been created with the color 216."))
    
      )
    (princ)
    )  


;**********************************************************PART-6************************************************************************ 

  (if (= AA:ExlNewLayerName "S-ANNO-BREAK-EXST") (setq AA:ExlNewLayerColor "135" AA:ExlLineType "Continuous") (princ))
  (if (= AA:ExlNewLayerName "S-ANNO-GRID-EXST") (setq AA:ExlNewLayerColor "216" AA:ExlLineType "Continuous") (princ))

  (princ)
)

;**********************************************************PART-7************************************************************************
 
(defun AA:ExlClearVariables ()
  (setq AA:ExlClayer nil AA:ExlEntityToChange nil AA:ExlEntityName nil AA:ExlEntityLayer nil AA:ExlNewLayerName nil AA:ExlNewLayerColor nil
	AA:ExlLayerList nil AA:ExlLineType nil)
  (princ)
)

 

 

0 Likes
1,168 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant

It looks too complicated to me. I made mine, hope simpler.

 

(defun c:LayerSwitch (/ lys ss 1st 2nd i lay)

  (setq lys '("E-BEAM-STEEL" . "S-BEAM-STEEL"))

  (or *ls-trg*
      (setq *ls-trg* "S-beam-steel"))

  (if (and (setq ss (ssget "_:L"))

           (or (tblsearch "LAYER" (car lys)) 									; if 1st layer not extist, create
               (vl-cmdf "_.LAYER" "_NEW" (car lys) "_Color" 7 (car lys) "_LT" "Continuous" (car lys) ""))

           (or (tblsearch "LAYER" (cdr lys))									; if 2nd layer not extist, create
               (vl-cmdf "_.LAYER" "_NEW" (cdr lys) "_Color" 1 (cdr lys) "_LT" "Continuous" (cdr lys) ""))
           )
    (progn

      (repeat (setq i (sslength ss)) ; go thru all the selected objects
        
        (setq lay (cdr (assoc 8 (entget (ssname ss (setq i (1- i)))))))	 ; get its layer
 
        (if (= lay (car lys)) (setq 1st T)) 	; if is equal to the 1st, set it True
        (if (= lay (cdr lys)) (setq 2nd T))) 	; if is equal to the 1st, set it True

      (cond ((or (and 1st 2nd)			; there are objects in the both layers within your selection set.
                 (not (or 1st 2nd)))		; there aren't objects in any of your two layers.

             (initget (strcat "E-beam-steel S-beam-steel")			; then pick the target layer
             (setq trg (cond ((getkword (strcat "\nSwitch layer to [E-beam-steel/S-beam-steel] <" *ls-trg* ">")))
                             (*ls-trg*))))   

            (1st				; if there are object at 1st layer only
             
             (setq *ls-trg* (cdr lys)))		; set the target layer to 2nd

            (2nd				; if there are object at 2nd layer only	

             (setq *ls-trg* (car lys))))	; set the target layer to 1st

      (command "_.chprop" ss "" "_Layer" *ls-trg* "")))  ; change the layer

  (princ)
  )
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thank you for your time with helping me with this issue. When i load the lsp i get a error message "; error: malformed list on input". I tried searching what this error means and using visual lisp to try and figure out if there are some unbalanced brackets, but i couldn't find any.

0 Likes
Message 4 of 4

ronjonp
Mentor
Mentor

Add a closing bracket on the line above the (princ).

0 Likes