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

field expressions in lisp

14 REPLIES 14
Reply
Message 1 of 15
andrew_nao
3978 Views, 14 Replies

field expressions in lisp

can i setq a field expression?

is it possible to use field expressions in lisp?
14 REPLIES 14
Message 2 of 15
Anonymous
in reply to: andrew_nao

can i setq a field expression?

I'm not sure what you mean. Do you want to read the value of the field or make one?


is it possible to use field expressions in lisp?

Yes, see the example code below. The bottom text box in the field command shows the format of the specific field value you want.
If you want the value of a specific object, you have to get the objectID from the desired entity and convert it to a string, like in
the example.


; Gets the centroid of a region


(defun C:Centroid ( / ent obj os pt area)

; Select the region
(setq ent (entsel "\nSelect region: "))

; Is an object selected?
(if ent
(progn
(setq ent_name (car ent))
; Is it a region
(if (= (cdr (assoc 0 (entget ent_name))) "REGION")
(progn
; Determine the centroid
(setq obj (vlax-ename->vla-object ent_name) )
(setq pt (vlax-get obj "Centroid"))
; Determine the area
(setq area (vlax-get obj "Area"))
(setq area (if (> area 144)
(strcat (rtos ( / area 144) 2 2) " ft%%202")
(strcat (rtos ( / area 1) 2 2) " in%%202")
)
)
(setq scale (/ (getvar "DIMSCALE") 40.0))
(setq os (getvar "osmode"))
(setvar "osmode" 0)
; Display coordinates
(princ "\nCentroid found at: ")
(princ (car pt))
(princ ",")
(princ (cadr pt))
; Write the area

(setq oid (vla-get-objectid obj))
(if (vlax-property-available-p obj 'Area)
(progn
(setq txt (strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa oid) ">%).Area \\f \"%pr2%lu2%ct4%qf1 sq ft>%"))
(command "text" "middle" pt (* (getvar "DIMSCALE") 0.09375) "" txt)
)
)
)
)
)
)
(princ)
)
Message 3 of 15
andrew_nao
in reply to: andrew_nao

I would like to do both there are some posts that explain how to make custom fields, but im not having much luck with that.
however this instance i was just looking to be able to have a lisp program i made that we use for title block creation to insert a couple of property fields when the lisp is ran, i wasnt sure if it could be done as i wasnt having much luck with it either. i will look over your code and see where i went wrong.

thanks for the help
Message 4 of 15
_gile
in reply to: andrew_nao

Hi,

Here's another code, for area too :

(defun c:chp_aire (/ AcDoc Space obj ins)
(vl-load-com)
(setq AcDoc (vla-get-ActiveDocument (vlax-get-acad-object))
Space (if (= 1 (getvar "CVPORT"))
(vla-get-PaperSpace AcDoc)
(vla-get-ModelSpace AcDoc)
)
)
(if (and (setq obj (car (entsel)))
(member (cdr (assoc 0 (entget obj)))
'("ARC" "CIRCLE" "ELLIPSE" "LWPOLYLINE" "REGION" "SPLINE")
)
(setq ins (getpoint "\nSpecify insertion point: "))
)
(vla-addMtext Space
(vlax-3d-point ins)
0.0
(strcat
"%<\\AcObjProp.16.2 Object(%<\\_ObjId "
(itoa (vla-get-ObjectID (vlax-ename->vla-object obj)))
">%).Area \\f \"%lu2%pr2%ps[,m²]%ct8[1e-006]\">%"
)
)
)
(princ)
)

To get the field expression, make a field as you need and copy the expression in the field dialog box (see picture link).
This expression is separate in two by the Object ID.
To make it availalable in a LISP expression, you have to add an anti-slash (\) behind every \ and "

picture : http://img67.imageshack.us/img67/5931/champll2.png


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 15
iharper
in reply to: _gile

I was hoping this would answer my question but the link to the Picture is not found.

I have a simple Autolisp question, similar to this if anyone can answer. I am writing a routine, and in the routine I want to be able to get (and Store with a Setq if needed) the value from a custom field in the drawing from the Drawing Properties. (for example: FLRLEVEL would be the custom field name in the Drawing Properties and the Value may be something like  02, for the second floor). This field is not really tied to any particular object in the drawing, so I am not sure if it can be done without an Object ID? (Which is what all of the examples have been of) I know I could have the lisp prompt for the floor number, but this routine will be ran multiple times in each DWG and I was hoping to have it stored with the DWG rather than the LISP routine; I have tried GETVAR but it is not found as a system variable. If anyone can assist me I will be forever grateful. Thank you in advance.

Message 6 of 15
bellandspina
in reply to: iharper

Is it possible to reference custom fields in LISP or SCR and use the values entered there to turn on and off layers?  Example of a custom field code referenced in attributes:

 

%<\AcSm SheetSet.CUSTOMFIELDNAME \f "%tc1">%

 

Theoretically if i enter a 1 or a 0 as the field property, cant i use a simple condition and layer on/off command to achieve this?

 

Thanks,

 

 

