Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Mentor
mdhutchinson
Posts: 2,322
Registered: ‎11-18-2003
Message 1 of 3 (561 Views)
Accepted Solution

Get a block count without opening dwg ObjectDBX

561 Views, 2 Replies
01-29-2013 03:30 PM

I need to write some code to count blocks inserted on the drawing....  I want to count also dynamic blocks... most of the time dynamic blocks are anonymous... so I'd need to check the EffectiveName.

 

This line gets the blocks in the block table...

... but how do I get the blocks actually inserted in the dwg?

 

(setq blks (vla-get-blocks odoc))

 

Will a regular lisp expressions (like below) work with ObjectDBX... (drawing file not open)?

 

(setq ss (ssget "X" (list (cons 0 "INSERT")(cons 2 "`*U*"))))

I have one from oldies, but don't remember how it works

with limited test it looks good though

(defun C:DYBC (/ acapp blkinfo blkname counts dbxdoc first fn group odbx opendoc qty)
(setq acapp (vlax-get-acad-object))
(if
(and (setq fn "C:\\Test\\dynamics.dwg")
(setq fn (findfile fn)))
(progn
(setq odbx (vla-getinterfaceobject
acapp
(strcat "ObjectDBX.AxDbDocument."
(itoa (atoi (getvar "acadver"))))))
(setq opendoc (not (vl-catch-all-error-p
(vl-catch-all-apply 'vla-open (list odbx fn)))))
(if opendoc
(progn
(setq dbxdoc odbx)
(vlax-for lt (vla-get-layouts dbxdoc)
(vlax-for blkobj (vla-get-block lt)
(if (eq (vla-get-objectname blkobj) "AcDbBlockReference")
(progn
(setq blkname (vla-get-effectivename blkobj))
(setq blkinfo (cons (cons blkname 1) blkinfo)))
)))
(setq counts nil)
(while
(setq first (caar blkinfo))
(setq qty (vl-remove-if
'(lambda (b) (not (eq (car b) first)))
blkinfo))
(setq group (cons first (apply '+ (mapcar 'cdr qty)))
counts (cons group counts))
(setq blkinfo (vl-remove-if '(lambda (x) (member x qty)) blkinfo))
)
)
)
(if odbx
(vl-catch-all-apply 'vlax-release-object (list odbx)))
(alert (vl-princ-to-string counts))
)
)
(princ)
)
(princ "\n\t---\tStart command with DYBC\t---")
(prin1)
(or(vl-load-com)(princ))

 

*Expert Elite*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 2 of 3 (545 Views)

Re: Get a block count without opening dwg ObjectDBX

01-29-2013 09:04 PM in reply to: mdhutchinson

 


mdhutchinson wrote:

I need to write some code to count blocks inserted on the drawing....  I want to count also dynamic blocks... most of the time dynamic blocks are anonymous... so I'd need to check the EffectiveName.

 

This line gets the blocks in the block table...

... but how do I get the blocks actually inserted in the dwg?

 

(setq blks (vla-get-blocks odoc))

 

Will a regular lisp expressions (like below) work with ObjectDBX... (drawing file not open)?

 

(setq ss (ssget "X" (list (cons 0 "INSERT")(cons 2 "`*U*"))))


No you cant.

 

  • No Selection Sets (use of ssget, ssname, ssdel etc)
  • No Command calls (command "_.line" ... etc)
  • No ent* methods (entmod, entupd etc)
  • No access to System Variables (getvar, setvar, vla-getvariable, vla-setvariable etc)

You can however use

 

(vlax-for layout (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
    (vlax-for i (vla-get-block layout)

              do your thing...... )

)

 

HTH

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (534 Views)

Re: Get a block count without opening dwg ObjectDBX

01-30-2013 01:37 AM in reply to: mdhutchinson

I have one from oldies, but don't remember how it works

with limited test it looks good though

(defun C:DYBC (/ acapp  blkinfo blkname counts dbxdoc first fn group odbx opendoc qty)
  (setq	acapp  (vlax-get-acad-object))
  (if
    (and (setq fn "C:\\Test\\dynamics.dwg")
	 (setq fn (findfile fn)))

     (progn

       (setq odbx (vla-getinterfaceobject
		    acapp
		    (strcat "ObjectDBX.AxDbDocument."
			    (itoa (atoi (getvar "acadver"))))))
       (setq opendoc (not (vl-catch-all-error-p
			    (vl-catch-all-apply 'vla-open (list odbx fn)))))
       (if opendoc
	 (progn
	   (setq dbxdoc odbx)
	   (vlax-for lt	 (vla-get-layouts dbxdoc)
	     (vlax-for blkobj  (vla-get-block lt)
	       (if (eq (vla-get-objectname blkobj) "AcDbBlockReference")
		 (progn
		   (setq blkname (vla-get-effectivename blkobj))
		   (setq blkinfo (cons (cons blkname 1) blkinfo)))
		 )))

	   (setq counts nil)
	   (while
	     (setq first (caar blkinfo))

	      (setq qty	(vl-remove-if
			  '(lambda (b) (not (eq (car b) first)))
			  blkinfo))
	      (setq group  (cons first (apply '+ (mapcar 'cdr qty)))
		    counts (cons group counts))
	      (setq blkinfo (vl-remove-if '(lambda (x) (member x qty)) blkinfo))
	      )
	   )
	 )
       (if odbx
	 (vl-catch-all-apply 'vlax-release-object (list odbx)))
       (alert (vl-princ-to-string counts))
       )
     )
		(princ)
		)
(princ "\n\t---\tStart command with DYBC\t---")
(prin1)
(or(vl-load-com)(princ))

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.