PDA

View Full Version : unsure start - get object info into attribute


rstiles
2006-07-12, 06:34 PM
I’m new to programming and i am trying to make a simple lisp that allows me to get info from an object and fill it into an attributed block. For example; getting the area of a polyline and insert it into a tag. I understand the area command and the attedit command. I can’t figure out how to get the area value from the area command and insert it into the attribute. How do I copy a value from one command to the attribute?

I started writing this as a string in a button, but I am going to make it lisp so I can define it as a single command.

T.Willey
2006-07-12, 07:00 PM
What version are you using? This won't work on some of the older versions.

(defun c:PutArea ()

(and
(setq Sel (entsel "\n Select polyline: "))
(= (cdr (assoc 0 (entget (car Sel)))) "LWPOLYLINE")
(setq PolyObj (vlax-ename->vla-object (car Sel)))
(setq Sel (nentsel "\n Select attribute to fill with area: "))
(= (cdr (assoc 0 (entget (car Sel)))) "ATTRIB")
(setq AttObj (vlax-ename->vla-object (car Sel)))
(vla-put-TextString AttObj (rtos (/ (vla-get-Area PolyObj) 144.0) 2 2))
)
(princ)
)


This assumes you are working with units = 1 inch, and you want square feet. If you are working in 2006 or later, then you might want to look at fields.

rstiles
2006-07-12, 07:28 PM
I wish i could use fields. My clients take their facility drawings back to r14 but they work in 2002. I have been trying to get them to update. They figure that it works great when they need to send a floor plan to a consultant as a background.

Anyway that code works great, thanks, I need to round the value to the nearest whole number though. Changing the precision did not work.

T.Willey
2006-07-12, 07:39 PM
This line of code tells you how many decimals to round to.

(vla-put-TextString AttObj (rtos (/ (vla-get-Area PolyObj) 144.0) 2 2))

More specific, it is this part

(rtos (/ (vla-get-Area PolyObj) 144.0) 2 2)

Just change the second 2 to a 0, and you should be good. If you want it in a differnt format, you can look up rtos in the help for more information on it.

Glad it worked for you.

rstiles
2006-07-12, 08:06 PM
OK so that makes sense.

Could you point out where it is defining the area value? Is it grabbing a value and then calling it up later?

I am having troubles breaking this down in my mind after the value is placed i want to insert common text. Let’s say "SF" for square footage. I can’t tell where i would place that.

T.Willey
2006-07-12, 08:20 PM
(defun c:PutArea ()

(and
(setq Sel (entsel "\n Select polyline: "))
; above selects an object

(= (cdr (assoc 0 (entget (car Sel)))) "LWPOLYLINE")
; this makes sure that the object selected is an LWPOLYLINE

(setq PolyObj (vlax-ename->vla-object (car Sel)))
; this makes it an ActiveX object (makes that later part easier)

(setq Sel (nentsel "\n Select attribute to fill with area: "))
; this will select a nested object

(= (cdr (assoc 0 (entget (car Sel)))) "ATTRIB")
; this makes sure that the nested object selected, is an attribute

(setq AttObj (vlax-ename->vla-object (car Sel)))
; this makes the select object an ActiveX object

(vla-put-TextString AttObj (rtos (/ (vla-get-Area PolyObj) 144.0) 2 2))
; this line is complex, so lets break it down
)
(princ)
)


(vla-put-TextString ; here is where we use ActiveX controls to update the property "TextString"
AttObj ; this is the object we want to update
(rtos ; this is where we turn a real to a string (rtos)
(/ ; simple division
(vla-get-Area PolyObj) ; this is where we get the area (property) of the Polyline object
144.0 ; divide the area by this number
)
2 ; part of the rtos function that tells it what type of string to turn it into.
2 ; how many decimal places to keep when converting the real
)
)


If you want to add "SF" to the string, then you would have to use (strcat...... and where you would use it is in the same line as above, so now it would look like

(vla-put-TextString ; here is where we use ActiveX controls to update the property "TextString"
AttObj ; this is the object we want to update
(strcat ; telling it that we want to combind some strings
(rtos ; this is where we turn a real to a string (rtos)
(/ ; simple division
(vla-get-Area PolyObj) ; this is where we get the area (property) of the Polyline object
144.0 ; divide the area by this number
)
2 ; part of the rtos function that tells it what type of string to turn it into.
2 ; how many decimal places to keep when converting the real
)
"SF" ; we want to add "SF" to the end of the number string
)
)


Hope that makes sense.

Wanderer
2006-07-12, 08:38 PM
I luff fields. ftr, my facility uses r2004 (using06) format... and this (http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151) is what I tell my consultants when they're working in something older.I wish i could use fields. My clients take their facility drawings back to r14 but they work in 2002. I have been trying to get them to update. They figure that it works great when they need to send a floor plan to a consultant as a background.

Anyway that code works great, thanks, I need to round the value to the nearest whole number though. Changing the precision did not work.

rstiles
2006-07-24, 03:37 PM
thanks for the link i will pass this on, also thanks for the help on writing the lisp. i am slowly getting a better grasp on how to write. i took a intro class but that def was not enough to get me going.

thanks Russ