Lisp to create table(s)

Lisp to create table(s)

1Moonlight
Participant Participant
3,095 Views
13 Replies
Message 1 of 14

Lisp to create table(s)

1Moonlight
Participant
Participant

Hello - I have a drawing with a table that extracts attributes from blocks. It uses a .dxe file that I need to recreate each time; but the good thing about that is that I can update the block then it will update the table. I've seen some people create a table with a lisp. Is there a way to use a lips so I don't need to recreate my table every time but still have the data linked so it can be updated? I don't know much about crating a lisp file. I would be very happy to have this work so I can insert it into multiple drawings. If a lisp does not work for this, is there a way to turn a .dxe file into a template?

The second item would be if the data could flow both directions. I've not found anyone that has been able to help with that. It would be great to update the table after it's been created, and have it update the block.

Thank you in advance for your help.

0 Likes
3,096 Views
13 Replies
Replies (13)
Message 2 of 14

john.uhden
Mentor
Mentor

@1Moonlight ,

I am pretty sure there is a way.

I only recently got into creating and populating and formatting tables, with great help from @Sea-Haven.  It wasn't easy for me at all.  I've been doing/learning this AutoLisp thing since about 1988, so the experience helped me get through it.  I applaud you for having the desire to conquer a new territory.  The only thing I can tell you is that experience with Excel is a big plus if you want to do computations within the table.  They are also a great way to display your data.

 

John F. Uhden

0 Likes
Message 3 of 14

Sea-Haven
Mentor
Mentor

1st comment "if the data could flow both directions. I've not found anyone that has been able to help with that." I did comment elsewhere that if you save the handle id of the block into Excel then you can update the block attribute and in turn update the table. 

 

Try this

 

 

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-create-table-s/td-p/12529849

; dump attributes into a table

