Code Red > AutoLISP (Vanilla / Visual)

How to start this lisp to sum numbers in each row?

(1/3) > >>

HasanCAD:
Hi,
How to start this lisp to sum numbers in each row?
As attached
Thanks

Lee Mac:
Is the text on the left of your image represented by individual text objects or an AutoCAD table?

HasanCAD:
It is Text objects

Lee Mac:
I would approach it this way:

* Make a selection of all text objects
* Iterate over the selection and create a list of the insertion point & content of each object
* Group the list by items with equal y-coordinate
* Sort each group by ascending x-coordinate
* Test whether the content of the first item in each group matches "F*"
* If so, sum the numerical content of the remaining items in the group.

HasanCAD:

--- Quote from: Lee Mac on June 15, 2019, 11:50:47 AM ---I would approach it this way:

* Make a selection of all text objects
* Iterate over the selection and create a list of the insertion point & content of each object
* Group the list by items with equal y-coordinate
* Sort each group by ascending x-coordinate
* Test whether the content of the first item in each group matches "F*"
* If so, sum the numerical content of the remaining items in the group.
--- End quote ---

This is what I ended up too but Sorting is out of my mind

--- Code - Auto/Visual Lisp: ---(Defun c:SumTbl (/ ent     enth    entl    entp    entps   entpx                   entpy   ents    entsm   entsps  entspx  entspxs                   entspy  entspys i       ss      sum     sump                 doc)   (setq Doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))  (vla-startundomark doc)      (setq entsPX nil)  (setq entsPY nil)  (setq entsPS nil)  (setq entsPXs nil)  (setq entsPYs nil)   (if (setq ss (ssget '( (0 . "text"))))    (repeat (setq i (sslength ss))      (setq ent (entget (ssname ss (setq i (1- i)))))      (setq entS (cdr (assoc 1 ent)))      (if (numberp (read entS))        (progn          (setq entPS (list entP entS))          (setq entsPS (cons entPS entsPS))           (setq entL (cdr (assoc 08 ent)))          (setq entH (cdr (assoc 40 ent)))           (setq entP (cdr (assoc 10 ent)))          (setq entPX (nth 0 entP))          (setq entPY (nth 1 entP))             (SETQ entSm (atoi sum))          (setq sumP (list (+ entPX (* 3 entH))  entPY))          (MakeText entSm sumP entL entH 0 0 1 )          )        )      )    )  (vla-endundomark doc)  (PRINC)  ) (defun MakeText (str pt lyr ht ro G72 G73)  (entmakex (list (cons 0 "TEXT")       ;***                  (cons 1 str)           ;* (the string itself)                 (cons 6 "BYLAYER")     ; Linetype name                  (cons 8 lyr)           ; layer                 (cons 10 pt)           ;* First alignment point (in OCS)                  (cons 11 pt)           ;* Second alignment point (in OCS)                  (cons 39 0.0)          ; Thickness (optional; default = 0)                 (cons 40 ht)           ;* Text height                 (cons 41 1.0)          ; Relative X scale factor, Width Factor, defaults to 1.0                 (cons 50 ro)           ; Text rotation angle                 (cons 51 0.0)          ; Oblique angle                  (cons 62 256)          ; color                  (cons 71 0)            ; Text generation flags                  (cons 72 G72)          ; Horizontal text justification type                 (cons 73 G73)          ; Vertical text justification type                 (cons 210 (list 0.0 0.0 1.0))))) (defun *error* ( msg )  (if (and msg (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")))    (princ (strcat "\nError: " msg))    )  (princ)  ) 

Navigation

[0] Message Index

[#] Next page

Go to full version