Get block and then erase block

Discussion in 'AutoCAD' started by J. Logan, Jul 7, 2004.

  1. J. Logan

    J. Logan Guest

    I have a block I want to erase from a set of drawings. It's insertion point
    is different in every drawing. I should be able to use ssget to find the
    block I want and then erase it...

    (setq ss (ssget "=" (list (cons 0 "blockname")))
    command "erase" "last"

    J. Logan
     
    J. Logan, Jul 7, 2004
    #1
  2. J. Logan

    T.Willey Guest

    (setq ss (ssget '((0 . "INSERT")(2 . "blockname"))))
    (command "._erase" ss "")

    I think this should work, didn't test it though.

    HTH.
    Tim
     
    T.Willey, Jul 7, 2004
    #2
  3. Several problems....

    You want to find that block from among all the objects in the drawing, which
    is the "X" option in (ssget). so I think you want:
    (ssget "X" <filter-list>)

    I haven't worked a lot with filter-lists, so I'm counting on interpreting
    Help's syntax right, in the following. And your dxf/assoc code 0 is for
    entity type -- you want 2 for the NAME of a block. So I think you want:
    (setq ss (ssget "X" '((cons 2 "blockname"))))
    or you could probably do
    (setq ss (ssget "X" (list (cons 2 "blockname"))))

    Your command function needs to be in parentheses. And this block probably
    won't be the last entity in the drawing, which is why you filtered selection
    and save a selection set to a name (ss). And you have to tell it you're
    done selecting things before it will erase them. So I think you want:
    (command "erase" ss "")

    Kent Cooper, AIA


    ...
     
    Kent Cooper, AIA, Jul 7, 2004
    #3
  4. As I said, I haven't worked with filter-lists much, so enlighten me.

    Wouldn't filtering for (2 . "blockname") be enough? Is it really necessary
    to also have the (0 . "INSERT") in there? I guess it's a question of
    whether code 2 is used for anything else where you might end up selecting
    something you didn't want to. I know 2 is used for the name of an Xref,
    too, but since you can't have Xref's and blocks in the same drawing with the
    same name, anyway, that shouldn't be a problem. Is code 2 used for anything
    else that might have the same kind of content as a block name? (Is there
    even an easy way to find out?)

    And am I right that doing it Tim's way would not find the block in question
    if it were in a different type of space, or on a frozen or off layer, or not
    in view? That's why I suggested the (ssget "X" '((.... approach.

    Here's another question: Could you simply bypass the saving of a selection
    set, and do (ssget) as direct input to the erase command? Would this work?
    (command "._erase" (ssget '((0 . "INSERT")(2 . "blockname"))) "")
    or, if I'm right about the filtering, just
    (command "._erase" (ssget '((2 . "blockname"))) "")

    Kent Cooper, AIA
     
    Kent Cooper, AIA, Jul 7, 2004
    #4
  5. J. Logan

    J. Logan Guest

    As I understand the ACAD Help file I think you're right in saying that it is
    for Block Names.
    Yes (ssget "x" '((...is exactly right for the purpose of selecting all data
    types within the drawing not just visible data types. Again as I read it
    from ACAD help. I'm just starting to try my hand at lisping so I'm not
    entirely sure here.
    I haven't tried these last two you suggested
    Below is what I currently have. With this I get a "syntax error" at the
    command line. So I'm close here. I'm just missing something or not seeing
    something I've typed wrong.

    (defun c:eraseblock (/ ss)
    (setq ss
    (ssget "X" '(list(cons 0 "insert")(2 "CONSTRUCTION STAMP")))
    (command "_.erase" ss "")
    )
    (princ)
    )

    J. Logan
     
    J. Logan, Jul 7, 2004
    #5
  6. J. Logan

    MP Guest

    it's likely but not certain

    Is it really necessary
    makes the code more explicit

    I guess it's a question of
    eg: an attdef object with a tagname matching the block name (etc)

    if the ssget call returns nil the code bombs
    always test to see if you got something before trying to modify it
    (if (setq ss(ssget ...))
    (dosomething ss)
    (princ"\nNothing selected")
    )

    just my opinion of course
    Mark
     
    MP, Jul 7, 2004
    #6
  7. J. Logan

    MP Guest

    assuming you want to delete all occurances of the insert regardless of tab
    (defun test( / blockname ss num idx)
    (setq blockname "BadBlock")
    (if(setq ss(ssget"x"(list(cons 0 "INSERT")(cons 2 blockname))))
    (progn
    (setq num(sslength ss) idx 0)
    (repeat num
    (vlax-invoke-method
    (vlax-ename->vla-object (ssname ss idx))
    'delete
    )
    (setq idx(1+ idx))
    );repeat
    (princ(strcat"\nDeleted " (itoa num) " inserts of: " blockname))
    );progn
    (princ(strcat"\nNo inserts of " blockname " found"))

    );if

    )
     
    MP, Jul 7, 2004
    #7
  8. J. Logan

    T.Willey Guest

    You won't be able to erase all the object because you can only erase per space you are in (ie blocks in model space when in model space).

    Here is your ssget function:
    (ssget "x" '((0 . "insert")(2 . "construction stamp")))

    If you want things in other spaces (tabs) beside the one your in, you need to have your lisp routine step into each one and preform your (ssget... and then (command "erase"....

    As per what (assoc 2.. pertains to, I'm not sure. I just know that if I want a block I add (0 . "INSERT") to make sure just in case.

    Yes you want to use the "x" filter to get all entities that match your criteria in the drawing data base.

    Tim
     
    T.Willey, Jul 7, 2004
    #8
  9. J. Logan

    MP Guest

    (ssget "X" '(list(cons 0 "insert")(2 "CONSTRUCTION STAMP")))
    missing a cons
    (ssget "X" '(list(cons 0 "insert")(cons 2 "CONSTRUCTION STAMP")))

    see my other response to your post,

    notes on the code below:
    if you don't have the block in question, ss will be nil and the command line
    will bomb
    that's why you should test ss before trying to erase it
    if the block exists but is on a layout tab other than the one that's current
    it will not be erased
    that's why I used vlax method to delete the block object if it is found in
    the ssget call
    hth
    Mark
     
    MP, Jul 7, 2004
    #9
  10. J. Logan

    T.Willey Guest

    That is a nice little way of getting objects in other named spaces Mark. Thanks for posting it, I learned a new trick.

    Tim
     
    T.Willey, Jul 7, 2004
    #10
  11. J. Logan

    Jürg Menzi Guest

    Hi Mark

    I couldn't keep my fingers to make a few changes...;-)

    (defun C:Test ( / ActDoc blockname CurEnt CurObj LayCol LayObj ss idx)
    (vl-load-com)
    (setq blockname "BadBlock")
    (if (setq ss (ssget "X" (list (cons 0 "INSERT") (cons 2 blockname))))
    (progn
    (setq idx 0
    ActDoc (vla-get-ActiveDocument (vlax-get-acad-object))
    LayCol (vla-get-Layers ActDoc)
    )
    (while (setq CurEnt (ssname ss 0))
    (ssdel CurEnt ss)
    (setq CurObj (vlax-ename->vla-object CurEnt)
    LayObj (vla-Item LayCol (vla-get-Layer CurObj))
    )
    ; handle inserts on locked layers:
    (if (= (vla-get-Lock LayObj) :vlax-true)
    (progn
    (vla-put-Lock LayObj :vlax-false)
    (vla-delete CurObj)
    (vla-put-Lock LayObj :vlax-true)
    )
    (vla-delete CurObj)
    )
    (setq idx (1+ idx))
    );repeat
    ; purge block reference:
    (vla-delete (vla-Item (vla-get-Blocks ActDoc) blockname))
    (princ (strcat "\nDeleted " (itoa idx) " inserts of: " blockname))
    );progn
    (princ (strcat "\nNo inserts of " blockname " found"))
    );if
    (princ)
    )

    Cheers
     
    Jürg Menzi, Jul 8, 2004
    #11
  12. J. Logan

    David Bethel Guest

    Kent,

    ACAD SHAPE entities have a group 2 name that is in thew same format as
    INSERTS, so (0 . "INSERT") a valid recommendation.

    Erasing a (ssget) call gets nasty when it returns nil ( no matches found )

    Also, ERASE will not work on entities that are not in the current TILEMODE.

    (setq ss (ssget "X" (list (cons 0 "INSERT")
    (cons 2 block_name)
    (cons 67 (- 1 (getvar "TILEMODE")))))


    -David
     
    David Bethel, Jul 8, 2004
    #12
  13. J. Logan

    J. Logan Guest

    David,

    so (cons 67 (- 1 (getvar "TILEMODE")))))...is switching tilemode?

    J. Logan
     
    J. Logan, Jul 8, 2004
    #13
  14. J. Logan

    J. Logan Guest

    Mark,

    Forgive my ignorance. But what piece of the code above is "searching all
    tabs" or the entire data base for the instance of the block?

    J. Logan
     
    J. Logan, Jul 8, 2004
    #14
  15. (if(setq ss(ssget"x"(list(cons 0 "INSERT")(cons 2 blockname))))

    The "X" filter searches the entire drawing.

    --
    R. Robert Bell


    Mark,

    Forgive my ignorance. But what piece of the code above is "searching all
    tabs" or the entire data base for the instance of the block?

    J. Logan
     
    R. Robert Bell, Jul 8, 2004
    #15
  16. J. Logan

    David Bethel Guest

    David,

    No, it selects only those entities in the current SPACE. Those are the
    only ones that the ERASE command will work with.

    -David
     
    David Bethel, Jul 8, 2004
    #16
  17. J. Logan

    MP Guest

    excellent as always
    :)
     
    MP, Jul 8, 2004
    #17
  18. J. Logan

    J. Logan Guest

    Thanks to everyone who contributed to my post. I appreciate all the input
    and will try to put it to good use.

    J. Logan
     
    J. Logan, Jul 8, 2004
    #18
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.