Jump to content

Rectangles to Single Line


Recommended Posts

I have a drawing with multiple rectangles. All rectangles are 100mm wide and varying in lengths.

They are drawn as closed polylines.

 

Is there a way for me to convert these polylines to a single line of the same length?

For example... If the rectangle was 2,000mm long it would replace the polyline with one single line at 2,000mm long.

 

If there is a way to do this I'd preferably like to convert multiple recangles at a time.rectline.JPG

 

Thanks

Andy:)

Link to comment
Share on other sites

Where would this line be? On one of the long edges? Centre of the existing rectangle?

Are all of the rectangles on the same and single layer?

Would want a line or a polyline with just two nodes? If it's a polyline you could always give the polylines a width to depict the area covered by the rectangle.

 

The short answer is no, there is no existing function in AutoCAD to do this, but a LISP or other routine could be written to do it.

Link to comment
Share on other sites

Where would this line be? On one of the long edges? Centre of the existing rectangle?

Are all of the rectangles on the same and single layer?

Would want a line or a polyline with just two nodes? If it's a polyline you could always give the polylines a width to depict the area covered by the rectangle.

 

The short answer is no, there is no existing function in AutoCAD to do this, but a LISP or other routine could be written to do it.

 

Hi Tyke,

 

Preferably the line would be central but it's not necessary.

They are all on a single layer.

The line could be a polyline or standard line.

 

I guess i'm off to research LISP's.

 

Thanks

Andy

Link to comment
Share on other sites

How many rectangles are there?

 

The single line will always be equivalent, in length, to the longest dimension of the rectangle no matter the orientation?

Link to comment
Share on other sites

Try this

 

(defun c:R2L (/ b c ename p1 p2 p3 p4 p5 p6 sn sn1 sn2 ss)
 (princ "\n Select rectangles: ")
 (setq	ss  (ssget '((-4 . "<and")
	     (0 . "LWPOLYLINE")
	     (70 . 1)
	     (90 . 4)
	     (-4 . "and>")
	    )
    )
sn  (sslength ss)
sn1 sn
 )
 (repeat sn
   (setq sn2	(1- sn1)
  ename	(ssname ss sn2)
  b	(entget ename)
  b	(member (assoc 10 b) b)
   )
   (while (member (assoc 10 b) b)
     (setq c (append c (list (cdr (assoc 10 b))))
    b (cdr b)
    b (member (assoc 10 b) b)
     )
   )

   (setq p1 (nth 0 c)
  p2 (nth 1 c)
  p3 (nth 2 c)
  p4 (nth 3 c)
  c  nil
   )
   (cond ((= (cadr p1) (cadr p2))
   (setq p5 (polar p1 (angle p1 p4) (/ (distance p2 p3) 2))
	 p6 (polar p2 (angle p2 p3) (/ (distance p2 p3) 2))
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	      (cons 210 (list 0.0 0.0 1.0))
	    )
   )
  )
  ((= (cadr p2) (cadr p3))
   (setq p5 (polar p2 (angle p2 p1) (/ (distance p2 p1) 2))
	 p6 (polar p3 (angle p3 p4) (/ (distance p2 p1) 2))
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	      (cons 210 (list 0.0 0.0 1.0))
	    )
   )
  )
   )
   (setq sn1 sn2)
 )
 (command "erase" ss "")
 (princ)
)

Link to comment
Share on other sites

How many rectangles are there?

 

The single line will always be equivalent, in length, to the longest dimension of the rectangle no matter the orientation?

 

There could be just 2 rectangles or 102. Each project I do has many different variables which could change the quatities and lengths of each rectangle.

And correct... the length of the line would be the same length as as the longest dimension of the rectangle.

Link to comment
Share on other sites

Try this

 

