if statement help

Discussion in 'AutoCAD' started by molokaiboy, Aug 5, 2004.

  1. molokaiboy

    molokaiboy Guest

    I am trying to create this routine that will insert in block "2COL-AddROW". If that block already exists, then I want an alert message to pop up and not re-insert it in.

    I have sample code below but doesn't seem to be working. Can anyone assist?

    (if (not (tblsearch "block" 2COL-AddROW)
    (Alert "test")

    (if (tblsearch "block" DashTable-2COL)
    (progn
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")


    )
    )
    ))

    TIA

    Collin
     
    molokaiboy, Aug 5, 2004
    #1
  2. Put the name to search for in quotes:

    (tblsearch "block" "2COL-AddROW")
     
    Allen Johnson, Aug 5, 2004
    #2
  3. molokaiboy

    T.Willey Guest

    (if (tblsearch "block" "2COL-AddROW")
    (alert "Block aready in drawing, will not insert.")
    (; here put you insert function)
    )

    Tim
     
    T.Willey, Aug 5, 2004
    #3
  4. molokaiboy

    Jürg Menzi Guest

    Collin

    Doesn't work with tblsearch - see my last answer to thread 'search for
    insertion point of blocks'...

    Cheers
     
    Jürg Menzi, Aug 5, 2004
    #4
  5. molokaiboy

    Chip Harper Guest

    Something like ....

    (if (tblsearch "block" 2COL-AddROW)
    (Alert "Block is there")
    (progn
    (Do your stuff here)))
     
    Chip Harper, Aug 5, 2004
    #5
  6. The "tblsearch" will only tell you if the block has been defined in the
    drawing, not necessarily inserted.
    Another method would be -
    (if (ssget "X" (list (cons 0 "INSERT") (cons 2 "2COL-AddROW)))
    (alert "test")
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg"
    DashTable-2COLbip "" "" "0")
    )

    "2COL-AddROW". If that block already exists, then I want an alert message
    to pop up and not re-insert it in.
     
    Alan Henderson @ A'cad Solutions, Aug 5, 2004
    #6
  7. I have seen this before and I don't understand it.
    You want to insert a block. If you set it up to insert the block complete
    with path and name and the block already exists in the drawing then it will
    insert the block that is already defined in the drawing. What value is there
    in having a macro that tests for a block defined in the drawing and if it
    finds it, inserts the block without the path but if it doesn't find the
    block, inserts the block with the path and name.

    This just seems more complicated than it needs to be.

    Dave Alexander
    Keen Engineering Co. Ltd.
    www.keen.ca

    "2COL-AddROW". If that block already exists, then I want an alert message
    to pop up and not re-insert it in.
     
    Dave Alexander, Aug 5, 2004
    #7
  8. molokaiboy

    molokaiboy Guest

    Thanks everyone for the replies.

    Collin
     
    molokaiboy, Aug 5, 2004
    #8
  9. molokaiboy

    molokaiboy Guest

    I get this error msg when trying to load:

    ; error: syntax error

    (if(tblsearch "block" 2COL-AddROW)
    (Alert "Block Already Exists!")
    (progn
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")
    )

    What is this?

    TIA
    Collin
     
    molokaiboy, Aug 5, 2004
    #9
  10. molokaiboy

    Chip Harper Guest

    It's an incomplete portion of code. At the very least to load it you need to
    add:

    (defun c:MyCode ( )

    <Example code Here>

    )

    Also if your only going to run the single command you can drop the (progn
    .....

    So it would look like this ...

    (defun c:MyCode ( )
    (if(tblsearch "block" 2COL-AddROW)
    (Alert "Block Already Exists!")
    (command "-layer" "S" "notes" "" "" "-insert"
    "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")
    ) ; end if
    ) ; end defun
     
    Chip Harper, Aug 5, 2004
    #10
  11. molokaiboy

    molokaiboy Guest

    Yes, have the rest of the code. For some reason, when I comment out the "Alert..." it loads fine.

    Collin
     
    molokaiboy, Aug 5, 2004
    #11
  12. molokaiboy

    molokaiboy Guest

    Chip,

    I found the error. Thanks.

    Collin
     
    molokaiboy, Aug 5, 2004
    #12
  13. molokaiboy

    molokaiboy Guest

    Chip,

    I have multiple if searches (3 or more). What is the best way to consolidate them?

    (if(tblsearch "block" 2COL-AddROW)
    (Alert "Block already exists!")
    (progn
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")
    )
    ;)

    (if(tblsearch "block" DashTable-3COL)
    (Alert "Block already exists!")
    (progn
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-3COL-AddROW.dwg" DashTable-3COLbip "" "" "0")
    )
    ;)

    TIA

    Collin
     
    molokaiboy, Aug 5, 2004
    #13
  14. molokaiboy

    molokaiboy Guest

    Alan,
    This would seem to be the better approach.
    What defines ssget? I think I am missing something here.

    Collin
     
    molokaiboy, Aug 5, 2004
    #14
  15. molokaiboy

    Jeff Mishler Guest

    it's missing a double quote.....

    (if (ssget "X" (list (cons 0 "INSERT") (cons 2 "2COL-AddROW")))
    (alert "2COL-AddROW block found in drawing!")
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg"
    DashTable-2COLbip "" "" "0")
    )

    HTH,
     
    Jeff Mishler, Aug 5, 2004
    #15
  16. sorry for the missing " on the first line (at end of block name).
    The ssget is used to search for all blocks (called insert by lisp) in the
    drawing that are named 2COL-AddRow.
    Note, if this is a huge drawing, this may take few seconds.
    I can not vouch for your line starting with (command .....
    But here is a version that will display alert boxes -
    (if (ssget "X" (list (cons 0 "INSERT") (cons 2 "2COL-AddROW")))
    (alert "2COL-AddROW already exists in drawing")
    (alert "Please insert 2COL-AddROW")
    )
     
    Alan Henderson @ A'cad Solutions, Aug 5, 2004
    #16
  17. molokaiboy

    molokaiboy Guest

    I got it to load, but the alert box doesn't pop up stop the insertion from happening when the block is found.

    (if (ssget "x" (list (cons 0 "INSERT") (cons 2 "2COL-AddROW")))
    (Alert "test")
    (progn
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")
    );progn close
    );if close

    What am I missing?

    Collin
     
    molokaiboy, Aug 5, 2004
    #17
  18. molokaiboy

    J. Logan Guest

    Missing quote at end of

    (cons 2 "2COL-AddROW)))

    Should be

    (cons 2 "2COL-AddROW")))

    ?

    J. Logan
     
    J. Logan, Aug 5, 2004
    #18
  19. molokaiboy

    molokaiboy Guest

    I found the missing quote, but it still doesn't run the alert box and it inserts in the block even if it already exists.

    What am I doing wrong?

    Collin
     
    molokaiboy, Aug 5, 2004
    #19
  20. did you try the new code with the 2 alert boxes?

    inserts in the block even if it already exists.
     
    Alan Henderson @ A'cad Solutions, Aug 5, 2004
    #20
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.