TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Kate M 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:
; 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?
;;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)
)
-
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.
-
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.
-
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
(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)
-
Okay, I changed it:
;;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:
; error: AutoCAD variable setting rejected: "SNAPBASE" nil
But I'd really like it to look like this:
;;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. :-))
-
If you only have one test (equal in your case) then use an If with Progn (for more that one action). So something like
;;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.
-
Just for fun. :-)
;;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)
-
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!!
-
Another one where Alan comes in at the end, and stills my glory. :x
Man.... :cry:
J/k Alan, nice alternative.
-
Tim,
Your code worked fine for me. I was just offering up another way to skin the cat. :?
-
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:
-
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.
-
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:)
-
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.
-
;; 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)
)
)
)
-
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.
-
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.