Author Topic: Length FIELD not working  (Read 2554 times)

0 Members and 1 Guest are viewing this topic.

QuestionEverything

  • Guest
Length FIELD not working
« on: October 06, 2016, 11:57:39 AM »
Hi, I am trying something simple:
1. Entmakex a line from 2 points
2. Entmakex a mtext in the middle, and populate it with Length FIELD from the line
Whats wrong with my field content?
Code: [Select]
(vl-load-com) (princ)
(defun C:test ( / p1 p2 a e c str id )

(if
(and
(setq p1 (getpoint "\nFirst point for the line: "))
(setq p2 (getpoint "\nSecond point for the line: " p1))
(setq a (angle p1 p2))
); and
(progn
(setq e (entmakex (list (cons 0 "LINE")(cons 10 p1)(cons 11 p2))))
(setq c (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p1 p2))
(setq str
(strcat
"%<\\AcObjProp.16.2 Object(%<\\_ObjId "
(setq id (Get-ObjectIDx64 (vlax-ename->vla-object e)))
">%).Length \\f \"%lu2%ds44%zs8%th32\">%"
)
)
(entmakex
(list
(cons 0 "MTEXT") (cons 100 "AcDbEntity") (cons 100 "AcDbMText") (cons 10 c)
(cons 40 (getvar 'textsize)) (cons 50 a) (cons 1 str) (cons 71 5)
)
)
)
)
(princ)
)

(defun Get-ObjectIDx64 (obj / util)
  (setq util (vla-get-Utility (vla-get-activedocument (vlax-get-acad-object))))
(if (> (vl-string-search "x64" (getvar "platform")) 0)
(vlax-invoke-method util "GetObjectIdString" obj :vlax-False)
(itoa (vla-get-Objectid obj))
)
)
I get this instead:
« Last Edit: October 06, 2016, 03:46:11 PM by QuestionEverything »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Length FIELD not working
« Reply #1 on: October 06, 2016, 12:49:43 PM »
entmake'ing a Field is relatively complex as you also have to create the accompanying dictionaries which drive the field update.

Using ActiveX methods, such dictionaries are generated automatically:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / ent ins mtx pt1 pt2 )
  2.     (if
  3.         (and
  4.             (setq pt1 (getpoint "\n1st point for line: "))
  5.             (setq pt2 (getpoint "\n2nd point for line: " pt1))
  6.         )
  7.         (progn
  8.             (setq pt1 (trans pt1 1 0)
  9.                   pt2 (trans pt2 1 0)
  10.                   ent (entmakex (list '(0 . "LINE") (cons 10 pt1) (cons 11 pt2)))
  11.                   ins (vlax-3D-point (mapcar '/ (mapcar '+ pt1 pt2) '(2.0 2.0 2.0)))
  12.                   mtx
  13.                 (vla-addmtext
  14.                     (if (= 1 (getvar 'cvport))
  15.                         (vla-get-paperspace (LM:acdoc))
  16.                         (vla-get-modelspace (LM:acdoc))
  17.                     )
  18.                     ins 0.0
  19.                     (strcat
  20.                         "%<\\AcObjProp Object(%<\\_ObjId "
  21.                         (LM:objectid (vlax-ename->vla-object ent))
  22.                         ">%).Length \\f \"%lu2%ds44%zs8%th32\">%"
  23.                     )
  24.                 )
  25.             )
  26.             (vla-put-attachmentpoint mtx acattachmentpointmiddlecenter)
  27.             (vla-put-rotation mtx (angle pt1 pt2))
  28.             (vla-put-insertionpoint mtx ins)
  29.         )
  30.     )
  31.     (princ)
  32. )
  33.  
  34. ;; ObjectID  -  Lee Mac
  35. ;; Returns a string containing the ObjectID of a supplied VLA-Object
  36. ;; Compatible with 32-bit & 64-bit systems
  37.  
  38. (defun LM:objectid ( obj )
  39.     (eval
  40.         (list 'defun 'LM:objectid '( obj )
  41.             (if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
  42.                 (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
  43.                '(itoa (vla-get-objectid obj))
  44.             )
  45.         )
  46.     )
  47.     (LM:objectid obj)
  48. )
  49.  
  50. ;; Active Document  -  Lee Mac
  51. ;; Returns the VLA Active Document Object
  52.  
  53. (defun LM:acdoc nil
  54.     (LM:acdoc)
  55. )
  56.  

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Length FIELD not working
« Reply #2 on: October 06, 2016, 12:56:21 PM »
This worked for me, assuming that your FIELDEVAL = 31...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test ( / p1 p2 a e c str mt )
  2.  
  3.   (if
  4.     (and
  5.       (setq p1 (getpoint "\nFirst point for the line: "))
  6.       (setq p2 (getpoint "\nSecond point for the line: " p1))
  7.       (setq a (angle p1 p2))
  8.     ); and
  9.     (progn
  10.       (setq e (entmakex (list (cons 0 "LINE")(cons 10 p1)(cons 11 p2))))
  11.       (setq c (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p1 p2))
  12.       (setq str
  13.         (strcat
  14.           "%<\\AcObjProp.16.2 Object(%<\\_ObjId "
  15.           (Get-ObjectIDx64 (vlax-ename->vla-object e))
  16.           ">%).Length \\f \"%lu2%ds44%zs8%th32\">%"
  17.         )
  18.       )
  19.       (setq mt
  20.         (entmakex
  21.           (list
  22.             (cons 0 "MTEXT") (cons 100 "AcDbEntity") (cons 100 "AcDbMText") (cons 10 c)
  23.             (cons 40 (getvar 'textsize)) (cons 50 a) (cons 1 str) (cons 71 5)
  24.           )
  25.         )
  26.       )
  27.       (vla-put-textstring (vlax-ename->vla-object mt) (eval (vla-get-textstring (vlax-ename->vla-object mt))))
  28.     )
  29.   )
  30.   (princ)
  31. )
  32.  
  33. (defun Get-ObjectIDx64 (obj / util)
  34.   (if (> (vl-string-search "x64" (getvar "platform")) 0)
  35.     (vlax-invoke-method util "GetObjectIdString" obj :vlax-False)
  36.     (itoa (vla-get-Objectid obj))
  37.   )
  38. )
  39.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

QuestionEverything

  • Guest
Re: Length FIELD not working
« Reply #3 on: October 06, 2016, 02:49:45 PM »
Hm, so as I understand for the Object ID to be "readen/evaluated" correctly, vla-object must be used for the text/mtext instead of entity.
This row of Marko Ribar perfectly describes this:
Code: [Select]
(vla-put-textstring (vlax-ename->vla-object mt) (eval (vla-get-textstring (vlax-ename->vla-object mt)))) I thought it was just enough to "put/paste" the content (with strcat) and It would work... just like vla-add... (which is commonly seen if one googles enough about FIELD & LISP).
Lee mac's example shows this - during vla-add that content/textstring is evaluated automatically. And I didn't knew that FIELD can be entmaked (thanks for shedding light on this subject).
Both answers I got are incredibly useful!

ChrisCarlson

  • Guest
Re: Length FIELD not working
« Reply #4 on: October 06, 2016, 03:48:11 PM »
Are the distances going to change? If not, you can easily just calculate the distance between pt1 and pt2 and pass it into the mtext entity as a string.

Code - Auto/Visual Lisp: [Select]
  1. (setq str (rtos (distance pt1 pt2) 2 2))

QuestionEverything

  • Guest
Re: Length FIELD not working
« Reply #5 on: October 06, 2016, 05:29:26 PM »
I was practicing with FIELDs, its way too easy to get the distance straight.

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Length FIELD not working
« Reply #6 on: October 07, 2016, 02:58:34 AM »
Lee is correct... You can remove (eval) from here :

Code: [Select]
(vla-put-textstring (vlax-ename->vla-object mt) (vla-get-textstring (vlax-ename->vla-object mt)))

It's about Activex functionality...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

QuestionEverything

  • Guest
Re: Length FIELD not working
« Reply #7 on: October 07, 2016, 03:19:23 PM »
Next time I'll try not to ignore Lee Mac's short comments, instead of giving straight look into the code.