lisp to Extract Data, attributes

lisp to Extract Data, attributes

jorgearone_inssitu
Enthusiast Enthusiast
677 Views
9 Replies
Message 1 of 10

lisp to Extract Data, attributes

jorgearone_inssitu
Enthusiast
Enthusiast

Hi everyone

I have many blocks with only one attribute
"CODIGO", almost all the blocks are also dynamic

For example this clamp

jorgearone_inssitu_0-1740328103792.png

which has these two views

 

When I finish an entire assembly drawing of parts
I always want a report of the quantity I need
Almost always in the same drawing I use the two views of the same block

 

 

So far I have solved it with the "DATA EXTRACTION"
but I always have to repeat the same process

jorgearone_inssitu_1-1740328335777.png

1. -SELECT ANY ONE file

 

 

 

2.- Select objects in the current drawing

jorgearone_inssitu_3-1740328475741.png

 

jorgearone_inssitu_2-1740328416265.png

 

3.- select only

Display blocks only

Display blocks with attributes only

jorgearone_inssitu_5-1740328619015.png

4.- select only, category filter: Attribute

jorgearone_inssitu_6-1740328685223.png

5.- order the table

jorgearone_inssitu_7-1740328731327.png

 

To

jorgearone_inssitu_8-1740328794697.png

CODIGO - Name - Count

CODIGO: IN ALPHABETICAL ORDER

 

6.- INSERT DATA EXTRACTION TABLE INTO DRAWING

jorgearone_inssitu_9-1740328922737.png

 

 

jorgearone_inssitu_10-1740328965388.png

 

MY QUESTION IS IF THIS ENTIRE PROCESS CAN BE DONE WITH A LISP

SINCE I USUALLY DO THIS 13 TIMES A DAY
AND THERE ARE MANY MORE BLOCKS THAN THE ONES I PRESENT

 

 

0 Likes
Accepted solutions (3)
678 Views
9 Replies
Replies (9)
Message 2 of 10

cadffm
Consultant
Consultant

Hi,

 

if you need to do it with the SAME selection set(same drawing, same filepath), yes.

Run -dataextration

 

if not, NO

 

Search for a 100% Lisp solution, rather than controle this native command.

 

Sebastian

0 Likes
Message 3 of 10

ec-cad
Collaborator
Collaborator

Here's what I have so far. No more time to work on it.

You drag/drop the lisp into an open drawing, builds a table.

I cannot seem to make the Table smaller ? , but it works.

 

ECCAD

0 Likes
Message 4 of 10

Sea-Haven
Mentor
Mentor

 @ec-cad this may be useful, makes a table style and sets current. When you make a table it uses current table style so sometimes is no where near what you want.

 

(defun CreateTableStyle (newdname txtht / dicts dictobj key class custobj )
    
 ;; 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"))

(vlax-for dname dictobj
(if (=  (vla-get-name dname) newdname) ; does it exist
(setvar 'ctablestyle newdname)
(progn

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

;; Set the name and description for the style
(vla-put-Name custObj newdname)
(vla-put-Description custObj (strcat newdname "custom table style"))

;; 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
(vla-put-HorzCellMargin custObj txtht )

;; Sets the vertical margin for the table cells
(vla-put-VertCellMargin custObj 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 newdname)
)
)
 )
(princ)
) ; CreateTableStyle

(CreateTableStyle  "EC-CAD" 0.5)

 

  

0 Likes
Message 5 of 10

ec-cad
Collaborator
Collaborator

Sea-Haven,

Thanks for that. I saved the code as 'Create_table_style.lsp', loaded it and I get this message.

Fails on line with: (setq custObj (vla-AddObject dictObj key class))

Command: (LOAD "C:/Disk8/create_table_style.lsp")
Error: Automation Error. AcRxClassName entry is not in the system registry
Error: Break...

 

I know that's an issue with my local PC, running ACAD license from work PC. I cannot get to the

Windows registry on that machine, so cannot test further. I see in the code, you can adjust text sizes

and 'style', but nothing to control Column Width (see OP sample.dwg)

OP (could just scale down that default table).

 

ECCAD

0 Likes
Message 6 of 10

paullimapa
Mentor
Mentor

@Sea-Haven I get that error too:

paullimapa_1-1740420430678.png

Though I do see the Table Style created but just not sure if it's complete:

paullimapa_0-1740420352082.png

I've used this function and modified it for me to create a Table Style:

;; aec_mkTableStyle function makes a Table Style if given Table Style Name does not exist
;; https://hyperpics.blogs.com/beyond_the_ui/2012/07/creating-a-table-style-with-autolisp-and-the-activex-api.html
; Arguments:
; tbl_name = custom table style name to create
; tbl_des = custom table style description
; cell_margin = cell margins horizontal & verticle
; ttl_height = title row height
; hdr_height = header row height
; dta_height = data row height 
; sty_name = text style name must exist
; (aec_mkTableStyle "Architectural Schedule" "This is Custom Table for Architecture" 0.22 1.5 1.25 1.0 "Standard")
(defun aec_mkTableStyle 
 (tbl_name tbl_des cell_margin ttl_height hdr_height dta_height sty_name 
  / GetObject acad class custObj doc dicts dictObj doc dicts dictObj
 )
;; GetObject
;; Returns an object (Custom Object) from the dictionary according to its name (or nil) if does not exist
;; Arguments
;; dict: the dictionary (vla-object)
;; name: the name of the object (string)
;; https://gilecad.azurewebsites.net/LISP/gc_AutomationHelpers.lsp
 (defun GetObject (dict name / obj)
  (if(not(vl-catch-all-error-p(vl-catch-all-apply (function (lambda () (setq obj (vla-GetObject dict name)))))))obj)
 ) ; defun GetObject
    ;; Get the AutoCAD application and current document
    (setq acad (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acad))

    ;; Get the Dictionaries collection and the TableStyle dictionary
    (setq dicts (vla-get-Dictionaries doc))
    (setq dictObj (vla-Item dicts "acad_tablestyle"))
    (if(not(GetObject dictObj tbl_name)) ; check if table style name exists
     (progn   
    ;; Create a custom table style
      (setq class "AcDbTableStyle")
      (setq custObj (vla-AddObject dictObj tbl_name class))

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

    ;; 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 supression of the table header
      (vla-put-HeaderSuppressed custObj :vlax-false)

    ;; Sets the horizontal margin for the table cells
      (vla-put-HorzCellMargin custObj cell_margin)

    ;; Sets the supression of the table title
      (vla-put-TitleSuppressed custObj :vlax-false)

    ;; Sets the vertical margin for the table cells
      (vla-put-VertCellMargin custObj cell_margin)

    ;; Set the alignment for the Data, Header, and Title rows
;      (vla-SetAlignment custObj (+ acDataRow acTitleRow) acMiddleLeft)
;      (vla-SetAlignment custObj (+ acDataRow acTitleRow) acMiddleCenter) ; data and title row alignment
      (vla-SetAlignment custObj acDataRow acMiddleCenter) ; data and title row alignment
      (vla-SetAlignment custObj acHeaderRow acMiddleCenter) ; header row alignment
      (vla-SetAlignment custObj acTitleRow acMiddleCenter)  ; title row alignment
    ;; possible future addition Set the background color for the Header and Title rows
;    (if(= (getvar 'product) "AutoCAD")  
;     (progn
;      (setq colObj (vlax-create-object (strcat "AutoCAD.AcCmColor." (substr (getvar "ACADVER") 1 2))))
;      (vla-SetRGB colObj 98 136 213)
;      (vla-SetBackgroundColor custObj (+ acHeaderRow acTitleRow) colObj)
    ;; Set the bottom grid color for the Title row
;      (vla-SetRGB colObj 0 0 255)
;      (vla-SetGridColor custObj acHorzBottom acTitleRow colObj)
    ;; Clear the background color for Data rows
;      (vla-SetBackgroundColorNone custObj acDataRow :vlax-true)
;     ) ; progn
;    ) ; if

    ;; Set the bottom grid lineweight for the Title row
      (vla-SetGridLineWeight custObj acHorzBottom acTitleRow acLnWt025)
    ;; Set the inside grid lines visible for the data and header rows
      (vla-SetGridVisibility custObj acHorzInside  (+ acDataRow acHeaderRow) :vlax-true)
    ;; Set the text height for the Title, Header and Data rows
      (vla-SetTextHeight custObj acTitleRow ttl_height)
;      (vla-SetTextHeight custObj acHeaderRow hdr_height)
;      (vla-SetTextHeight custObj acDataRow dta_height)
      (vla-SetTextHeight custObj (+ acDataRow acHeaderRow) dta_height) ; set both to same height
    ;; Set the text height and style for the Title, Header & Data rows
      (vla-SetTextStyle custObj (+ acDataRow acHeaderRow acTitleRow) sty_name)

    ;; Release the color object
      (if colObj(vlax-release-object colObj))
      (princ(strcat"\nTable Style Name [" tbl_name "] Successfully Created."))(princ)
   ) ; progn
   (progn
      (princ(strcat"\nTable Style Name [" tbl_name "] Already Exists."))(princ)
   )
	) ; if
    (princ)
) ; defun aec_mkTableStyle

Another option after the Table is completed is to Scale the Table object up to match with the reciprocal cannoscalevalue:

(/ 1 (getvar"cannoscalevalue"))

 


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

paullimapa
Mentor
Mentor
Accepted solution

Found the error. Your code assumes a Text Style named "Arial" is already created in the drawing:

;; Set the text height and style for the Title row
 (vla-SetTextStyle custObj (+ acDataRow acHeaderRow acTitleRow) "Arial")

If you don't want to check and create a Text Style named "Arial" then you can just go with the default Text Style of Standard then it won't end with the error message:

;; Set the text height and style for the Title row
 (vla-SetTextStyle custObj (+ acDataRow acHeaderRow acTitleRow) "Standard")

 


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

Sea-Haven
Mentor
Mentor

@paullimapa thanks will change to Standard. 

 

I had problems setting number of column and rows as part of style but somewhere it is there. Will try to find.

 

Thought about the (createstyle "250table" 10.0 3  '(30 100 30)) so 10 is text height, 3 rows etc the number of columns is length of list and their respective width. So redoing my "make a table.lsp". 

0 Likes
Message 9 of 10

ec-cad
Collaborator
Collaborator
Accepted solution

Jorgearone_inssitu,

I have solved the Column Width issue. New program asks for picking selection set via Window.

2 points, with rectangle action. It gathers the info, into lists, sorts and quantifies each unique

value, then asks for a Point to place the Table and builds it there.

Hope you like this version.

 

ECCAD

 

Sea-Haven, key for making the ColumnWidth:

              ;; Define the row height and column width
              (setq rowheight 1.0)
              (setq colwidth 120)

              ;; Add the table to the drawing
              (setq objtable (vla-addtable curspace pt1 numrows numcolumns rowheight colwidth))

              ;; Set the header row
              (vla-settext objtable 1 0 "Part Number")
              (vla-settext objtable 1 1 "Part Name")
              (vla-settext objtable 1 2 "Quantity")
              (vla-setColumnWidth objtable 0 40); Added for Column 1
              (vla-setColumnWidth objtable 1 120); for Column 2
              (vla-setColumnWidth objtable 2 40);  for Column 3

              ;; Populate the table with data
              (setq row 2)
              (foreach item flist
                  (setq Att (car item))
                  (setq Nam (cadr item))
                  (setq Qua (caddr item))
                  (vla-settext objtable row 0 Att)
                  (vla-settext objtable row 1 Nam)
                  (vla-settext objtable row 2 Qua)
                  (setq row (1+ row))
              )

 You could send those values in via a List, then car,cadr,caddr that.

Bob

 

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor
Accepted solution

@ec-cad I understand when using vla-addtable it asks for columnwidth I was looking for a way to set the default value in the actual table style definition. But thinking more about it more , setting rows and column width allows for more rows & columns but still using the style say for height in particular. 

 

 (createstyle "10texttable" 10.0 3  '(30 100 30)) will look into this more. 

0 Likes