TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ArgV on March 31, 2010, 03:39:08 PM

Title: Purging with ODBX?
Post by: ArgV on March 31, 2010, 03:39:08 PM
Just wondering if anyone knows if, or how, to purge a drawing with ODBX? Or if it's even possible?

I realize there are no graphical entities with ODBX, so it would be hard for the application to determine whether an item can be purged or not, but I dunno.

thanks!
Title: Re: Purging with ODBX?
Post by: MP on March 31, 2010, 03:45:08 PM
Yes it is possible but it's an activity that requires more than a quick answer to detail.
Title: Re: Purging with ODBX?
Post by: ArgV on March 31, 2010, 03:47:03 PM
Yes it is possible but it's an activity that requires more than a quick answer to detail.

I could imagine. Well, I'd love to know how to do it, but if it's super involved then maybe I'll just write a script to do it.
Title: Re: Purging with ODBX?
Post by: T.Willey on March 31, 2010, 03:50:10 PM
Can you just use the ' delete ' method?  I thought it wouldn't let you delete items that are referenced.  Not sure though.
Title: Re: Purging with ODBX?
Post by: TimSpangler on March 31, 2010, 03:53:15 PM
Just wondering if anyone knows if, or how, to purge a drawing with ODBX? Or if it's even possible?

I realize there are no graphical entities with ODBX, so it would be hard for the application to determine whether an item can be purged or not, but I dunno.

thanks!
Here is some code to purge a block(S).  Modify it to suite your neeeds
Code: [Select]
;; Check to see if the block is a DW block
(foreach X BlockList
(if (= "DW-" (strcase (substr X 1 3)))
(progn
;; Check to see if the block is used and purge it
;; Method Thanks to Jimmy Bergmark
(if
(vl-catch-all-error-p
(vl-catch-all-apply
'vla-delete
(list (vl-catch-all-apply
'vla-item
(list (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) X)
)
)
)
)
nil ; name cannot be purged or doesn't exist
)
)
)
)

