• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Visual LISP, AutoLISP and General Customization

    Reply
    Valued Contributor
    Posts: 84
    Registered: ‎06-16-2004

    COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

    321 Views, 21 Replies
    04-02-2012 01:18 PM

    Hi all,

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

     

    Thanks

    Please use plain text.
    Valued Mentor
    _Tharwat
    Posts: 459
    Registered: ‎07-02-2010

    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)
    )

     

    Please use plain text.
    Valued Mentor
    _Tharwat
    Posts: 459
    Registered: ‎07-02-2010

    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)
    )

     

    Please use plain text.
    Distinguished Contributor
    Posts: 1,596
    Registered: ‎03-14-2004

    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.

    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    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

    Please use plain text.
    Distinguished Contributor
    Posts: 1,596
    Registered: ‎03-14-2004

    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))))
    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    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

    Please use plain text.
    Valued Mentor
    _Tharwat
    Posts: 459
    Registered: ‎07-02-2010

    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.



    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    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

     

    Please use plain text.
    Valued Contributor
    Posts: 84
    Registered: ‎06-16-2004

    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

    Please use plain text.