Message 7 of 15
Lineabove
in reply to: _gile

Thank you for sharing the code.

 

The field is inserted at top left corner.

How may I change that to insert at middle centre?

 

It seems that the field format is the same if it is Top Left or Middle Centre.

 

 

Message 8 of 15
rudi17
in reply to: Lineabove


Thank you both for sharing the code. Very useful!

 

I have a polyline and a block with attribute "Length".
I have two tasks:
- to set a field expression in the block attribute with the lenght of the polyline;
- to read the expression from the field attribute and find the handle of the polyline.

 

Thanks!

Message 9 of 15
hmsilva
in reply to: rudi17


@rudi17 wrote:

...

I have a polyline and a block with attribute "Length".
...


Hi rudi17,
could you please, attach a sample dwg with the 'polyline and the block?

 

Henrique

EESignature

Message 10 of 15
rudi17
in reply to: hmsilva

Here is a sample.

One polyline and a block. The attribute in the block refere the length ot the polyline.

I need a lisp code that read the block reference and find the handle of the polyline.

Thanks!

Message 11 of 15
dbroad
in reply to: rudi17

Wow. With a 3 month response time for each message, this conversation could take a while. Smiley Sad

Architect, Registered NC, VA, SC, & GA.
Message 12 of 15
devitg
in reply to: rudi17

 

 

 

As far as I can see, there is not any relation between the block and the polyline , and the attribute is not a FIELD , 

So how do you think it can be do?

IMHO you have to do 2 clicks, one at the block , and other at the polyline  

 

 

Message 13 of 15
Ranjit_Singh2
in reply to: rudi17

Rudi, try the attached as a starting point. May want to add standard error checking, and also catch if the block with same name exists

 

(defun c:plfld ( /  plent pt1 pt2 curnomutt curcmd)
(prompt "\nSelect Polyline :")
(setq plent (car (entsel))
	pt1 (getpoint "\nSelect insertion point :")
	curcmd (getvar 'cmdecho)
	curnomutt (getvar 'nomutt)
	pt2 (subst (+ (car pt1) 26) (car pt1) pt1))
(setvar 'cmdecho 0)
(setvar 'nomutt 1)
(entmake (list (cons 0 "BLOCK") (cons 100 "AcDbEntity") (cons 100 "AcDbBlockBegin") (cons 8 "0") (cons 10 pt1) (cons 2 "SomeBlk") (cons 70 2)))
(entmake (list (cons 0 "TEXT") (cons 100 "AcDbEntity") (cons 100 "AcDbText") (cons 8 "0") (cons 10 pt1) (cons 40 12.0) (cons 1 "L=")))
(entmake (list (cons 0 "ATTDEF") (cons 100 "AcDbEntity") (cons 100 "AcDbText") (cons 8 "0") (cons 10 pt2) (cons 40 12.0) (cons 1 (strcat "%<\\AcObjProp.16.2 Object(%<\\_ObjId " (itoa (vla-get-objectID (vlax-ename->vla-object plent))) ">%,1).Length \\f \"%lu2%pr0\">%")) (cons 100 "AcDbAttributeDefinition") (cons 3 "Len") (cons 2 "LEN") (cons 70 0)))
(entmake (list (cons 0 "ENDBLK") (cons 100 "AcDbEntity") (cons 100 "AcDbBlockEnd") (cons 8 "0")))
(command "._-Insert" "SomeBlk" pt1 "1" "" "0" "")
(setvar 'cmdecho curcmd)
(setvar 'nomutt curnomutt)
(princ)
)


Message 14 of 15
rudi17
in reply to: Ranjit_Singh2

Thank you, it works! Very useful!

 

I will try to insert already made block for a large

number polylines, not to create new block many times.

Message 15 of 15

You can create anything in AutoCAD!!! though it may take a while...but that is the "fun" in it right?

I suggest looking for clues as to how things can be done, and not just answers to a test (because you wont understand what to do when something breaks)

Being an educator, I am very opposed feeding the fisherman fish...you get it.


And Yes, you can even a block that can resemble a process contained in a cloud!
(not just data, but a process to generate/obtain/update the data - a field can contain an autolisp variable.fine - but I needed something that would be more dynamic and change as my drawing changed automatically (like a field based on an autolisp function result))

 

In short, some reactor code to "process" your code and set the text values on-the-fly and/or when you want the processing performed.  The code used to obtain the value desired can also be housed in the drawing.

Grab the code, do the processing as desired, and update the text values accordingly.


Since there are a number of ways to implement this loose design...
I'll leave it up to you to create an implementation...
I do it on the daily, but am unable to share this code as it is not mine to share...sorry.

 

The main difference here is the complexity of the calculations required may be fairly involved, but it is your choice about the when and where the processing to update your values occurs instead of only relying on a variable value (without discovery/calculation/processing).

 

If anyone inquires as to posting a sample, I may be able create a simpler scenario for the community to illustrate the process.  (I would need to ask the people who pay me first).

TANX.Rob

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

Post to forums  

Autodesk Design & Make Report

”Boost