Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoLisp and Fields

33 REPLIES 33
SOLVED
Reply
Message 1 of 34
angiegauthier
10608 Views, 33 Replies

AutoLisp and Fields

Hi Everyone,

 

Is it possible to access fields with autolisp? I'd like to be able to link them with dimension text, but I want the user to be able to select the dimension they want, and then the field would update (using autolisp). I know very little about fields, and haven't been able to see how to change/update them using Lisp. (I'm using ACAD 2008, if that makes a difference.)

 

Angie

33 REPLIES 33
Message 21 of 34
pbejse
in reply to: pbejse

<Not Tested for newer version>  2010 onwards 

 

(defun c:Oarea (/ Get-ObjectIDx64 space ss pt are sst tm)(vl-load-com)
;;;	Jimmy Bergmark			;;;
;;;	http://blog.jtbworld.com/	;;;
(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))
    )
  )
;;;		pBe Oct 18 2012		;;;
  (if (and (setq ss (car (entsel "\n Select Object for Area : ")))
	   (vlax-property-available-p (setq ss (vlax-ename->vla-object ss)) 'Area)
	   (setq pt (getpoint "\n Specify Text Location :"))
      )
	(progn
	  (setq are (rtos (* (vla-get-area ss) 0.0001) 2 2)
	        sst (vlax-invoke  (vlax-get (vla-get-ActiveLayout
	                 (vla-get-activedocument (vlax-get-acad-object)))
	                                         'Block)
	                                   'addMText pt 1.
			(strcat	"%<\\AcObjProp Object(%<\\_ObjId "
				(Get-ObjectIDx64 ss)
				">%).Area \\f \"%lu2%pr2%ps[, m²]%ct8[0.0001]\">%")
			)
		tm (textbox (list (cons 1 (strcat (vl-string-translate "1" "0" are) " m²"))
			      (cons 40 (vla-get-height sst))
			      (cons 7 (getvar 'TextStyle)))))
		(vla-put-width sst (distance (Car tm)(list (caadr tm)(cadar tm))))
	  )(princ "\nNo object for Area Text Selected"))
  (princ)
)

 

command: Oarea

 

HTH

 

Message 22 of 34
Gustavo_Bernardi
in reply to: pbejse

For the Arial font does not work only with this value. But if doubling works well.
I changed the code:

 

(vla-put-width sst (distance (Car tm)(list (caadr tm)(cadar tm))))

 

by

 

(vla-put-width sst (* 2(distance (Car tm)(list (caadr tm)(cadar tm)))))

and worked!

 

Thanks

 

I intend in the future to increase. I'll put a "GetString" to capture a name for the room. Also select the name of the layer via a hatch field to define the type of flooring. etc

Message 23 of 34

I am working on another project were I need to use a field, but in this case we are showing imperial and metric in the dimension. (with alternate units) the field only shows the primary units. How can I get it to show both? I've tried monkeying around with it a bit using the field expression that shows up when I create the field with no luck.

Is it possible to do this?

 

angie

Message 24 of 34

I'm not sure how.

but you can convert the value using the following calculation:

1 m² = (3.28083989501) ² = 10.7639104167 ft ²

can see that in my case I need to convert from cm  to m² in the following line:

(setq are (rtos (* (vla-get-area ss) 0.0001) 2 2)

believe you can replicate content writing line area and do the conversion to their units (imperial)

 

I hope someone more expert can give a more concrete help.

 

 

Message 25 of 34
angiegauthier
in reply to: pbejse

...so I cheated a little to make this work.

I drew a line along the edge I was dimensioning. then I had the field reference the length of the line instead of using the measurement. This way I was able to get the units I needed, and if the user stretches the object, the line I drew will stretch along with it and the field will still update. An extra step, but it works.

 

Angie

Message 26 of 34
abaluuuu
in reply to: angiegauthier

This rutine is very good:)

 

(vl-load-com)
(defun c:Test (/ space ss pt)
  ; Tharwat 14. 06. 2011
  (setq space (vla-get-modelspace
                (vla-get-ActiveDocument (vlax-get-acad-object))
              )
  )
  (if (and (setq ss (car (entsel "\n Select a Dimension : ")))
           (setq pt (getpoint "\n Specify Text Location :"))
      )
    (vla-addMText
      space
      (vlax-3d-point pt)
      4.
      (strcat "%<\\AcObjProp Object(%<\\_ObjId "
              (itoa (vla-get-ObjectID (vlax-ename->vla-object ss)))
              ">%).Measurement \\f \"%lu6\">%"
      )
    )
    (princ)
  )
  (princ)
)

 

 

 

 

 

 

 

 

 

Can you help me how can write the mtext with text height 50? And after I need to explode it to convert for normal text.

 

Thank you!!!!!!!!

Message 27 of 34
korayozal
in reply to: abaluuuu

Thank you about the rutine display above, it returns with Mtext

 

is it possible to obtain text version of it? 

 

thank you again. 

Message 28 of 34
lelandleahy4987
in reply to: korayozal

Just curious has anyone been able to do the reverse of a field? Find the object that field if refering to? Example, if have text thats value is the contents of another text entity. How can i find where the refernce is coming from?

Leland
Civil3d 2020
Windows 10 64x (32 gigs ram)
Message 29 of 34
hmsilva
in reply to: lelandleahy4987


@lelandleahy4987 wrote:

Just curious has anyone been able to do the reverse of a field? Find the object that field if refering to? Example, if have text thats value is the contents of another text entity. How can i find where the refernce is coming from?


Hi Leland,

 

one way at go.lsp by jdvillarreal...

 

Hope that helps

Henrique

EESignature

Message 30 of 34
boicottms
in reply to: angiegauthier

Hello,

I'm trying to add some simple calculation (area/8) starting from the similar lisp of Lee Mac' Areas to Field.
Studyng the field's that I statically created the formula is

%<\AcExpr (%<\_FldPtr 2785267405104>%/8) \f "%lu2%pr1">%

where the brackets should be the following area

%<\AcObjProp Object(%<\_ObjId 2750103214752>%).Area \f "%lu2%pr2">%

So I'm trying to change Lee Mac's code that gets the area (and is very similar to the code hereby)

(strcat
"%<\\AcObjProp Object(%<\\_ObjId "
(LM:ObjectID (vlax-ename->vla-object (ssname sel 0)))
">%).Area \\f \"" fmt "\">%"
)

adding the surrounding part of the field:

(strcat
"%<\\AcExpr ("  ;; field's function start
"%<\\AcObjProp Object(%<\\_ObjId " ;; original object's area
(LM:ObjectID (vlax-ename->vla-object (ssname sel 0)))
">%).Area" ;; original object's area end
">/8)"  ;; function end
" \\f \"
" fmt "\">%"
)

(Notice that fmt is just a variable that contains the formatting string of the field).

Using formulas wuold add power to the field, but at the moment alla I get is the text %<cExpr (###

Can someone help?

Thank you

Message 31 of 34
boicottms
in reply to: boicottms

I managed to do it:

		 "%<\\AcExpr (%<\\AcObjProp Object(%<\\_ObjId  "
		 (LM:ObjectID (vlax-ename->vla-object (ssname sel 0)))
		 ">%).Area>%/8) \\f  \"" fmt "\">%"
	       )

and if your'e trying to mod the Lee Mac code don't forget to change also

                        (strcat
                            "%<\\AcExpr "
                            (apply 'strcat (reverse (cdr (reverse lst))))
                            " \\f \"" fmt "\">%"
                        )

to

		 (strcat
		   "%<\\AcExpr ("
		   (apply 'strcat (reverse (cdr (reverse lst))))
		   ")/8 \\f \""
		   fmt
		   "\">%"
		 )
Message 32 of 34
Lee_Mac
in reply to: angiegauthier

I'm pleased that you find my program useful - and glad you were able to figure out how to modify it successfully!

Message 33 of 34

do you have de file complete, i made those changes but still not working. even if my autocad says "successfully loaded"

Message 34 of 34

Hi,

sorry responding so late. I have this which is a mod of @Lee_Mac 's great http://www.lee-mac.com/lengthfield.html, where I just add commands to calculate sqm in cm drawings and so on. If you see the code see how simple is to add your ones - notice, this can give you areas or lenghts - :

;; Length Field Commands                                                ;;
;;----------------------------------------------------------------------;;

(defun c:l2f  ( ) (lengthfield nil "%lu2%pr2"))            ;; Current units, 2 decimals
(defun c:l2fm ( ) (lengthfield nil "%lu2%pr2%ct8[0.01]")) ;; Current units with 0.001 conversion factor (cm->m), 2 decimals
(defun c:l2f0  ( ) (lengthfield nil "%lu2%pr0"))            ;; Current units, 2 decimals
(defun c:l2fm0 ( ) (lengthfield nil "%lu2%pr0%ct8[0.01]")) ;; Current units with 0.001 conversion factor (cm->m), 2 decimals

;;----------------------------------------------------------------------;;
;; Area Field Commands                                                  ;;
;;----------------------------------------------------------------------;;

;;(defun c:af  ( ) (areafield nil "%lu6%qf1"))           ;; Current units
(defun c:a2f  ( ) (areafield nil "%lu2%pr2"))           ;; Current units - 2 decimals
(defun c:a2f0 ( ) (areafield nil "%lu2%pr0"))           ;; Current units - 0 decimals
(defun c:a2fm ( ) (areafield nil "%lu2%pr2%ct8[0.0001]")) ;; Current units with 1e-6 (0.0001) conversion factor (cm2->m2)
(defun c:a2fm0 ( ) (areafield nil "%lu2%pr0%ct8[0.0001]")) ;; Current units with 1e-6 (0.0001) conversion factor (cm2->m2)

...I just formatted a sample of what I wanted in the field editor and copied here those ugly formulas.

Thanks LeeMac

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost