Need Help With AutoLisp Routine

Need Help With AutoLisp Routine

Domziman
Advocate Advocate
739 Views
10 Replies
Message 1 of 11

Need Help With AutoLisp Routine

Domziman
Advocate
Advocate

Hi Guys i need some help with my lisp routing, i want to simplify every day match calculations we need to work out. Here is an example:

 

Elevation + (Distance between 2 points / fall)

 

For elevation i use the Getstring

For the distance between 2 points i use the getpoint and then use the distance function to get the length between the points.

 

Here is my Code So far:, i cant seem to get the alert working to display the value, am i missing something here)

 

 

(VL-LOAD-COM)
(DEFUN C:ElCalc (/StartElevation Point1 Point2 D1 Slope Math1 Math2 )

(setq StartElevation (Getstring "\nEnter Elevation: "))
(setq Point1 (Getpoint "\nChoose First Point: "))
(setq Point2 (Getpoint "\nChoose Second Point: "))
(setq D1 (Distance Point1 Point2))
(setq Slope (Getstring "\nEnter Slope: "))
(setq Math1 (/ D1 Slope))
(setq Math2 (+ StartElevation Math1))
(alert "Elevation is:" Math2 )

)

Civil 3D Certified Professional
0 Likes
740 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Instead of (both) Getstring use getdist (or getreal) to get a REAL number.

Also, (/   StartElevation ; a space needs to follow the /.

Also the last line is wrong. (alert) requires a SINGLE string. You have 2 items, the second is not even a string.

 

"25.4" is a string (from getstring). It's not a number, just a set of characters... your program does not know that it has some numerical value. You cannot make any calculations... it's just a text.

25.4 is a real number, you can do calculations with that.

 

Spoiler alert.

(DEFUN C:ElCalc (/ StartElevation Point1 Point2 D1 Slope Math1 Math2 )

(if (and (setq StartElevation (getreal "\nEnter Elevation: "))
	 (setq Point1 (Getpoint "\nChoose First Point: "))
	 (setq Point2 (Getpoint point1 "\nChoose Second Point: "))
	 (setq D1 (Distance Point1 Point2))
	 (setq Slope (Getreal "\nEnter Slope: "))
	 (setq Math1 (/ D1 Slope))
	 (setq Math2 (+ StartElevation Math1))
	 )
  (alert (strcat "Elevation is:" (rtos Math2 )))) ; math has to be a string. Strings must be merged to 1.
  )

 

0 Likes
Message 3 of 11

Domziman
Advocate
Advocate

(VL-LOAD-COM)
(DEFUN C:ElCalc (/StartElevation Point1 Point2 D1 Slope Math1 Math2 )

 


(setq StartElevation (Getdist "\nEnter Elevation: "))
(setq Point1 (Getpoint "\nChoose First Point: "))
(setq Point2 (Getpoint "\nChoose Second Point: "))
(setq D1 (distance Point1 Point2))
(setq Slope (Getdist "\nEnter Slope:"))
(setq Direction (getstring "\nSlope Direction: Up or Down?"))
(setq Calc1 (/ D1 Slope))
; Start of If
(if (= (direction) down )
(setq Math1 (- StartElevation Calc1) alert Math1)
(setq Math2 (+ StartElevation Calc1) alert Math2)
); Close of If
)

Civil 3D Certified Professional
0 Likes
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

Direction is not a function but a variable. No parents. 

Down is not a variable but a string. Add quotes.

 

BTW Better way to get up/down direction would be (getkword) with (initget)

0 Likes
Message 5 of 11

Domziman
Advocate
Advocate

i have not used autolisp much and do not understand all the functions ect

Civil 3D Certified Professional
0 Likes
Message 6 of 11

ВeekeeCZ
Consultant
Consultant

Nobody understands all the functions. Smarter ones know where to look.

0 Likes
Message 7 of 11

john.uhden
Mentor
Mentor

@Domziman 

In addition to to what the masters have indicated, you had better watch what you're getting for the distance between P1 and P2.  If they they have differing Zs, then your distance is a 3D distance, which will lower your computed slope value.

I always use:

(defun @2d (p)(list (car p)(cadr p))),

but you could also use:

(defun @2d (p)(mapcar '* p '(1.0 1.0))) ;; which eliminates the Z by absence of a multiplier.

each of which for example would convert '(2.5 3.4 5.0) to '(2.5 3.4).

John F. Uhden

0 Likes
Message 8 of 11

calderg1000
Mentor
Mentor

Regards @Domziman 

Try this code, with some corrections to the code you show...
For this case, I prefer "Getreal" instead of "Getdist" since it has two input options and can be confusing. The "Osnapz" variable has been configured to obtain the slope only in 2D.

(DEFUN C:ElCalc1 (/ Oz StartElevation Point1 Point2 D1 Slope Direction)
  (setq Oz (getvar 'Osnapz))
  (setvar 'Osnapz 1)
  (setq StartElevation (Getreal "\nEnter Elevation: "))
  (setq Point1 (princ(Getpoint "\nChoose First Point: ")))
  (setq Point2 (princ(Getpoint "\nChoose Second Point: ")))
  (setq D1 (distance Point1 Point2))
  (setq Slope (Getdist "\nEnter Slope:"))
  (setq Direction (getstring "\nSlope Direction [Up/Down]<U>:?"))
  (setq Calc1 (/ D1 Slope)); Start of If
  (if (= Direction "D")
    (alert (strcat "\nMath1= " (rtos (- StartElevation Calc1) 2 3)))
    (alert (strcat "\nMath2= " (rtos (+ StartElevation Calc1) 2 3)))
  ); Close of If
  (setvar 'osnapz Oz)
  (princ)
)

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 9 of 11

Domziman
Advocate
Advocate
Thank you i will give it a try and thaks for everone's help
Civil 3D Certified Professional
0 Likes
Message 10 of 11

Domziman
Advocate
Advocate

one last question i have is i need 3 decimal places after the . for example 25.352.

Civil 3D Certified Professional
0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@Domziman wrote:

.... i need 3 decimal places after the . for example 25.352.


Are you not getting that from the (rtos .... 2 3) functions?  That's what that 3 is for [read about (rtos) in the AutoLisp Reference].  If you want 3 of them even if some at the end are zeros, set the DIMZIN System Variable to a value that will not suppress trailing zeros.

Kent Cooper, AIA
0 Likes