Jump to content

Lisp for filtering certain blocks and delete them


MarcoW

Recommended Posts

How does this LISP work?

 

I key in the following but do not work

 

DELETEBLOCK LIFT$0$A$C5AD1096B

 

(DeleteBlock "LIFT$0$A$C5AD1096B")

 

This is the LISP i am using from LEE

 

;; Delete Block  -  Lee Mac
;; Deletes all references of a block from a drawing, including nested references (nested to any level).
;; Proceeds to delete the block definition from the drawing, if possible.
;; Arguments:
;; block  -  block to be deleted (not case-sensitive)

(defun DeleteBlock ( block / doc lst )
   (setq block (strcase block))
   
   (if (tblsearch "[b][color=darkred]LIFT$0$A$C5AD1096B[/color][/b]" block)
       (progn
           (setq doc (vla-get-activedocument (vlax-get-acad-object)))

           (vlax-for lay (vla-get-layers doc)
               (if (eq :vlax-true (vla-get-lock lay))
                   (progn
                       (setq lst (cons lay lst))
                       (vla-put-lock lay :vlax-false)
                   )
               )
           )
           (vlax-for def (vla-get-blocks doc)
               (vlax-for obj def
                   (if
                       (and
                           (eq "AcDbBlockReference" (vla-get-objectname obj))
                           (or
                               (and
                                   (vlax-property-available-p obj 'effectivename)
                                   (eq block (strcase (vla-get-effectivename obj)))
                               )
                               (eq block (strcase (vla-get-name obj)))
                           )
                       )
                       (vl-catch-all-apply 'vla-delete (list obj))
                   )
               )
           )
           (vl-catch-all-apply 'vla-delete (list (vla-item (vla-get-blocks doc) block)))
           
           (foreach lay lst
               (vla-put-lock lay :vlax-true)
           )
       )
   )
   (princ)
)
(vl-load-com) (princ)

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    15

  • Patrick_35

    7

  • Bobzy20

    7

  • vernonlee

    7

You should not modify the code in any way, but instead call the function with the name of the block to be deleted, i.e.:

(DeleteBlock "LIFT$0$A$C5AD1096B")

 

I would also recommmend that you refer to my Delete Blocks program here; upon loading the program you can either use the delblocks command and select the blocks from the dialog, or call the LM:deleteblocks function with a list of block names, e.g.:

(LM:deleteblocks '("LIFT$0$A$C5AD1096B"))

Link to comment
Share on other sites

Oh I see. WIll try it next week.

 

Btw, i also tried your delete block program but the width of the dialog box can't seen to be adjusted thus i cannot see the full name of the block. Incidenly i did email you this query through your website. Perhaps you can advise me here? Thanks

Link to comment
Share on other sites

Btw, i also tried your delete block program but the width of the dialog box can't seen to be adjusted thus i cannot see the full name of the block. Incidenly i did email you this query through your website. Perhaps you can advise me here?

 

I responded to your email some time ago.

Link to comment
Share on other sites

Hi Lee. Retested the previous LISP I was clarify about & it works now.

 

I intend to run it through a script to remove multiple blocks at once.

eg. door block, window block, grid block, text block

 

How to run without multiple scripts?

 

Something like this?

 

^C^C(deleteblock "door block, window block, grid block, text block");

Link to comment
Share on other sites

For multiple blocks, you should use my [noparse]LM:DeleteBlocks[/noparse] function which may be called with the ActiveX Document object and a list of block names defined in the Blocks Collection for the supplied Document, e.g. for blocks defined in the active drawing, you would use:

(LM:deleteblocks
   (vla-get-activedocument (vlax-get-acad-object))
  '("door block" "window block" "grid block")
)

You may find this thread of interest.

Edited by Lee Mac
Link to comment
Share on other sites

You're welcome - just be aware that the use of ObjectDBX will cause all drawings to be saved to the native version (i.e. the highest available version in the host software), and drawing thumbnails will be lost until the next 'manual' save.

 

Lee

Link to comment
Share on other sites

Hi Lee,

 

Not sure what i am doing wrong but i have the following error when i enter this command

 

Command: (LM:deleteblocks '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE"  "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" "xBNRY"))
[color=red][b]; error: too few arguments[/b][/color]

Link to comment
Share on other sites

Hi Lee,

 

Not sure what i am doing wrong but i have the following error when i enter this command

 

Command: (LM:deleteblocks '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE"  "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" "xBNRY"))
[color=red][b]; error: too few arguments[/b][/color]

 

My apologies - you will also need to supply the Document object containing the Blocks Collection in which the blocks are defined, for example, for the active drawing you would use:

(LM:deleteblocks 
   (vla-get-activedocument (vlax-get-acad-object)) 
  '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE" "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" "xBNRY")
)

I will update my earlier post for the benefit of others who may read this thread.

Link to comment
Share on other sites

Hi Lee,

 

How do i incorporate it into a LISP form for multiple blocks? I copied the LISP from your linked thread & replace with your new command but got an error instead:-

 

(defun c:test ( / lst )
   (if (setq lst (LM:getfiles "Select Drawings to Process" nil "dwg;dws;dwt"))
       (LM:odbx '(lambda ( doc ) (LM:deleteblocks 
   (vla-get-activedocument (vlax-get-acad-object)) 
  '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE" "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" 

"xBNRY")
)) lst t)
       (princ "\n*Cancel*")
   )
   (princ)
)

[color=red][b]Command: TEST
; error: no function definition: LM:GETFILES[/b][/color]

Also when i just paste the command directly, the blocks indicated got deleted but at the end there is an error. Should i be concern?

 

Command: (LM:deleteblocks

(_> (vla-get-activedocument (vlax-get-acad-object))

(_> '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE" "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" "xBNRY")

(_> )

; error: Automation Error. Key not found

Thanks
Link to comment
Share on other sites

How do i incorporate it into a LISP form for multiple blocks? I copied the LISP from your linked thread & replace with your new command but got an error instead:-

 

(defun c:test ( / lst )
   (if (setq lst (LM:getfiles "Select Drawings to Process" nil "dwg;dws;dwt"))
       (LM:odbx '(lambda ( doc ) (LM:deleteblocks 
   (vla-get-activedocument (vlax-get-acad-object)) 
  '("X-COMMGRID-1" "X-COMMGRID (office)" "X-SECLINE" "X-OFF-ELEVLINE" "X-OFF-ELEVLINE" 

"xBNRY")
)) lst t)
       (princ "\n*Cancel*")
   )
   (princ)
)

[color=red][b]Command: TEST
; error: no function definition: LM:GETFILES[/b][/color]

 

You cannot simply copy the expression I posted earlier into the other program, as the program from the linked thread is referencing the ObjectDBX Document as opposed to the Active Document.

 

Where the code in the linked thread is concerned, you should only change the block names highlighted in red; the document object should remain as 'doc' (this symbol is assigned the ObjectDBX Document object for every drawing processed).

 

You are receiving the error 'no function definition: LM:GETFILES' because you haven't loaded my LM:getfiles function.

 

As noted in the linked thread:

 

If you need more control over which drawings are processed, you can also use my Get Files Dialog function - ensure that this program, my Delete Blocks program and my ObjectDBX Wrapper program are loaded and try the following code...

 

Also when i just paste the command directly, the blocks indicated got deleted but at the end there is an error. Should i be concern?

 

I'm not entirely sure why you are receiving this error (the error indicates that an item is not present in a VL Collection), as my Delete Blocks function will remove any blocks which are not defined in the Blocks Collection of the supplied Document object from the supplied list of block names before processing the blocks.

 

Do you receive this error consistently for every use of the function?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...