Jump to content

Lisp to create "rectangle selection effect (graphic)"


sadhu

Recommended Posts

Hello,

 

I'd like to include in a lisp routine the "rectangle selection effect" that exists in autocad. When you click any point on the screen and then a second point this effect appears.

 

Something like this but with a "rectangle effect" instead of a line.

 

(setq pt1 (getpoint "Select point 1... "))
(setq pt2 (getpoint pt1 "Opposite corner... "))

Link to comment
Share on other sites

Decided to do a daily practice:

(defun C:test ( / oldclp pt1 LoopFlag UserIn TypeUserIn ReturnChar vsz SS SSn in1 in2 )
(sssetfirst nil nil)
(while T
	(if (setq pt1 (getpoint "\nSpecify first point" ))
		(progn
			(setq oldclp (getvar 'clipromptlines))
			(setvar 'clipromptlines 1)
			(redraw)
			(princ "\nSpecify second point: ")
			(setq LoopFlag T)
			(while LoopFlag
				(setq UserIn (grread T))
				(setq TypeUserIn (car UserIn))
				(setq ReturnChar (cadr UserIn))
				(cond
					((= TypeUserIn 5) ; cursor is moved
						(princ "\nSpecify second point: ")
						(setq vsz (* (getvar 'viewsize) 10))
						(or
							(setq in1 (inters pt1 (polar pt1 (angtof "90") vsz) ReturnChar (polar ReturnChar (angtof "0") vsz)))
							(setq in1 (inters pt1 (polar pt1 (angtof "90") vsz) ReturnChar (polar ReturnChar (angtof "180") vsz)))
							(setq in1 (inters pt1 (polar pt1 (angtof "270") vsz) ReturnChar (polar ReturnChar (angtof "0") vsz)))
							(setq in1 (inters pt1 (polar pt1 (angtof "270") vsz) ReturnChar (polar ReturnChar (angtof "180") vsz)))
						)
						(or
							(setq in2 (inters pt1 (polar pt1 (angtof "0") vsz) ReturnChar (polar ReturnChar (angtof "90") vsz)))
							(setq in2 (inters pt1 (polar pt1 (angtof "0") vsz) ReturnChar (polar ReturnChar (angtof "270") vsz)))
							(setq in2 (inters pt1 (polar pt1 (angtof "180") vsz) ReturnChar (polar ReturnChar (angtof "90") vsz)))
							(setq in2 (inters pt1 (polar pt1 (angtof "180") vsz) ReturnChar (polar ReturnChar (angtof "270") vsz)))
						)
						(if (and pt1 in1 in2 ReturnChar)
							(progn
								(redraw)
								(grvecs
									(list
										1 pt1 in1
										1 pt1 in2
										1 ReturnChar in1
										1 ReturnChar in2
									)
								)
							)
							(redraw)
						)
					)
					((= TypeUserIn 3) ; LMB is pressed
						(if oldclp (setvar 'clipromptlines oldclp))
						(setq LoopFlag nil)
						(if SS (progn (setq SSn SS) (sssetfirst nil nil)))
						(if (setq SS (ssget "_CP" (list pt1 in1 ReturnChar in2)))
							(progn
								(setq SS (acet-ss-union (list SSn SS))) ; requires express tools!
								(sssetfirst nil SS)
							)
							(setq SS nil)
						)
					)
					(T nil)
				);cond			
			);while LoopFlag
		)
	)
)
(princ)
)


Its based on what I've learned from Tharwat, Lee Mac, CAB...

Link to comment
Share on other sites

Its based on what I've learned from Tharwat, Lee Mac, CAB...

 

Quickie one ;)

 

(defun c:sel (/ p1 gr p2 p3)
 ;; Tharwat - Emulate cursor selection set	;;
 (if (setq p1 (getpoint "\n First point :"))
   (while
     (eq (car (setq gr (grread t 15 0))) 5)
      (redraw)
      (grvecs (list -3 p1 (setq p2 (list (car (cadr gr)) (cadr p1) 0.)) p2 (cadr gr)
                           (cadr gr)  (setq p3 (list (car p1) (cadr (cadr gr)) 0.))
                           p3
                           p1
              )
      )
   )
 )
 (redraw)
 (princ)
)

Link to comment
Share on other sites

Quickie one ;)

 

(defun c:sel (/ p1 gr p2 p3)
 ;; Tharwat - Emulate cursor selection set	;;
 (if (setq p1 (getpoint "\n First point :"))
   (while
     (eq (car (setq gr (grread t 15 0))) 5)
      (redraw)
      (grvecs (list -3 p1 (setq p2 (list (car (cadr gr)) (cadr p1) 0.)) p2 (cadr gr)
                           (cadr gr)  (setq p3 (list (car p1) (cadr (cadr gr)) 0.))
                           p3
                           p1
              )
      )
   )
 )
 (redraw)
 (princ)
)

 

Nice one!

Now I see that I might had to use the X and Y values to find pt2 and pt3. Still my (inters) method might work as an alternative way.

Also I didn't knew that grvecs could work this way, though I was reading the HELP file.

...and your code is alot shorter :)

Link to comment
Share on other sites

Always try to give the user an option to choose which way to go with or to cancel the program safely, but in your program you forced the user to hit ESC button to end the program.

Link to comment
Share on other sites

Always try to give the user an option to choose which way to go with or to cancel the program safely, but in your program you forced the user to hit ESC button to end the program.

 

Yes, I know.. this is just a habbit of mine (I test the code's behaviour multiple times in ACAD, before I decide to post it).. and because I'm lazy to re-run the command.

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