Author Topic: Create rectangle from centroid (center point)  (Read 3476 times)

0 Members and 1 Guest are viewing this topic.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Create rectangle from centroid (center point)
« on: December 02, 2011, 08:50:11 AM »
Is anyone willing to share some (classy, slick VLSP) code to create a rectangle from a center pick point?

I can do it the old-fashioned way by calculating the X & Y values using polar and half the length of the sides but I figured there's got to be a better (read, less code) way.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Create rectangle from centroid (center point)
« Reply #1 on: December 02, 2011, 09:26:26 AM »
Quickly written:

Code: [Select]
(defun Rectangle ( c w h )
    (   (lambda ( c ) (mapcar '(lambda ( a ) (mapcar 'apply a c)) '((- -) (+ -) (+ +) (- +))))
        (mapcar 'list c (list (/ w 2.) (/ h 2.)))
    )
)

Test:

Code: [Select]
(defun c:test ( / h p n w )
    (setq w 5
          h 3
          n (trans '(0. 0. 1.) 1 0 t)
    )
    (while (setq p (getpoint "\nCenter Point: "))
        (entmake
            (append
               '((0 . "LWPOLYLINE") (100 . "AcDbEntity") (100 . "AcDbPolyline") (90 . 4) (70 . 1))
                (mapcar '(lambda ( x ) (cons 10 (trans x 1 n))) (Rectangle p w h))
                (list (cons 210 n))
            )
        )
    )
    (princ)
)

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Create rectangle from centroid (center point)
« Reply #2 on: December 02, 2011, 10:00:07 AM »
Yup.... much more elegant than my way.   :oops:

Thanks!  :-)
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Create rectangle from centroid (center point)
« Reply #3 on: December 02, 2011, 10:26:04 AM »
Cheers!  :-)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Create rectangle from centroid (center point)
« Reply #4 on: December 03, 2011, 10:31:55 AM »
Lee, that nice! I had to scratch my head about 5 times before the light went on! Now it's a situation of: "Why didn't I think of that!"  :ugly:

BTW, why to you first get the transformation matrix through trans of 0,0,1 from UCS to WCS? Is it more efficient than simply using trans's own calculations? Sorry, haven't tested this.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Create rectangle from centroid (center point)
« Reply #5 on: December 03, 2011, 11:26:33 AM »
Lee, that nice! I had to scratch my head about 5 times before the light went on! Now it's a situation of: "Why didn't I think of that!"  :ugly:

Thanks Irné!  8-)

BTW, why to you first get the transformation matrix through trans of 0,0,1 from UCS to WCS? Is it more efficient than simply using trans's own calculations? Sorry, haven't tested this.

I calculate the UCS Normal using trans before the while loop for better efficiency (save calculating it for every loop), but this does assume that the user will not alter the orientation of the UCS Normal whilst picking points.