+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Full Member
    Using
    AutoCAD 2014
    Join Date
    Nov 2012
    Location
    London, England
    Posts
    25

    Default Draw rectangle from centre line

    Registered forum members do not see this ad.

    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?

  2. #2
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,085

    Default

    Quickly done

    Code:
    (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)
    )

  3. #3
    Full Member
    Using
    AutoCAD 2014
    Join Date
    Nov 2012
    Location
    London, England
    Posts
    25

    Default

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

  4. #4
    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,364

    Default

    Here's another way, using some trans trickery:
    Code:
    (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)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

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

  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,364

    Default

    And another, using matrix math:
    Code:
    (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)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

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

  6. #6
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,085

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by robertbon View Post
    That's brilliant Tharwat, exactly what I need, thank you.
    You're welcome .

Similar Threads

  1. Draw rectangle
    By spiker7221 in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 29th Aug 2013, 02:23 pm
  2. 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
  3. Replies: 2
    Last Post: 4th Oct 2010, 12:16 pm
  4. How to accurately draw a rectangle ?
    By JamFam in forum AutoCAD Beginners' Area
    Replies: 2
    Last Post: 11th Apr 2006, 07:47 pm
  5. Draw Rectangle by Area
    By Brunello19 in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 9th Nov 2005, 04:50 am

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