Rectangle drawing LISP – help
Hello everyoneI tried to make a lisp that would draw a rectangle 3,-3 points from the first point i select, and -3,3 from the second.
But, something weird id going on, this is my failed attempt:
(defun c:rf (/ pt1 pt2)(setq pt1 (getpoint "PT1:"))(setq pt2 (getpoint "PT2:"))(command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3") )
If i select points in "empty" space it works. But when i select specific points on objects (with OSNAP) it draws a rectangle without offset of 3, ie. precisely on selected points.
Thanks in advance! (defun c:rf (/ pt1 pt2)(setq pt1 (getpoint "PT1:"))(setq pt2 (getpoint "PT2:"))(command "_rectangle" "_from" "_non" pt1"_non" "@3,-3" "_from" "_non" pt2"_non" "@-3,3") ) Thanks, works! _NON is like "disable snappoints" for 1 action only.
For commands, use "_." as prefix to suit other languages. I was told, this to use it.
You could use some coding to set the old variable:
(defun c:rf (/ pt1 pt2)(setq pt1 (getpoint "PT1:"))(setq pt2 (getpoint "PT2:"))(setq oldosmode (getvar "osmode"))(setq osmode 0)(command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")(setq osmode "oldosmode"))
Or this one:
(defun c:rf (/ pt1 pt2)(setq pt1 (getpoint "PT1:") pt2 (getpoint "PT2:") oldosmode (getvar "osmode") ;;remember old osnapmode osmode 0 ;; set the osnapmode to no osnappoints )(command "._rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")(setq osmode "oldosmode") ;; back to the previous osnapmode(princ) ;;clean exit)
I'm not that great programmer but I have learned a lot in here so I am glad to say that this is one of my first contibutes to the forum.
Cheers.
Marco, its great that you are learning
Just a few things:
(defun c:rf (/ oldosmode pt1 pt2)(setq pt1 (getpoint "PT1:"))(setq pt2 (getpoint "PT2:"))(setq oldosmode (getvar "osmode"))(setvar "osmode" 0)(command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")(setvar "osmode" oldosmode)(princ) ; Exit Cleanly) Lee shouldn't you be seriously sweating with your exam?
You are right, some stupid mistakes I made. Did test it but it ran... at least I thought...
(defun c:rf (/ oldosmode pt1 pt2)
Yes, it should be cleared after command's ready.
(setvar "osmode" 0)
We'll ehm... yes.
(princ) ; Exit Cleanly
What's wrong here?
Yes, just had the exam this morning, could've gone better I think... but it was OK...
As for the "princ", you missed it in your first code... shout if I'm being too picky
Nothing "wrong" per se, but it's sort of the programming equivalent of "good manners" to null returns from a function, unless you want it to return something. A function returns the last argument it evaluated, which in your code would have been the return of (setvar "osmode" oldosmode), which means, your old osnap setting.
In this case, since it's a command, it doesn't really matter much. When you run the command, you'll probably see a number printed indicating your old OSMODE. Since this number isn't hurting anyone or anything, it's fine for it to be there. If you put that (princ) before the parentheses to close the defun, however, it'll return what (princ) returns instead of what (setvar) returns, that is to say, nil.
Just something to be aware of. It's a good habit to get into. ^^
页:
[1]