cond/if/and statement help

Discussion in 'AutoCAD' started by John Hatfield, May 26, 2004.

  1. 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
     
    John Hatfield, May 26, 2004
    #1
  2. John Hatfield

    Jeff Mishler Guest

    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:
     
    Jeff Mishler, May 26, 2004
    #2
  3. John Hatfield

    Jürg Menzi Guest

    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
     
    Jürg Menzi, May 26, 2004
    #3
  4. John Hatfield

    NParcon Guest

    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
     
    NParcon, May 26, 2004
    #4
  5. John Hatfield

    Joe Burke Guest

    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
     
    Joe Burke, May 26, 2004
    #5
  6. John Hatfield

    Steve Doman Guest

    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
     
    Steve Doman, May 26, 2004
    #6
  7. John Hatfield

    Rudy Tovar Guest

    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.
     
    Rudy Tovar, May 26, 2004
    #7
  8. John Hatfield

    TCEBob Guest

    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
     
    TCEBob, May 26, 2004
    #8
  9. John Hatfield

    NParcon Guest

    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..
     
    NParcon, May 26, 2004
    #9
  10. John Hatfield

    Joe Burke Guest

    Noah,

    OK. I should have said, in my opinion...

    Joe Burke
     
    Joe Burke, May 27, 2004
    #10
  11. John Hatfield

    Tom Smith Guest

    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
     
    Tom Smith, May 27, 2004
    #11
  12. John Hatfield

    Tom Smith Guest

    Correction, I was testing using my own layer names. Script should be:

    laymrg t cab t cabinet y
     
    Tom Smith, May 27, 2004
    #12
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.