TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Shade on March 10, 2006, 09:52:58 AM

Title: Testing for a point
Post by: Shade on March 10, 2006, 09:52:58 AM
How does one test for a point?

Example code...
Code: [Select]
(initget "Options")
(setq PNT (getpoint "\nSelect Point, <O>ptions: "));
(if (not PNT [color=Red]Point[/color])
   (OPTIONS)
);

I thought of testing the variable PNT to see if its a list, but I would like to know if there is a better more secure way of testing for a point
     
Thanks in advance for any help...
Shade
Title: Re: Testing for a point
Post by: T.Willey on March 10, 2006, 11:33:43 AM
If you want to see if it is a point (list) then you can use the "type" function on the variable.
Code: [Select]
(if (= (type Pnt) 'LIST)
 .... do whatever
 ... or do your options thingy here.
)

??
Title: Re: Testing for a point
Post by: CAB on March 10, 2006, 11:45:12 AM
Here is my "Over The Top" routine. :-o
Code: [Select]
;;;=======================[ getpoint_only.lsp ]=======================
;;; Author: Charles Alan Butler
;;; Version:  1.0 Jun. 16, 2005
;;; Purpose: Only get a point, do not return without one
;;; Sub_Routines: -None
;;; Arguments: -msg  string containing the prompt message
;;; Usage:  (getpoint_only msg)
;;; Returns: -the point picked
;;; Note: -User may enter a point at the command line 10,10 or 10,10,0
;;;====================================================================
;;  This is an attempt at preventing the user from entering a distance
;;  which ACAD uses with the cursor direction to derive the new point.
(defun getpoint_only (msg / pt loop fuzz lastp)
  (setq loop  t
        fuzz  0.0001
        lastp (getvar "lastpoint")
  )
  (setq msg (strcat "\n" (cond (msg) ("Please select a point: "))))
  ;;  in case the user pressed escape last time here
  (if (< fuzz (distance '(1.9999 1.9999 0.0) (getvar "lastpoint")))
    (setvar "lastpoint" '(1.9999 1.9999 0.0))
    (setvar "lastpoint" '(1.8999 1.8999 0.0))
  )
  (while loop
    ;;  pt must meet the following requirements
    (if (and (setq pt (getpoint msg))    ; not nil
             (listp pt)                  ; must be a list
             ;;  do not allow bad numeric entry
             (< fuzz (distance pt (getvar "lastpoint")))
        )
      (setq loop nil) ; ok to exit, else
      (prompt "\nYou must select a point, please try again. ")
    )
  )
  (setvar "lastpoint" lastp)
  pt
)
Title: Re: Testing for a point
Post by: CAB on March 10, 2006, 12:11:41 PM
This is one way
Code: [Select]
(if (not (and pnt (listp pnt) (=(length pnt) 3)))
  (options)
)

or like tim said
Code: [Select]
(if (and pnt (listp pnt))
  (do your point stuff)
  (options)
)
Title: Re: Testing for a point
Post by: T.Willey on March 10, 2006, 12:20:49 PM
or like tim said
Code: [Select]
(if (and pnt (listp pnt))
  (do your point stuff)
  (options)
)
I didn't say it that direct.  I forgot all about "listp" Alan.  :wink:
I take the credit if you want to give it to me though.  :angel:
Title: Re: Testing for a point
Post by: LE on March 10, 2006, 01:21:32 PM
Another...

Code: [Select]
(defun pt? (pt)
  (and (vl-consp pt)
       (= (length pt) 3)
       (vl-every '(lambda (coord) (= (type coord) 'real)) pt)))
Title: Re: Testing for a point
Post by: zoltan on March 10, 2006, 06:45:33 PM
That's a good one, Luis.

At the end of the day, I would end up naming the function PointP, but that's just me :)

By the way, could'nt a point be a list of integers also?
Title: Re: Testing for a point
Post by: Kerry on March 10, 2006, 06:51:03 PM
Yes, it can.
It can also be a 2d point, which is acceptable for most aCAD commands.
Title: Re: Testing for a point
Post by: Kerry on March 10, 2006, 07:02:11 PM
Perhaps ;
Code: [Select]
(defun point-p3D (pt)
  (and (vl-consp pt)
       (= (length pt) 3)
       (vl-every '(lambda (coord) 'numberp) pt)
  )
)

Code: [Select]
(setq 3D '(1 2 3))
(setq 2D '(4 5))

(point-p3D 3d )
(point-p3D 2d )

// kwb
Title: Re: Testing for a point
Post by: MP on March 10, 2006, 07:25:19 PM
Abused from another post (http://www.theswamp.org/index.php?topic=7464.msg93380#msg93380) --

Code: [Select]
(defun IsPoint ( point )
    (and
        (listp point)
        (< 1 (length point) 4)
        (vl-every 'numberp point)
    )
)

Code: [Select]
(defun Is3DPoint ( point )
    (and
        (listp point)
        (eq 3 (length point))
        (vl-every 'numberp point)
    )
)

(vl-every '(lambda (coord) 'numberp) pt)

Hmmm, digesting ...



Title: Re: Testing for a point
Post by: MP on March 10, 2006, 07:30:28 PM
I can't help but wonder if the initial inquiry would be moot if initget were invoked before the getpoint call.  Hmmm.
Title: Re: Testing for a point
Post by: Kerry on March 10, 2006, 07:45:15 PM

(vl-every '(lambda (coord) 'numberp) pt)

Hmmm, digesting ...

yep, supreme redundency :-)
Title: Re: Testing for a point
Post by: Kerry on March 10, 2006, 07:48:53 PM
Abused from another post (http://www.theswamp.org/index.php?topic=7464.msg93380#msg93380)

I've noticed recently there are a lot of conversations I've forgotten.
Title: Re: Testing for a point
Post by: MP on March 10, 2006, 08:36:56 PM
You're not alone Kerry.

The sleep medication I'm taking is really affecting my memory.

Speaking of such, humour that writes itself:  Over supper I was talking to my wife about said memory loss, and said something like "It's a little disconcerting how much the medication is affecting my memory".

She replies: "Really? What have you forgotten?"

<blink.mpg>

<crickets.wav>
Title: Re: Testing for a point
Post by: Kerry on March 10, 2006, 08:45:31 PM
(clapping-enthusiastic.wav>
Title: Re: Testing for a point
Post by: LE on March 10, 2006, 09:41:29 PM
By the way, could'nt a point be a list of integers also?

Yes...

Even a combination of integers and reals.... there is a better alternative in the post by MP.

I needed reals for that function... no remember why.... [real excuse #23214325423545343256432]