BOLT TEXT COUNTING AND SUM

BOLT TEXT COUNTING AND SUM

Gaganpatel
Collaborator Collaborator
1,343 Views
21 Replies
Message 1 of 22

BOLT TEXT COUNTING AND SUM

Gaganpatel
Collaborator
Collaborator

Dear Sir,

 

I want to count and make the sum.

 

1*M16*40

2*M16*35

8*M16*50

1*M16*40

2*M16*35

2*M16*45

1*M16*6PW

1*M16*10PW

2*M16*45

2*M16*35

1*M16*10PW

1*M16*6PW

 

select object after and before text sum total drawing text sum.

 

Final result will be

M16x35-6

M16x40-2

M16x45-4

PW16x6-2

PW16x10-2

0 Likes
Accepted solutions (1)
1,344 Views
21 Replies
Replies (21)
Message 2 of 22

ВeekeeCZ
Consultant
Consultant

Possibly like this

 

(vl-load-com)

(defun c:Tsum ( / s i c n l)

  (if (setq s (ssget '((0 . "TEXT") (1 . "*M16*"))))
    (repeat (setq i (sslength s))
      (setq c (cdr (assoc 1 (entget (ssname s (setq i (1- i))))))
	    n (atoi c)
	    c (strcat (if (wcmatch c "*PW") "PW" "M") "16x"
		      (substr (vl-string-subst "" "PW" c) 7) "-")
	    l (if (setq a (assoc c l))
		(subst (cons c (+ (cdr a) n)) a l)
		(cons (cons c n) l)))))
  (foreach i l
    (princ (strcat "\n" (car i) (itoa (cdr i)))))
  (princ)
  )
0 Likes
Message 3 of 22

Kent1Cooper
Consultant
Consultant

My take on it, which allows for other than sizes of 16, and sorts the categories in reporting:

 

(defun C:BOLTSUM (/ pre* post* ss n txt size sizes)
  (defun pre* () (substr txt 1 (vl-string-position 42 txt)))
  (defun post* () (substr txt (+ (vl-string-position 42 txt) 2)))
  (if (setq ss (ssget '((0 . "TEXT") (8 . "Bolt_Qty"))))
    (progn ; then
      (repeat (setq n (sslength ss)) ; then
        (setq
          txt (cdr (assoc 1 (entget (ssname ss (setq n (1- n))))))
          size (vl-string-subst "x" "*" (post*))
        ); setq
        (if (wcmatch txt "*PW") ; move PW to front in place of M
          (setq size (vl-string-subst "PW" "M" (vl-string-right-trim "PW" size)))
        ); if
        (if (not (member size sizes)) (progn (setq sizes (cons size sizes)) (set (read size) 0)))
        (set (read size) (+ (eval (read size)) (atoi (pre*))))
      ); repeat
      (foreach item (vl-sort sizes '<)
        (prompt (strcat "\n" item "-" (itoa (eval (read item)))))
      ); foreach
    ); progn
  ); if
  (princ)
); defun

 

Kent Cooper, AIA
0 Likes
Message 4 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

Thanks for help


I want to many types text show but select only bolt text and final result is place in text pick point.

Please see the example file.

0 Likes
Message 5 of 22

ancrayzy
Advocate
Advocate

I get the error

Command: BOLTSUM
Select objects: Specify opposite corner: 271 found
Select objects: Specify opposite corner: 271 found (271 duplicate), 271 total
Select objects:  ; error: bad argument type: numberp: nil

 

0 Likes
Message 6 of 22

Kent1Cooper
Consultant
Consultant

@ancrayzy wrote:

I get the error

....
Select objects:  ; error: bad argument type: numberp: nil

 


That's because the rules have been changed on us.  In the original drawing, all Text objects on the "Bolt_Qty" Layer were applicable, so the selection filtered for object type and Layer.  In the newer one, there are many Text objects on the same Layer that don't fit the text-content format in the original.

 

IF it is consistent that the relevant Text objects will always contain * in them somewhere, and no others will, then this addition:

 

  (if (setq ss (ssget '((0 . "TEXT") (8 . "Bolt_Qty") (1 . "*`**"))))

 

results in this [at the command line for now], from grabbing everything in a big window:


M16x105-1
M16x110-1
M16x35-14
M16x40-6
M16x45-1
M16x65-6
M16x75-34
M16x80-16
M16x85-6
M16x95-6
M20x85-6
M20x95-12
M24x105-10
M24x110-2
M24x130-78
M24x140-2
PW16x6-6

 

So, @Gaganpatel , is that a quality of the text content that can be relied on absolutely, or are there more things that might be different from what you've posted so far?

Kent Cooper, AIA
0 Likes
Message 7 of 22

Gaganpatel
Collaborator
Collaborator

 

Dear Sir,

 

I am not lisp programmer please give complete lisp program.

0 Likes
Message 8 of 22

Kent1Cooper
Consultant
Consultant

@Gaganpatel wrote:

... please give complete lisp program.


Not without an answer to my latest question, and some others:

Do you really want the Text listing on the "ANGLE" Layer with a color override?

 

Would the Text Style and height always be the same in any drawing, or should the height be determined by the intended drawing scale, or should it just use whatever the current Text Style and height are?

 

If it should be a particular named Style, can you be sure the Style will always be defined in the drawing?

 

Will it always be a fixed-height Style as in your sample?

Kent Cooper, AIA
0 Likes
Message 9 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

 

Text height should be not fix

1. layer fixed

2. text style fixed

0 Likes
Message 10 of 22

Kent1Cooper
Consultant
Consultant

@Gaganpatel wrote:

....

Text height should be not fix

....

2. text style fixed


The Text in the sample drawing is of fixed height.  Do the above-quoted parts mean that the Text height should not always be the same in all drawings, but that the Text style name will always the same?  Will that always have a fixed height as in the sample drawing, but the height may vary in different drawings?

 

You still haven't answered the question at the end of Message 6.

 

EDIT:  Also, would there ever be letter prefixes in the final report listing other than "M" and "PW"?

Kent Cooper, AIA
0 Likes
Message 11 of 22

Gaganpatel
Collaborator
Collaborator

 

Dear Sir,

 

Please find the attached final example file and please help me.

Final result is show in text pick point.

0 Likes
Message 12 of 22

Gaganpatel
Collaborator
Collaborator
Dear Sir,

Please help
0 Likes
Message 13 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

 

Please help 

0 Likes
Message 14 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

 

Please help me bolt counting.

0 Likes
Message 15 of 22

daniel_cadext
Advisor
Advisor

Sorry for the intrusion, I was just working on something similar for python, making a data extraction using pandas

https://pyarx.blogspot.com/2023/11/autocad-python-pandas-and-excel.html

 

import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed
import traceback
 
import pandas as pd

def PyRxCmd_doit():
    try:
        data = {'PART': [],
                'QTY': []}
        #ss
        filter = [(Db.DxfCode.kDxfStart, "TEXT"),  # type
                  (Db.DxfCode.kDxfLayerName, "Bolt_Qty")]  # layer
        ssres = Ed.Editor.selectAll(filter)
        if ssres[0] != Ed.PromptStatus.eNormal:
            return #bail
        
        #get data
        for dbTextId in ssres[1].objectIds():
            dbText = Db.Text(dbTextId)
            strVal = dbText.textString()
            
            #edit the rules
            if not 'M' in strVal:
                continue
            strVal = strVal.replace('PW','')
            
            splittext = strVal.split('*')
            if len(splittext) != 3:
                continue
    
            data['PART'].append(splittext[1]+'X'+splittext[2])
            data['QTY'].append(int(splittext[0])) #cast
            
    
        #create the dataframe, then group by
        df = pd.DataFrame(data)
        dfgr = df.groupby(['PART'],
                         sort=False, as_index=False).agg({'QTY': 'sum'})
        
        print(dfgr.to_string())
        #with pd.ExcelWriter('e:\\pandas_to_excel.xlsx') as writer:
        #    dfgr.to_excel(writer, sheet_name='sheet1')
 
    except Exception as err:
        traceback.print_exception(err)
 
     

 

Command: DOIT
       PART  QTY
0    M16X80   16
1    M20X95   12
2    M20X85    6
3    M16X75   34
4   M24X105   10
5   M24X110    2
6    M16X65    6
7    M16X85    6
8   M24X130   78
9    M16X95    6
10  M24X140    2
11  M16X110    1
12  M16X105    1
13    M16X6    6
14   M16X35   14
15   M16X45    1
16   M16X40    6

 

 

 

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 16 of 22

Sea-Haven
Mentor
Mentor

Try this, version 1, can be improved.

 

(defun c:wow ( / lst lst2 lst3 lst2a x ss ent val pt1 pt2)

;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] String to process
;; del - [str] Delimiter by which to separate the string
;; Returns: [lst] List of strings
 
(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)
; By Gile
(defun my-count (a L)
  (cond
   ((null L) 0)
   ((equal a (car L)) (+ 1 (my-count a (cdr L))))
   (t (my-count a (cdr L))))
)

;; List Duplicates  -  Lee Mac
;; Returns a list of items appearing more than once in a supplied list

(defun LM:ListDupes ( ll )
   (if ll
       (if (member (car ll) (cdr ll))
           (cons (car ll) (LM:ListDupes (vl-remove (car ll) (cdr ll))))
           (LM:ListDupes (vl-remove (car ll) (cdr ll)))
       )
   )
)

(defun Text (pt hgt str)
 (entmakex (list (cons 0 "TEXT")
                 (cons 10  pt)
                 (cons 40 hgt)
                 (cons 1  str)))
)


(setq ss (ssget '((0 . "TEXT")(8 . "Bolt_Qty"))))
(if (= ss nil)
(alert "No text found on layer Bolt_Qty")
(progn
(setq lst '())
(repeat (setq x (sslength ss))
(setq ent (entget (ssname ss (setq x (1- x)))))
(setq lst (cons (LM:str->lst (cdr (assoc 1 ent)) "*" ) lst))
)

(setq lst2 '())
(foreach val lst
(repeat (atoi (car val))
(setq lst2 (cons (list (cadr val)(caddr val)) lst2))
)
)

(setq lst2 (vl-sort lst2
 '(lambda (a b)
  (cond
  ((< (car a) (car b)))
  ((= (car a) (car b)) (< (cadr a) (cadr b)))
  )
  )
  )
)

(setq lst3 '())
(setq lst2a (LM:ListDupes lst2))
(foreach val lst2a
 (setq cnt (my-count  val lst2))
 (setq lst3 (cons (list  (nth 0 val)(nth 1 val) cnt) lst3))
)

(setq lst3 (vl-sort lst3
 '(lambda (a b)
  (cond
  ((< (car a) (car b)))
  ((= (car a) (car b)) (< (cadr a) (cadr b)))
  )
  )
  )
)

(setq pt1 (getpoint "\nPick top left for text "))
(setq y 3)
(text pt1 40 "%%uBolt Summation")
(setq pt2 (mapcar '+ pt1 (list 0 (* -64.75 2.) 0.0)))

(foreach val lst3
  (text pt2 40 (strcat (car val) "X" (cadr val) "-" (rtos (caddr val) 2 0)))
  (setq pt2 (mapcar '+ pt2 (list 0 -64.75 0.0)))
  (setq y (1+ y))
)

(setq pt1 (mapcar '+ pt1 (list -140 121 0.0)))
(setq pt2 (mapcar '+ pt1 (list 813 (* (+ y 2) -64.75) 121 0.0)))
(command "Rectang" Pt1 pt2)

)
)
(princ)
)
(c:wow)

 

0 Likes
Message 17 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

Thank you very Much 🙏🙏🙏

Work is good.

One modify please output results Bolt qty. Summary required small size to large size.

0 Likes
Message 18 of 22

Sea-Haven
Mentor
Mentor

Explain more what you want not sure.

0 Likes
Message 19 of 22

Gaganpatel
Collaborator
Collaborator

Dear Sir,

 

Please modify bolt summation result by smallest to largest.

 

Gaganpatel_0-1699336140598.png

 

 

0 Likes
Message 20 of 22

Sea-Haven
Mentor
Mentor

Ok code modified above to re-sort the list before making the legend.