Create a Field Formula from Field Expressions

Create a Field Formula from Field Expressions

Julio_Soto
Advisor Advisor
618 Views
9 Replies
Message 1 of 10

Create a Field Formula from Field Expressions

Julio_Soto
Advisor
Advisor

I have a lisp that I'm writing that takes a length parameter from a dynamic block and divides it by the unit factor (for dwg with different units)

when I do this manually in AutoCAD the field looks like this

%<\AcExpr (%<\_FldPtr 1947961495696>% / %<\_FldPtr 1947961496080>%) \f "%lu2%zs12">%

but each FldPtr looks like this
%<\AcObjProp.16.2 Object(%<\_ObjId 1948028287920>%).Parameter(1).UpdatedDistance \f "%lu6%zs12">%

and
%<\AcObjProp.16.2 Object(%<\_ObjId 1948028287920>%).InsUnitsFactor \f "%lu6%zs12">%


I can get those two fields with Lee-Mac's getobject Id and pramters, ect. but I don't know how to build the FldPtr expressions. 

 

is this possible with LISP?

0 Likes
Accepted solutions (1)
619 Views
9 Replies
Replies (9)
Message 2 of 10

paullimapa
Mentor
Mentor

did you create this field using the field formula option?

 %<\_FldPtr 1947961496080>%)


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

Julio_Soto
Advisor
Advisor

Yes.  I used the "Formula" option, then in the window where you enter formulas I create two more fields.

I can create each field just fine with AutoLISP, but I dont' know how to create a FldPtr that AutoCAD uses, like this
%<\AcExpr (%<\_FldPtr 1947961495696>% / %<\_FldPtr 1947961496080>%) \f "%lu2%zs12">%

each field was assigned some ID by autocad.

 

Those FldPtr are referencing the other two fields.

0 Likes
Message 4 of 10

paullimapa
Mentor
Mentor

In place of both: %<\_FldPtr 1947961495696>% and %<\_FldPtr 1947961496080>%

use the actual %<\AcObjProp reference which would be something like this:

(strcat
"%<\\AcExpr "
"("
%<\\AcObjProp.16.2 Object(%<\\_ObjId 1948028287920>%).Parameter(1).UpdatedDistance \\f \"%lu6%zs12\">%"
" / "
"%<\\AcObjProp.16.2 Object(%<\\_ObjId 1948028287920>%).InsUnitsFactor \\f \"%lu6%zs12\">%"
")"
"\\f \"%lu6%zs12\">%"
)

 


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

Julio_Soto
Advisor
Advisor

i was tying something similar but this is the MTEXT output

 

Julio_Soto_1-1755545121850.png

 


This is my test code, with the ObjectID hardcoded into the lisp for testing. also attached is the block i'm working with.

(defun c:TestBoltLengthField (/ pt objId paramId str) 
  (setq pt (getpoint "\nPick insertion point for test field: "))

  (setq str (strcat 
              "%<\\AcExpr "
              "("
              "%<\\AcObjProp.16.2 Object (%<\\_ObjId 2429014325728).Parameter(1).UpdatedDistance \\f \"%lu6%zs12\">%"
              " / "
              "%<\\AcObjProp.16.2 Object(%<\\_ObjId 2429014325728).InsUnitsFactor \\f \"%lu6%zs12\">%"
              ")"
              "\\f \"%lu6%zs12\">%"
            )
  )


  ;; Insert it as MText
  (entmakex 
    (list 
      '(0 . "MTEXT")
      '(100 . "AcDbEntity")
      '(100 . "AcDbMText")
      (cons 10 pt)
      (cons 40 0.2) ;; text height
      (cons 1 str) ;; the field string
      (cons 7 "Arial")
      (cons 71 1) ;; top-left attachment
      (cons 72 5) ;; L-to-R flow
      (cons 11 (list 1.0 0.0 0.0))
      (cons 210 (list 0.0 0.0 1.0))
    )
  )
  (princ)
)

(princ "\nTest TEst")

 

0 Likes
Message 6 of 10

paullimapa
Mentor
Mentor
Accepted solution

Don't think you can hardcode the ObjectID. 

So I would use this function to get it and convert it from integer to string:

(itoa (vla-get-objectid obj))

