Author Topic: User input as relative coordinates in a (command) function  (Read 1750 times)

0 Members and 1 Guest are viewing this topic.

qwrtz

  • Newt
  • Posts: 22
User input as relative coordinates in a (command) function
« on: July 25, 2023, 01:00:50 PM »
I want to get user input X and Y values and then use them as relative coordinates of the endpoint of a line (relative to the start point). If I weren't in a lisp function, I would just type something like @36,42. In a lisp function I can get the relative coordinates this way:
(setq y1 (rtos (getreal "Rise: ")))
(setq x1 (rtos (getreal "Run: ")))
Then I thought I could draw the line this way:
(command "Line" p1 "_non" "@" x1 "," y1 "")
But that doesn't work. What's the right way to do it?

ribarm

  • Gator
  • Posts: 3293
  • Marko Ribar, architect
Re: User input as relative coordinates in a (command) function
« Reply #1 on: July 25, 2023, 01:38:28 PM »
Untested...

(command "_.LINE" "_non" p1 "_non" (strcat "@" x1 "," y1) "")
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

qwrtz

  • Newt
  • Posts: 22
Re: User input as relative coordinates in a (command) function
« Reply #2 on: July 25, 2023, 02:38:15 PM »
That works!  Concatenation.
Thank you, Marko.
« Last Edit: July 25, 2023, 02:46:28 PM by qwrtz »

Lee Mac

  • Seagull
  • Posts: 12917
  • London, England
Re: User input as relative coordinates in a (command) function
« Reply #3 on: August 10, 2023, 05:38:39 AM »
Alternatively, (and to avoid the string conversion and potential loss of precision), you might consider the following:
Code - Auto/Visual Lisp: [Select]
  1. (if (and (setq p1 (getpoint "\nFirst point: "))
  2.          (setq y1 (getreal  "\nRise: "))
  3.          (setq x1 (getreal  "\nRun: "))
  4.     )
  5.     (command "_.line" "_non" p1 "_non" (mapcar '+ p1 (list x1 y1)) "")
  6. )

qwrtz

  • Newt
  • Posts: 22
Re: User input as relative coordinates in a (command) function
« Reply #4 on: August 27, 2023, 11:12:17 PM »
Thanks very much, Lee.  I don't fully understand it yet, but it works, and I'm glad to be able to eliminate any possibility of imprecision caused by strcat.

I've noticed some imprecision in another function I wrote.  I'll have to try to remember which function it was, and see if strcat is involved.

Lee Mac

  • Seagull
  • Posts: 12917
  • London, England
Re: User input as relative coordinates in a (command) function
« Reply #5 on: August 29, 2023, 06:50:04 AM »
You're welcome  :-)

and I'm glad to be able to eliminate any possibility of imprecision caused by strcat.

Note that the imprecision is caused by the conversion to a string (i.e. rtos in this particular example); strcat merely concatenates two or more strings. Without the conversion, the point coordinate values will be expressed using the maximum precision afforded by a double-precision floating point value, around 15-16 significant figures.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: User input as relative coordinates in a (command) function
« Reply #6 on: August 31, 2023, 11:06:54 AM »
ENTMAKE might work in your example as well.

Code: [Select]
(setq p1 (getpoint "\nSpecify first point:"))
(setq p2 (getpoint p1 "\nSpecify next point:"))
(entmake (list '(0 . "LINE") (cons 10 p1) (cons 11 p2)))

https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-D47983BA-1E5D-417D-85B8-6F3DE5F506BA
TheSwamp.org  (serving the CAD community since 2003)