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

lisp routine - select polyline and apply formula help

21 REPLIES 21
SOLVED
Reply
Message 1 of 22
mpa-la
1502 Views, 21 Replies

lisp routine - select polyline and apply formula help

I know this is a really simple routine, if someone could help me out I'd be very appreciative.  Here is the formula I would like to use:

(166.24 / X squared) * area of selected polyline.  An example for clarity, if X is 12 and the area of the selected polyline was 100, it would be (166.24 / 144) * 100 = 115.

 

I would like to issue the command, enter the number for X, pick the polyline, and have the resultant whole number (no decimals) output on the command line.  I would like it to remember whatever X is (for that drawing session) until I change it so that I can enter past that question if I'm not changing it.

 

Thanks again if you can help!

 

21 REPLIES 21
Message 2 of 22
mid-awe
in reply to: mpa-la

Is this what you are looking for?

 

(DEFUN c:SP-n-AF (/ AREA CUROBJ TTL X)
(VL-LOAD-COM) (SETQ X (GETREAL "\nEnter a value for X: ") CUROBJ (VLAX-ENAME->VLA-OBJECT (CAR (ENTSEL))) AREA (VLA-GET-AREA CUROBJ) TTL (* (/ 166.24 (* X X)) AREA) ) (PRINC (strcat "\n\t=>> Total after formula applied: " (FIX ttl) " \n")) (PRINC) )

 

Message 3 of 22
mpa-la
in reply to: mid-awe

It works!  When it returns the value though, it says (on the command line) "Select object: ; error: bad argument type: stringp" and then the formula result.  Is that a quick fix?

 

Thanks so much, this will be a real time saver!  Allison

Message 4 of 22
hmsilva
in reply to: mid-awe

Change

(FIX ttl)

to

(itoa(FIX ttl))

 

HTH

Henrique

EESignature

Message 5 of 22
mid-awe
in reply to: mpa-la

I'm not sure about the "noisy" command output. Everything finishes quietly here, but I'm on AC2013. Either way it doesn't look like a problem so long as the output is correct.

I'm glad it works for you. 🙂
Message 6 of 22
mpa-la
in reply to: mid-awe

That fixed it!  I'm in 2014, btw.  Thank you so much!  Allison

Message 7 of 22
aliensinearth
in reply to: mpa-la

Here is the modified code that will remember the last value of X until you close the drawing.

 

(DEFUN c:SP-n-AF (/ AREA CUROBJ TTL X)
  (VL-LOAD-COM)

  (IF (NOT *X)
    (SETQ *X 100)
  )

  (PRINC "\nEnter a value for X or <")
  (PRINC *X)
  (SETQ X (GETREAL "> : "))
  (IF (NOT X)
    (SETQ X *X)
    (SETQ *X X)
  )  
  
  (SETQ	CUROBJ (VLAX-ENAME->VLA-OBJECT (CAR (ENTSEL)))
	AREA   (VLA-GET-AREA CUROBJ)
	TTL    (* (/ 166.24 (* X X)) AREA)
  )
  (PRINC (strcat "\n\t=>> Total after formula applied: " (ITOA (FIX TTL)) " \n"))
  (PRINC)
)

 

Message 8 of 22
mpa-la
in reply to: aliensinearth

Bonus!  Thanks for that, I wasn't going to ask Smiley Wink

 

Message 9 of 22
mid-awe
in reply to: aliensinearth

You need to remove X from the localized variables for the "remember the last value of X until you close the drawing" to work.

Message 10 of 22
aliensinearth
in reply to: mid-awe

Hi mid-awe,

 

Thanks for your reponse.

Your code was very short and sweet.

 

I think, we don't need to remove X. Because, I have used another varibale as "*X". This "*X" will remember the last value of X.

Message 11 of 22
mid-awe
in reply to: aliensinearth

Right on. I missed that * in front of the X. My appologies, and thank you for the addition.
Message 12 of 22
Lee_Mac
in reply to: mpa-la

Here's another way to write it:

 

(defun c:af ( / a e x )
    (setq x (cond ((getreal (strcat "\nX-Value <" (rtos (cond(x)(100)) 2) ">: ")))(x)(100)))
    (if (setq e (car (entsel)))
        (if (vl-catch-all-error-p (setq a (vl-catch-all-apply 'vlax-curve-getarea (list e))))
            (princ "\nInvalid object.")
            (princ (strcat "\nResult: " (itoa (fix (* a (/ 166.24 x x))))))
        )
    )
    (princ)
)
(vl-load-com) (princ)
Message 13 of 22
mid-awe
in reply to: Lee_Mac

(getreal (strcat "\nX-Value <" (rtos (cond(x)(100)) 2) ">: "))
& then you go and
(vl-catch-all-apply 'vlax-curve-getarea (list e))
you just
(itoa (fix (* a (/ 166.24 x x))))

says, Lee Mac.
"Were not worthy" 🙂 Thank you.
Message 14 of 22
Kent1Cooper
in reply to: Lee_Mac


@Lee_Mac wrote:

....

... (itoa (fix (* a (/ 166.24 x x))))

....


If what you want [it isn't clear to me from the original question] is actually the result rounded to the nearest whole number [up or down], rather than stripped of any decimal component no matter how large [i.e. rounded down only as the above does], replace the above part with:

 

(rtos (* a (/ 166.24 x x)) 2 0)

Kent Cooper, AIA
Message 15 of 22
mpa-la
in reply to: Kent1Cooper

I did want it rounded, thanks for that clarification.

Message 16 of 22
Lee_Mac
in reply to: mid-awe


@mid-awe wrote:
(getreal (strcat "\nX-Value <" (rtos (cond(x)(100)) 2) ">: "))
& then you go and
(vl-catch-all-apply 'vlax-curve-getarea (list e))
you just
(itoa (fix (* a (/ 166.24 x x))))

says, Lee Mac.
"Were not worthy" 🙂 Thank you.

Smiley Very Happy You're too kind mid-awe -

 

Though, looking back over my code, there is a slight mistake: the variable 'x' should be global (and perhaps renamed to something more obscure).

Message 17 of 22
mid-awe
in reply to: Lee_Mac

I've been studying your "Object Align" code. I see you used "oa|rot" & "oa|off", two globals. I thought it was good practice to wrap globals in **? Or, you are using an old trick for namespacing? ObjectAlign.rotation & ObjectAlign.offset?
I use javascript a lot and I do something simular with my globals.

$oa = {
       rot: value,
       off: value,
       }

$oa.rot = value;
$oa.off = value;

if ($ao.rot === 'false') {
        $oa.rot = 0.0
        }

if ($ao.off === 'false') {
        $oa.off = 0.0
        }

 Or, it could be thought of as a local-global 😉 I refere to the unlikely event that you'd reuse the same global for two different things.

Message 18 of 22
mid-awe
in reply to: mpa-la

Allison, would you mark the thread, solved? Thank you.
Message 19 of 22
aliensinearth
in reply to: mpa-la

Thank you Mail.

Hello mid-awe,
This solution belongs you. Hence, I give full credit to you.

Hi Lee_Mac,
Special Thanks to you.
You will always have another way of coding which is out of Box.
Message 20 of 22
mpa-la
in reply to: aliensinearth

Whoops - sorry mid awe, I used the one from aliensinearth becuase it remembered my initial x value.  I didn't notice that it wasn't you!  I'm gonna see if I can accept two as solutions.  Allison

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

Post to forums  

Autodesk Design & Make Report

”Boost