inserting/deleting a block based on filename

Discussion in 'AutoCAD' started by WashCaps37, Nov 1, 2004.

  1. WashCaps37

    WashCaps37 Guest

    Hello,

    Is it possible for a script or lisp routine to insert a block at predetermined coordinates or deletes a specific block name based on the filename. If so, How?

    For example, if a drawings filename is BlahBlah_Draft.dwg, a block named Draft (which is a dwg in a folder defined in the ACAD search path) will be inserted at say 0,4,0 and if the filename does not contain a "_Draft" the routine deletes a block called "Draft".

    Your help is very much appreciated!
     
    WashCaps37, Nov 1, 2004
    #1
  2. WashCaps37

    ECCAD Guest

    Something like this: (untested)

    (defun C:draft ( dwg check ss )
    (setq dwg (getvar "dwgname")); get dwg name
    (setq dwg (substr dwg 1 (- (strlen dwg) 4))); strip the .dwg
    (setq check (strcase (substr dwg (- (strlen dwg) 6) 6)))
    (if (= check "_DRAFT")
    (progn
    (setq ss (ssget "X" '((2 . "draft"))))
    (if ss
    (command "_erase" ss "")
    ); if
    ); progn
    ); if
    (if (/= check "_DRAFT")
    (command "-insert" "draft" "0,4" "" "" "")
    ); if
    (princ)
    ); fuction

    Bob
     
    ECCAD, Nov 1, 2004
    #2
  3. WashCaps37

    WashCaps37 Guest

    Hey Bob,

    Thanks for helping me out. I trying your code and I get a "Too Few Arguments Function Cancelled" message. Any ideas of what it can mean? I apologize for my lack of LISP knowledge.

    Thanks again!
     
    WashCaps37, Nov 1, 2004
    #3
  4. WashCaps37

    ECCAD Guest

    (defun C:draft ( / dwg check ss )

    Forgot the / in above, add it in.

    Bob
     
    ECCAD, Nov 1, 2004
    #4
  5. WashCaps37

    WashCaps37 Guest

    That put me on track as far as inserting the draft block when the filename is filename_Draft.dwg. But when the draft block is already in the drawing and the filename is filename.dwg it won't delete the draft block and I receive the message "bad argument value: positive -2". Any ideas? This is what I have so far.

    (defun C:draft ( / dwg check ss )
    (setq dwg (getvar "dwgname")); get dwg name
    (setq dwg (substr dwg 1 (- (strlen dwg) 4))); strip the .dwg
    (setq check (strcase (substr dwg (- (strlen dwg) 6) 6)))
    (if (= check "_Draft")
    (progn
    (setq ss (ssget "X" '((2 . "DRAFT"))))
    (if ss
    (command "_erase" ss "")
    ); if
    ); progn
    ); if
    (if (/= check "_Draft")
    (command "-insert" "DRAFT2" "0,4" "" "" "")
    ); if
    (princ)
    ); function

    I temporarily changed this line (setq ss (ssget "X" '((2 . "DRAFT")))) to (setq ss (ssget "X" '((2 . "DRAFT2")))) to match the actual block name reloaded the LISP and restarted ACAD but it didn't help resolve the issue.
     
    WashCaps37, Nov 1, 2004
    #5
  6. WashCaps37

    3ABTPA Guest

    Everything is possible...
    ....for reasonable price...
    :)


    predetermined coordinates or deletes a specific block name based on the
    filename. If so, How?
    Draft (which is a dwg in a folder defined in the ACAD search path) will be
    inserted at say 0,4,0 and if the filename does not contain a "_Draft" the
    routine deletes a block called "Draft".
     
    3ABTPA, Nov 1, 2004
    #6
  7. WashCaps37

    ECCAD Guest

    (defun C:draft ( / dwg check ss )
    (setq dwg (getvar "dwgname")); get dwg name
    (setq dwg (substr dwg 1 (- (strlen dwg) 4))); strip the .dwg
    (setq check (strcase (substr dwg (- (strlen dwg) 6) 6)))
    (setq ss (ssget "X" '((2 . "DRAFT2"))))
    (if ss
    (command "_erase" ss "")
    ); if
    (if (/= check "_Draft")
    (command "-insert" "DRAFT2" "0,4" "" "" "")
    ); if
    (princ)
    ); function

    Will delete the block 'draft2' - if it exists in any drawing.
    Bob
     
    ECCAD, Nov 1, 2004
    #7
  8. WashCaps37

    WashCaps37 Guest

    I just realized that it's because block2 is a nested block. How does that affect the Routine you've created? I wish they offered classes on this stuff.
     
    WashCaps37, Nov 1, 2004
    #8
  9. WashCaps37

    ECCAD Guest

    It won't 'find' the nested block - all you can do is to explode the parent block - expose the nested block, then run the routine.

    Bob
     
    ECCAD, Nov 1, 2004
    #9
  10. WashCaps37

    ECCAD Guest

    Anything better to do today ?

    :)
     
    ECCAD, Nov 1, 2004
    #10
  11. WashCaps37

    ECCAD Guest

    In the following line:
    Uppercase the "_DRAFT".

    (if (/= check "_Draft")

    Bob
     
    ECCAD, Nov 1, 2004
    #11
  12. WashCaps37

    WashCaps37 Guest

    Ok, I resolved the nested block problem issue and now another problem has developed. In a file where the filename for example is filename.dwg the routine deletes the draft block but inserts a draft block at 0,4,0 when I only need the draft block inserted in a drawing that named for example filename_Draft.dwg.

    Things that make you go Hmmmmm.
     
    WashCaps37, Nov 1, 2004
    #12
  13. WashCaps37

    WashCaps37 Guest

    looks like someone doesn't know the meaning of helping others out. Always looking looking on the ground for a penny. :)
     
    WashCaps37, Nov 2, 2004
    #13
  14. WashCaps37

    ECCAD Guest

    Change the code to be:

    (defun C:draft ( / dwg check ss )
    (setq dwg (getvar "dwgname")); get dwg name
    (setq dwg (substr dwg 1 (- (strlen dwg) 4))); strip the .dwg
    (setq check (strcase (substr dwg (- (strlen dwg) 6) 6)))
    (setq ss (ssget "X" '((2 . "DRAFT2"))))
    (if ss
    (command "_erase" ss "")
    ); if
    (if (= check "_DRAFT")
    (command "-insert" "DRAFT2" "0,4" "" "" "")
    ); if
    (princ)
    ); function

    Note changes: UpperCase on '_DRAFT', and = rather than /=.

    Bob
     
    ECCAD, Nov 2, 2004
    #14
  15. WashCaps37

    WashCaps37 Guest

    Bob,

    Sorry for the delay on replying to your last post. I did as you suggested, "UpperCase on '_DRAFT', and = rather than /=." The routine would not run so I had to add the / in order to get the routine to at least function. Don't know if it makes a difference but I am running ACAD 2004.

    The routine works perfectly if the filename has "_Draft" in it. If I have a drawing with a filename say, filename.dwg with a Draft2 block in the drawing, the routine still won't erase or delete the Draft2 block. I am thinking that the routine is missing another a line stating that "if / is 'not equal to' '_Draft', then erase ss.

    From my understanding of the routine,
    the routine will check the filename, look at the 6 characters before the .dwg and if those characters = "_DRAFT" then it will insert a Draft2 block at the given coordinates.

    Could there be another check missing?
     
    WashCaps37, Nov 3, 2004
    #15
  16. WashCaps37

    WashCaps37 Guest

    To clarify what I meant by "The routine works perfectly if the filename has '_Draft' in it." Meaning, if the filename has "_Draft" in it, the routine performs the function of inserting the Draft2 block.
     
    WashCaps37, Nov 3, 2004
    #16
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.