Jump to content

Rectangle drawing LISP – help needed


the-trooper

Recommended Posts

Hello everyone

 

I 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!

Link to comment
Share on other sites

(defun c:rf (/ pt1 pt2)
  (setq pt1 (getpoint "PT1:"))
  (setq pt2 (getpoint "PT2:"))
  (command "_rectangle" "_from" [color="Red"]"_non"[/color] pt1  [color="Red"]"_non" [/color]"@3,-3" "_from" [color="Red"]"_non"[/color] pt2  [color="Red"]"_non"[/color] "@-3,3")
 )

Link to comment
Share on other sites

_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.

Link to comment
Share on other sites

 
(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")
)

 
(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

)

 

Marco, its great that you are learning :D

 

Just a few things:

 

(defun c:rf (/ [color=Red][b]oldosmode[/b][/color] pt1 pt2)
  (setq pt1 (getpoint "PT1:"))
  (setq pt2 (getpoint "PT2:"))
  (setq oldosmode (getvar "osmode"))
  ([b][color=Red]setvar[/color][/b] [color=Red][b]"[/b][/color]osmode[b][color=Red]"[/color][/b] 0)
  (command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")
([color=Red][b]setvar[/b][/color] [b][color=Red]"[/color][/b]osmode[b][color=Red]"[/color][/b] oldosmode)
[b][color=Red](princ) ; Exit Cleanly[/color][/b]
)

Link to comment
Share on other sites

:) 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?

Link to comment
Share on other sites

:) 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... :unsure:

 

As for the "princ", you missed it in your first code... shout if I'm being too picky :P

Link to comment
Share on other sites

 
(princ) ; Exit Cleanly

What's wrong here?

 

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. ^^

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...