Here is the code to open and close the drawing.  credit to Credit to jbuzbee.
Code: [Select]
;;; ------------ OPEN A DBXDOCUMENT
(defun BLOCK_OpenDbx (dbxDrawingName / Application Document dbxDocument dbxOpen)

(setq Application (vlax-get-acad-object))
(setq Document (vla-get-activedocument Application))
(if (/= dbxDrawingName (vla-get-fullname Document))
(progn
(cond
((= (substr (getvar "ACADVER") 1 5) "15.06")
(setq dbxDocument (vla-GetInterfaceObject Application "ObjectDBX.AxDbDocument"))
(setq dbxOpen (vl-catch-all-apply 'vla-open (list dbxDocument dbxDrawingName)))
)
(T
(setq dbxDocument (vla-GetInterfaceObject Application (strcat "ObjectDBX.AxDbDocument." (substr (getvar "acadver") 1 2))))
(setq dbxOpen (vl-catch-all-apply 'vla-open (list dbxDocument dbxDrawingName)))
)
)
(if (vl-catch-all-error-p dbxOpen)
(setq dbxDocument nil)
)
)
)
(vlax-release-object Application)
(vlax-release-object Document)
dbxDocument
)
;;; ------------ CLOSE DBXDOCUMENT
(defun BLOCK_CloseDbx (dbxDocument / dbxDocument)

(if (= (type dbxDocument) 'VLA-OBJECT)
(progn
(vlax-release-object dbxDocument)
(setq dbxDocument nil)
)
)
)

Just start hacking things together.  When you have things some what usable post it.   :mrgreen:


Title: Re: Purging with ODBX?
Post by: Andrea on March 31, 2010, 04:56:47 PM
try this...

change the "C:\test\33.dwg" by your drawing name..

Code: [Select]
(setq AutoCADapp
       (vlax-create-object
(strcat "AutoCAD.Application."
(substr (getvar "acadver") 1 2)
)
       )
)
(setq documents (vla-get-documents AutoCADapp))
(setq ii (vla-open documents "C:\\test\\33.dwg" nil))
(vla-sendcommand
  ii
  (strcat "_-purge"
 (chr 13)
 "_A"
 (chr 13)
 "_*"
 (chr 13)
 "_N"
 (chr 13)
  )
)
(vlax-invoke-method documents "close")
(vlax-invoke-method AutoCADapp "quit")
(mapcar 'vlax-release-object
(mapcar 'eval '(AutoCADapp documents ii))
)
Title: Re: Purging with ODBX?
Post by: T.Willey on March 31, 2010, 05:03:42 PM
If you are going to open the drawing, then there is no need to use ' sendcommand ', as there is a ' purgeall ' method of a document that is open in editor, even if it's not the current document.

Quote
Command: (dump (vla-item (vla-get-documents (vlax-get-acad-object)) "Drawing3.dwg"))

; IAcadDocument: An AutoCAD drawing
; Property values:
<snip>
; Methods supported:
;   Activate ()
;   AuditInfo (1)
;   Close (2)
;   CopyObjects (3)
;   EndUndoMark ()
;   Export (3)
;   GetVariable (1)
;   HandleToObject (1)
;   Import (3)
;   LoadShapeFile (1)
;   New (1)
;   ObjectIdToObject (1)
;   Open (2)
;   PurgeAll ()
;   Regen (1)
;   Save ()
;   SaveAs (3)
;   SendCommand (1)
;   SetVariable (2)
;   StartUndoMark ()
;   Wblock (2)
Title: Re: Purging with ODBX?
Post by: Krushert on March 31, 2010, 05:04:34 PM
Alright I will play dumb which takes very little effort from me.

What is the significance of checking for the ACAD version?
Title: Re: Purging with ODBX?
Post by: dgorsman on March 31, 2010, 05:14:51 PM
One thing I have noticed, that using the Delete method through ODBX will only catch the top-level objects i.e. a block that is part of a block will only delete the parent, not the child.  Not sure if connecting to the file multiple times will solve that.
Title: Re: Purging with ODBX?
Post by: Krushert on March 31, 2010, 05:17:46 PM
If you are going to open the drawing, then there is no need to use ' sendcommand ', as there is a ' purgeall ' method of a document that is open in editor, even if it's not the current document.

I have been meaning to ask this question.
I read somewhere that -purge is more through that purgeall.  
Is this true?
Title: Re: Purging with ODBX?
Post by: alanjt on March 31, 2010, 05:28:43 PM
Know that this will not purge Multileader styles.

If you are going to open the drawing, then there is no need to use ' sendcommand ', as there is a ' purgeall ' method of a document that is open in editor, even if it's not the current document.

Quote
Command: (dump (vla-item (vla-get-documents (vlax-get-acad-object)) "Drawing3.dwg"))

; IAcadDocument: An AutoCAD drawing
; Property values:
<snip>
; Methods supported:
;   Activate ()
;   AuditInfo (1)
;   Close (2)
;   CopyObjects (3)
;   EndUndoMark ()
;   Export (3)
;   GetVariable (1)
;   HandleToObject (1)
;   Import (3)
;   LoadShapeFile (1)
;   New (1)
;   ObjectIdToObject (1)
;   Open (2)
;   PurgeAll ()
;   Regen (1)
;   Save ()
;   SaveAs (3)
;   SendCommand (1)
;   SetVariable (2)
;   StartUndoMark ()
;   Wblock (2)
Title: Re: Purging with ODBX?
Post by: T.Willey on March 31, 2010, 05:40:28 PM
If you are going to open the drawing, then there is no need to use ' sendcommand ', as there is a ' purgeall ' method of a document that is open in editor, even if it's not the current document.

I have been meaning to ask this question.
I read somewhere that -purge is more through that purgeall. 
Is this true?

Wouldn't know.  I never ran any tests.  I just know it's there.   :wink:

Know that this will not purge Multileader styles.

<snip>

Good to know.  Don't use mleaders yet, so one for the memory bank.
Title: Re: Purging with ODBX?
Post by: ArgV on March 31, 2010, 05:49:47 PM
Just wondering if anyone knows if, or how, to purge a drawing with ODBX? Or if it's even possible?

I realize there are no graphical entities with ODBX, so it would be hard for the application to determine whether an item can be purged or not, but I dunno.

thanks!
Here is some code to purge a block(S).  Modify it to suite your neeeds
Code: [Select]
;; Check to see if the block is a DW block
(foreach X BlockList
(if (= "DW-" (strcase (substr X 1 3)))
(progn
;; Check to see if the block is used and purge it
;; Method Thanks to Jimmy Bergmark
(if
(vl-catch-all-error-p
(vl-catch-all-apply
'vla-delete
(list (vl-catch-all-apply
'vla-item
(list (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) X)
)
)
)
)
nil ; name cannot be purged or doesn't exist
)
)
)
)

Here is the code to open and close the drawing.  credit to Credit to jbuzbee.
Code: [Select]
;;; ------------ OPEN A DBXDOCUMENT
(defun BLOCK_OpenDbx (dbxDrawingName / Application Document dbxDocument dbxOpen)

(setq Application (vlax-get-acad-object))
(setq Document (vla-get-activedocument Application))
(if (/= dbxDrawingName (vla-get-fullname Document))
(progn
(cond
((= (substr (getvar "ACADVER") 1 5) "15.06")
(setq dbxDocument (vla-GetInterfaceObject Application "ObjectDBX.AxDbDocument"))
(setq dbxOpen (vl-catch-all-apply 'vla-open (list dbxDocument dbxDrawingName)))
)
(T
(setq dbxDocument (vla-GetInterfaceObject Application (strcat "ObjectDBX.AxDbDocument." (substr (getvar "acadver") 1 2))))
(setq dbxOpen (vl-catch-all-apply 'vla-open (list dbxDocument dbxDrawingName)))
)
)
(if (vl-catch-all-error-p dbxOpen)
(setq dbxDocument nil)
)
)
)
(vlax-release-object Application)
(vlax-release-object Document)
dbxDocument
)
;;; ------------ CLOSE DBXDOCUMENT
(defun BLOCK_CloseDbx (dbxDocument / dbxDocument)

(if (= (type dbxDocument) 'VLA-OBJECT)
(progn
(vlax-release-object dbxDocument)
(setq dbxDocument nil)
)
)
)

Just start hacking things together.  When you have things some what usable post it.   :mrgreen:




I'll tinker with that, but as far as I know (which isn't very far, mind you), the objectDBX interface doesn't have a purge method. Though I may be wrong, I didn't see one.

However, if the 'delete' method works like a purge, then I'm golden.

thank you! :)
Title: Re: Purging with ODBX?
Post by: alanjt on March 31, 2010, 05:54:06 PM
If you are going to open the drawing, then there is no need to use ' sendcommand ', as there is a ' purgeall ' method of a document that is open in editor, even if it's not the current document.

I have been meaning to ask this question.
I read somewhere that -purge is more through that purgeall. 
Is this true?

Wouldn't know.  I never ran any tests.  I just know it's there.   :wink:

Know that this will not purge Multileader styles.

<snip>

Good to know.  Don't use mleaders yet, so one for the memory bank.

This was my solution...

Code: [Select]
;;; Purge Multileader Styles (since vla-purgeall ignores them)
;;; Alan J. Thompson, 08.24.09
(defun AT:MleaderStylePurge (/)
  (vl-remove-if
    'null
    (mapcar
      '(lambda (x)
         (and
           (eq 350 (car x))
           (not (eq 330 (car (nth 5 (entget (cdr x))))))
           (not (eq (getvar "cmleaderstyle")
                    (vla-get-name (vlax-ename->vla-object (cdr x)))
                ) ;_ eq
           ) ;_ not
           (entdel (cdr x))
         ) ;_ and
       ) ;_ lambda
      (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
    ) ;_ mapcar
  ) ;_ vl-remove-if
) ;_ defun
Title: Re: Purging with ODBX?
Post by: ArgV on March 31, 2010, 05:58:45 PM
try this...

change the "C:\test\33.dwg" by your drawing name..

Code: [Select]
(setq AutoCADapp
       (vlax-create-object
(strcat "AutoCAD.Application."
(substr (getvar "acadver") 1 2)
)
       )
)
(setq documents (vla-get-documents AutoCADapp))
(setq ii (vla-open documents "C:\\test\\33.dwg" nil))
(vla-sendcommand
  ii
  (strcat "_-purge"
 (chr 13)
 "_A"
 (chr 13)
 "_*"
 (chr 13)
 "_N"
 (chr 13)
  )
)
(vlax-invoke-method documents "close")
(vlax-invoke-method AutoCADapp "quit")
(mapcar 'vlax-release-object
(mapcar 'eval '(AutoCADapp documents ii))
)

Will this work with ObjectDBX? I noticed this is a standard autocad application, which supports the 'vla-purgeall' command.
Title: Re: Purging with ODBX?
Post by: MP on March 31, 2010, 09:16:15 PM
Quasi pseudo code food for thought ...

Code: [Select]
([color=blue]defun [/color][color=red]_PurgeAxdbDocument[/color] ( axdbDocument / purgeableCollections initialCount thisCount lastCount flag )

    ([color=blue]setq [/color]
        purgeableCollections (_[color=red]GetPurgeAbleCollections [/color]axdbDocument)
        initialCount         ([color=blue]apply '+[/color] ([color=blue]mapcar [/color]'[color=red]_GetDeepCount [/color]purgeableCollections))
        thisCount            initialCount
        flag                 T
    )       

    ([color=blue]while [/color]flag [color=green];; do at least once[/color]
       
        ([color=blue]foreach [/color]collection purgeableCollections
       
            ([color=red]_RecursivelyKillItWithFire[/color] collection)
           
        )
       
        ([color=blue]setq [/color]
            lastCount thisCount
            thisCount ([color=blue]apply '+[/color] (mapcar '[color=red]_GetDeepCount[/color] purgeableCollections))
            flag      ([color=blue]/=[/color] lastCount thisCount)
        )
   
    )
   
    ([color=blue]princ [/color]([color=blue]strcat [/color][color=purple]"Purged "[/color] ([color=blue]itoa [/color]([color=blue]-[/color] initialCount thisCount)) [color=purple]" object(s).\n"[/color]))
   
    ([color=blue]princ[/color])
   
)
Title: Re: Purging with ODBX?
Post by: T.Willey on April 01, 2010, 01:09:09 AM
Not you too Michael.  Those color code post are horrible to read on Black22 theme.   :cry:
Title: Re: Purging with ODBX?
Post by: MP on April 01, 2010, 01:14:13 AM
For the benefit of the theme challenged:

Code: [Select]
(defun _PurgeAxdbDocument ( axdbDocument / purgeableCollections initialCount thisCount lastCount flag )

    (setq
        purgeableCollections (_GetPurgeAbleCollections axdbDocument)
        initialCount         (apply '+ (mapcar '_GetDeepCount purgeableCollections))
        thisCount            initialCount
        flag                 T
    )       

    (while flag ;; do at least once
       
        (foreach collection purgeableCollections
       
            (_RecursivelyKillItWithFire collection)
           
        )
       
        (setq
            lastCount thisCount
            thisCount (apply '+ (mapcar '_GetDeepCount purgeableCollections))
            flag      (/= lastCount thisCount)
        )
   
    )
   
    (princ (strcat "Purged " (itoa (- initialCount thisCount)) " object(s).\n"))
   
    (princ)
   
)
Title: Re: Purging with ODBX?
Post by: Kerry on April 01, 2010, 02:56:16 AM
Quasi pseudo code food for thought ...

Code: [Select]
([color=blue]defun [/color][color=red]_PurgeAxdbDocument[/color] ( axdbDocument / purgeableCollections initialCount thisCount lastCount flag )

    ([color=blue]setq [/color]
        purgeableCollections (_[color=red]GetPurgeAbleCollections [/color]axdbDocument)
        initialCount         ([color=blue]apply '+[/color] ([color=blue]mapcar [/color]'[color=red]_GetDeepCount [/color]purgeableCollections))
        thisCount            initialCount
        flag                 T
    )       

 < .... >   
)
For the benefit of the theme challenged:
Code: [Select]
(defun _PurgeAxdbDocument ( axdbDocument / purgeableCollections initialCount thisCount lastCount flag )

    (setq
        purgeableCollections (_GetPurgeAbleCollections axdbDocument)
        initialCount         (apply '+ (mapcar '_GetDeepCount purgeableCollections))
        thisCount            initialCount
        flag                 T
    ) 
< .. >

Both read fine for me in the Mercury theme ....  :angel:

I can even read the code without re-formatting  :-D
Title: Re: Purging with ODBX?
Post by: Lee Mac on April 01, 2010, 07:18:03 AM
Perhaps this  (http://www.theswamp.org/index.php?topic=31827.0)may help?

Maybe feed it this function  :-)

Code: [Select]
(Mac-ODBX '(lambda (x) (repeat 3 (vla-purgeall x)) (vla-save x)) nil)

{ Untested }