Draw rectangle from centre line

Recommended Posts

I have a drawing with lots of lines approx. 300mm long. Each of these lines is the centreline of a rectangle that is 20mm wide and the same length as the line.

So my CAD exercise is to offset the centreline 10mm in each direction and then connect the end points of these new lines to create a rectangle. And repeat several hundred times!

I know that a lisp routine should be able to do this for me and that it probably involves polar coordinates, but I am at a loss at how to do it.

Can anyone help?

Share on other sites

Quickly done

```(defun c:Test (/ ss i e 1p 2p a d p1 p2 p3 p4 _line)
(defun _line (q p)
(entmakex (list '(0 . "LINE") (cons 10 q) (cons 11 p)))
)
(if (setq ss (ssget "_:L" '((0 . "LINE"))))
(repeat (setq i (sslength ss))
(setq e  (entget (ssname ss (setq i (1- i))))
1p (cdr (assoc 10 e))
2p (cdr (assoc 11 e))
a  (angle 1p 2p)
d  (distance 1p 2p)
)
(setq p1 (polar 1p (+ a (* pi 0.5)) 10.)
p2 (polar p1 a d)
p3 (polar 1p (+ a (* pi 1.5)) 10.)
p4 (polar p3 a d)
)
(mapcar '_line
(list p1 p2 p4 p3)
(list p2 p4 p3 p1)
)
)
)
(princ)
)
```

Share on other sites

That's brilliant Tharwat, exactly what I need, thank you.

Share on other sites

Here's another way, using some trans trickery:

```(defun c:l2r ( / i e l n p q s )
(if (setq s (ssget '((0 . "LINE"))))
(repeat (setq i (sslength s))
(setq e (entget (ssname s (setq i (1- i))))
p (cdr (assoc 10 e))
q (cdr (assoc 11 e))
n (mapcar '- p q)
l (mapcar
'(lambda ( x )
(mapcar '(lambda ( y ) (mapcar x (trans y 0 n) '(10 0 0)))
(list p q)
)
)
'(+ -)
)
)
(entmake
(append
'(   (000 . "LWPOLYLINE")
(100 . "AcDbEntity")
(100 . "AcDbPolyline")
(090 . 4)
(070 . 1)
)
(mapcar '(lambda ( x ) (cons 10 (trans x n 0))) (append (car l) (reverse (cadr l))))
)
)
)
)
(princ)
)```

Share on other sites

And another, using matrix math:

```(defun c:l2r ( / a d e i p q s )
(if (setq s (ssget '((0 . "LINE"))))
(repeat (setq i (sslength s))
(setq e (entget (ssname s (setq i (1- i))))
p (cdr (assoc 10 e))
q (cdr (assoc 11 e))
a (angle    p q)
d (distance p q)
)
(entmake
(append
'(   (000 . "LWPOLYLINE")
(100 . "AcDbEntity")
(100 . "AcDbPolyline")
(090 . 4)
(070 . 1)
)
(   (lambda ( m )
(mapcar '(lambda ( x ) (cons 10 (mapcar '+ p (mapcar '(lambda ( y ) (apply '+ (mapcar '* y x))) m))))
'(
(0.0 -0.5)
(1.0 -0.5)
(1.0  0.5)
(0.0  0.5)
)
)
)
(list
(list (* (cos a) d) (* (sin a) -20.0))
(list (* (sin a) d) (* (cos a)  20.0))
)
)
)
)
)
)
(princ)
)```

Share on other sites
That's brilliant Tharwat, exactly what I need, thank you.

You're welcome .