Jump to content

Recommended Posts

Posted (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 by Vittorio
code correction
Posted
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)))))

 

  • Like 2
Posted
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?

Posted

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.

  • Like 1
Posted

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.

Posted
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)
)

 

  • Like 1
Posted

@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)))...

Posted

I still like this version of Lee's for 2d points 😎

(mapcar '+ '(0 0) pt)

 

  • Thanks 1
Posted (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 by BIGAL
Posted
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...

Posted

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...

Posted

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

  • Agree 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...