AutoCAD 2002 I have tried and tried to get the following code to work with various forms of success and plain old blunders. Here is what I am trying to do. I want to be able to test for two different layers and perform different functions if one or both layer names exist. Example: 1.) If layer "CAB" exists and layer "CABINET" does not exist, then rename layer "CAB" to "CABINET". 2.) If Layer "CAB" and "CABINET" exists, then change everything on layer "CAB" to layer "CABINET". Here is the code I have. Although it does not work, at least I am trying. I have read the web page AFRALISP and the write up on COND vs. IF and I can't seem to get either to work. I apologize for the code format. I used the VLISP in AutoCAD. (defun c:CABFIX (/) (setq ss1 (ssget "X" '((-4 . "<AND") (8 . "CAB") (8 . "CABINET") (-4 . "AND>")) ) ) ) (setq ss2 (ssget "X" '((-4 . "<NOT") (8 . "CABINET") (-4 . "NOT>")))) (cond (ss1 (command "chprop" ss1 "" "p" "la" "CABINET" "c" "bylayer" "") ) (t (princ "\nNo CAB Layer") ) ; end cond 1 ;; (cond (ss2 (command "-rename" "layer" "CAB" "CABINET") ) (t (princ "\nLayer CAB renamed to CABINET") ) ; end cond 2 ) ; end defun Thanks in advance, John Hatfield
Hi John, Give this a try: (defun c:CABFIX (/ ss1) (cond ((and (tblsearch "Layer" "CAB") (not (tblsearch "Layer" "CABINET")) ) (command "-rename" "layer" "CAB" "CABINET") (princ "\nLayer \"CAB\" renamed to \"CABINET\"...") );end first cond ((and (tblsearch "Layer" "CAB") (tblsearch "Layer" "CABINET") ) (if (setq ss1 (ssget "X" '((8 . "CAB")))) (progn (command "chprop" ss1 "" "p" "la" "CABINET" "c" "bylayer" "") (princ (strcat "\nMoved " (itoa (sslength ss1)) " objects to layer \"CABINET\"...")) ) ) );end second cond (princ "\nLayer \"CAB\" not found in this drawing...");other cond's failed ) (princ);exit quietly );end defun Also, when creating selections sets you can drastically reduce the Filter lists when you want everything on certain layers. (setq ss1 (ssget "X" '((-4 . "<AND") (8 . "CAB") (8 . "CABINET") (-4 . "AND>"))) can be written as: (setq ss1 (ssget "X" '((8 . "CAB,CABINET")))) HTH, Jeff forms of success and plain old blunders. different layers and perform different functions if one or both layer names exist. Example:
John Hatfield In addition to Jeff's reply... You can't use the 'AND'-condition to get entities on both Layers. An entity is normally not on two Layers, ergo this filter fails: (ssget "X" '((-4 . "<AND") (8 . "CAB") (8 . "CABINET") (-4 . "AND>"))) To explain the basics on filters: '((0 . "LINE") (8 . "CAB")) = AND (selects all Lines on Layer 'CAB') '((8 . "CAB,CABINET")) = OR (selects all entities on Layers 'CAB' or 'CABINET') Cheers
Alternatively, you can also use 2 IF staements: (if (tblsearch "layer" "CAB") ;;if layer "CAB" exists, check for layer "CABINET" (if (tblsearch "layer" "CABINET") ;;if both layers "CAB" & "CABINET" exists (progn (select all objects in layer "CAB") (move all objects in layer "CAB" to layer "CABINET") );progn ;;else if only layer "CAB" exist (rename layer "CAB" to "CABINET") );end 2ND if ;;else if layer "CAB" does not exist, do nothing ;;or you can inform user );end 1ST if Noah
Noah, Which just underlines the fact, when nested ifs look tempting... cond is usually a better choice. If for no other reason than the fact a cond statement is essentially self commented. Joe Burke
John, If the goal is to merge layers, that is change old layer name to new layer name, where new layer name may or may not exist, then the "Change" command is not sufficient. You must also dig into entities on old layer which exist in inside blocks, as well as old layer referenced in vports, and check if the old layer is active and/or locked. (did I miss anything?) However in my past experience doing this sort of thing, I just wienie-out and use AutoCAD's "LayTrans" command. Steve Doman
Very well said. I was just about to comment on the very subject. Steve as mentioned, you'll also have to dig into nested blocks entities, attributes, dimensions, text, etc. This part of the discussion was posted about a couple of weeks ago. -- AUTODESK Authorized Developer http://www.Cadentity.com MASi different layers and perform different functions if one or both layer names exist. Example:trying. I have read the web page AFRALISP and the write up on COND vs. IF and I can't seem to get either to work. I apologize for the code format. I used the VLISP in AutoCAD.
Didn't know one could do that it laytrans. Help talks about translating standards and, as I have none, never followed through. However, I do use Laymrg on the express tools menu and recommend it. rs
Depends on how you look at it. As for me, 2 ifs gives me a clear understanding than using a cond BTW, the comments in the code are for the poster/readers..
As far as the logic, I think this does the job, in yet another form. Like Joe, I think that multiple/nested if's will usually collapse to a simpler form with cond. (defun c:cabfix ( ) (cond ((null (tblsearch "layer" "CAB"))) ((null (tblsearch "layer" "CABINET")) (command "rename" "layer" "CAB" "CABINET")) ((command "script" "cabfix"))) (princ)) The cabfix.scr would run LayMrg as follows: laymrg t layer1 t sf y