3-point rectangle: Lee Mac Script

3-point rectangle: Lee Mac Script

nathanwheeler27
Advocate Advocate
2,671 Views
12 Replies
Message 1 of 13

3-point rectangle: Lee Mac Script

nathanwheeler27
Advocate
Advocate

Hi,

 

I am using the Lee Mac script: http://www.lee-mac.com/3pointrectangle.html

 

Can this be modified so that a key value can be entered for the 3rd point? It lets you specify a distance for the 2nd point (length of rectangle) but I also would like it to allow a value for the 3rd point (width of rectangle).

 

Any help would be appreciated.

Thank you.

0 Likes
Accepted solutions (1)
2,672 Views
12 Replies
Replies (12)
Message 2 of 13

hak_vz
Advisor
Advisor

@nathanwheeler27  You should ask Lee Mac to add that option to his code.

There is option to answer third point input using relative coordinate (@) but it don't work for me.

 

Code to draw rectangle of certain width  (that without using grreed and dinamic draw)  is simple

 

 

(defun c:wrec nil (wrec))
(defun wrec (/ p1 p2 ang width p3 p4)
(setq p1 (getpoint "\nFirst point >"))
(setq p2 (getpoint "\nSecond point >"))
(setq ang (- (angle p2 p1) (* pi 0.5)))
(setq width (getreal "\nRectangle width >"))
(setq p3 (polar p2 ang width))
(setq p4 (polar p1 ang width))
(command "pline" p1 p2 p3 p4 "C")
(princ)
)

Third point is created in counterclockwise direction to line from first to second point with desired width entered. So if width value is positive rectangle is drawn in that direction, if negative in opposite direction. Positive value draws upwards, negative downwards,

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 13

devitg
Advisor
Advisor

@hak_vz  please allow me to show my 2 cent improvement 

 

Trace rubber band from P1 , and use GETDIST from p2  , instead GETREAL , and also set OSMODE to 0 , and retrieve it to original osmode 

 

(defun c:wrec nil (wrec))

