Lisp Table

Lisp Table

Lex_van_Ham
Advocate Advocate
13,585 Views
18 Replies
Message 1 of 19

Lisp Table

Lex_van_Ham
Advocate
Advocate

 Is there a way I can make a table with values in there using lisp?

Thanks

0 Likes
Accepted solutions (2)
13,586 Views
18 Replies
Replies (18)
Message 2 of 19

DannyNL
Advisor
Advisor
Accepted solution

Yes, you can. Many examples can be found in this forum or other forums.

Check the thread below for one of those many examples; my code creates a table with information collected from the drawing in this case.

 

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-request-block-count-inside-a-cl...

0 Likes
Message 3 of 19

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use the ActiveX vla-AddTable function to create the table and the use the Table object methods and properties



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 19

Lex_van_Ham
Advocate
Advocate

Im not sure if its included in your last reply (probably is but i can't find/don't understand it) but how would i use the same lisp script to put text/number/other stuff into the cells like a-2 = 10 or something.

0 Likes
Message 5 of 19

ВeekeeCZ
Consultant
Consultant

Well, tables are not that simple to handle that after couple of days of learning LISP. Rather post your idea what it should look like and we may help you to make that happen.

Message 6 of 19

Lex_van_Ham
Advocate
Advocate

Oke so:

I want a table with 2 columns 28 rows with in the top row the table name

Below that the names of the columns 

All the rows below that Column 1 will always have the same name with exeption for row 5 and 6 They will be like:

(cond 

 (.....)

    (Different product

 (.....) 

and so on.

 

Column 2 will be the amount of items based on calculations from my last script.

Example:

Table.PNG

0 Likes
Message 7 of 19

_gile
Consultant
Consultant

Here's an example from which you should get som inspiration.

It creates a table to list the selected block references.

 

(defun c:listBlock (/ ss name ref refs insPt table row)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (or *blocks*
      (setq *blocks* (vla-get-Blocks *acdoc*))
  )
  (prompt "\nSelect blocks to list or <All>")
  (or (setq ss (ssget '((0 . "INSERT"))))
      (setq ss (ssget "_X" '((0 . "INSERT")))
      )
  )
  (if ss
    (progn
      (vlax-for	x (setq ss (vla-get-ActiveSelectionSet *acdoc*))
	(if (not (vlax-property-available-p x 'Path))
	  (setq	name (vla-get-EffectiveName x)
		refs (if (setq ref (assoc name refs))
		       (subst (cons name (1+ (cdr ref))) ref refs)
		       (cons (cons name 1) refs)
		     )
	  )
	)
      )
      (vla-delete ss)
      (initget 1)
      (setq insPt (trans (getpoint "\nInsertion point: ") 1 0))
      (setq table (vla-addtable
		    (vla-get-modelspace *acdoc*)
		    (vlax-3d-point insPt)
		    (+ 2 (length refs))	; number of rows (including title and header)
		    3			; number of colums
		    20			; cell height
		    80			; row width
		  )
      )
      (vla-put-VertCellMargin table 4.0)
      (vla-put-TitleSuppressed table :vlax-false)
      (vla-put-HeaderSuppressed table :vlax-false)
      (vla-setText table 0 0 "Blocks")
      (vla-setText table 1 0 "Name")
      (vla-setText table 1 1 "Count")
      (vla-setText table 1 2 "Symbol")
      (setq row 2)
      (foreach item refs
	(vla-settext table row 0 (car item))
	(vla-settext table row 1 (cdr item))
	(vla-SetBlockTableRecordId
	  table
	  row
	  2
	  (vla-get-ObjectId (vla-Item *blocks* (car item)))
	  :vlax-true
	)
	(setq row (1+ row))
      )
    )
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 19

ВeekeeCZ
Consultant
Consultant

Lets start from the beginning. Are you able to build a list of values for your table? It should look like this:

 

'(("name1" "amount1")
  ("name2" "amount2")
  ("name3" "amount3")
  ("name4" "amount4")
  ...
  ("name16" "amount16")
  )
0 Likes
Message 9 of 19

DannyNL
Advisor
Advisor

And looking at your example table.......what are you exactly counting?

 

If you are counting blocks in the current drawing or in a set of drawings, this might be easily solved by using the Extract Data tools (Ribbon Insert-> Panel Linking & Extraction --> Extract Data) as this will be able to count and automatically create a table for you.

0 Likes
Message 10 of 19

Lex_van_Ham
Advocate
Advocate
Like this?
If you mean replace the name of "name1" to what ever it will be when done ill do that when I got the entire thing working.
'(("name1" "amount1")
  ("name2" "amount2")
  ("name3" "amount3")
  ("name4" "amount4")
  ("name5" "amount5")
  ("name6" "amount6")
  ("name7" "amount7")
  ("name8" "amount8")
  ("name9" "amount9")
  ("name10" "amount10")
  ("name11" "amount11")
  ("name12" "amount12")
  ("name13" "amount13")
  ("name14" "amount14")
  ("name15" "amount15")
  ("name16" "amount16")
  ("name17" "amount17")
  ("name18" "amount18")
  ("name19" "amount19")
  ("name20" "amount20")
  ("name21" "amount21")
  ("name22" "amount22")
  ("name23" "amount23")
  ("name24" "amount24")
  ("name25" "amount25")
  ("name26" "amount26")

 

0 Likes
Message 11 of 19

Lex_van_Ham
Advocate
Advocate

I made a script that creates a drawing with blocks, counting the blocks can be done with that script but then i have other parts where the amount of them is defined by values from the script .

(example W / 10 * H) 

0 Likes
Message 12 of 19

Lex_van_Ham
Advocate
Advocate

I think I can make it now Smiley Happy

Not sure yet but it feels good Smiley Tongue

0 Likes
Message 13 of 19

ВeekeeCZ
Consultant
Consultant

Well, get your list the way fits you best.

 

Anyway, what I've meant was "build a list" - meaning the process that creates a list. If you're feeling lucky to make it by yourself, consider the following as a spoiler.

 

(defun c:ListBuilding (/ lst)
  (setq i 0)
  (repeat 16
    (setq lst (cons (list (strcat "name" (itoa (setq i (1+ i)))) (strcat "amount" (itoa i))) lst)))
  (reverse lst)
  )

 

0 Likes
Message 14 of 19

Lex_van_Ham
Advocate
Advocate
(defun c:listBlock (/ ss name ref refs insPt table row)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (or *blocks*
      (setq *blocks* (vla-get-Blocks *acdoc*))
  )
  (prompt "\nSelect blocks to list or <All>")
  (or (setq ss (ssget '((0 . "INSERT"))))
      (setq ss (ssget "_X" '((0 . "INSERT")))
      )
  )
  (if ss
    (progn
      (vlax-for	x (setq ss (vla-get-ActiveSelectionSet *acdoc*))
	(if (not (vlax-property-available-p x 'Path))
	  (setq	name (vla-get-EffectiveName x)
		refs (if (setq ref (assoc name refs))
		       (subst (cons name (1+ (cdr ref))) ref refs)
		       (cons (cons name 1) refs)
		     )
	  )
	)
      )
      (vla-delete ss)
      (initget 1)
      (setq insPt (trans (getpoint "\nInsertion point: ") 1 0))
      (setq table (vla-addtable
		    (vla-get-modelspace *acdoc*)
		    (vlax-3d-point insPt)
		    (+ 2 (length refs))	; number of rows (including title and header)
		    3			; number of colums
		    20			; cell height
		    80			; row width
		  )
      )
      (vla-put-VertCellMargin table 4.0)
      (vla-put-TitleSuppressed table :vlax-false)
      (vla-put-HeaderSuppressed table :vlax-false)
      (vla-setText table 0 0 "Blocks")
      (vla-setText table 1 0 "Name")
      (vla-setText table 1 1 "Count")
      (vla-setText table 1 2 "Symbol")
      (setq row 2)
      (foreach item refs
	(vla-settext table row 0 (car item))
	(vla-settext table row 1 (cdr item))
	(vla-SetBlockTableRecordId
	  table
	  row
	  2
	  (vla-get-ObjectId (vla-Item *blocks* (car item)))
	  :vlax-true
	)
	(setq row (1+ row))
      )
    )
  )
  (princ)
)

 

Can I please get a bit more explanation on which part of this code does what so I can better modify it to work the way I want?

Message 15 of 19

_gile
Consultant
Consultant
(defun c:listBlock (/ ss name block blocks insPt table row)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (or *blocks*
      (setq *blocks* (vla-get-Blocks *acdoc*))
  )

  ;; Select blocks
  (prompt "\nSelect blocks to list or <All>")
  (or (setq ss (ssget '((0 . "INSERT"))))
      (setq ss (ssget "_X" '((0 . "INSERT")))
      )
  )
  (if ss
    (progn

      ;; Build a list of dotted pairs (blockName . numberOfReferences) from the selection set
      (vlax-for	x (setq ss (vla-get-ActiveSelectionSet *acdoc*))
	(if (not (vlax-property-available-p x 'Path))
	  (setq	name   (vla-get-EffectiveName x)
		blocks (if (setq block (assoc name blocks))
			 (subst (cons name (1+ (cdr block))) block blocks)
			 (cons (cons name 1) blocks)
		       )
	  )
	)
      )
      (vla-delete ss)

      ;; Prompt for inserrtion point
      (initget 1)
      (setq insPt (trans (getpoint "\nInsertion point: ") 1 0))

      ;; Create the table
      (setq table (vla-addtable
		    (vla-get-modelspace *acdoc*)
		    (vlax-3d-point insPt)
		    (+ 2 (length blocks)) ; number of rows (including title and header)
		    3			; number of colums
		    20			; cell height
		    80			; row width
		  )
      )
      (vla-put-VertCellMargin table 4.0)
      (vla-put-TitleSuppressed table :vlax-false)
      (vla-put-HeaderSuppressed table :vlax-false)

      ;; Fill the titel and the header
      (vla-setText table 0 0 "Blocks")
      (vla-setText table 1 0 "Name")
      (vla-setText table 1 1 "Count")
      (vla-setText table 1 2 "Symbol")

      ;; fill the following rows from the list contents
      (setq row 2)
      (foreach item blocks
	(vla-settext table row 0 (car item)) ; block name
	(vla-settext table row 1 (cdr item)) ; references count
	(vla-SetBlockTableRecordId	; block symbol
	  table
	  row
	  2
	  (vla-get-ObjectId (vla-Item *blocks* (car item)))
	  :vlax-true
	)
	(setq row (1+ row))
      )
    )
  )
  (princ)
)

But, if you're not able to understand such code, maybe you should learn the AutoLISP basics before trying to achieve such task.

We all have to learn how to walk before trying to run...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 16 of 19

braudpat
Mentor
Mentor

 

Hello @_gile

 

Beautiful Routine !

 

Please is it possible to have a Micro-Micro-Improvment ?

 

On the command/text Windows : the total of the Blocks ...

 

Thanks in advance, Regards, Patrice

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 17 of 19

Anonymous
Not applicable

@_gile

Thank for excellence code.

If I don't need 1 st row(Header row), only required the title of row and below data how can do that.

(see attached screenshot)

0 Likes
Message 18 of 19

_gile
Consultant
Consultant

@Anonymous a écrit :

@_gile

Thank for excellence code.

If I don't need 1 st row(Header row), only required the title of row and below data how can do that.

(see attached screenshot)


You should have been able to do it by yourself (look at the commented red lines).

 

(defun c:listBlock (/ ss name block blocks insPt table row)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (or *blocks*
      (setq *blocks* (vla-get-Blocks *acdoc*))
  )

  ;; Select blocks
  (prompt "\nSelect blocks to list or <All>")
  (or (setq ss (ssget '((0 . "INSERT"))))
      (setq ss (ssget "_X" '((0 . "INSERT")))
      )
  )
  (if ss
    (progn

      ;; Build a list of dotted pairs (blockName . numberOfReferences) from the selection set
      (vlax-for	x (setq ss (vla-get-ActiveSelectionSet *acdoc*))
	(if (not (vlax-property-available-p x 'Path))
	  (setq	name   (vla-get-EffectiveName x)
		blocks (if (setq block (assoc name blocks))
			 (subst (cons name (1+ (cdr block))) block blocks)
			 (cons (cons name 1) blocks)
		       )
	  )
	)
      )
      (vla-delete ss)

      ;; Prompt for inserrtion point
      (initget 1)
      (setq insPt (trans (getpoint "\nInsertion point: ") 1 0))

      ;; Create the table
      (setq table (vla-addtable
		    (vla-get-modelspace *acdoc*)
		    (vlax-3d-point insPt)
		    ;(+ 2 (length blocks)) ; number of rows (including title and header)
		    (+ 1 (length blocks)) ; number of rows (including header)
		    3			; number of colums
		    20			; cell height
		    80			; row width
		  )
      )
      (vla-put-VertCellMargin table 4.0)
      ;(vla-put-TitleSuppressed table :vlax-false)
      (vla-put-TitleSuppressed table :vlax-true)
      (vla-put-HeaderSuppressed table :vlax-false)

      ;; Fill the title and the header
      ;(vla-setText table 0 0 "Blocks")
      ;(vla-setText table 1 0 "Name")
      ;(vla-setText table 1 1 "Count")
      ;(vla-setText table 1 2 "Symbol")
      ;; Fill the header
      (vla-setText table 0 0 "Name")
      (vla-setText table 0 1 "Count")
      (vla-setText table 0 2 "Symbol")

      ;; fill the following rows from the list contents
      ;(setq row 2)
      (setq row 1)
      (foreach item blocks
	(vla-settext table row 0 (car item)) ; block name
	(vla-settext table row 1 (cdr item)) ; references count
	(vla-SetBlockTableRecordId	; block symbol
	  table
	  row
	  2
	  (vla-get-ObjectId (vla-Item *blocks* (car item)))
	  :vlax-true
	)
	(setq row (1+ row))
      )
    )
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 19 of 19

jeguizaASRZM
Enthusiast
Enthusiast

Hello, Great Lisp!

I had a quick question. How is the size of the text being driven in this lisp routine? Can I Change the text Size to maybe 100? I know I was Able to change the table row sizes, but that's as far I was able to change. I tried to change my TextSize but that also did not work. 

 

Any Comments or help would be appreciated! 😃

0 Likes