Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

25 REPLIES 25
Reply
Message 1 of 26
tdcad
2255 Views, 25 Replies

COUNT DYNAMIC BLOCK INCLUDING TOTAL LENGTH

Hi all,

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

 

Thanks

25 REPLIES 25
Message 2 of 26
_Tharwat
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)
)

 

Message 3 of 26
_Tharwat
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)
)

 

Message 4 of 26
devitg
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.

Message 5 of 26
pbejse
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

Message 6 of 26
devitg
in reply to: pbejse

Hi PBEJSE , thanks again for it (vlax-invoke itm 'getdynamicblockproperties))))
Message 7 of 26
pbejse
in reply to: devitg


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

Good for you Devtg

 

Cheers

Message 8 of 26
_Tharwat
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.



Message 9 of 26
pbejse
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

 

Message 10 of 26
tdcad
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

Message 11 of 26
devitg
in reply to: tdcad

Despite your misinformation , all we learn some new facts. But you can use the DATAEXTRACTION , The fire is just invented
Message 12 of 26
pbejse
in reply to: devitg


@devitg wrote:
But you can use the DATAEXTRACTION ......

I concur.

 

For the fun of it

 

(defun c:CDL (/ ss val_rnd lst Pname)
  (setq	bn    "_P1000"
	Pname "Distance"
	lst   nil
  )
  (setq _rnd (lambda (n) (fix (+ n (/ n (abs n) 2.)))))
  (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)
		 )
		 (eq (strcase (vla-get-effectivename itm)) bn)
		 (setq val (vl-remove-if-not
			     '(lambda (x)
				(eq (vla-get-PropertyName
				      x
				    )
				    Pname
				)
			      )
			     (vlax-invoke
			       itm
			       'getdynamicblockproperties
			     )
			   )
		 )
	    )
	  (if (setq d
		     (assoc (_rnd (setq l (vlax-get (car val) 'value))) lst)
	      )
	    (setq lst (subst (cons (car d) (1+ (cdr d))) d lst))
	    (setq lst (cons (cons l 1) lst))
	  )
	)
      )
      (vla-delete ss)
      (textscr)
      (princ "\nQTY\tLength\t   Total:")
      (foreach it (vl-sort lst '(lambda (k l) (< (car k) (car l))))
	(princ (strcat "\n"
		       (itoa (cdr it))
		       "\t\t"
		       (rtos (car it) 2 2)
		       "\t\t"
		       (rtos (* (cdr it) (car it)) 2 2)
	       )
	)
      )
    )
    (prompt "\n** Nothing selected ** ")
  )

  (princ)
)

 

Command: cdl

QTY    Length          Total:
1      12.00           12.00
3      18.00           54.00
2      22.00           44.00
3      24.00           72.00
1      28.00           28.00

 

Sorted by Length

 

HTH

Message 13 of 26
tdcad
in reply to: tdcad

thanks for all !!!

Message 14 of 26
pbejse
in reply to: tdcad


@tdcad wrote:

thanks for all !!!


Did the code worked for you?

Message 15 of 26
tdcad
in reply to: devitg

Hi Pbejse,

Thanks for asking, at this time it does not work for me 😞

and if you don't mind would you help me one more time.

I am looking for.

 

QTY    BLOCK(S) NAME    LENGTH

  1          _P1000                   1'-0"

  1          _P1000                   1'-6"

  1          _P1000                   1'-10"

  1          _P1000                   2'-0"

  1          _P1000                   2'-4"

 

Of course we have more than one bock in a drawing.

 

Thanks

Message 16 of 26
pbejse
in reply to: tdcad


@tdcad wrote:

Hi Pbejse,

Thanks for asking, at this time it does not work for me 😞

and if you don't mind would you help me one more time.

 

Of course we have more than one bock in a drawing.

 

Thanks


Ok tdcad , humor me.

I would presume those are Dynamic blocks?  and does it all have DISTANCE parameter?

And the names differs from every drawing?

The routine would have done and over with if you  laid out everything from the get-go.

 

I perosnally dont mind helping you out, but you need to be very CLEAR about what you're trying to accomplish.

Everything we did here were based on your sample drawing

 

BTW: Did you try Data Extraction command suggested by devtg?

 

