Get Rectangle Center Point

Get Rectangle Center Point

Anonymous
Not applicable
2,630 Views
11 Replies
Message 1 of 12

Get Rectangle Center Point

Anonymous
Not applicable

Hello, I need a function which "finds the center point of a rectangle" and also, store distance from center to width in a variable and distance from center to length in another variable.  Thanks!

 

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

ВeekeeCZ
Consultant
Consultant

A rectangle from a polyline? Or lines? Better post a dwg example.

And which would be the width and which length? Just general w<l?

0 Likes
Message 3 of 12

Anonymous
Not applicable

A Polyline rectangle. Any width and length. 

I want to draw a circle with its center as rectangle center with below function. But circle radius shouldn't go outside rectangle. So I want to use distances from sides to limit radius size (if that's the right approach)!

 

(defun C:DRWCRC ()

  	(initget 1)
	(setq c (getpoint "\nChoose Center:"))
	(initget 7)
	(setq rad (getdist c "\nRadius Size:"))
	
	(command "circle" c rad)
 
        (princ)

)

 

 

0 Likes
Message 4 of 12

ВeekeeCZ
Consultant
Consultant
Accepted solution

Sure. Your func is not bad.. but this one is a bit better - less dependent on environment settings.

 

(vl-load-com)

(defun c:CircleInscribed ( / s e p d r)

  (if (setq s (ssget '((0 . "LWPOLYLINE"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq p (mapcar '/ (mapcar '+ (vlax-curve-getpointatparam e 0) (vlax-curve-getpointatparam e 2)) '(2 2)))
      (setq r (min (setq d (vlax-curve-getdistatparam e 1)) (- (vlax-curve-getdistatparam e 2) d)))
      (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 (/ r 2))))))
  (princ)
  )

 

If your rectangle is from 4 vertices (if else the routine fails), then parameter 0 is the first one, and +1 next one.

Message 5 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

If you can count on always selecting an actually rectangular Polyline, and want the largest internal Circle drawn, something like this? [untested]:

 

(vl-load-com); in case needed

(defun C:CMR ; = Circle at Midpoint of Rectangle

  (/ rec)

  (setq

    rec (car (entsel "\nRectangle to draw Circle in the middle of: "))

    dim1 (/ (distance (vlax-curve-getStartPoint rec) (vlax-curve-getPointAtParam rec 1)) 2)

    dim2 (/ (distance (vlax-curve-getPointAtParam rec 1) (vlax-curve-getPointAtParam rec 2)) 2)

  ); setq

  (command "_.circle"

    "_non"

    (mapcar '/

      (mapcar '+ (vlax-curve-getStartPoint rec) (vlax-curve-getPointAtParam rec 2))

      '(2 2 2)

    ); mapcar /

    (min dim1 dim2); radius

  ); command

  (princ)

); defun

    

That leaves the distances you want in the 'dim1' and 'dim2' variables.

 

It could be made to verify that you selected a Polyline, that it's closed and with 4 vertices, and even check whether it's rectangular (I have code for that if you want).

Kent Cooper, AIA
Message 6 of 12

Anonymous
Not applicable
This is really good, Thanks! But I want to be able to draw it MAX (to the sides of rectangle) MIN (any size smaller)!
0 Likes
Message 7 of 12

Anonymous
Not applicable
Thanks! Same here, I want to be able to draw it MAX (to the sides of rectangle) MIN (any size smaller)!
0 Likes
Message 8 of 12

ВeekeeCZ
Consultant
Consultant

One routine for min and second for max?

... if yes, then find a MIN function and change it to MAX. As simple as that.

0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:
.... I want to be able to draw it MAX (to the sides of rectangle) MIN (any size smaller)!

This should [untested] offer the MAXimum size that fits within the rectangle as the default, and accept any other size input from the User:

(vl-load-com); in case needed
(defun C:CMR ; = Circle at Midpoint of Rectangle
  (/ rec rad)
  (setq
    rec (car (entsel "\nRectangle to draw Circle in the middle of: "))
    dim1 (/ (distance (vlax-curve-getStartPoint rec) (vlax-curve-getPointAtParam rec 1)) 2)
    dim2 (/ (distance (vlax-curve-getPointAtParam rec 1) (vlax-curve-getPointAtParam rec 2)) 2)
  ); setq
  (initget 6); no zero, no negative
  (setq rad
    (cond
      ( (getdist
          (strcat
            "\nRadius of Circle <max. "
            (rtos (min dim1 dim2)); in current Units mode/precision
            ">: "
          ); strcat
        ); getdist
      ); User-input condition
      ((min dim1 dim2)); default on User Enter
    ); cond
  ); setq
  (command "_.circle"
    "_non"
    (mapcar '/
      (mapcar '+ (vlax-curve-getStartPoint rec) (vlax-curve-getPointAtParam rec 2))
      '(2 2 2)
    ); mapcar /
    rad ; radius
  ); command
  (princ)
); defun

It could be made to reject sizes larger than what fits in the rectangle, if that's needed.

Kent Cooper, AIA
Message 10 of 12

Anonymous
Not applicable

This is Perfect. Toast! 🍻😀

0 Likes
Message 11 of 12

hak_vz
Advisor
Advisor
(defun circle_in_rect ( min_max / *error* adoc take take2 e eo eco coords a b c d1 d2 m p q)
	(defun *error* ( msg )
			(if (not (member msg '("Function cancelled" "quit / exit abort")))
				(princ (strcat "\nError: " msg))
			)
			(setvar 'cmdecho 1)
			(princ)
		)
	(defun take (amount lst / ret) (repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
	(defun take2 (lst) (take 2 lst))
	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc) 
	(setvar 'cmdecho 0)
		(while (setq e (car(entsel "\nSelect rectangle >")))
			(setq eo (vlax-ename->vla-object e) co(vlax-get eo 'Coordinates))
			(while co (setq coords (cons (take2 co) coords) co (cddr co)))
				(setq
					a (car coords) 
					b (cadr coords)
					c (caddr coords)
					p (mapcar '*(mapcar '+ a b) '(0.5 0.5))
					q (mapcar '*(mapcar '+ b c) '(0.5 0.5))
					m (mapcar '*(mapcar '+ a c) '(0.5 0.5))
					d1 (distance m p)
					d2 (distance m q)
				)
				(if(eq min_max "<")
					(command "_.circle" "_non" m (car(vl-sort (list d1 d2) '<)))
					(command "_.circle" "_non" m (car(vl-sort (list d1 d2) '>)))
				)
		)
	(vla-endundomark adoc)
	(setvar 'cmdecho 1)
	(princ)
)

(defun c:min_circle_in_rect nil (circle_in_rect "<"))
(defun c:max_circle_in_rect nil (circle_in_rect ">"))
(defun min_circle_in_rect nil (circle_in_rect "<"))
(defun max_circle_in_rect nil (circle_in_rect ">"))

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 12

Sea-Haven
Mentor
Mentor

Nice idea 4 options, for me add a overall (defun c:cinr then use a cond on pick. Need multi radio buttons.lsp. The button picked can be saved so next run just click OK.

 

screenshot372.png

 

0 Likes