Need LISP Script to find Length of Multiple same size rectangle Length

Need LISP Script to find Length of Multiple same size rectangle Length

patilST
Enthusiast Enthusiast
3,680 Views
16 Replies
Message 1 of 17

Need LISP Script to find Length of Multiple same size rectangle Length

patilST
Enthusiast
Enthusiast

Hi All,

 

Anybody Know How to find Length of Multiple same size rectangle Length by using Autocad LISP script. 

Please share as soon as possible. In urgent need.

 

Situation is, Cosider i have multiple ractangle of same widht and length is different, So at the end i need individual lenght of each rectangle and addition of all the length in answer.

 

Thanks in Advance

 

Shiva

 

0 Likes
3,681 Views
16 Replies
Replies (16)
Message 2 of 17

Ajilal.Vijayan
Advisor
Advisor

Hi Shiva,

The posts related to LISP should be posted in the Visual LISP, AutoLISP forum.

 

You can use DATAEXTRACTION command to extract the total length of rectangles to an excel sheet and use this calculation to calculate the length.

totallength - (width * 2).

0 Likes
Message 3 of 17

Kent1Cooper
Consultant
Consultant

I agree this would be better over at the Customization Forum [not at all version-dependent], and by the way, a "LISP" and a "script" are different things in AutoCAD, but....

 