Then the complete string formatting will look like this:

(setq str (strcat 
              "%<\\AcExpr "
              "("
              "%<\\AcObjProp.16.2 Object(%<\\_ObjId " (itoa (vla-get-objectid obj)) ">%).Parameter(90).lookupString \\f \"%lu6%zs12\">%"
              " / "
              "%<\\AcObjProp.16.2 Object(%<\\_ObjId " (itoa (vla-get-objectid obj)) ">%).InsUnitsFactor \\f \"%lu6%zs12\">%"
              ")"
              " \\f \"%lu6%zs12\""
              ">%"
            )
)

Now you can't add the string into the MText object when you create it so I would keep the string empty like this:

(entmakex 
    (list 
      '(0 . "MTEXT")
      '(100 . "AcDbEntity")
      '(100 . "AcDbMText")
      (cons 10 pt)
      (cons 40 0.2) ;; text height
;      (cons 1 str) ;; the field string
      (cons 1 "")
      (cons 7 "Arial")
      (cons 71 1) ;; top-left attachment
      (cons 72 5) ;; L-to-R flow
      (cons 11 (list 1.0 0.0 0.0))
      (cons 210 (list 0.0 0.0 1.0))
    )
)

Then use this vla function to put the text in:

(vla-put-textstring (vlax-ename->vla-object (entlast)) str)

Now editing the MText Field object, it does show both numerator and denominator:

paullimapa_0-1755554214517.png

But it still evaluates to ### because of the mm unit string that comes with the 65.

 

 

 


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

Julio_Soto
Advisor
Advisor

so if you double click the field "65mm" you'll see the other to fields

Julio_Soto_0-1755554703693.png



the "mm" is just from "Additional formatting"

 

aside fromthe 65mm you were able to get it to work?

0 Likes
Message 8 of 10

paullimapa
Mentor
Mentor

yes, the fields do show up when I run the codes I provided you in my previous post


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

Julio_Soto
Advisor
Advisor

Thanks for the help dude, this is the code that is working right

(defun c:TestBoltLengthField (/ pt blkObj objectID str mtextEnt)
  (setq pt (getpoint "\nPick insertion point for test field: "))
  (setq obj (vlax-ename->vla-object (car (entsel "\nSelect block: "))))
  (setq objectID (getobjectid obj)) ;; Lee Mac helper

  ;; Build field string
(setq str (strcat 
              "%<\\AcExpr "
              "("
              "%<\\AcObjProp.16.2 Object(%<\\_ObjId " objectID ">%).Parameter(1).UpdatedDistance  \\f \"%lu6%zs12\">%"
              " / "
              "%<\\AcObjProp.16.2 Object(%<\\_ObjId " objectID ">%).InsUnitsFactor \\f \"%lu6%zs12\">%"
              ")"
              " \\f \"%lu2%zs12\""
              ">%"
            )
)

  ;; Insert dummy MTEXT
  (setq mtextEnt
    (entmakex
      (list
        '(0 . "MTEXT")
        '(100 . "AcDbEntity")
        '(100 . "AcDbMText")
        (cons 10 pt)
        (cons 40 0.2) ;; text height
        (cons 1 "-")  ;; placeholder
        (cons 7 "Arial")
        (cons 71 1)   ;; top-left attachment
        (cons 72 5)   ;; L-to-R flow
        (cons 11 (list 1.0 0.0 0.0))
        (cons 210 (list 0.0 0.0 1.0))
      )
    )
  )

  (vla-put-TextString (vlax-ename->vla-object mtextEnt) str)

  (princ)
)



(defun GetObjectID (BlockVLA / acadobj acaddoc BlockEname BlockVLA) 
  (setq acadobj (vlax-get-acad-object)
        acaddoc (vla-get-activedocument acadobj)
  )
  ;; Lee Mac
  (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE"))) 
    (vlax-invoke-method (vla-get-Utility acaddoc) 'GetObjectIdString BlockVLA :vlax-false)
    (itoa (vla-get-Objectid BlockVLA))
  )
)

 

in addition to some formatting issue, my main problem was putting my string in ENTMAKEX.  using vla-put-TextString was the trick. thanks again my friend!

(also i used lee-mac getobjectID)

0 Likes
Message 10 of 10

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


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