Jump to content
robertbon

Draw rectangle from centre line

Recommended Posts

robertbon

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 this post


Link to post
Share on other sites
Tharwat

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 this post


Link to post
Share on other sites
robertbon

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

Share this post


Link to post
Share on other sites
Lee Mac

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 this post


Link to post
Share on other sites
Lee Mac

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 this post


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

You're welcome . :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×