PDA

View Full Version : A simple routine


mert523
2008-12-04, 06:07 PM
I do not know AutoLISP, unfortunately. What I am looking for, and I'm sure this would be a simple routine, is to be able to select a number (single line text) in a drawing, perform a calculation on it (add, subtract, multiply or divide) and return the results to the drawing. I can be reached through the forum or email at mgarrison@skookum.org. Thanks!

rkmcswain
2008-12-04, 06:32 PM
Here is some quick and dirty code, without any error checking...


; select an entity
(setq num (entget (car (entsel))))

; get it's textstring value (remember, no error checking
; so make sure you select a TEXT entity that contains a numeric value)
(setq val (cdr (assoc 1 num)))

; convert this to a numeral
(setq val2 (atof val))

; perform the math, in this case add 15.
(setq newval (+ val2 15.0))

; convert this back to a string
(setq newvalS (rtos newval))

; Put this value back into the string
(setq elist (subst (cons 1 newvalS) (assoc 1 num) num))

; Modify the entity
(entmod elist)

mert523
2008-12-04, 06:35 PM
Thanks. I will give this a try.

mert523
2008-12-04, 07:01 PM
Here is some quick and dirty code, without any error checking...


; select an entity
(setq num (entget (car (entsel))))

; get it's textstring value (remember, no error checking
; so make sure you select a TEXT entity that contains a numeric value)
(setq val (cdr (assoc 1 num)))

; convert this to a numeral
(setq val2 (atof val))

; perform the math, in this case add 15.
(setq newval (+ val2 15.0))

; convert this back to a string
(setq newvalS (rtos newval))

; Put this value back into the string
(setq elist (subst (cons 1 newvalS) (assoc 1 num) num))

; Modify the entity
(entmod elist)

OK. I gave it a try and it worked up to a point. I modified your calculation to do a divide. My original number was 173576 and I divided it by 144 and got 1205.39 but the result that was written back to the drawing was 100'-5 25/64. I wanted the 1205.39 to be written back to the drawing.

hofcad
2008-12-04, 07:09 PM
OK. I gave it a try and it worked up to a point. I modified your calculation to do a divide. My original number was 173576 and I divided it by 144 and got 1205.39 but the result that was written back to the drawing was 100'-5 25/64. I wanted the 1205.39 to be written back to the drawing.

RTOS converts a number into a string

(rtos number [mode [precision]])

The rtos function returns a string that is the representation of number according to the settings of mode, precision, and the system variables UNITMODE, DIMZIN, LUNITS, and LUPREC.

Try: (setq newvalS (rtos newval 2))

Regards HofCAD CSI.

mert523
2008-12-04, 07:16 PM
RTOS converts a number into a string

(rtos number [mode [precision]])

The rtos function returns a string that is the representation of number according to the settings of mode, precision, and the system variables UNITMODE, DIMZIN, LUNITS, and LUPREC.

Try: (setq newvalS (rtos newval 2))

Regards HofCAD CSI.
Great!! That works. I might have to tweak a little.

irneb
2008-12-05, 07:07 AM
Just curious ... why don't you use fields? With fields it would work a lot more like Excel, where the calculated value gets updated if you update the text.

mert523
2008-12-05, 06:01 PM
I guess I wasn't clear to begin with. First of all, I am using AutoCAD 2002. I am also using a plugin utility from GeoTools. I am required to calculate square footage of rooms in a building or parcels of land. GeoTools has a routine that will calculate the area of a closed polygon and display the results on your drawing. However, when I am in architectural units, it is displaying the results as square inches and I want square feet. That is why I wanted something that would allow me to select the displayed square inches, divide it by 144, and place the results back in the drawing. I have communicated with Four Dimension Technologies, the creators of GeoTools, and explained my situation. They explained how to correct my problem but it involves upgrading to a newer version of AutoCAD and a new version of GeoTools. Can't do that at this time.

Thanks,
mert523

ccowgill
2008-12-05, 08:58 PM
Ive got something like that:

(defun C:acarea (/ acar smsg)
(command "area" "o")
(while (>= (getvar "cmdactive") 1)
(command PAUSE)
)
(setq acar (rtos (/ (getvar "area") 144.0))
smsg (strcat "Area = " acar " Sft")
)
(princ smsg)
(princ)
)

rkmcswain
2008-12-05, 10:09 PM
Ive got something like that...
And you could push the output into a FIELD as mentioned above....

irneb
2008-12-06, 12:57 PM
And you could push the output into a FIELD as mentioned above....Not with 2002 (only 2005 and later) ... not sure if an RText could do that though?

BTW, does the GeoTools package update the area if the polyline changes? If so they're using some type of reactor / vba event. The attached is a thing I did a while back (I think for 2004), may work in your 2002. It uses a lisp object reactor to update the text if the polyline changes (so you need to add the lsp to ACAD.LSP / ACADDOC.LSP / Sartup Suite). Also has options for converting units & adding prefix / suffix. But, as I said, this is a while back, I've not used it since fields became available.

Oh, sorry, yes ... the commands are:

iDimArea, to place area of one polyline only
iDimArea1, for adding / subtracting more than one pl
iDimLength, for placing text of length of polyline

rkmcswain
2008-12-06, 05:00 PM
Not with 2002...Correct. I missed that part. Thanks.

mert523
2008-12-08, 06:25 PM
Ive got something like that:

(defun C:acarea (/ acar smsg)
(command "area" "o")
(while (>= (getvar "cmdactive") 1)
(command PAUSE)
)
(setq acar (rtos (/ (getvar "area") 144.0))
smsg (strcat "Area = " acar " Sft")
)
(princ smsg)
(princ)
)

What is a good resource, for a novice, to learn AutoLISP?

Mert Garrison
CAD Technician

rkmcswain
2008-12-08, 06:29 PM
What is a good resource, for a novice, to learn AutoLISP?

Mert Garrison
CAD Technician

Here are some free resources:
http://cadpanacea.com/node/111

If you learn better from one-on-one training than from reading, then check with your reseller. They may offer training classes on this subject.

ccowgill
2008-12-08, 06:32 PM
What is a good resource, for a novice, to learn AutoLISP?

Mert Garrison
CAD Technician
This forum is pretty good, there are a few sticky threads at the top of this forum you can also read. I learned most of what I know from others here at AUGI.