Author Topic: Compare points  (Read 4960 times)

0 Members and 1 Guest are viewing this topic.

Kate M

  • Guest
Compare points
« on: July 24, 2006, 03:33:07 PM »
I'm trying to write a tiny little routine that will set the SNAPBASE to a point I pick if it's set to 0,0, and will reset it to 0,0 if it's not. Basically a toggle, with one of the "positions" user-defined.

I thought an IF statement would do it, since I'm only checking one thing, but it keeps crashing at the IF line:
Quote
; error: bad function: 0.0

I have a feeling it's a problem with my conditional, but I'm not quite sure how to fix it. Am I doing the list wrong?

Code: [Select]
;;Snapbase Reset Routine
(defun C:SB ()
  (setq useros (getvar "OSMODE"))                          ;Stores current osnaps
    (setvar "OSMODE" 0)                                    ;Turns off osnaps
  (setq mybase (getvar "SNAPBASE"))                        ;Stores current SNAPBASE
  (if (equal mybase (0.0 0.0 0.0))
    (setvar "SNAPBASE" (getpoint "\nSelect Snap Origin: ") ;If equal to 0,0, selects new
    )
    (setvar "SNAPBASE" (0.0 0.0)                           ;If not 0,0, resets to origin
    )
  )                                                        ;End IF
  (setvar "OSMODE" useros)                                 ;Restores osnaps
  (princ)
)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #1 on: July 24, 2006, 03:45:06 PM »
Your if statement won't work as written since you don't tell it that (0. 0 0.0 0.0) is a list.  Since you know all the elements, then you can just single quote it, '(0.0 0.0 0.0).  One thing I noticed, is that when you get snap base (at least on mine 2006) it returns a 2d point.  So you might want to check '(0.0 0.0) instead.  Hope that helps.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kate M

  • Guest
Re: Compare points
« Reply #2 on: July 24, 2006, 03:54:51 PM »
Ah -- the apostrophe. Didn't know about that. That fixed that problem...

Now it's crashing because, as you mentioned, SNAPBASE is an XY variable, and "getpoint" returns XYZ. Looks like this won't be quite as simple as I thought. :-)

Thanks for the quick answer -- I'll keep playing with it, and come back if I run into something else.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #3 on: July 24, 2006, 04:01:04 PM »
Ah -- the apostrophe. Didn't know about that. That fixed that problem...

Now it's crashing because, as you mentioned, SNAPBASE is an XY variable, and "getpoint" returns XYZ. Looks like this won't be quite as simple as I thought. :-)

Thanks for the quick answer -- I'll keep playing with it, and come back if I run into something else.
You're welcome.  Since it only wants the x and y values of the point, you can pass it like

Code: [Select]
(if (setq tmpPt (getpoint "\nSelect Snap Origin: "))
 (setvar "snapbase" (list (car tmpPt) (cadr tmpPt)))
)
or you can just issue the command version of snapbase like

(command "_.snapbase" pause)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kate M

  • Guest
Re: Compare points
« Reply #4 on: July 24, 2006, 04:22:45 PM »
Okay, I changed it:

