Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
tdcad
Posts: 85
Registered: ‎06-16-2004
Message 1 of 24 (522 Views)

COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

522 Views, 23 Replies
04-02-2012 01:18 PM

Hi all,

Can some one help me a lisp to count dynamic block including total lenght.

 

Thanks

Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 2 of 24 (521 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 01:26 PM in reply to: tdcad

Hi

The first block into your drawing is not considered as a Dynamic Block , so the following routine would would the trick on

the same dynamic blocks that you uploaded with you sample drawing .

 

(defun c:TesT (/ total selectionset count intger selectionsetname vl a b) (vl-load-com)
  ;;; Tharwat 02 . April . 2012 ;;;
  ;; Count number of selected Dynamic blocks and length of total selected dynamic blocks
  (if (setq total 0
            selectionset
             (ssget '((0 . "INSERT") (2 . "`*U*")))
      )
    (progn
      (setq count (sslength selectionset))
      (repeat (setq intger (sslength selectionset))
        (setq selectionsetname
               (ssname selectionset
                       (setq intger (1- intger))
               )
        )
        (setq vl (vlax-ename->vla-object selectionsetname))
        (vla-getboundingbox vl 'a 'b)
        (setq total (+ total
                       (distance (list (car (vlax-safearray->list b))
                                       (cadr (vlax-safearray->list a))
                                 )
                                 (vlax-safearray->list a)
                       )
                    )
        )
      )
      (print (strcat " Total number of Dynamic Blocks :"
                     "< "
                     (itoa count)
                     " >"
             )
      )
      (print (strcat " Total lengths of Dynamic Blocks :"
                     "< "
                     (rtos total 2)
                     " >"
             )
      )
    )
    (princ)
  )
  (princ)
)

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 3 of 24 (516 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 01:49 PM in reply to: tdcad

This one is a little bit better for the same block name and wth message box .

 

(defun c:Test
              (/ total selectionset count intger selectionsetname a b)
  (vl-load-com)
;;; Tharwat 02 . April . 2012 ;;;
  ;; Count number of selected Dynamic blocks and length of selected dynamic blocks
  (if (setq total 0
            selectionset
             (ssget '((0 . "INSERT")
                      (-4 . "<or")
                      (2 . "`*U*")
                      (2 . "_P1000")
                      (-4 . "or>")
                     )
             )
      )
    (progn
      (setq count (sslength selectionset))
      (repeat (setq intger (sslength selectionset))
        (setq selectionsetname
               (ssname selectionset
                       (setq intger (1- intger))
               )
        )
        (vla-getboundingbox
          (vlax-ename->vla-object selectionsetname)
          'a
          'b
        )
        (setq total (+ total
                       (distance (list (car (vlax-safearray->list b))
                                       (cadr (vlax-safearray->list a))
                                 )
                                 (vlax-safearray->list a)
                       )
                    )
        )
      )
      (alert (strcat " Total number of Dynamic Blocks :"
                     "< "
                     (itoa count)
                     " >"
                     "\n"
                     " Total lengths of Dynamic Blocks :"
                     "< "
                     (rtos total 2)
                     " >"
             )
      )
    )
    (princ)
  )
  (princ)
)

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Mentor
devitg
Posts: 1,704
Registered: ‎03-14-2004
Message 4 of 24 (502 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 05:42 PM in reply to: _Tharwat

Hi Tharwat, I know the OP show only the _p1000 "original" block. 
But, what could be happen if there is another "original" block , and furthermore, what about if it have a inclination?.

I think the user shall pick the block to size and count, then using the EFFECTIVE NAME , select all them , or split them from the inserted blocks.

If they are not a 0 inclination or slope , there will be a issue to get the real distance.

 

Gabriel.

*Expert Elite*
pbejse
Posts: 2,500
Registered: ‎11-24-2009
Message 5 of 24 (500 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 08:08 PM in reply to: tdcad

Try this

 

(defun c:CDL ( / ss val ttl)
      (setq  bn "_P1000"
             ttl 0 i 0)
      (if (ssget "_X" (list '(0 . "INSERT")(cons 2 (strcat bn ",`*U*"))))
  	(progn
	      (vlax-for  itm  (setq ss (vla-get-activeselectionset
	                               (vla-get-activedocument
	                                     (vlax-get-acad-object))))
	 (if (and (= :vlax-true
                     (vla-get-isdynamicblock itm))
                  (setq val  (vl-remove-if-not
                                   '(lambda (x)
                                          (eq   (vla-get-PropertyName
                                                      x)
                                                "Distance"))
                                   (vlax-invoke
                                         itm
                                         'getdynamicblockproperties))))
               (setq ttl (+ ttl (vlax-get (car val) 'value)) i (1+ i))
               )
                    )
	 (vla-delete ss)
              (alert (strcat "Number of Blocks: " (itoa i)
                            "\nTotal Length: " (rtos ttl 2 2)))
              )
    (prompt "\n** Nothing selected ** "))
      
  (princ))

 HTH

Mentor
devitg
Posts: 1,704
Registered: ‎03-14-2004
Message 6 of 24 (498 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 08:18 PM in reply to: pbejse
Hi PBEJSE , thanks again for it (vlax-invoke itm 'getdynamicblockproperties))))
*Expert Elite*
pbejse
Posts: 2,500
Registered: ‎11-24-2009
Message 7 of 24 (496 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-02-2012 08:32 PM in reply to: devitg

devitg wrote:
Hi PBEJSE , thanks again for it (vlax-invoke itm 'getdynamicblockproperties))))

Good for you Devtg

 

Cheers

Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 8 of 24 (485 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-03-2012 03:55 AM in reply to: devitg

devitg wrote:

Hi Tharwat, I know the OP show only the _p1000 "original" block. 
But, what could be happen if there is another "original" block , and furthermore, what about if it have a inclination?.

I think the user shall pick the block to size and count, then using the EFFECTIVE NAME , select all them , or split them from the inserted blocks.

If they are not a 0 inclination or slope , there will be a issue to get the real distance.

 

Gabriel.


You're right devitg .
I knew that and I did not write the rotine before taking a look at the attached drawing . if all blocks are similar to the once in the drawing , the code would give correct lengths , otherwise there be a mistake in the total of lengths .
the routine that given by pBe is very accurate completely .
Thanks a lot.



____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
pbejse
Posts: 2,500
Registered: ‎11-24-2009
Message 9 of 24 (476 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-03-2012 05:26 AM in reply to: _Tharwat

_Tharwat wrote:
 You're right devitg .the routine that given by pBe is very accurate completely .
Thanks a lot.

Not at all Tharwat. it lacks error trapping besides i missed this part

 ....

(= :vlax-true (vla-get-isdynamicblock itm))
    (eq (strcase (vla-get-effectivename itm)) bn)

   (setq val  (vl-remove-if-not......

 

But thanks nonetheless tharwat.

 

Cheers

 

Valued Contributor
tdcad
Posts: 85
Registered: ‎06-16-2004
Message 10 of 24 (456 Views)

Re: COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

04-04-2012 12:13 PM in reply to: tdcad

Thanks for all, but what I mean is total of length for each piece including original block (_P1000) so I can able to cut them off.

 

Thanks

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.