Jump to content

Recommended Posts

Posted

Hi. This code create rectangle with 3 ways

 

1) By picking 3 points

2) Pick 2 points and give the length of the side (to the direction you want)

3) Pick 2 points and create rectangle Left or Right of the p1,p2 line

 

The problem is  option 1 and 2 because  we have p1,p2,p3 , and p1p2 line is not vertical with p2p3 line.

 

Can anyone help me to adjust given p3 (as depth of the rectangle)   and create a p4  incase  p2p4 line be vertical to p1p2 line?

 

(defun c:test (/ foo _dist p1 p2 p3 ang)
  (defun foo (l)
    (entmake
      (append '((0 . "LWPOLYLINE") (100 . "AcDbEntity") (100 . "AcDbPolyline") (90 . 4) (70 . 129))
              (mapcar (function (lambda (p) (cons 10 (reverse (cdr (reverse (trans p 1 0))))))) l)
      )
    )
  )

  (defun _dist (a b) (distance (list (car a) (cadr a)) (list (car b) (cadr b))))

  (if (and (setq p1 (getpoint "\nGive point1: "))
           (setq p2 (getpoint p1 "\nGive point2: "))
           (not (grdraw p1 p2 3 1))
      )
    (if (setq p3 (initget 0 "Left Right")
              p3 (getpoint p2 "\nGive point3 or length of the side or square [Left (L)/ Right (R)]: ")
        )
      (cond ((vl-consp p3) (foo (list p1 p2 p3 (polar p3 (angle p2 p1) (_dist p1 p2)))))
            ((eq (type p3) 'STR)
             (cond
               ((eq p3 "Left") (setq ang (+ (/ pi 2.) (angle p1 p2))))
               ((eq p3 "Right") (setq ang (+ (* pi 1.5) (angle p1 p2))))
             )
             (foo (list p1 p2 (polar p2 ang (_dist p1 p2)) (polar p1 ang (_dist p1 p2))))
            )
      )
    )
  )
  (redraw)
  (princ)
)

 

 

Thanks

  • mhy3sx changed the title to Help: 3 point rectangle lisp
Posted

Wait, what result do you want?

 

- Typing L or R makes a square.  You're happy with that one, right?

 

- giving a third point, or giving a length gives a trapezium.

You want a rectangle instead, right?

Posted

Hi Emmanuel Delay. Yes I want rectangle

Posted (edited)

Using the rectang command instead of making polylines will create a right angled polygone (rectangle or square)

 

Something like this:

 

 

(defun c:rectest ( / templine )
  (defun RtD (r) (* 180.0 (/ r pi)))
  (setq p1 (getpoint "Select Point 1"))
  (princ "\nSelect point for angle ")
  (command "line" p1 pause "") ; gives a visual idea of angle
  (setq templine (entlast))
  (setq p2 (cdr (assoc 11 (entget (entlast)))))
  (entdel templine)
  (setq p1-p2-ang (RtD (angle p1 p2)))
  (command "rectang" p1 "R" p1-p2-ang pause )
) ; end defun

 

Can use the Rectang options such as Dimensions to specify face lengths, of select corners using a mouse click, entering an absolute point (eg 10,10 ) or a relative point (eg @10,10)

 

Edited by Steven P
Posted

Only problem with mine above is it sets the Rectangle angle to the new angle, ideally I would like it reset to what it was before... anyone know what the system variable is?

Posted

Keeping mostly to the original code...

 

Instead of getpoint I use getdist.

Then you can give a point or enter a distance.

 

Now, rectangle to the left or to the right... A positive distance will make a rectangle to the right as seen from p1-p2 line. 

- If you flip p1 and p2 you'll get the rectangle to the other side.

- Or you enter a negative distance, this also flips the rectangle

 


(defun c:test (/ foo _dist p1 p2 p3 ang p3_ p4_)
	;; #@param l is a list of points.  Foo makes any closed polyline.
  (defun foo (l)
    (entmake
      (append '((0 . "LWPOLYLINE") (100 . "AcDbEntity") (100 . "AcDbPolyline") (90 . 4) (70 . 129))
              (mapcar (function (lambda (p) (cons 10 (reverse (cdr (reverse (trans p 1 0))))))) l)
      )
    )
  )

  (defun _dist (a b) (distance (list (car a) (cadr a)) (list (car b) (cadr b))))

  (if (and (setq p1 (getpoint "\nGive point1: "))
           (setq p2 (getpoint p1 "\nGive point2: "))
           (not (grdraw p1 p2 3 1))
      )
    (if (setq p3 (initget 0 "Left Right")
              p3 (getdist p2 "\nGive point3 or length of the side or square [Left (L)/ Right (R)]: ")
        )
      (cond ((vl-consp p3) (foo (list p1 p2 p3 (polar p3 (angle p2 p1) (_dist p1 p2)))))
            ((eq (type p3) 'STR)
             (cond
                ((eq p3 "Left") 
					(setq ang (+ (/ pi 2.) (angle p1 p2))) 
				)
                ((eq p3 "Right") 
					(setq ang (+ (* pi 1.5) (angle p1 p2)))
				)

              )
				(setq p3_ (polar p2 ang (_dist p1 p2))) 
				(setq p4_ (polar p1 ang (_dist p1 p2)))  
				(foo (list p1 p2 p3_ p4_))
            )
			;; p3 is now a distance
			((eq (type p3) 'REAL)
				(setq ang (+ (* pi 1.5) (angle p1 p2)))
				(setq p3_ (polar p2 ang p3))
				(setq p4_ (polar p1 ang p3))
				(foo (list p1 p2 p3_ p4_))
			)			
      )
    )
  )
  (redraw)
  (princ)
)

 

  • Like 1
Posted

Thanks, Emmanuel Delay works perfect now 😀

  • Like 1

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