Message 17 of 26
tdcad
in reply to: devitg

Hi Pbejse,

 1- In a drawing I had more than one dynamic bock (they are unistruts but different type, name) withdistance  parameters

 2- All bock name will be same and never change.

 3- I did try  DATAEXTRACT (suggested by Devitg) but it's make me alot confusing.

 

Thanks

Message 18 of 26
pbejse
in reply to: tdcad


@tdcad wrote:

Hi Pbejse,

 1- In a drawing I had more than one dynamic bock (they are unistruts but different type, name) withdistance  parameters

 2- All bock name will be same and never change.

 3- I did try  DATAEXTRACT (suggested by Devitg) but it's make me alot confusing.

 

Thanks



1-2. We could re-write the cod above and use a fitler for block names.  would you want it to be generic?. Thing is the Dynamic Property name "distance" can be modiied. it could be "distance1" "dist" depending on the user. If not a generic code a specific sets of properties and block names should be met fot he code to work


As for this

QTY    BLOCK(S) NAME    LENGTH

  1          _P1000                   1'-0"

  1          _P1000                   1'-6"

  1          _P1000                   1'-10"

 

You notice the difference between what i posted

QTY    Length          Total:
1      12.00           12.00
3      18.00           54.00

 

Does that mean you dont need/want to see the total for every varying lenghts? We could do this:

Block(S) Name   QTY    Length          Total:
_P1000                1         1'-0"              1'-0"
_P500                  3         1'-6"              4'-6"

 

Message 19 of 26
tdcad
in reply to: devitg

Pbejse,

Sound good but I have a question is if we have some blocks with same name but the length are different.

Can you able call out those lengths with same bocks name?

 

Example:

Block(S) Name   QTY    Length          Total:
_P1000                1         1'-0"              1'-0"
_P500                  3         1'-6"              4'-6"

_P500                  2         2'-0"              4'-0"

 

Thanks

Message 20 of 26
pbejse
in reply to: tdcad

 

Try:

(defun c:CDL (/ ss strnum bnm val lst d l Pname)
;;;		pBe Arpil 2012		     ;;;
(vl-load-com)      
  (setq	Pname "Distance"
	lst   nil
  )
  (defun strnum  (var val / p)
      (repeat (abs (- val (strlen var)))
            (setq p (strcat " "
                            (if p p " "))))
      (strcat var p)
      )
  (if (ssget "_X" '((0 . "INSERT")(2 . "_P1000,_P57,_P2000,`*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 bnm (vla-get-effectivename itm))
		 (setq val (vl-remove-if-not
			     '(lambda (x)
				(eq (vla-get-PropertyName
				      x ) Pname ))
			     (vlax-invoke itm
			       'getdynamicblockproperties
			     )
			   )
		 )
	    )
	  (if (setq d
		     (assoc (list (setq l (vlax-get (car val) 'value)) bnm) lst)
	      )
	    (setq lst (subst (list (list l bnm) (1+ (cadr d))) d lst))  
	    (setq lst (cons (list (list l bnm) 1 ) lst))
	  )
	)
      )
      (vla-delete ss)
      (textscr)
      (princ "\nBlock Name       QTY     Length         Total")
      (foreach it (vl-sort lst '(lambda (k l) (< (cadar k) (cadar l))))
		(princ (strcat "\n"
		(strnum (cadar it) 16)
		(strnum (itoa (last it)) 7)
		(strnum (rtos (caar it) 4 4) 14)
		(rtos (* (caar it) (last it)) 4 4)))
      )
    )
    (prompt "\n** Nothing selected ** ")
  )
  (princ)
)

  

The blocknames are hardcoded on the ssget filter

(ssget "_X" '((0 . "INSERT")(2 . "_P1000,_P57,_P2000,`*U*")));<-- do not removed that last entry

You need to change these Block names according to your drawing.

 

Block Name       QTY     Length         Total
_P1000               2       1'-0"               2'-0"
_P1000               3       1'-11 1/2"       5'-10 9/16"
_P57                   1       1'-6"               1'-6"
_P57                   1       1'-10"             1'-10"
_P57                   1       2'-0"               2'-0"
_P57                   1       1'-0"               1'-0"
_P57                   2       2'-4"               4'-8"

 

HTH

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost