Autolisp - Data Handling & Creating Tables with Autocad LT

Autolisp - Data Handling & Creating Tables with Autocad LT

traci_haberghamJQEJT
Enthusiast Enthusiast
1,173 Views
10 Replies
Message 1 of 11

Autolisp - Data Handling & Creating Tables with Autocad LT

traci_haberghamJQEJT
Enthusiast
Enthusiast

Hi all,

Please could I have some advice on the best way to approach what I am wanting to do.

 

Our drawings contain area blocks which contain attributes containing the name and areas (m2 & ft2).  I am wanting to schedule these into tables but will need to analyse the data first so the right information goes in the right table. 

 

At present we use the same block (existing and proposed areas).  This could change to ease the data handling.

 

I then need to filter the information into Gross/NIA Areas (existing and proposed) and General Areas (existing and proposed).

 

Would I be best extracting this information to a csv file, sorting out the tables in there and importing the tables into Autocad

 

or 

 

Data handling when selecting the blocks and creating a table from that data?

 

(this data will be updated from time to time so need the ability to either update or regenerate a table).

 

My programming skills aren't the best, I already have the extract csv file set up but just checking the best way to approach.

 

Any help appreciated.

 

Thank you, Traci

0 Likes
Accepted solutions (2)
1,174 Views
10 Replies
Replies (10)
Message 2 of 11

Sea-Haven
Mentor
Mentor

I tried something I have and got this is it what you want ?

 

SeaHaven_0-1731731608929.png

 

0 Likes
Message 3 of 11

traci_haberghamJQEJT
Enthusiast
Enthusiast

Yes that's exactly what I want but just columns B, C & D.  Is it easy to do?  Thank you

 

 

0 Likes
Message 4 of 11

Sea-Haven
Mentor
Mentor

You say have LT is it 2024+ as that has lisp functionality. 

 

The image was created by a out of box code but it could be modified for column order and number of columns. 

 

Please answer LT question.

0 Likes
Message 5 of 11

traci_haberghamJQEJT
Enthusiast
Enthusiast

Hi there, yes its LT 2025 😊 so luckily autolips functionality.

 

0 Likes
Message 6 of 11

Sea-Haven
Mentor
Mentor

Been on holidays so added to my to do list.

 

Back again try this.

 

(defun c:wow ( / AH:table_make att atts lst lst2 obj obj2 ss txtsz total val)

(defun AH:table_make (numrows numcolumns  txtht /  curspc colwidth numcolumns  objtable rowheight sp doc)
 ;; Get the Dictionaries collection and the TableStyle dictionary
 (setq dicts (vla-get-Dictionaries (vla-get-ActiveDocument(vlax-get-acad-object))))
 (setq dictObj (vla-Item dicts "acad_tablestyle"))
 (setq tabname (strcat "TXT" (rtos txtsz 2 2)))
 (vlax-for dname dictobj
 (if (=  (vla-get-name dname) tabname) ; does it exist
 (princ "\nfound")
 (progn

;; Create a custom table style
(setq key tabname class "AcDbTableStyle")
(setq custObj (vla-AddObject dictObj key class))

;; Set the name and description for the style
(vla-put-Name custObj tabname)
(vla-put-Description custObj tabname)

;; Sets the bit flag value for the style
(vla-put-BitFlags custObj 1)

;; Sets the direction of the table, top to bottom or bottom to top
(vla-put-FlowDirection custObj acTableTopToBottom)

;; Sets the horizontal margin for the table cells
(vlax-put  custObj 'HorzCellMargin txtht)

;; Sets the vertical margin for the table cells
(vlax-put custObj 'VertCellMargin txtht)

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

;; Set the text height for the Title, Header and Data rows
(vla-SetTextHeight custObj acDataRow txtht)
(vla-SetTextHeight custObj acHeaderRow (* txtht 1.2))
(vla-SetTextHeight custObj acTitleRow (* txtht 1.5))
  
   ;; Set the text height and style for the Title row
(vla-SetTextStyle custObj (+ acDataRow acHeaderRow acTitleRow) "Standard")
  
)
)
)
(setvar 'ctablestyle tabname)
(princ)
)

(setq txtsz (getreal "Enter text size "))
(setq sp (vlax-3d-point (getpoint "Pick top left")))

(setq doc  (vla-get-activedocument (vlax-get-acad-object) ))
(if (= (vla-get-activespace doc) 0)
  (setq  curspc (vla-get-paperspace doc))
  (setq curspc (vla-get-modelspace doc))
)

(setq numrows 3 numcolumns 3)
(setq rowheight txtsz)
(setq colwidth 10)

(AH:table_make 3 3 txtsz)

(setq objtable (vla-addtable curspc sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 0 0 "TABLE title")
(vla-settext objtable 1 0 "Room Name")
(vla-settext objtable 1 1 "m²")
(vla-settext objtable 1 2 "Sq Ft")

(setq obj2 (vlax-ename->vla-object (entlast)))
(vla-setcolumnwidth obj2 0 (* txtsz 38))
(vla-setcolumnwidth obj2 1 (* txtsz 10))
(vla-setcolumnwidth obj2 2 (* txtsz 10))
(vla-setRowHeight obj2 1 (* txtsz 2.0))

(setq ss (ssget '((0 . "insert"))))

(setq lst '())
(repeat (setq  x (sslength ss))
  (setq lst2 '())
  (setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
  (if  (= (vla-get-hasattributes obj) :vlax-true)
   (progn
   (setq atts (vlax-invoke obj "getattributes"))
   (setq lst2 (cons (vla-get-EffectiveName obj) lst2))
   (foreach att atts
    (setq lst2 (cons (vla-get-textstring att) lst2))
   )
   (setq lst (cons (reverse lst2) lst))
   )
   (progn
    (setq lst (cons (list (vla-get-EffectiveName obj)) lst))
   )
  )
)

(setq lst (vl-sort lst '(lambda ( a b ) (< (cadr a) (cadr b)))))
(setq tot1 0.0 tot2 0.0)
(foreach val lst
(setq tot1 (+ (atof (nth 4 val)) tot1))
(setq tot2 (+ (atof (nth 5 val)) tot2))
)

(setq rownum 2)
(foreach val lst
(vla-InsertRows obj2  rownum  (vla-GetRowHeight obj2 (- rownum 1)) 1)
(vla-settext obj2 rownum  0 (nth 1 val))
(vla-settext obj2 rownum  1 (nth 4 val))
(vla-settext obj2 rownum  2 (nth 5 val))
(setq rownum (1+ rownum))
)
(vla-InsertRows obj2  rownum  (vla-GetRowHeight obj2 (- rownum 1)) 1)
(vla-settext obj2 rownum  1 "Total")
(vla-settext obj2 rownum  1 (strcat (rtos tot1 2 3) " m²"))
(vla-settext obj2 rownum  2 (strcat (rtos tot2 2 3) " ft²"))
(princ)
)
(c:wow)

 

Message 7 of 11

traci_haberghamJQEJT
Enthusiast
Enthusiast

Thank you, and hope you had a nice holiday 😊

 

Yes that's great.  Sorry just one more questions - is it easy to add totals to columns?  If so, where do I look for the code?

 

Thank you for your amazing work and help on this, it is greatly appreciated.

0 Likes
Message 8 of 11

Sea-Haven
Mentor
Mentor

When you say totals what do you mean ? A master total or a count of items that have similar values ? ie room names the same, the sq m the same. Could be done that was what I did with the out of box answer but there were no duplicates of room name.

 

 

0 Likes
Message 9 of 11

traci_haberghamJQEJT
Enthusiast
Enthusiast

The area totals of the number columns.  As the elements are added to the ss, would you add the values to a figure at that point?  Thanks

0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor
Accepted solution

Ok I updated the code above a quick fix I also changed to the area values with no sq.

Message 11 of 11

traci_haberghamJQEJT
Enthusiast
Enthusiast
Accepted solution

Thank you, I will have a look and learn from how you've added the totals.  Much appreciated and a massive thank you 😀

0 Likes