Rectangle corner points

Rectangle corner points

Anonymous
Not applicable
3,605 Views
11 Replies
Message 1 of 12

Rectangle corner points

Anonymous
Not applicable

Hi 

How to get rectangle all four corner points and center point (user select rectangle) using Autolisp.

Please help me.

 

0 Likes
Accepted solutions (2)
3,606 Views
11 Replies
Replies (11)
Message 2 of 12

marko_ribar
Advisor
Advisor

Untested though, but should work...

 

 

(defun c:rectanglepts ( / lw enx )
  (while
    (or
      (not (setq lw (car (entsel "\nPick RECTANGLE - LWPOLYLINE..."))))
      (if lw
        (or
          (/= (cdr (assoc 0 (setq enx (entget lw)))) "LWPOLYLINE")
          (/= 1 (logand 1 (cdr (assoc 70 enx))))
          (/= 4 (cdr (assoc 90 enx)))
          (vl-some '(lambda ( x ) (not (zerop x))) (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 42)) enx)))
        )
      )
    )
    (prompt "\nMissed or picked entity not rectangular LWPOLYLINE...")
  )
  (setq *pts* (mapcar '(lambda ( p ) (trans (list (car p) (cadr p) (cdr (assoc 38 enx))) lw 0)) (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) enx))))
  (prompt "\nPoints of rectangle in WCS are : ") (princ *pts*)
  (prompt "\nCenter point of rectangle in WCS is : ") (princ (setq *cen* (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) (car *pts*) (caddr *pts*))))
  (prompt "\nPoint list is stored in global variable *pts* and center point in global variable *cen*... You can call them with : !*pts* or !*cen*...")
  (princ)
)

 

 

HTH., M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 3 of 12

Kent1Cooper
Consultant
Consultant

This should [untested] give you the four corners and midpoint [in World Coordinates, whether or not the rectangle is drawn in them], and would work on both "heavy" [including 3D] and "lightweight" Polylines.  It assumes that you select a closed 4-vertex Polyline of straight line segments only, but with more code could be made to verify all that, and if it's a 3D Polyline whether it's planar if it should be, and even whether it's actually rectangular.  It would leave the corners as a list of 4 points in the 'corners' variable, and the midpoint in the 'midpt' variable, for you to use as you like.

 

(vl-load-com); if needed

(defun C:RCM ; = Rectangle Corners & Midpoint

  (/ pl v1 v3)
  (setq

    pl (car (entsel "\nSelect rectangle: "))

    corners

      (list

        setq v1 (vlax-curve-getPointAtParam pl 0)

        (vlax-curve-getPointAtParam pl 1)

        setq v3 (vlax-curve-getPointAtParam pl 2)

        (vlax-curve-getPointAtParam pl 3)

      ); list

    midpt (mapcar '/ (mapcar '+ v1 v3) '(2 2 2))

  ); setq

  (princ)

); defun

Kent Cooper, AIA
0 Likes
Message 4 of 12

Anonymous
Not applicable

Thank you Kent Cooper, AIA

looks sooooo good very simple to ... for information, I am working in 2D only

But I am not able to use the values for further programming, please resolve it (!v1 and !v3 showing nil) i want to utilize car cadr of lower left corner & right upper corners points of rectangle for further programing.

 

or in a simple manner i want to insert blocks(user defined) at all four corners and at center of the rectangle please help me.

 

0 Likes
Message 5 of 12

Anonymous
Not applicable

Thank you Marko Ribar

I am getting left top corner point pt list (*pts*) & centre pt list (*cen*) please provide coding to get diagonal corner point pt list(right bottom corner point pt list) please.

0 Likes
Message 6 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... (!v1 and !v3 showing nil) i want to utilize car cadr of lower left corner & right upper corners points of rectangle for further programing.

 

or in a simple manner i want to insert blocks(user defined) at all four corners and at center of the rectangle please help me.


Those v1 and v3 variables are localized, which means they don't live past the end of the routine.  Those two [the 1st and 3rd vertices, being opposite corners] are saved to those variables only for use in calculating halfway between them for the midpoint [that's why the other two are not saved as their own variables].  But I hope you understand that those two will not always be the lower left and upper right corners.

 

You don't need them.  The corners [all  of them] are in the corners variable, which is not  localized, so it will still be there afterwards, along with the midpt variable for the center.  You can do something like:

 

(command

   "_.insert" "YourBlockName" (nth corners 0) "" "" "" ;; <-- this happens to be v1

   "_.insert" "YourBlockName" (nth corners 1) "" "" ""

   "_.insert" "YourBlockName" (nth corners 2) "" "" "" ;; <-- this happens to be v3

   "_.insert" "YourBlockName" (nth corners 3) "" "" ""

   "_.insert" "YourBlockName" midpt "" "" ""

)

 

[If your Block is defined for uniform scaling, remove one of the "" Enters from each line.  If you want to use different Blocks at the different places, obviously edit in different Block names.]

 

If you really need to know which corners are lower left and upper right specifically, that is not difficult.

Kent Cooper, AIA
0 Likes
Message 7 of 12

Anonymous
Not applicable

 

 

0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... please help me to get/extract pt list of all points as need car cadr of those for further. .... 


The corners variable is  a "pt list of all points."  Is there some reason you want the X and Y coordinates of any of them separately, rather than use just the "whole" points?  If so, to get the X coordinate of the first vertex:

 

(car (nth corners 0))

 

and I assume you can figure out the rest.

Kent Cooper, AIA
0 Likes
Message 9 of 12

Anonymous
Not applicable

@Kent1Cooper 

Tried (car (nth 0 corners)) to get x min and y min also x max & y max but it is not working.

-----------------------------------------------------

!min #<SUBR @000001ba6b62fae8 MIN>

This error showing 

------------------------------------------------------

0 Likes
Message 10 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

Sorry -- I had a parentheses problem in the original [that's what I get for not testing it].  Try this:

 

(defun C:RCM ; = Rectangle Corners & Midpoint
  (/ pl v1 v3)
  (setq
    pl (car (entsel "\nSelect rectangle: "))
    corners
      (list
        (setq v1 (vlax-curve-getPointAtParam pl 0))
        (vlax-curve-getPointAtParam pl 1)
        (setq v3 (vlax-curve-getPointAtParam pl 2))
        (vlax-curve-getPointAtParam pl 3)
      ); list
    midpt (mapcar '/ (mapcar '+ v1 v3) '(2 2 2))
  ); setq
  (princ)
); defun

 

And you don't need to pull the individual X and Y coordinates out of all the corners to find the minimum and maximum of each:

 

(apply 'min (mapcar 'car corners)); minimum X coordinate
(apply 'max (mapcar 'car corners)); maximum X
(apply 'min (mapcar 'cadr corners)); minimum Y
(apply 'max (mapcar 'cadr corners)); maximum Y

 

But remember that if the rectangle is not orthogonally oriented, combining those together will give points that are not at the corners of the rectangle, but at the corners of its bounding box.

 

Kent Cooper, AIA
0 Likes
Message 11 of 12

Sea-Haven
Mentor
Mentor
Accepted solution

"or in a simple manner i want to insert blocks(user defined) at all four corners and at center of the rectangle please help me."

 

I use this
(setq plent (entsel "\nPick rectang"))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent)))))
 
So for the question of insert block can use a foreach pt co-ord

 

The mapcar mid now (nth 0 and (nth 2

 

GP has a nice centroid of a pline ie not a rectang that would be my approach as a wider solution.

 

0 Likes
Message 12 of 12

Anonymous
Not applicable

0 Likes