+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Forum Newbie
    Using
    AutoCAD 2010
    Join Date
    Apr 2013
    Posts
    2

    Default Draw Rectangle Dimension

    Registered forum members do not see this ad.

    Hello! I was wondering how I would go about writing a function that would calculate the length of a diagonal of a rectangle using SQRT function? I've been trying but so far nothing!
    Thanks!

  2. #2
    Forum Deity MSasu's Avatar
    Discipline
    Construction
    MSasu's Discipline Details
    Occupation
    engineer
    Discipline
    Construction
    Details
    AutoLISP programmer
    Using
    AutoCAD 2013
    Join Date
    Mar 2009
    Location
    Brasov, Romania
    Posts
    4,008

    Default

    Can you post the code you wrote so far?
    Presuming that you know the length, respectively width of the rectangle, you should use Pythagoras formula:
    Code:
    (setq sizeDiagonal (sqrt (+ (expt sizeLength 2.0) (expt sizeWidth 2.0))))
    Regards,
    Mircea

    AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3

  3. #3
    Forum Newbie
    Using
    AutoCAD 2010
    Join Date
    Apr 2013
    Posts
    2

    Default

    Thanks! I had a few simple lines, but I erased those as yours was basically what I needed. Would it be possible to write a function that would find the length & width itself? This one is out of curiosity.

  4. #4
    Full Member
    Computer Details
    fabriciorby's Computer Details
    Operating System:
    Windows 7 64 bits
    Discipline
    See details...
    fabriciorby's Discipline Details
    Discipline
    See details below.
    Details
    Telecom
    Using
    AutoCAD 2011
    Join Date
    Mar 2013
    Location
    Brazil
    Posts
    92

    Default

    Quote Originally Posted by nejadarea View Post
    Thanks! I had a few simple lines, but I erased those as yours was basically what I needed. Would it be possible to write a function that would find the length & width itself? This one is out of curiosity.
    Try this:
    Code:
    (defun LM:MAssoc ( key lst / item )
        (if (setq item (assoc key lst))
            (cons (cdr item) (LM:MAssoc key (cdr (member item lst))))
        )
    )
    (defun c:getdist (/ s pt1 pt2 pt3 pt4 x y ent lst)
    (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE"))))
    (if (= s nil)
    	(alert "Select a rectangle.")
    	(progn
    	(setq ent  (entget(ssname s 0)))
        (setq lst (LM:MAssoc 10 ent))
    	(princ)
    	(setq pt1 (nth 0 lst))
    	(setq pt2 (nth 1 lst))
    	(setq pt3 (nth 2 lst))
    	(setq pt4 (nth 3 lst))
    	(setq x(distance pt1 pt2))
    	(setq y(distance pt1 pt4))
    	(princ (strcat "\nHeight: " (rtos y) "\nWidth: " (rtos x)))
    	(princ)
    	)
    	)
    )
    Yes, thanks to Lee Mac haha

    Edit: But there is some problem with this code, if you rotate your rectangle by 90º the height will become width and vice versa.
    Nothing that a if can't solve hehe
    Last edited by fabriciorby; 18th Apr 2013 at 04:34 pm.

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,322

    Default

    Registered forum members do not see this ad.

    Here is another method:
    Code:
    (defun c:test ( / lst sel )
        (if (setq sel (ssget "_+.:E:S" '((0 . "LWPOLYLINE"))))
            (progn
                (setq lst
                    (mapcar 'cdr
                        (vl-remove-if-not '(lambda ( x ) (= 10 (car x)))
                            (entget (ssname sel 0))
                        )
                    )
                )
                (apply 'mapcar
                    (cons '-
                        (mapcar
                            '(lambda ( x ) (apply 'mapcar (cons x lst)))
                            '(max min)
                        )
                    )
                )
            )
        )
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

Similar Threads

  1. Draw a rectangle on a Block
    By rodrigo_sjc_sp in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 8th Apr 2013, 02:38 pm
  2. Draw rectangle on polyline's vertex
    By Rakumbada in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 31st Jul 2011, 11:35 pm
  3. How to accurately draw a rectangle ?
    By JamFam in forum AutoCAD Beginners' Area
    Replies: 2
    Last Post: 11th Apr 2006, 07:47 pm
  4. Draw Rectangle by Area
    By Brunello19 in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 9th Nov 2005, 04:50 am
  5. Draw a rectangle in 2000 or AC LT97
    By hindsight in forum AutoCAD Beginners' Area
    Replies: 1
    Last Post: 3rd Mar 2004, 04:09 pm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts