Author Topic: Modifying a Field to become a formula for a label  (Read 2676 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Modifying a Field to become a formula for a label
« on: September 09, 2021, 04:54:25 PM »
Gotta question, I stumbled upon Mr Rons Area Field routine. how would I modify the field value to become a formula?

I want to create a formula which would take the value,

i.e. (((([Area Field]+[Calculated Area Field])/2)*4)/43560)

Calculated Area Field
Taking the selected polyline, offsetting it 12' inside and using that value. (Not sure how to make that happen. lol.)

Thanks for any direction!


Code: [Select]
(strcat
      "%<\\AcObjProp Object(%<\\_ObjId "
      (if (> (vl-string-search "x64" (getvar 'platform)) 0)
(vlax-invoke-method
  (cond (oUtil)
((setq oUtil (vla-get-utility acDoc)))
  )
  'getobjectidstring
  x
  :vlax-False
)
(rtos (vla-get-objectid x) 2 0)
      )
      ">%).Area \\f \"%pr2%lu2%ct4%qf1 SQ. FT.\">%" ;; I think this is the area to modify
    )



Code: [Select]
(vl-load-com)

(defun c:FOO (/ *error* acDoc oSpace origin insertionPoint oUtil oMText)

  (defun *error* (msg)
    (if acDoc
      (vla-endundomark acDoc)
    )
    (cond ((not msg)) ; Normal exit
  ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit)
  ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it
    )
    (princ)
  )

  (if (ssget "_:L" '((0 . "CIRCLE,*POLYLINE")))
    (progn
      (vla-startundomark
(setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
      )
      (setq oSpace
     (vlax-get
acDoc
(if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)
       )
      )
      (setq origin (vlax-3d-point '(0 0 0)))
      (vlax-for x (vla-get-activeselectionset acDoc)

;; Calculate the centroid
(vla-getboundingbox x 'mn 'mx)
(setq insertionPoint
       (vlax-3d-point
(mapcar '*
(mapcar '+
(vlax-safearray->list mn)
(vlax-safearray->list mx)
)
'(0.5 0.5 0.5)
)
       )
)


;; Add mtext
(setq oMText
  (vla-addmtext
    oSpace
    origin
    0.0
    (strcat
      "%<\\AcObjProp Object(%<\\_ObjId "
      (if (> (vl-string-search "x64" (getvar 'platform)) 0)
(vlax-invoke-method
  (cond (oUtil)
((setq oUtil (vla-get-utility acDoc)))
  )
  'getobjectidstring
  x
  :vlax-False
)
(rtos (vla-get-objectid x) 2 0)
      )
      ">%).Area \\f \"%pr2%lu2%ct4%qf1 SQ. FT.\">%"
    )
  )
)
(vla-put-attachmentpoint oMText acmiddlecenter)
(vla-move oMText origin insertionPoint)

;;<-- Set MText height, style, etc. here... Example:
(vla-put-height oMText (/ 1. (getvar 'cannoscalevalue)))
;; ... others.
      )
    )
  )
  (*error* nil)
)
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Modifying a Field to become a formula for a label
« Reply #1 on: September 09, 2021, 05:49:25 PM »
You can create a formula using the field command to decipher the syntax needed:
%<\AcExpr ((%<\_FldPtr 2465500283456>%+%<\_FldPtr 2465500283968>%/2)*4/43560) \f "%lu2">%

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Modifying a Field to become a formula for a label
« Reply #2 on: September 09, 2021, 09:52:42 PM »
I'm posting a link to SMadsen's "Room Area lisp"; it doesn't use fields but it's just a simple attribute block tied to a pline's handle. Very nice routine.

https://www.theswamp.org/index.php?topic=32060.msg375362#msg375362
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Modifying a Field to become a formula for a label
« Reply #3 on: September 10, 2021, 12:08:13 PM »
I was able to figure the top part out.

Code: [Select]
;;       "%<\\AcObjProp Object(%<\\_ObjId "

              "%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId "

I know what the FLDPTR is supposed to do, but not sure how to get that to work in this scenario.

 %<\AcExpr ((%<\_FldPtr 2465500283456>%+%<\_FldPtr 2465500283968>%/2)*4/43560)



Code: [Select]
;; Add mtext
(setq oMText
  (vla-addmtext
    oSpace
    origin
    0.0
    (strcat
;;       "%<\\AcObjProp Object(%<\\_ObjId "

              "%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId "

      (if (> (vl-string-search "x64" (getvar 'platform)) 0)
(vlax-invoke-method
  (cond (oUtil)
((setq oUtil (vla-get-utility acDoc)))
  )
  'getobjectidstring
  x
  :vlax-False
)
(rtos (vla-get-objectid x) 2 0)
      )

;;       ">%).Area \\f \"%pr2%lu2%ct4%qf1 SQ. FT.\">%"
;;             %<\AcExpr ((%<\_FldPtr 2465500283456>%+%<\_FldPtr 2465500283968>%/2)*4/43560) \\f "%lu2">%

       %<\AcExpr ((%<\_FldPtr 2465500283456>%+%<\_FldPtr 2465500283968>%/2)*4/43560)

                        \\f "%lu2">%

    )
  )
)

bashing my head lol
Civil3D 2020

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Modifying a Field to become a formula for a label
« Reply #4 on: September 10, 2021, 10:04:55 PM »
Need to think a bit more but need something like
"%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId " (itoa (vla-get-Objectid att)) ">%).TextString>% * 2 >%" for twice size

This was select blocks 1 attribute and add the attributes and put answer into a mtext.

Code: [Select]
(defun c:test ( / obj len area lay)
(setq lst '() x -1 str "")
(if (setq ss (ssget '((0 . "INSERT"))))
  (progn
  (foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS (setq x (+ x 1)) )) 'getattributes)
        (if (=(vla-get-tagstring att) "NAME")
        (setq str (strcat str "%<\\AcObjProp Object(%<\\_ObjId " (itoa (vla-get-Objectid att)) ">%).TextString>%"))
        )
     )
  (repeat (- (sslength ss) 1)
     (foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS (setq x (+ x 1)) )) 'getattributes)
        (if (=(vla-get-tagstring att) "NAME")
        (setq str (strcat str "+%<\\AcObjProp Object(%<\\_ObjId " (itoa (vla-get-Objectid att)) ">%).TextString>%"))
        )
     )
  )
   
(setq str (strcat "%<\\AcExpr " str " >%"))
  )
)
(princ)

(setq obj (vlax-ename->vla-object (car  (entsel "Pick Mtext obj"))))
(vla-put-textstring obj str)
(princ)
)
(c:test)


You need to make the string and yes can use .textstring not fldptr, look at STR type !str.
A man who never made a mistake never made anything

kozmos

  • Newt
  • Posts: 114
Re: Modifying a Field to become a formula for a label
« Reply #5 on: August 17, 2023, 03:22:56 AM »
Is there a way to get the field pointer ID when we have field code such as "%<\\AcObjProp Object(%<\\_ObjId " (itoa (vla-get-Objectid att)) ">%).TextString>%"?

if yes, we can then use the FldPtr instead of using standard fieldcode when using AcExpr .
KozMos Inc.