If/Then Statement for a LISP Command?

If/Then Statement for a LISP Command?

Anonymous
Not applicable
2,188 Views
2 Replies
Message 1 of 3

If/Then Statement for a LISP Command?

Anonymous
Not applicable

Hello! 

Im trying to make a LISP command so that I can change the name of some selected layers. It runs properly if all the layers are present in the DWG, but if a layer in the LISP is missing in the DWG, it cancels the command. Is there a way I can edit the LISP to have a sort of if/then statement so that if the layer is missing from the DWG, then it skips it and continues on to the layers that ARE there?

 

Heres my LISP, Thankyou:)

 

(defun c:AtoIFURN ()
(prompt "All layers have begun being renamed")
(command "CMDECHO" "0")(terpri)
(prompt "Setting layers")(princ)
(command "layer" "unlock" "*" "thaw" "*" "on" "*" "")
(command "layer" "r" "A-FURN-FIXD" "I-FURN-FIXD" "" "")
(command "layer" "r" "A-FURN-SEAT" "I-FURN-SEAT" "" "")
(command "layer" "r" "A-FURN" "I-FURN" "" "")
(command "layer" "s" "0" "")
(command "purge" "LA" "" "N" "" "")
(command "CMDECHO" "1")
(prompt "All layers have been renamed... We hope.")
(princ)
);end defun

0 Likes
2,189 Views
2 Replies
Replies (2)
Message 2 of 3

CodeDing
Advisor
Advisor

@Anonymous,

 

Try this code. I added comments to help identify what's happening. I try avoiding using "command" functions wherever I can, but renaming layers is a little bit more advanced so we'll stick to the basics.

(defun c:AtoIFURN ( / oldLayers layer newName)
;if we know layers to rename, save in list
(setq oldLayers (list "A-FURN-FIXD"
		      "A-FURN-SEAT"
		      "A-FURN"
		      ))
(setvar 'CMDECHO 0)
;for each "layer" (whatever you want to name it) in our list (oldLayers)
(foreach layer oldLayers
  ;if layer exists in dwg
  (if (tblsearch "LAYER" layer)
    (progn
      ;create new name for layer, by replacing first letter
      (setq newName (strcat "I" (substr layer 2)))
      ;rename old layer
      (command "-LAYER" "r" layer newName "")
    );progn
  ;else
    (prompt (strcat "\nLayer, " layer ", was not found..."))
  );if
);foreach
;set layer "0" to current
(setvar 'CLAYER "0")
(setvar 'CMDECHO 1)
;purge layers
(command "-PURGE" "LA" "" "N" "" "")
(princ)
);defun

Best,

~DD

0 Likes
Message 3 of 3

Anonymous
Not applicable

It works! Thank you so much, I owe ya one:)

0 Likes