(defun wrec (/ p1 p2 ang width p3 p4)
(setq osmode (getvar 'osmode))
 (setvar 'osmode 0) 
  (setq p1 (getpoint "\nFirst point >"))
(setq p2 (getpoint p1 "\nSecond point >"))
(setq ang (- (angle p2 p1) (* pi 0.5)))
(setq width (getdist p2 "\nRectangle width >"))
(setq p3 (polar p2 ang width))
(setq p4 (polar p1 ang width))
(command "pline" p1 p2 p3 p4 "C")
  (setvar 'osmode osmode)
(princ)
)

 

Message 4 of 13

hak_vz
Advisor
Advisor

@devitgSetting osnap to 0 is good point.

 

Getdist allows us to pick point. For rectangle to pass through that point we would need perpendicular distance from that point to line defined with points p1 and p2. That's why I opted for getreal. Option to pick third point is already included in Lee's code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 13

devitg
Advisor
Advisor

@hak_vz ,  getdist return a value , not a point , GETPOINT return a point.

 

GETDIST  show the rubber band that GETREAL do not. 

 

You can type the REAL value with GETDIST 

 

0 Likes
Message 6 of 13

hak_vz
Advisor
Advisor

@devitg wrote:

@hak_vz ,  getdist return a value , not a point , GETPOINT return a point.

 

GETDIST  show the rubber band that GETREAL do not. 

You can type the REAL value with GETDIST 


Yes, getdist returns a value. User can pick a second point or type in a distance.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 13

nathanwheeler27
Advocate
Advocate

Thank you for that, if I can't get the dynamic (grread) version to work, I might have to use that. But I was really wanting the visual of the rectangle to show. 

 

Does anyone have a fix for that?

On Lee-Macs script, the 3rd point allows @x,y to be entered. I would think that could be converted to @distance?

0 Likes
Message 8 of 13

hak_vz
Advisor
Advisor

I'll add dynamic option to this code, but would also like you to contact Lee to add asked option to his code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 9 of 13

stevor
Collaborator
Collaborator

We have made many such tools since R9, here is a likey form,

if it actually gets attached

S
0 Likes
Message 10 of 13

nathanwheeler27
Advocate
Advocate

@hak_vz 

Thank you, I will contact him.

 

@stevor 

Drawing the rectangle is the easy part here, but using grread to make it dynamic is where I get lost. Lee Macs version has snapping so it is the closest thing I can find at the moment.

0 Likes
Message 11 of 13

hak_vz
Advisor
Advisor

@nathanwheeler27   Try this:

 

(defun c:wrec nil (wrec))

(defun wrec ( /  p1 p2 ang width p3 p4 vectorSide rad_to_deg perperdicular_from_point_to_line rv mp pp side di ch number);

(defun vectorSide (v1 v2 p / r *fuzz*)
(setq r (- (* (-(car v2)(car v1))(-(cadr p)(cadr v1)))
           (* (-(cadr v2)(cadr v1))(-(car p)(car v1)))
        )
	*fuzz* 1e-10
)
(cond ((equal r 0.0 *fuzz*) 0) (t (fix (/ (abs r) r))))
)

(defun rad_to_deg (rad)(* 180.0 (/ rad pi)))

(defun perperdicular_from_point_to_line (lin1 lin2 p / x1 y1 x2 y2 x3 y3 k m n ret)
;returns point on a line (line1 line2) as a perpendicular projection from point p
	(mapcar 'set '(x1 x2 x3) (mapcar 'car (list lin1 lin2 p)))
	(mapcar 'set '(y1 y2 y3) (mapcar 'cadr (list lin1 lin2 p)))
	(setq 
		m (-(*(- y2 y1) (- x3 x1))(*(- x2 x1) (- y3 y1)))
		n (+(* (- y2 y1)(- y2 y1))(*(- x2 x1)(- x2 x1)))
	)
	(cond 
		((/= n 0.0) 
			(setq 
				k (/ m n)
				ret (list(- x3 (* k(- y2 y1)))(+ y3 (* k(- x2 x1))))
			)
		)
	)
	ret
)

(setq p1 (getpoint "\nFirst point >"))
(setq p2 (getpoint p1 "\nSecond or vector point>"))
(setq number "")
(grdraw p1 p2 5 0)
(setq width (getreal "\nEnter rectangle width or enter for distance to second point > "))
(if (and width) (setq p2 (polar p1 (angle p1 p2) width)) (setq p2 p2))
(grdraw p1 p2 1 0)
(setq ang (- (angle p2 p1) (* pi 0.5)) off 0)
(prompt "Rectangle height or pick endpoint >")
(while (and (setq rv (grread T)) (= off 0) (or (= 2 (car rv))(= 5 (car rv))))
	(redraw)
	(cond 
		((= 5 (car rv))
			(setq mp (cadr rv))
			(setq pp (perperdicular_from_point_to_line p1 p2 mp))
			(setq side (vectorSide  p1 p2 mp))
			(setq di (* side (distance pp mp)))
			(setq p3 (polar p2 ang di))
			(setq p4 (polar p1 ang di))
			(grdraw p2 p3 1 0)
			(grdraw p3 p4 1 0)
			(grdraw p4 p1 1 0)
			(grdraw p1 p2 1 0)
		)
		((= 2 (car rv))
			(grdraw p1 p2 1 0)
				(if (and (>= (cadr rv) 44)(<= (cadr rv) 57) (/= (cadr rv) 47))
				  (progn
					(setq ch (chr (cadr rv)))
					(setq number (strcat number ch))
					(princ ch)
				  )
				)
				(if (= (cadr rv) 8)
				  (progn
					(princ (chr 8))
					(if (> (strlen number) 1)(setq number (substr number 1 (- (strlen number) 1))))
				  )
				)
				
				(if (or (= 13 (cadr rv))(= 32 (cadr rv)))
						(setq number (vl-string-subst "." "," number) di (atof number) p3 (polar p2 ang di) p4 (polar p1 ang di) off 1)
					
				)
		)
	)
)
(command "pline" p1 p2 p3 p4 "C")
(redraw)
(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 12 of 13

nathanwheeler27
Advocate
Advocate

Thank you for all that work. I don't have time right now to see if I can work out these things, but these are some changes I need to look into:

  1. It looks like there are two ways to use this:
    1. Pick basepoint -> pick a point that sets the angle -> type in the width -> type in the height
    2. Pick basepoint -> pick angle and width -> hit enter -> then you can only drag to specify the height
    3. I would like to change this so its Pick basepoint -> pick angle and width (one action) -> type in height or drag to specify height. 
  2. From what I can tell, it can be a little buggy. For example, a 12x12 box forms a triangle.
  3. I don't think it will be that big of a deal, but it would be nice to utilize Lee Macs grread snapping functions so picking a point for the height can snap to other points. That way you don't always have to type the height.
  4. Changing it all feet and inches on input. Also not really important.
0 Likes
Message 13 of 13

nathanwheeler27
Advocate
Advocate
Accepted solution

Lee Mac just updated his script to allow key input for the width and length of the rectangle.

 

Thanks for everyone's help here!

0 Likes