(defun ahmktable ( / colwidth numcolumns numrows rowheight sp vgms x)
(vl-load-com)

(setq dicts (vla-get-Dictionaries (vla-get-ActiveDocument(vlax-get-acad-object))))
(setq dictObj (vla-Item dicts "acad_tablestyle"))
(vlax-for dname dictobj
  (if (=  (vla-get-name dname) "PartSchedule" )
   (progn
    (setvar 'ctablestyle "PartSchedule")
    (princ "found")
   )
   (progn
    (alert "The PartSchedule table style does not exist \nWill now exit")
    (exit)
   )
  )
)


(setq txtht 0.125)
(setq doc (vla-get-ActiveDocument(vlax-get-acad-object)))
(setq sp (vlax-3d-point (getpoint "select point for table")))
(setq vgms (vlax-get-property doc(if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
(setq numrows 3)
(setq numcolumns 7)
(setq rowheight 0.3125)
(setq colwidth 1.75)

(setq objtable (vla-addtable vgms sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 0 0 "Part Schedule")
(vla-settext objtable 1 0 "Part Number") 
(vla-settext objtable 1 1 "Qty") 
(vla-settext objtable 1 2 "Part Description")
(vla-settext objtable 1 3 "Detail")
(vla-settext objtable 1 4 "Mix Design")
(vla-settext objtable 1 5 "Texture")
(vla-settext objtable 1 6 "Mat'l")
(vla-SetTextHeight Objtable (+ acDataRow acTitleRow) txtht)
(vla-SetAlignment Objtable (+ acDataRow acHeaderRow acTitleRow) acMiddleCenter)
(vla-SetTextHeight Objtable acHeaderRow (* 1.5 txt))
(vla-Setcolumnwidth Objtable  0 1.75)
(vla-Setcolumnwidth Objtable  1 0.85)
(vla-Setcolumnwidth Objtable  2 4.125)
(vla-Setcolumnwidth Objtable  3 1.0)
(vla-Setcolumnwidth Objtable  4 1.6)
(vla-Setcolumnwidth Objtable  5 1.6)
(vla-Setcolumnwidth Objtable  6 0.85)
(setq numrows 2)

(setq ss (ssget '((0 . "INSERT")(2 . "PARTLIST-TXT"))))
(if (= ss nil)
  (progn (alert "No blocks found called PARTLIST-TXT \nWill exit now")(exit))
  (progn
  (repeat (setq x (sslength ss))
   (setq lst '())
   (setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
   (setq atts (vlax-invoke obj 'Getattributes))
   (foreach att atts
    (setq lst (cons (vlax-get att 'Textstring) lst))
   )
   (vla-insertrows objtable  numrows  (vla-GetRowHeight objtable numrows) 1)
   (vla-settext objtable numrows  0 (nth 0 lst))
   (vla-settext objtable numrows  1 (nth 1 lst))
   (vla-settext objtable numrows  2 (nth 2 lst))
   (vla-settext objtable numrows  3 (nth 3 lst))
   (vla-settext objtable numrows  4 (nth 5 lst))
   (vla-settext objtable numrows  5 (nth 6 lst))
   (vla-settext objtable numrows  6 (nth 7 lst))
   (setq numrows (1+ numrows))
   )
  )
)

(princ)
)

(ahmktable)

 

 

 

0 Likes
Message 4 of 14

1Moonlight
Participant
Participant

Thank you for your thoughts. I'm a longtime AutoCAD user but stayed away from tables until recently. I thought they were a bit "clunky", and difficult to use. However, I think their getting closer to something worthwhile. I feel the existing tools are close to making it feasible for the average user to get the results they want.  

0 Likes
Message 5 of 14

1Moonlight
Participant
Participant

Thank you very much for your help. I'm not quite sure how to run this lisp though.

Re. your comment about using an excel file; I'm not a fan of having an additional link to the drawings. I have several draftsman and concerned something like that would become disassociated quickly. 

0 Likes
Message 6 of 14

pendean
Community Legend
Community Legend

@1Moonlight wrote:

Thank you very much for your help. I'm not quite sure how to run this lisp though....


First, you copy/paste all of that code into Windows Notepad, then save the file with that LISP name with an LSP extension. Then https://www.lee-mac.com/runlisp.html 

 

Intro to LISP creation https://help.autodesk.com/view/ACDLT/2024/ENU/?guid=GUID-3B8EDFF1-A130-434F-B615-7F2EC04322EE#:~:tex...

 


@1Moonlight wrote:

...I'm not a fan of having an additional link to the drawings. I have several draftsman and concerned something like that would become disassociated quickly. 


But you asked for "...The second item would be if the data could flow both directions...": explain please.

 

 

0 Likes
Message 7 of 14

1Moonlight
Participant
Participant

Thank you. I do know how to crate the lisp file with Notepad. However, after I've loaded it into AutoCAD the name does not come up when I type it in the command bar to run the program.

0 Likes
Message 8 of 14

john.uhden
Mentor
Mentor

@1Moonlight ,

Does your code begin with (defun c:<yourname> ?

Without the C: prefix it is not an AutoLisp command function.

John F. Uhden

0 Likes
Message 9 of 14

1Moonlight
Participant
Participant

The code above says defun ahmktable. I tried that, but it didn't work. 

defun ahmktable
0 Likes
Message 10 of 14

john.uhden
Mentor
Mentor

@1Moonlight ,

That will create a function that you can use at the command prompt, but it must be enclosed in parentheses, e,g,

Command: (ahmktable)

To make it a command function,

change

(defun ahmktable ...

to

(defun c:ahmktable ...

John F. Uhden

0 Likes
Message 11 of 14

paullimapa
Mentor
Mentor

The way to execute this code at the AutoCAD command line is by including open & closing parenthesis:

 

 

(ahmktable numr)

 

 

I just don't know what that numr argument is for since that's not used anywhere in the entire code.

So enter anything like "1" would work:

 

 

(ahmktable "1")

 

 

Or modify the beginning of the code to eliminate the need to include the numr argument and execute the command without the parenthesis:

 

(defun c:ahmktable (/ colwidth lst numcolumns numrows rowheight sp vgms x)

 

Then entering the following at the command line will run it:

 

 

ahmktable

 

 

Now reading through the code it requires the following additional changes:

1. Though the drawing may already have an existing Table Style called: "PartSchedule", this part of the code will cause it to exit:

paullimapa_1-1706746689940.png

 

 

(vlax-for dname dictobj
  (if (=  (vla-get-name dname) "PartSchedule" )
   (progn
    (setvar 'ctablestyle "PartSchedule")
    (princ "found")
   )
   (progn
    (alert "The PartSchedule table style does not exist \nWill now exit")
    (exit)
   )
  )
)

 

 

So I would change it to this:

 

 

(vlax-for dname dictobj
  (if (=  (vla-get-name dname) "PartSchedule" )
   (progn
    (setvar 'ctablestyle "PartSchedule")
    (princ "found")
   )
  )
)
(if (/= (getvar 'ctablestyle) "PartSchedule")
  (progn
    (alert "The PartSchedule table style does not exist \nWill now exit")
    (exit)
  )
)

 

 

2. The Block called: PARTLIST-TXT actually has 8 Attributes but the current code does not include the Attribute Tag: NOTE? 

paullimapa_3-1706744181150.png

So if you want to include this in the Table then a number of lines would need to be changed to accommodate an additional column in the Table:

 

 

;(setq numcolumns 7)
; replaced with
(setq numcolumns 8)
;
; commented out
;(vla-settext objtable 1 3 "Detail")
;(vla-settext objtable 1 4 "Mix Design")
;(vla-settext objtable 1 5 "Texture")
;(vla-settext objtable 1 6 "Mat'l")
; replaced with
(vla-settext objtable 1 3 "Note")
(vla-settext objtable 1 4 "Detail")
(vla-settext objtable 1 5 "Mix Design")
(vla-settext objtable 1 6 "Texture")
(vla-settext objtable 1 7 "Mat'l")
;
; commented out
;(vla-Setcolumnwidth Objtable  3 1.0)
;(vla-Setcolumnwidth Objtable  4 1.6)
;(vla-Setcolumnwidth Objtable  5 1.6)
;(vla-Setcolumnwidth Objtable  6 0.85)
; replaced with
(vla-Setcolumnwidth Objtable  3 2.6)
(vla-Setcolumnwidth Objtable  4 1.0)
(vla-Setcolumnwidth Objtable  5 1.6)
(vla-Setcolumnwidth Objtable  6 1.6)
(vla-Setcolumnwidth Objtable  7 0.85)
;
; commented out
;   (vla-settext objtable numrows  4 (nth 5 lst))
;   (vla-settext objtable numrows  5 (nth 6 lst))
;   (vla-settext objtable numrows  6 (nth 7 lst))
; replaced with
   (vla-settext objtable numrows  4 (nth 4 lst))
   (vla-settext objtable numrows  5 (nth 5 lst))
   (vla-settext objtable numrows  6 (nth 6 lst))
   (vla-settext objtable numrows  7 (nth 7 lst))
;  

 

 

3. The following lines crash the code so change them to:

 

 

; commented out
;(vla-SetAlignment Objtable (+ acDataRow acHeaderRow acTitleRow) acMiddleCenter)
; replaced with
(vla-SetAlignment Objtable acHeaderRow acMiddleCenter)
;
; commented out
;  (vla-SetTextHeight Objtable acHeaderRow (* 1.5 txt))
; replaced with
  (vla-SetTextHeight Objtable acHeaderRow (* 1.5 txtht)) 
;

 

 

4. You'll also need at least one inserted PARTLIST-TXT Block for selection to include in the Table like on of these:

paullimapa_2-1706743969724.png

5. Lastly after the Block selection & Attribute value collection in the code they need to be properly positioned in the Table by adding this line of code:

 

 

; added
  (setq lst (reverse lst))
;

 

So when the code now runs the result looks like this:

paullimapa_0-1706746407854.png

I've included the revised code + sample dwg.

 

 

FYI: Reference following site for Table creation:

https://hyperpics.blogs.com/beyond_the_ui/2012/07/creating-a-table-style-with-autolisp-and-the-activ...

 

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 12 of 14

Sea-Haven
Mentor
Mentor

Thanks guys, yes my fault remove the numr from the defun, the notes were not in the sample dwg so was left out on purpose, the dwg had a table style set up so did use that, I tend now to make a table style if it does not exist but left that out for now.

 

Same with the request about double access to Excel there has to be some form of "Key" for that to work and the Handle is the obvious answer here and must exist in the Excel as commented elsewhere today.

SeaHaven_0-1706747958824.png

 

0 Likes
Message 13 of 14

1Moonlight
Participant
Participant

Paul - Thank you for putting the time into this. I do not need the "note" section. I downloaded your example but after running it, it asks to select the blocks. I wasn't sure how to make that work. On my template, the one posted a the top, the blocks are on all the paper space sheets. How do we make that work? 

0 Likes
Message 14 of 14

paullimapa
Mentor
Mentor

The attached revised version takes care of again skipping the NOTE? tag and also automatically selecting the Block with Attribute inserts. But as I mentioned in my previous reply:

4. You'll also need at least one inserted PARTLIST-TXT Block for selection to include in the Table like on of these:

paullimapa_0-1706818449840.png

This is the requirement/limitation for this lisp routine to work.

Also if more Blocks are added or Erased, the lisp routine will have to run again to generate a new Table and you'll have to Erase the old Table. Whereas the DATAEXTRACTION command offers you the option to select a drawing with the Block inserted for Table generation and the TABLE object will automatically update

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes