How can I use the lisp FieldObjectsV1-0 with a Field in Table cell

How can I use the lisp FieldObjectsV1-0 with a Field in Table cell

le-tan-phuc
Enthusiast Enthusiast
3,155 Views
10 Replies
Message 1 of 11

How can I use the lisp FieldObjectsV1-0 with a Field in Table cell

le-tan-phuc
Enthusiast
Enthusiast

Hello,
I tried hard to understand Lee Mac's program to be able to use it with a field in a table cell but it is too difficult for me.
Please see the attached image to understand the error when using the program with a field located in a table cell.
If possible, someone please help me, thank you so much !
Phuc Le.
(Google translator)

0 Likes
3,156 Views
10 Replies
Replies (10)
Message 2 of 11

Sea-Haven
Mentor
Mentor

This is just 1st step getting some thing to work next step is to add field values, but I have a problem the sum function is not working, not sure why not, done manually works. 

 

 

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-can-i-use-the-lisp-fieldobjectsv1-0-with-a-field-in-table/td-p/13004617

; PL area custom table 
; BY AlanH Sep 2024

(defun c:plarea ( / )
(defun cmaxstrlen ( l )
    (if (apply 'or l)
        (cons (apply 'max (mapcar 'strlen (subst "" nil (mapcar 'car l))))
              (cmaxstrlen (mapcar 'cdr l))
        )
    )
)


(defun CreateTableStyle ( / 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"))

(setq dname (strcat "table" (rtos txtht 2 0)))

(vlax-for dictname dictobj
(if (=  (vlax-get dictname 'name) dname)
(princ "yes")

(progn

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

;; Set the name and description for the style
(vla-put-Name custObj dname)
(vla-put-Description custObj (strcat dname " 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 0.5))

;; Sets the vertical margin for the table cells
(vla-put-VertCellMargin custObj (* txtht 0.5))

;; 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 dname)
(princ)
)

(defun AH:maketable ( / )
(command "table" 2 3 (getpoint "\nPick point for table "))
(setq objtable (vlax-ename->vla-object (entlast)))
(vla-Setcolumnwidth objtable 0 4800 )
(vla-Setcolumnwidth objtable 1 2250 )
(vla-settext objtable 0 0 "TABLE title")
(vla-settext objtable 1 0 "layer")
(vla-settext objtable 1 1 "Area m²")
)


(setq ss (ssget '((0 . "LWPOLYLINE"))))
(setq lst '() lenlay 0)
(repeat (setq x (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(setq objid (rtos (vla-get-objectid obj)))
(setq lay (vlax-get obj 'Layer))
(setq laylen (strlen lay))
(if (< lenlay laylen)(setq lenlay laylen))
(setq lst (cons (list lay objid) lst))
)
(setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y)))))

(setq collst (cmaxstrlen lst))
(setq txtht (getint "\nEnter text height "))
(CreateTableStyle)
(ah:maketable)

(setq rownum 2)
(setq rowht (vla-getrowheight objtable 2))

(foreach val lst
  (vla-InsertRows objtable rownum  rowht 1)
  (setq lay (strcat "%<\\AcObjProp Object(%<\\_ObjId " (cadr val) ">%).Layer>%"))
  (setq area (strcat "%<\\AcObjProp Object(%<\\_ObjId " (cadr val) ">%).Area \\f \"%lu2 %ct8[1e-006]\">%"))
  (vla-settext objtable rownum  0 lay)
  (vla-settext objtable rownum  1 area)
  (setq rownum (1+ rownum))
)

(setvar 'luprec 2)
(setq ans (strcat "Total=" "%<\\AcExpr (Sum(B3:B" (RTOS rownum 2 0) ")) \\f \"%lu2 %ct8[1e-006]\">%"))
(vla-settext objtable rownum 1 ans)
(command "regen")
(princ)
)
(c:plarea)

 

 

 

Hoping some one can help sort out sum.  

0 Likes
Message 3 of 11

le-tan-phuc
Enthusiast
Enthusiast

thank you very much for your reply !
but the problem I am facing is shown in the attached GIF
what I want is the green rectangle to be placed in the correct position of the Field in the cell table

0 Likes
Message 4 of 11

le-tan-phuc
Enthusiast
Enthusiast

2.gif

0 Likes
Message 5 of 11

Sea-Haven
Mentor
Mentor

Understand this is not finished code. Will add fields next version.

 

Found my typo need \\ so get a single \ in formula. Changed code above.

 

 

0 Likes
Message 6 of 11

le-tan-phuc
Enthusiast
Enthusiast

Thank you for your enthusiasm Sea Haven !
But the purpose of this post is to find the location of objects referenced by Fields (Find object associated to a Field)
the Lisp FieldObjectV1.0 has done very well with Fields in text, mtext, attribute but when executing with Fields in a table cell, the result is not very accurate, so please help me or guide me how to modify to be able to achieve the correct result when using lisp FieldobjectV1.0 with Fields in a table cell
Phuc Le
(Google Translator)

0 Likes
Message 7 of 11

paullimapa
Mentor
Mentor

It works when the insertion point of the TABLE (upper left hand corner) is placed at 0,0,0.

paullimapa_0-1725847732061.png

 

Since the code is copyrighted by the author, I suggest you contact Lee Mac for any revisions:

Author: Lee Mac, Copyright © 2011 - www.lee-mac.com


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

Sea-Haven
Mentor
Mentor

Please try the updated code in post 2. 

SeaHaven_0-1725848697369.png

 

0 Likes
Message 9 of 11

le-tan-phuc
Enthusiast
Enthusiast

Thank you so much Paul Li !
I will contact Lee Mac.
I always thought it would be difficult to contact Lee Mac but hopefully I will be lucky enough to get help from him.
Phuc Le

0 Likes
Message 10 of 11

le-tan-phuc
Enthusiast
Enthusiast

Thank you for your time and effort !
But I got error with field in table, and i have changed this line as show below, then the lisp worked correctly !
Thank you again !
Phuc Le
(sorry for my poor English)

(setq ss (ssget '((0 . "LWPOLYLINE"))))
(setq lst '() lenlay 0)
(repeat (setq x (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(setq objid (itoa (vla-get-objectid obj))) ;; I have changed this line so it can work correctly on my AutoCAD 2024

 

0 Likes
Message 11 of 11

Sea-Haven
Mentor
Mentor

Thanks, had it as rtos originally fixed code above.

0 Likes