It sounds as if, by "length," you do not mean the length as AutoCAD would measure it of the Polyline entity that forms the rectangle [assuming that's what it is, and not, for example, a Block or Wipeout or 2D Solid or 3DFace or Trace or...], which would be the overall perimeter of the rectangle.  But rather I assume you want one dimension of the rectangular shape it forms.  If that's the case, some question arise:

 

Would you always want the longer dimension of every rectangle?  [That wouldn't be too hard, depending on the answers to the other questions.]  Or might some be shorter than their common width?  For example, if they are all 4 units wide, with some of them 6 units long, some 11 units long, etc., for those you would want their longer dimensions, but if one is 3 units long, you would want its shorter dimension.

 

Would they always all be drawn orthogonally?  [That would make it a lot easier.]

 

Would they always all be closed Polylines of 4 and only 4 vertices?  Or might there sometimes be, for example, intervening vertices within rectangle edges, or Polylines that look closed but are not, and have 5 vertices with the first and last at the same place?

 

Would you want a routine to determine whether or not a selected object is actually rectangular?

 

Would you want a routine to determine whether or not multiple rectangles share a common width, and what that is, for example to only account for those that do?  Or would you rely on the User to select only appropriate rectangles?

Kent Cooper, AIA
0 Likes
Message 4 of 17

wispoxy
Advisor
Advisor
Why are you using LISP for something AutoCAD can already do naturally?
0 Likes
Message 5 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> Why are you using LISP for something AutoCAD can already do naturally?

Please let me know how AutoCAD can find a length and a width of a rectangle ... knowing that the rectangle command does nothing else than creating a closed polyline using 4 points from a calculation out of 2 points. At the moment the polyline is created I don't know any command which knows a length and a width of a polyline with 4 points.

 

BTW: that's also why I guess DATAEXTRACTION can't do that with polylines, if you have block-insertions, then yes, but not from the objects created by command _RECTANGLE.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 6 of 17

3wood
Advisor
Advisor

As a quick start, there are routines to remove lines as long as they are shorter than specified length, and also routines to add up total length of line objects.

I would suggest complete the task in 4 steps without creating a new routine.

1. Explode polyline rectangles into lines.

2. Remove shorter lines and retain longer lines with a routine.

3. Add up total length of rest lines with a routine.

4. Divide the result by 2.

0 Likes
Message 7 of 17

wispoxy
Advisor
Advisor
The same way AutoCAD finds the area of objects, it's built into the quick calculator already. Posting a screenshot soon.
0 Likes
Message 8 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> The same way AutoCAD finds the area of objects

My understanding is: not the area is asked to get, the question was how to get the length and the width of a rectangle.

Whereas "AREA" is a property of a closed polyline and so can be queried with DATAEXTRACTION, there is no property "LENGTH" and/or "WIDTH" and so no built in function(s) to get these informations.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 9 of 17

wispoxy
Advisor
Advisor

What AutoCAD version is everyone using? After creating a closed poly rectangle you get the following two at the bottom of your properties toolbar. Use REGEN for it to update, or it stays at 0.

 

8.27.2015_00001.png

0 Likes
Message 10 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

I use plain AutoCAD 2016 now, but also in older releases I never saw properties like you show "Polyline X" and "Polyline Y".

And as your screenshot shows both values = 0.000 it can't be length and width of a rectangle when the area > 0

 

So either you are running a vertical product or you have apps or plugins which add these properties in the property window.

 

My property window shows:

 

2015-08-27 14-03-53.png

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 11 of 17

wispoxy
Advisor
Advisor

"So either you are running a vertical product or you have apps or plugins which add these properties in the property window."

 

Sorry everyone, he's right. It's a MEP plugin. I looked into my AutoCAD 2016 - no plugins, not there.

0 Likes
Message 12 of 17

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

.... by "length," you do not mean the length as AutoCAD would measure it of the Polyline entity .... you want one dimension of the rectangular shape it forms. ....

 

.... might some be shorter than their common width? ....

.... 

Would they always all be closed Polylines of 4 and only 4 vertices?

....

Would you want a routine to determine whether or not multiple rectangles share a common width, and what that is....


On the assumption that the answers to the above are all affirmative, for the exercise of it, I came up with something [lightly tested]:

 

(vl-load-com); if needed
(defun C:RLCW ; = Rectangle Lengths if Common Width
  (/ match ss p d1 d2 sides width)
  (defun match (dist); find whether 'dist' is in every sub-list [with tolerance]
    (not (member nil
      (mapcar
        '(lambda (x) (or (equal dist (car x) 1e-6) (equal dist (cadr x) 1e-6)))
        sides
      ); mapcar
    )); member/not
  ); defun -- match
  (setq ss (ssget '((0 . "LWPOLYLINE") (90 . 4) (-4 . "&") (70 . 1))))
  (repeat (setq n (sslength ss))
    (setq ; list of lists of length & width of each rectangle
      p (ssname ss (setq n (1- n)))
      d1 (vlax-curve-getDistAtParam p 1); length of first segment
      d2 (- (vlax-curve-getDistAtParam p 2) d1); length of second segment
      sides (cons (list d1 d2) sides); add sublist to list of sub-lists
    ); setq
  ); repeat
  (setq width
    (cond
      ((match d1) d1); length of 1st segment of 1st Polyline shared by all
      ((match d2) d2); length of 2nd segment of 1st Polyline shared by all
    ); cond
  ); setq
  (if width ; there is a common width
    (progn ; then
      (setq
        RLlengths (mapcar '(lambda (x) (if (equal width (car x) 1e-6) (cadr x) (car x))) sides)
        RLtotal (apply '+ RLlengths)
      ); setq
      (prompt "\nLengths of rectangles: ")
      (princ (mapcar 'rtos RLlengths)); expressed in current Units settings
      (prompt (strcat "\nTotal of lengths = " (rtos RLtotal) "."))
    ); progn
    (prompt "\nSelected rectangles do not share a common width."); else
  ); if
  (princ)
); defun -- C:RLCW

It leaves the list of lengths in the RLlengths variable, and the total in the RLtotal variable, non-localized [with RL prefix on them to avoid possible conflict with any variables from other routines], so you can use the results in other calculations, send them out to a file, or whatever.

 

It could easily be made to also report what the common width is.

 

It doesn't care about my question of whether selected rectangles are orthogonal or not.  It does not verify that each Polyline is actually rectangular, but it could be made to do so.  It could also be made to report on rectangles that do share a common width and ignore any that don't, rather than give up if they don't all, though it would be harder to determine what that common-to-most width is if the first one in the selection doesn't share it; there is surely some way to do that, although there might sometimes be multiple contenders for what value to use.

Kent Cooper, AIA
0 Likes
Message 13 of 17

3wood
Advisor
Advisor
I just tested another way without using any add-on. 1. Explode all polyline rectangles into lines. 2. Use QSELECT to select lines in rectangle length direction, ignoring shorter lines which represent rectangle width. (Line, Length, Greater than) 3. Make a copy of selected rectangle length lines in Step 2. 4. Use DATAEXTRACT to select and export length information of lines in Step 3 into an xls file. 5. Calculate the sum of length in Excel and divide the result by 2.
0 Likes
Message 14 of 17

Kent1Cooper
Consultant
Consultant

@3wood wrote:
.... 1. Explode all polyline rectangles into lines. 2. Use QSELECT to select lines in rectangle length direction, ignoring shorter lines which represent rectangle width. (Line, Length, Greater than) ....

I'd still like to hear back from the OP with more detail on the circumstances.  For example, finding only Lines of greater than a certain length would give incorrect results if the lengths of any rectangles are shorter than [or equal to, for that matter] their common width -- see the third paragraph in Post 3.  My routine will account for those, but it may also be based on some incorrect assumptions.

Kent Cooper, AIA
0 Likes
Message 15 of 17

wispoxy
Advisor
Advisor

If they are only rectangles, why don't you just use smart / quick dimension? Command QDIM , DIMALI , etc.

 

It would be a lot faster than running LISP.

0 Likes
Message 16 of 17

3wood
Advisor
Advisor

QDIM doesn't work if rectangles overlap with each other.

DIMALI can only pick up one rectangle at a time, not effective enough if there are more than 100 objects?

 

0 Likes
Message 17 of 17

wispoxy
Advisor
Advisor

Other than the plugin I spoke about, I've done high quantity objects with QDIM / Select Similar (Defined), took less than 2 min. same with high rise buildings where multiple floors are stacked.

-------------------------------------------------------------------------------------------------------------
Would be nice if AutoCAD had an Auto Dimension tool like other drafting software.
-------------------------------------------------------------------------------------------------------------

And from his top post, how do you know he wants to dimension rectangles overlapping each other?

0 Likes