(defun c:R2L (/ b c ename p1 p2 p3 p4 p5 p6 sn sn1 sn2 ss)
 (princ "\n Select rectangles: ")
 (setq    ss  (ssget '((-4 . "<and")
            (0 . "LWPOLYLINE")
            (70 . 1)
            (90 . 4)
            (-4 . "and>")
           )
       )
   sn  (sslength ss)
   sn1 sn
 )
 (repeat sn
   (setq sn2    (1- sn1)
     ename    (ssname ss sn2)
     b    (entget ename)
     b    (member (assoc 10 b) b)
   )
   (while (member (assoc 10 b) b)
     (setq c (append c (list (cdr (assoc 10 b))))
       b (cdr b)
       b (member (assoc 10 b) b)
     )
   )

   (setq p1 (nth 0 c)
     p2 (nth 1 c)
     p3 (nth 2 c)
     p4 (nth 3 c)
     c  nil
   )
   (cond ((= (cadr p1) (cadr p2))
      (setq p5 (polar p1 (angle p1 p4) (/ (distance p2 p3) 2))
        p6 (polar p2 (angle p2 p3) (/ (distance p2 p3) 2))
      )
      (entmake (list
             (cons 0 "LINE")
             (cons 10 p5)
             (cons 11 p6)
             (cons 210 (list 0.0 0.0 1.0))
           )
      )
     )
     ((= (cadr p2) (cadr p3))
      (setq p5 (polar p2 (angle p2 p1) (/ (distance p2 p1) 2))
        p6 (polar p3 (angle p3 p4) (/ (distance p2 p1) 2))
      )
      (entmake (list
             (cons 0 "LINE")
             (cons 10 p5)
             (cons 11 p6)
             (cons 210 (list 0.0 0.0 1.0))
           )
      )
     )
   )
   (setq sn1 sn2)
 )
 (command "erase" ss "")
 (princ)
)

 

 

Hi Paul, there seems to be something wrong with this. It draws the lines for only vertical rectangles but it draws the 100mm line rather than the longest length.

 

I've attached a drawing below of an example and it also shows how'd I'd like the finished product to be. If you could get this to work it would be massively appreciated.

 

rectline.dwg

 

Thanks Andy

Link to comment
Share on other sites

Please, try this one

 

(defun c:R2L (/ b c ename p1 p2 p3 p4 p5 p6 sn sn1 sn2 ss)
 (princ "\n Select rectangles: ")
 (setq	ss  (ssget '((-4 . "<and")
	     (0 . "LWPOLYLINE")
	     (70 . 1)
	     (90 . 4)
	     (-4 . "and>")
	    )
    )
sn  (sslength ss)
sn1 sn
 )
 (repeat sn
   (setq sn2	(1- sn1)
  ename	(ssname ss sn2)
  b	(entget ename)
  b	(member (assoc 10 b) b)
   )
   (while (member (assoc 10 b) b)
     (setq c (append c (list (cdr (assoc 10 b))))
    b (cdr b)
    b (member (assoc 10 b) b)
     )
   )

   (setq p1 (nth 0 c)
  p2 (nth 1 c)
  p3 (nth 2 c)
  p4 (nth 3 c)
  c  nil
   )
   (cond ((= (distance p1 p2) 100)
   (setq p5 (polar p1 (angle p1 p2) 50)
	 p6 (polar p3 (angle p3 p4) 50)
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	    )
   )
  )
  ((= (distance p2 p3) 100)
   (setq p5 (polar p2 (angle p2 p3) 50)
	 p6 (polar p1 (angle p1 p4) 50)
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	    )
   )
  )
   )
   (setq sn1 sn2)
 )
 (command "erase" ss "")
 (princ)
)

Link to comment
Share on other sites

Please, try this one

 

(defun c:R2L (/ b c ename p1 p2 p3 p4 p5 p6 sn sn1 sn2 ss)
 (princ "\n Select rectangles: ")
 (setq	ss  (ssget '((-4 . "<and")
	     (0 . "LWPOLYLINE")
	     (70 . 1)
	     (90 . 4)
	     (-4 . "and>")
	    )
    )
sn  (sslength ss)
sn1 sn
 )
 (repeat sn
   (setq sn2	(1- sn1)
  ename	(ssname ss sn2)
  b	(entget ename)
  b	(member (assoc 10 b) b)
   )
   (while (member (assoc 10 b) b)
     (setq c (append c (list (cdr (assoc 10 b))))
    b (cdr b)
    b (member (assoc 10 b) b)
     )
   )

   (setq p1 (nth 0 c)
  p2 (nth 1 c)
  p3 (nth 2 c)
  p4 (nth 3 c)
  c  nil
   )
   (cond ((= (distance p1 p2) 100)
   (setq p5 (polar p1 (angle p1 p2) 50)
	 p6 (polar p3 (angle p3 p4) 50)
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	    )
   )
  )
  ((= (distance p2 p3) 100)
   (setq p5 (polar p2 (angle p2 p3) 50)
	 p6 (polar p1 (angle p1 p4) 50)
   )
   (entmake (list
	      (cons 0 "LINE")
	      (cons 10 p5)
	      (cons 11 p6)
	    )
   )
  )
   )
   (setq sn1 sn2)
 )
 (command "erase" ss "")
 (princ)
)

 

You Sir are a genius!

 

I can't thank you enough.

 

Thanks

Andy:)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...