Copy block definition from another drawing: Lisp Help

Copy block definition from another drawing: Lisp Help

gccdaemon
Collaborator Collaborator
1,921 Views
3 Replies
Message 1 of 4

Copy block definition from another drawing: Lisp Help

gccdaemon
Collaborator
Collaborator

Need help with this. Not sure what's going wrong. Looked for several variations and keep getting this error message:

 

; error: Automation Error. Problem in loading application

 

(defun GETBLOCK (DWG BLK / DBX)
	(vl-load-com)
	(setq DBX	(vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument"))
	(vla-open DBX DWG)
	(vla-CopyObjects DBX	(vlax-safearray-fill	(vlax-make-safearray vlax-vbObject '(0 . 0))
							(list (vla-item (vla-get-blocks DBX) BLK)))
				(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
	(vlax-release-object DBX)
)

 

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
0 Likes
Accepted solutions (1)
1,922 Views
3 Replies
Replies (3)
Message 2 of 4

hmsilva
Mentor
Mentor
Accepted solution

HI Andrew,

 

try

(if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
        (setq DBX (vlax-create-object "ObjectDBX.AxDbDocument"))
        (setq DBX (vlax-create-object
                     (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))
                   )
        )
      )

 

Hope this helps,
Henrique

EESignature

Message 3 of 4

gccdaemon
Collaborator
Collaborator

Yah, it's working now. A few quick questions since I'm still learning and all. How does the ACADVER variable play into this? Is it related to the object creation in the current drawing? If so why would it matter what version?

 

Working Code:

 

(defun GETBLOCK (DWG BLK / DBX)
	(vl-load-com)
	(if	(<	(atoi	(substr (getvar "ACADVER") 1 2)	)	16)
		(setq DBX (vlax-create-object "ObjectDBX.AxDbDocument"))
		(setq DBX (vlax-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))))
	)
	(vla-open DBX DWG)
	(vla-CopyObjects DBX	(vlax-safearray-fill	(vlax-make-safearray vlax-vbObject '(0 . 0))
							(list (vla-item (vla-get-blocks DBX) BLK)))
				(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
	(vlax-release-object DBX)
)

 

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
0 Likes
Message 4 of 4

hmsilva
Mentor
Mentor

@gccdaemon wrote:

Yah, it's working now. A few quick questions since I'm still learning and all. How does the ACADVER variable play into this? Is it related to the object creation in the current drawing? If so why would it matter what version?


Andrew,

Prior to AC2004, "ObjectDBX.AxDbDocument"
after AC2004, (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))

 

I also would suggest, to test first 'dwg' with 'findfile', and test the 'blk' existence at block collection with something like this

 

(if (not
      (vl-catch-all-error-p
        (vl-catch-all-apply
          (function (lambda () (setq blkdbx (vla-item (vla-get-blocks dbx) blk))))
        )
      )
    )
  (vla-copyobjects
    dbx
    (vlax-safearray-fill
      (vlax-make-safearray vlax-vbObject '(0 . 0))
      (list blkdbx)
    )
    (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  )
)

 

Hope this helps,
Henrique

 

EESignature