if statement help

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

  1. molokaiboy

    molokaiboy Guest

    yes, still doesn't work. Here is the code I have:

    (if (ssget "X" (list (cons 0 "INSERT") (cons 2 "2COL-AddROW")))
    (progn
    (alert "Block Already Exists! Use COPY Command To Instance Block")
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg" DashTable-2COLbip "" "" "0")
    );progn close
    );if close

    Collin
     
    molokaiboy, Aug 5, 2004
    #21
  2. then there is a problem with your statement -
    (command "-layer" "S" "notes" "" "" "-insert" "DashTable-2COL-AddROW.dwg"
    DashTable-2COLbip "" "" "0")
    There is an extra "" in the layer set
    Change to -
    (command "-layer" "S" "notes" "" "-insert" "DashTable-2COL-AddROW.dwg"
    DashTable-2COLbip "" "" "0")

    Plus is DashTable-2COLbip a variable with a point list?
     
    Alan Henderson @ A'cad Solutions, Aug 5, 2004
    #22
  3. molokaiboy

    molokaiboy Guest

    That still doesn't do it. I will only run if I comment out the following code:
    ;(progn
    ;);progn close

    Not sure why it is doing this.

    Collin
     
    molokaiboy, Aug 5, 2004
    #23
  4. molokaiboy

    chemlab Guest

    i don't know about anyone else but i always put a test
    in my if statements ...

    (if (= (tblsearch "block" "blockname") nil)
    (alert "msg") ; if block doesn't exist in drawing
    (progn ; if block exists run this
    (comand "insert" ...........)
    ) ; end progn
    ) ; end if

    also i would break out all the commands to seperate
    lines, more work i know but easier to debug.

    my 2 cents
     
    chemlab, Aug 6, 2004
    #24
  5. I am not questioning the manner of the test but the reason for the test in
    the first place. If you are inserting a block, what difference if the block
    is already defined in the drawing or not. If it is, you cannot over write
    with a new block a new block comming off your system and if it isn't then
    you get the block off your system anyway.

    If you could explain the rational behind this I would be very grateful.

    Dave Alexander
    Keen Engineering Co. Ltd.
    www.keen.ca
     
    Dave Alexander, Aug 6, 2004
    #25
  6. molokaiboy

    Tom Smith Guest

    Dave, I agree. It's difficult to force a redefinition of a block when needed. As you say, if the block is already defined in the drawing, and you run the insert command with an explicit path to an external file, it will still insert the existing block and ignore the path. An out-and-out bug in my opinion. As you say, testing for the block definition first accomplishes nothing

    I suppose you have to force the blockname=filename syntax, with an explicit path to the external block. In that case, again. it wouldn't mater whether or not you diid a tblsearch first.
     
    Tom Smith, Aug 6, 2004
    #26
  7. Oh well Tom,

    May be someone nows the reason for testing first to see if the block exists.

    Thanks,

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

    needed. As you say, if the block is already defined in the drawing, and you
    run the insert command with an explicit path to an external file, it will
    still insert the existing block and ignore the path. An out-and-out bug in
    my opinion. As you say, testing for the block definition first accomplishes
    nothing
    explicit path to the external block. In that case, again. it wouldn't mater
    whether or not you diid a tblsearch first.
     
    Dave Alexander, Aug 6, 2004
    #27
  8. The original post said to check to see if the block exists, if so,
    don't reinsert. Haven't read all the posts, but maybe they are
    getting off track.

    --
    Ken Alexander
    Acad2004
    Windows XP

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Aug 6, 2004
    #28
  9. In an "if" statement you have -
    1st an expression to be tested
    if true, then, only 1 then expression can be evaluated
    if not true, else, only 1 then expression can be evaluated
    To get around this limitation the "progn" can be used to evaluate multiple
    expressions.

    Try -
    (setq MyNumber 1)
    then
    (setq MyNumber 2)
    for the following 2 examples to test the if statement

    Example of only 1 expression for then & else
    (if (= MyNumber 1) ;check if statement is true
    (alert "MyNumber = 1") ;if true run this statement
    (alert "MyNumber /= 1") ;if not true run this statement
    )

    Example of only multiple expressions for then & else
    (if (= MyNumber 1) ;check if statement is true (in this case - yes)
    (progn ;progn allows multiple statements for the true test
    (alert "1st - MyNumber = 1") ;1st action in true statement
    (alert "2nd - MyNumber = 1") ;2nd action in true statement
    )
    (progn ;progn allows multiple statements for the false test
    (alert "1st - MyNumber /= 1") ;1st action in false statement
    (alert "2nd - MyNumber /= 1") ;2nd action in false statement
    )
    )
     
    Alan Henderson @ A'cad Solutions, Aug 6, 2004
    #29
  10. One reason for testing to see if a block already exists.

    I have a routine that draws steel shapes and makes a block out of them.
    Since there are hundreds of steel shapes, the blocks are created on the fly, rather than storing
    each shape on disk.
    But if the block has already been created in the drawing, there is no need to remake the block
    again, just insert the existing block name.
     
    Allen Johnson, Aug 6, 2004
    #30
  11. Hey Ken,

    You are right. I just re-read the original post. It sort of makes you think
    that the blocks are one of a kind that are changed after being inserted so
    you may want to use a different block if it already exists.
    However, the system we use here does check to see if the block exists and
    then if so uses the block defination in the drawing. I wonder if it is a
    attribute thing where a specific block always has he same attribute valve in
    a drawing so when you want to reuse the block it inerts the block already in
    the drawing without having to set the attribute.

    You know, I think I will ask the guy that wrote our system why he tests for
    the block in the drawing befor inserting the block.

    I have a routine that uses the value for the attribute as a default value
    for blocks with attributes so that if the value of the attribute is the
    same, you can accept the default or type in a new value.

    Thanks Ken
     
    Dave Alexander, Aug 6, 2004
    #31
  12. Hey Allen,

    My system went the other way. You went for a block, (hvac duct fittings
    selected by type size) and if it wasn't there, then you could create it and
    then using the generated block name for that particular fitting, (complete
    with path) you could save the block with the system name. You didn't need to
    decide on a name or select a path or anything, even care what the name was.
    Wouldn't it make sense to set up a system with a names to write block the
    blocks as you go so that you could reuse them?

    Dave Alexander
    Keen Engieering Co. Ltd.
    www.keen.ca
     
    Dave Alexander, Aug 6, 2004
    #32
  13. You are probably right Dave. I didn't take the time to read all the
    posts to see what all he was up to.

    --
    Ken Alexander
    Acad2004
    Windows XP

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Aug 6, 2004
    #33
  14. molokaiboy

    chemlab Guest

    hi,
    after reading everyone's response and taking a second
    look at what i suggested i think my code was backwards.
    perhaps i should have used a T in place of NIL?

    (if (= (tblsearch "block" "blockname") T) ; search for block
    (alert "block exists msg") ; if it exists alert user
    (progn ; if it doesn't then insert it
    (command ..... )
    )
    )

    a simple test to see if the block exists, if so alert the user if
    it doesn't exist then insert the block or whatever he wants
    to do at that point. my example doesn't explicitly state to
    redefine the block but rather do a command.
     
    chemlab, Aug 6, 2004
    #34
  15. molokaiboy

    chemlab Guest

    when using -insert in a command line doesn't the *blockname
    redefine the block?
     
    chemlab, Aug 6, 2004
    #35
  16. molokaiboy

    molokaiboy Guest

    This is correct, but it was brought to my attention that because I am trying to search for the insertion point of the block, and if it doesn't exist, then it bombs out.

    Collin
     
    molokaiboy, Aug 6, 2004
    #36
  17. molokaiboy

    chemlab Guest

    then replace the tblsearch with the filters option from above.
    IF will work without the = test but i don't prefer using if that
    way, im my opinion that could be the hangup. but, be aware
    that you can have the block in the database but not in the
    drawing space, in which case i don't think the filters will
    return true.
     
    chemlab, Aug 6, 2004
    #37
  18. molokaiboy

    Tom Smith Guest

    No, the tblsearch won't return T, and (= <something> T) is an unlisplike test anyway. Tblsearch will return the table entry if found, or nothing. Just do:

    (if (tblsearch "block" "blockname")
    (it_exists)
    (it_doesn't)
    )

    There are all kinds of test in lisp for various kinds of equality or the status of a symbol. The numerical equality test = should be reserved for numbers. To test for anythingness versus nothingness, use (if (null <thing) ... ) or conversely (if <thing> ... ).
     
    Tom Smith, Aug 7, 2004
    #38
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.