Author Topic: Input point OR number (user's choice)?  (Read 2448 times)

0 Members and 1 Guest are viewing this topic.

qwrtz

  • Newt
  • Posts: 22
Input point OR number (user's choice)?
« on: January 23, 2021, 03:06:42 PM »
A beginner's question:  Is there a way I can prompt the user to either pick a point -OR- enter a floating point number, whichever the user chooses, and accept either input?  And know which type of input was made?  If it's a number, I want to use it as an angle, not as a distance or a coordinate.

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Input point OR number (user's choice)?
« Reply #1 on: January 23, 2021, 04:35:31 PM »
Hi
Something like this ?
Code - Auto/Visual Lisp: [Select]
  1. (or (setq p (getpoint "\nSpecify a point or hit enter for < Number > : "))
  2.     (setq n (getreal "\nEnter any real number :"))
  3.     )
  4.  

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Input point OR number (user's choice)?
« Reply #2 on: January 23, 2021, 05:12:16 PM »
Ordinarily getpoint will interpret numerical input as a displacement from the origin in the direction of the cursor, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. 10
  2. (8.52676 5.2244 0.0)
  3.  
  4. Command: (distance '(0 0) '(8.52676 5.2244))
  5. 10.0

However, if you precede the getpoint prompt with a call to initget supplied with bit 128 to permit arbitrary input, the numerical input will be returned as a literal string, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. nil
  2.  
  3. 4
  4. "4"

Which can be easily tested using the type function, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (initget 128)
  2. (if (setq pnt (getpoint "\nSpecify point or number: "))
  3.     (if (= 'str (type pnt))
  4.         (princ "\nUser entered a number.")
  5.         (princ "\nUser picked a point.")
  6.     )
  7.     (princ "\nUser dismissed the prompt.")
  8. )

However, since bit 128 allows for arbitrary input, you'll probably want to test that the user has actually provided a valid numerical value, e.g.:

Code - Auto/Visual Lisp: [Select]
  1.     (and
  2.         (progn
  3.             (initget 128)
  4.             (setq pnt (getpoint "\nSpecify point or number: "))
  5.         )
  6.         (= 'str (type pnt))
  7.         (not  (distof pnt))
  8.     )
  9.     (princ "\nPlease provide either a point or number.")
  10. )


qwrtz

  • Newt
  • Posts: 22
Re: Input point OR number (user's choice)?
« Reply #3 on: January 23, 2021, 09:19:58 PM »
Thanks very much, Tharwat and Lee Mac.  After a lot of head scratching I think I understand at least part of what each of you wrote.

I got my custom command to work using Tharwat's function.  The user has to press Enter instead of the second point, in order to then be able to enter a number to be used for the rotation angle.  I was hoping the user could just enter a number instead of picking a point, as is the case with the built-in Rotate command.  But this is really not much different.

Lee Mac's function does allow entering a number instead of picking a point, without a preliminary Enter.  But I can't seem to use the number as a rotation angle, maybe because it's a text string instead of a real number.  Is that right?  If so, can the text string be converted to a real number?

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Input point OR number (user's choice)?
« Reply #4 on: January 24, 2021, 06:26:37 AM »
Hi,
You can use the angle function also if you would like the user to pick two points on the screen instead of using the function getpoint two times then to use angle function to get the angle in radians.

Of course you can convert a string into real or integer values based on the input string itself. eg:

Code - Auto/Visual Lisp: [Select]
  1. _$ (distof "123.456")
  2. 123.456
  3. _$ (read "123.456")
  4. 123.456
  5. _$ (atoi "123.456") ;;  note the change from real to integer based on the use of the function atoi = A string to Integer.
  6. 123
  7.  

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Input point OR number (user's choice)?
« Reply #5 on: January 24, 2021, 07:12:11 AM »
@Tharwat,
You forgot the most important one :

Code - Auto/Visual Lisp: [Select]
  1. (atof "123.123")
  2.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Input point OR number (user's choice)?
« Reply #6 on: January 24, 2021, 07:27:59 AM »
@Tharwat,
You forgot the most important one :

Code - Auto/Visual Lisp: [Select]
  1. (atof "123.123")
  2.  

Yeah that is also as well.
I was just giving examples to the OP to learn from and not to cover all functions.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Input point OR number (user's choice)?
« Reply #7 on: January 24, 2021, 12:55:49 PM »
Lee Mac's function does allow entering a number instead of picking a point, without a preliminary Enter.  But I can't seem to use the number as a rotation angle, maybe because it's a text string instead of a real number.  Is that right?  If so, can the text string be converted to a real number?

Certainly - as is shown in my last code snippet where I test whether the string can be interpreted as a number using the distof function.

The appropriate conversion to a numerical value representing an angle will depend on how you intend to use the resulting angle - for example, if you intend to supply the angle to an AutoCAD command, the command will expect a value expressed in the angular units of the drawing, and so in this case you might extract the numerical value directly from the user input using a function such as distof; however, if, for example, you intend to use the angle as a DXF group value, the value would need to be expressed in radians, and so you could perform the conversion using the angtof function - observe the difference:
Code - Auto/Visual Lisp: [Select]
  1. Command: (distof "30")
  2. 30.0
  3. Command: (angtof "30")
  4. 0.523599 ;; equals (/ pi 6)

qwrtz

  • Newt
  • Posts: 22
Re: Input point OR number (user's choice)?
« Reply #8 on: January 24, 2021, 02:11:15 PM »
Thanks, everyone.  Now my custom command works just the way I wanted it to, and I learned a lot in the process.  I still haven't understood everything written here, but I'll do some more research to try to figure it out.

I use Rotate a lot, usually by vertex and sweep points, but occasionally by vertex and degrees; never any other method.  I wanted a custom version that only offers those two options and doesn't require any option letters or picking the vertex twice.

Code - Auto/Visual Lisp: [Select]
  1. ; aRotate rotates a selection set by vertex and sweep points or vertex and degrees.
  2. ; It prompts for two points, the first of which is the vertex of the rotation angle.
  3. ; The second input is either the angle in degrees or the start point of the sweep.
  4. ; You can always enter a number instead of a point in response to (getpoint).
  5. ; Normally the number's interpreted as distance from origin toward cursor.
  6. ; But with (initget 128) = arbitrary input it's treated as a string.
  7. ; Then convert that string to a real number with (atof).
  8. ; If p2 is a number, i.e. (if (= 'str (type p2)), it's passed to the Rotate command.
  9. ; If not, point p2 is passed to the Rotate command, which prompts for a third point.
  10.  
  11. (defun c:aRotate (/ ss1 p1 p2 n1)
  12. (setq ss1 (ssget) )
  13. (setq p1 (getpoint "\nVertex of rotation angle: "))
  14. (initget 128)
  15. (setq p2 (getpoint p1 "\nPick start point of sweep or Enter angle in degrees: "))
  16. (if (= 'str (type p2))
  17.  (progn (setq n1 (atof p2)) (command "Rotate" ss1 "" p1 n1))
  18.  (progn (command "Rotate" ss1 "" p1 "B" p1 p2)
  19.    (while (> (getvar "cmdactive") 0) (command pause)))
  20.  )
  21. )
« Last Edit: January 24, 2021, 02:15:55 PM by qwrtz »