Vittorio Posted July 19 Posted July 19 (edited) Hi everyone, I just started practicing and VLIDE instantly throws an error "error: bad function" after picking two points in the drawing area. My function: ; Function to get 2D distance in XY-plane (defun c:myFun001 (/ p1 p2) ; pick 1. point in drawing area (setq p1 (getpoint "\nfirst point: ")) ; pick 2. point in drawing area (setq p2 (getpoint "\nsecond point: ")) (princ (strcat "\Distance in XY-plane: " ; sqrt((p2x-p1x)²+(p2y-p1y)²) = sqrt(dx²+dy²) = sqrt(dX(p1,p2)²+dY(p1,p2)²) (rtos (sqrt (+ (sqr (dX (p1 p2))) (sqr (dY (p1 p2)))))) ) ) ) ; function: square (defun sqr(x) (* x x) ) ; function: delta X (defun dX (p1 p2) (- (car p2) (car p1)) ) ; function: delta Y (defun dY (p1 p2) (- (cadr p2) (cadr p1)) ) Since I'm new to AutoLISP I haven't figured out the reason for the error yet. Any help appreciated. Best regards Edited July 19 by Vittorio code correction Quote
lastknownuser Posted July 19 Posted July 19 5 hours ago, Vittorio said: Hi everyone, I just started practicing and VLIDE instantly throws an error "error: bad function" after picking two points in the drawing area. My function: ; Function to get 2D distance in XY-plane (defun c:myFun001 (/ p1 p2) ; pick 1. point in drawing area (setq p1 (getpoint "\nfirst point: ")) ; pick 2. point in drawing area (setq p2 (getpoint "\nsecond point: ")) (princ (strcat "\Distance in XY-plane: " ; sqrt((p2x-p1x)²+(p2y-p1y)²) = sqrt(dx²+dy²) = sqrt(dX(p1,p2)²+dY(p1,p2)²) (rtos (sqrt (+ (sqr (dX (p1 p2))) (sqr (dY (p1 p2)))))) ) ) ) ; function: square (defun sqr(x) (* x x) ) ; function: delta X (defun dX (p1 p2) (- (car p2) (car p1)) ) ; function: delta Y (defun dY (p1 p2) (- (cadr p2) (cadr p1)) ) Since I'm new to AutoLISP I haven't figured out the reason for the error yet. Any help appreciated. Best regards Remove brackets from arguments in delta functions (rtos (sqrt (+ (sqr (dX p1 p2)) (sqr (dY p1 p2))))) 2 Quote
Vittorio Posted July 19 Author Posted July 19 2 hours ago, lastknownuser said: Remove brackets from arguments in delta functions (rtos (sqrt (+ (sqr (dX p1 p2)) (sqr (dY p1 p2))))) Many thanks! One more thing. In the Command Line Window I get repeated output: first point: second point: Distance in XY-plane: 1.234"\Distance in XY-plane: 1.234" Is this due to a setting? Or wrong syntax? Quote
Lee Mac Posted July 19 Posted July 19 Add (princ) after your (princ (strcat ...)) expression. The defun expression is returning the result of the last evaluated expression, hence returning the value returned by your princ expression, which is the string argument supplied to it. 1 Quote
CyberAngel Posted July 19 Posted July 19 Wouldn't it be a bit simpler to use (expt x 2) instead of defining a new function to do the same thing? That also removes the potential confusion between sqr and sqrt, which I first thought was a typo. Quote
Lee Mac Posted July 19 Posted July 19 5 hours ago, CyberAngel said: Wouldn't it be a bit simpler to use (expt x 2) instead of defining a new function to do the same thing? That also removes the potential confusion between sqr and sqrt, which I first thought was a typo. ...or just forego the explicit Pythagorean calculation and use the distance function - (defun c:myFun001 ( / 2d p1 p2 ) (setq 2d (lambda ( x ) (list (car x) (cadr x)))) (if (and (setq p1 (getpoint "\n1st point: ")) (setq p2 (getpoint "\n2nd point: ")) ) (princ (strcat "\nDistance in XY-plane: " (rtos (distance (2d p1) (2d p2))))) ) (princ) ) 1 Quote
marko_ribar Posted July 19 Posted July 19 @Lee Mac If OP works in BricsCAD, that (setq 2d (lambda ...)) construction won't work well... So still you'll have to use (defun 2d ( x ) (list (car x) (cadr x)))... Quote
ronjonp Posted July 20 Posted July 20 I still like this version of Lee's for 2d points (mapcar '+ '(0 0) pt) 1 Quote
BIGAL Posted July 20 Posted July 20 (edited) In another ronjonp post this is X & Y & Z of 2 points. A handy function, thinking bounding Box. (setq lenwid (mapcar 'abs (mapcar '- p1 p2))) Edited July 20 by BIGAL Quote
Lee Mac Posted July 21 Posted July 21 On 7/19/2024 at 7:22 PM, marko_ribar said: @Lee Mac If OP works in BricsCAD, that (setq 2d (lambda ...)) construction won't work well... So still you'll have to use (defun 2d ( x ) (list (car x) (cadr x)))... It works fine for me in V24... Quote
marko_ribar Posted July 21 Posted July 21 It works and for me in V23, but if I can recall that's not always the case... I remember that I mod. those (lambda)'s into (defun)'s because BricsCAD throwed some errors which I can't remember now... The solution was hopefuly very simple... Quote
Vittorio Posted July 22 Author Posted July 22 Wow, many thanks to all of you for helping! I slightly modified my function based on some answers and here's what I finally got: (defun c:2DD (/ p1 p2) (setq p1 (getpoint "\nPick 1. Point: ")) (setq p2 (getpoint "\nPick 2. Point: ")) (princ (strcat "\nDistance in XY-Plane: " (rtos (sqrt (+ (sqr (dX p1 p2)) (sqr (dY p1 p2))))) ) ) (princ) ) (defun sqr(x) (* x x) ) (defun dX (p1 p2) (- (car p2) (car p1)) ) (defun dY (p1 p2) (- (cadr p2) (cadr p1)) ) Actually it's pretty much the same with some extra paragraphs for readability. The reason why I use this instead of distance is simple: I'm still in learning phase and I'd like to match my AutoLISP with my current knowledge (use only what I've learned so far) and I haven't played around with the distance function yet. It may not sound logical to some of you, but this way I can visually see the coding progress in my functions. Nevertheless I'll have a look at all of your suggestions. Best regards 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.