count and list all dynamic block with a same linear parameter (stretched)

count and list all dynamic block with a same linear parameter (stretched)

eric.ngTK42G
Participant Participant
547 Views
8 Replies
Message 1 of 9

count and list all dynamic block with a same linear parameter (stretched)

eric.ngTK42G
Participant
Participant

hi all, here are some dynamic block and i would like to count them, these blocks have linear parameter and linear stretch, attri label XX is their own name. i tried with this program and it can't get the length of stretch distrance

(defun c:CT (/ ss blocks attrTag attrValue attrList countTable resultTable acadDoc modelSpace tableInsertionPoint table inputValue)
  ;; 设置属性标签
  (setq attrTag "XX")
  
  ;; 获取用户输入的通配符值
  (setq inputValue (getstring "\n请输入属性值的通配符(例如T4*): "))
  
  ;; 选择所有包含属性标签为XX的块
  (prompt "\n选择包含属性标签为XX的块: ")
  (setq ss (ssget '((0 . "INSERT") (66 . 1))))
  
  (if ss
    (progn
      ;; 初始化属性值列表
      (setq attrList '())
      
      ;; 遍历选择的块
      (setq i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        (setq entData (entget ent))
        
        ;; 获取块的属性
        (setq attEnt (entnext ent))
        (setq distance1 nil)
        (while (and attEnt (/= (cdr (assoc 0 (entget attEnt))) "SEQEND"))
          (setq attData (entget attEnt))
          (if (= (cdr (assoc 2 attData)) attrTag)
            (progn
              (setq attrValue (cdr (assoc 1 attData)))
              ;; 检查属性值是否符合通配符
              (if (wcmatch attrValue inputValue)
                (progn
                  ;; getting linear para distance(assume that attribute label is 'distance1')
                  (setq distance1 (cdr (assoc 1 (entget (entnext ent)))))
                  (setq attrList (cons (list attrValue distance1) attrList))
                )
              )
            )
          )
          (setq attEnt (entnext attEnt))
        )
        (setq i (1+ i))
      )
      
      ;; Count the number of blocks with the same attribute value and linear parameter distance
      (setq countTable '())
      (foreach attrPair attrList
        (setq attrValue (car attrPair))
        (setq distance1 (cadr attrPair))
        (setq found nil)
        (foreach item countTable
          (if (and (equal (car item) attrValue) (equal (cadr item) distance1))
            (progn
              (setq found t)
              (setq countTable (subst (list attrValue distance1 (1+ (caddr item))) item countTable))
            )
          )
        )
        (if (not found)
          (setq countTable (cons (list attrValue distance1 1) countTable))
        )
      )
      
      ;; 获取当前文档和模型空间
      (setq acadDoc (vla-get-activedocument (vlax-get-acad-object)))
      (setq modelSpace (vla-get-modelspace acadDoc))
      
      ;; setting insert point for sheet
      (setq tableInsertionPoint (getpoint "\n指定表格插入点: "))  ;;select insert point
      (if (not tableInsertionPoint)
        (setq tableInsertionPoint '(0 0 0)) ; 默认插入点为原点
      )
      
      ;; create sheet
      (setq table (vla-addtable modelSpace
                                (vlax-3d-point tableInsertionPoint)
                                (+ (length countTable) 2) ; 行数(属性值数量 + 表头)
                                3                        ; 列数(属性值、数量和线性参数距离)
                                450.0                    ; 行高
                                1700.0))                 ; 列宽
      
      ;; setting sheet title
      (vla-settext table 0 0 "动态块统计")
      (vla-setcelltextheight table 0 0 100) ; 标题文字高度
      (vla-setcelltextstyle table 0 0 "LPA") ; 标题文字样式
      
      ;; setting sheet head
      (vla-settext table 1 0 "属性值")
      (vla-settext table 1 1 "数量")
      (vla-settext table 1 2 "线性参数距离")
      (vla-setcelltextheight table 1 0 100) ; 表头文字高度
      (vla-setcelltextheight table 1 1 100)
      (vla-setcelltextheight table 1 2 100)
      (vla-setcelltextstyle table 1 0 "LPA") ; 表头文字样式
      (vla-setcelltextstyle table 1 1 "LPA")
      (vla-setcelltextstyle table 1 2 "LPA")
      
      ;; about filling data to sheet
      (setq row 2)
      (foreach item countTable
        (vla-settext table row 0 (car item)) ; 属性值
        (vla-settext table row 1 (itoa (caddr item))) ; 数量
        (vla-settext table row 2 (cadr item)) ; 线性参数距离
        (vla-setcelltextheight table row 0 100) ; 数据文字高度
        (vla-setcelltextheight table row 1 100)
        (vla-setcelltextheight table row 2 100)
        (vla-setcelltextstyle table row 0 "LPA") ; 数据文字样式
        (vla-setcelltextstyle table row 1 "LPA")
        (vla-setcelltextstyle table row 2 "LPA")
        (setq row (1+ row))
      )
      
      ;; 提示完成
      (princ "\n表格已创建并插入到图形中。") ;;sheet has inserted to the drawing
    )
    (princ "\n未选择任何块。") ;;doesn't selected any block
  )
  (princ)
)

 

ericngTK42G_0-1739155854909.png

in my exception, the third colume should show me the distance i stretch the block, value can't be see in here:

ericngTK42G_1-1739156061424.png

 

so my problem is :

1. how to get the value i need? i tried vlax-get-propety but return error

2. how to modify the program to function as I expected

0 Likes
Accepted solutions (1)
548 Views
8 Replies
Replies (8)
Message 2 of 9

komondormrex
Mentor
Mentor
Accepted solution

@eric.ngTK42G 

hey there,

check the following.

 

(defun c:ct (/ type_selected_list attrTag inputValue insert_sset insert_data_list xx_attribute counttable acadDoc modelSpace tableInsertionPoint table row) 
  (setq attrTag "XX")
  (setq inputValue (getstring "\nEntger wildcard for attribute value (eg. T4*): "))
  (if (setq insert_sset (ssget '((0 . "insert") (66 . 1))))
	  (if (setq insert_data_list (mapcar '(lambda (insert) (list (getpropertyvalue insert "XX") (getpropertyvalue insert "AcDbDynBlockPropertydistance")))  
					      (vl-remove-if-not '(lambda (insert) (vl-some '(lambda (attribute) (and (= attrTag (vla-get-tagstring (setq xx_attribute attribute)))
													       	     (wcmatch (vla-get-textstring xx_attribute) inputValue)
													        )
										            )
										            (vlax-invoke (vlax-ename->vla-object insert) 'getattributes)
										  )
							          )
					      		          (vl-remove-if 'listp (mapcar 'cadr (ssnamex insert_sset)))
					     )
				    )
	      )
	    	(progn
		  (foreach insert_data insert_data_list
		    (if (not (member (car insert_data) type_selected_list)) (setq type_selected_list (append type_selected_list (list (car insert_data)))))
		  )
		  (setq counttable (mapcar '(lambda (type) (setq type_list (vl-remove-if-not '(lambda (type_data) (= type (car type_data))) insert_data_list))
					     		   (list type (length type_list) (apply '+ (mapcar 'cadr type_list)))
					    )
					    (setq type_selected_list (acad_strlsort type_selected_list))
			  	   )
		  )
		  (setq acadDoc (vla-get-activedocument (vlax-get-acad-object)))
	      	  (setq modelSpace (vla-get-modelspace acadDoc))
	      
	          ;; setting insert point for sheet
	      	  (setq tableInsertionPoint (getpoint "\nTable insertion point: "))
	      	  (if (not tableInsertionPoint)
	        	(setq tableInsertionPoint '(0 0 0))
	      	  )
	      
	     	  ;; create table
	          (setq table (vla-addtable modelSpace
	                        (vlax-3d-point tableInsertionPoint)
	                        (+ (length countTable) 2) 
	                        3                        
	                        450.0                    
	                        1700.0))                 
	      
	          ;; setting table title
	          (vla-settext table 0 0 "Stats")
	          (vla-setcelltextheight table 0 0 100)
	          (vla-setcelltextstyle table 0 0 "LPA")
	      
	          ;; setting sheet head
	          (vla-settext table 1 0 "PValue")
	          (vla-settext table 1 1 "QTY")
	          (vla-settext table 1 2 "LDist")
	          (vla-setcelltextheight table 1 0 100) 
	          (vla-setcelltextheight table 1 1 100)
	          (vla-setcelltextheight table 1 2 100)
	          (vla-setcelltextstyle table 1 0 "LPA") 
	          (vla-setcelltextstyle table 1 1 "LPA")
	          (vla-setcelltextstyle table 1 2 "LPA")
	      
	          ;; about filling data to sheet
	          (setq row 2)
		  (foreach item countTable
			(vla-settext table row 0 (car item)) 
		        (vla-settext table row 1 (itoa (cadr item))) 
		        (vla-settext table row 2 (rtos (caddr item) 2 1)) 
		        (vla-setcelltextheight table row 0 100) 
		        (vla-setcelltextheight table row 1 100)
		        (vla-setcelltextheight table row 2 100)
		        (vla-setcelltextstyle table row 0 "LPA") 
		        (vla-setcelltextstyle table row 1 "LPA")
		        (vla-setcelltextstyle table row 2 "LPA")
		        (setq row (1+ row))
	          )
	          (princ "\nTable ceated and inserted") ;;sheet has inserted to the drawing
	        )
	        (princ "\nNo block selected") ;;doesn't selected any block
	  )
  )
)

 

0 Likes
Message 3 of 9

Sea-Haven
Mentor
Mentor

It may be worthwhile to add these settings after making the table. You can set text height etc.

;; Set the alignment for the Data, Header, and Title rows
(vla-SetAlignment table (+ acDataRow acHeaderRow acTitleRow) acMiddleCenter)

;; Set the text height for the Title, Header and Data rows
(vla-SetTextHeight table acDataRow txtht)
(vla-SetTextHeight table acHeaderRow (* txtht 1.2))
(vla-SetTextHeight table acTitleRow (* txtht 1.5))

 

0 Likes
Message 4 of 9

eric.ngTK42G
Participant
Participant

Thx.😁

0 Likes
Message 5 of 9

eric.ngTK42G
Participant
Participant

it works on 2025, thx. 🙂

but on 2025 LT, it will report a error : ADS require error (i'm in chinese version so i can't sure the translation is right)

 

and i try to read your code :

looks like the linear parameter distance is stroaged in "AcDbDynBlockPropertydistance"?  and getting this value via getpropertyvalue

 

how can i know how many values and its name in a block? is there any  reference manual? lol

0 Likes
Message 6 of 9

eric.ngTK42G
Participant
Participant

updated : the value LDist on table from your code is sum of the values ​​of the selected block of the XX, what I want is their value( eg. T4(1) in the dwg i attached, LDist is 13500, but your table is 40500, it is the sum for all 3 pcs )

0 Likes
Message 7 of 9

komondormrex
Mentor
Mentor

so you want every dynamic block data to be listed in table separatedly ?

0 Likes
Message 8 of 9

komondormrex
Mentor
Mentor

using dumpallproperties function.

(dumpallproperties (car (entsel))

komondormrex_0-1739253212742.png

 

0 Likes
Message 9 of 9

eric.ngTK42G
Participant
Participant

copy that, thx. again

maybe i can finish those code by myself and no disturb others next time 😀

0 Likes