Code: [Select]
;;Snapbase Reset Routine
(defun C:SB ();(/ mybase)
;(princ "Snapbase Reset loaded. Type SB to run.")           ;Print description
  (setq mybase (getvar "SNAPBASE"))                        ;Stores current SNAPBASE
  (if (equal mybase '(0.0 0.0))
    (setvar "SNAPBASE" (command "_.snapbase" pause)        ;If equal to 0,0, selects new
    )
    (setvar "SNAPBASE" '(0.0 0.0)                          ;If not 0,0, resets to origin
    )
  )                                                        ;End IF
  (princ)
)

And it seems to work, but it gives me an error:
Quote
; error: AutoCAD variable setting rejected: "SNAPBASE" nil

But I'd really like it to look like this:
Code: [Select]
;;Snapbase Reset Routine
(defun C:SB ();(/ mybase)
;(princ "Snapbase Reset loaded. Type SB to run.")           ;Print description
  (setq mybase (getvar "SNAPBASE"))                        ;Stores current SNAPBASE
  (cond
    (equal mybase '(0.0 0.0))
      (setvar "SNAPBASE" (command "_.snapbase" pause))    ;If equal to 0,0, selects new
    (equal mybase '(0.0 0.0))
      (setvar "SNAPBASE" '(0.0 0.0))                       ;If not 0,0, resets to origin
      (princ "Snapbase reset to 0,0")
  )                                                        ;End COND
  (princ)
)

I switched to the COND so I could have multiple actions per condition -- I haven't run it yet, 'cause I don't know what the opposite of "equal" is. :-) Am I on the right track or should I go to an IF/PROGN combination? (I've been reading Afralisp. :-))

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #5 on: July 24, 2006, 04:38:10 PM »
If you only have one test (equal in your case) then use an If with Progn (for more that one action).  So something like
Code: [Select]
;;Snapbase Reset Routine
(defun C:SB (/ mybase)
;(princ "Snapbase Reset loaded. Type SB to run.")           ;Print description
  (setq mybase (getvar "SNAPBASE"))                        ;Stores current SNAPBASE
  (if (equal mybase '(0.0 0.0) 0.001)
      (command "_.snapbase" pause)                          ;If equal to 0,0, selects new
      (progn
        (setvar "SNAPBASE" '(0.0 0.0))                         ;If not 0,0, resets to origin
        (princ "Snapbase reset to 0,0")
      )
  )                                                                        ;End IF
  (princ)
)

One of the good things about using Equal, you can test with a fuzz factor.  One thing about comparing points, is they are very VERY rarely equal, so the fuzz factor is a big help.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare points
« Reply #6 on: July 24, 2006, 05:13:14 PM »
Just for fun. :-)
Code: [Select]
;;Snapbase Reset Routine
(defun c:sb (/ mybase pt)
  (or
    (and
      (> (distance (setq mybase (getvar "SNAPBASE")) '(0.0 0.0)) 0.001)
      (setvar "SNAPBASE" '(0.0 0.0))
      (not (prompt "\nSnapbase reset to 0,0"))
    )
    (and
      (setq pt (getpoint "\nPick or enter the new snapbase."))
      (setvar "SNAPBASE" (list (car pt) (cadr pt)))
      (prompt (strcat "\nSnapbase reset to " (rtos (car pt)) "," (rtos (cadr pt))))
    )
  )
  (princ)
)
(princ "Snapbase Reset loaded. Type SB to run.")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kate M

  • Guest
Re: Compare points
« Reply #7 on: July 24, 2006, 05:18:59 PM »
Wow, you guys are awesome. :love:

I don't know why mine didn't work, but CAB's works great, so I'm keeping it. 8-)

Thanks again!!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #8 on: July 24, 2006, 05:24:26 PM »
Another one where Alan comes in at the end, and stills my glory. :x

Man....  :cry:

J/k Alan, nice alternative.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare points
« Reply #9 on: July 24, 2006, 05:33:03 PM »
Tim,
Your code worked fine for me. I was just offering up another way to skin the cat.  :?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #10 on: July 24, 2006, 05:41:33 PM »
Tim,
Your code worked fine for me. I was just offering up another way to skin the cat.  :?
I know Alan, but
I don't know why mine didn't work, but CAB's works great, so I'm keeping it. 8-)
:cry:    :lmao:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Bob Wahr

  • Guest
Re: Compare points
« Reply #11 on: July 24, 2006, 05:44:30 PM »
Another one where Alan comes in at the end, and stills my glory. :x

Man....  :cry:

J/k Alan, nice alternative.
Welcome to my world

The plus side to that is that when someone comes along and trumps you like that, you can learn a lot.

Kate M

  • Guest
Re: Compare points
« Reply #12 on: July 25, 2006, 08:39:18 AM »
Whoops -- sorry, Tim. I tried to modify my code to match yours (instead of copy/paste), and ended up with an extra setvar statement. :oops:

At any rate, now I have two routines that work, and I learned a few things too. (Like pay attention when somebody tells you how to fix your code. :roll:)
« Last Edit: July 25, 2006, 08:43:48 AM by Kate M »

Joe Burke

  • Guest
Re: Compare points
« Reply #13 on: July 25, 2006, 10:06:22 AM »
Personally I think a fuzz factor is not appropriate in this case.
Either snap base is 0,0 (default) or it isn't. Proceed on that basis.
 
;; Reset Snap Base
;; returns the current snap base point
(defun c:RSB (/ pt)
  (if
    (and
      (equal '(0.0 0.0) (getvar "snapbase"))
      (setq pt (getpoint "\nSelect snap origin point: "))
    )
    (setvar "snapbase" (list (car pt) (cadr pt)))
    (setvar "snapbase" '(0.0 0.0))
  )
)

I fail to see the reason for Alan's (or (and... structure.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Compare points
« Reply #14 on: July 25, 2006, 10:40:35 AM »
Code: [Select]
;;  Snap Base Toggle

(defun c:SBT ( / snapbase x y )
    (setvar "snapbase"
        (if
            (and
                (equal '(0.0 0.0) (getvar "snapbase"))
                (setq snapbase (getpoint "\nSnapBase: "))
            )
            (mapcar 'set '(x y) snapbase)
           '(0.0 0.0)
        )
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare points
« Reply #15 on: July 25, 2006, 10:59:44 AM »
Quote from: Joe Burke
I fail to see the reason for Alan's (or (and... structure.

Just playing around with OR & AND Joe, not trying to make the perfect lisp.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Compare points
« Reply #16 on: July 25, 2006, 11:10:07 AM »
Whoops -- sorry, Tim. I tried to modify my code to match yours (instead of copy/paste), and ended up with an extra setvar statement. :oops:

At any rate, now I have two routines that work, and I learned a few things too. (Like pay attention when somebody tells you how to fix your code. :roll:)
It's all good Kate.  I was just giving Alan a hard time (started from another post at AUGI).  As long as you learn something, then it is all worth it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.