Results 1 to 9 of 9

Thread: Drawing a row of four lines with Lisp.

  1. #1
    Member
    Join Date
    2011-09
    Posts
    4

    Question Drawing a row of four lines with Lisp.

    So I've got a bit of lisp that I just threw together. The goal is to create three plines and a single line with at half inch space between the lines. I also only want it to work when the line being drawn is perfectly vertical or horizontal.

    This bit of code seems to work about 1/2 the time. It blows up when you try to draw a really long line or if you draw a vertical segment and then osnap a horizontal segment to it.

    Thanks,
    -Brandon

    (defun C:lll()
    ;;;--- command echo off
    (setvar "cmdecho" 0)

    (setq pt1(getpoint "\n Start Point: "))
    (setq pt2(getpoint pt1"\n Second Point: "))
    (setq x1(car pt1))
    (setq x2(car pt2))
    (setq y1(cadr pt1))
    (setq y2(cadr pt2))
    (COMMAND "_PLINE" pt1 pt2 "")

    (cond ((= x1 x2)

    (setq bxpt1(+ x1 0.5))
    (setq bxpt2(+ x2 0.5))
    (setq bxline1(list bxpt1 y1 0))
    (setq bxline2(list bxpt2 y2 0))
    (COMMAND "_PLINE" bxline1 bxline2 "")


    (setq cxpt1(+ x1 1))
    (setq cxpt2(+ x2 1))
    (setq cxline1(list cxpt1 y1 0))
    (setq cxline2(list cxpt2 y2 0))
    (COMMAND "_PLINE" cxline1 cxline2 "")


    (setq gxpt1(+ x1 1.5))
    (setq gxpt2(+ x2 1.5))
    (setq gxline1(list gxpt1 y1 0))
    (setq gxline2(list gxpt2 y2 0))
    (COMMAND "_LINE" gxline1 gxline2 "")


    );_ end of the first then condition
    ((= y1 y2)

    (setq bypt1(- y1 0.5))
    (setq bypt2(- y2 0.5))
    (setq byline1(list x1 bypt1 0))
    (setq byline2(list x2 bypt2 0))
    (COMMAND "_PLINE" byline1 byline2 "")


    (setq cypt1(- y1 1))
    (setq cypt2(- y2 1))
    (setq cyline1(list x1 cypt1 0))
    (setq cyline2(list x2 cypt2 0))
    (COMMAND "_PLINE" cyline1 cyline2 "")


    (setq gypt1(- y1 1.5))
    (setq gypt2(- y2 1.5))
    (setq gyline1(list x1 gypt1 0))
    (setq gyline2(list x2 gypt2 0))
    (COMMAND "_LINE" gyline1 gyline2 "")


    );_ end of the second then condition
    (t
    (princ "\nDraw a straight line.<<<------------------------------------------")
    ) ;_ end of optional else condition

    ) ;_ end of cond statement

    ;;;--- command echo back on
    (setvar "cmdecho" 1)

    (princ)

    )

  2. #2
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    478

    Default Re: Drawing a row of four lines with Lisp.

    Would this be of any help ?

    Code:
    (defun c:TesT (/ p1 p2 a ang d p3 p4 p5 p6 p7 p8)
    ;; Tharwat 17. Nov. 2011 ;;
      (while
        (and (setq p1 (getpoint "\n Start Point :"))
             (setq p2 (getpoint p1 "\n Next Point :"))
        )
         (setq a   0.5
               ang (angle p1 p2)
               d   (distance p1 p2)
               p3  (polar p1 (+ ang (* pi 1.5)) a)
               p4  (polar p3 (+ ang (* pi 1.5)) a)
               p5  (polar p4 (+ ang (* pi 1.5)) a)
               p6  (polar p3 ang d)
               p7  (polar p4 ang d)
               p8  (polar p5 ang d)
         )
         (command "_.pline" "_non" p1 "_non" p2 "")
         (command "_.pline" "_non" p3 "_non" p6 "")
         (command "_.pline" "_non" p4 "_non" p7 "")
         (command "_.pline" "_non" p5 "_non" p8 "")
      )
      (princ)
    )

  3. #3
    Member
    Join Date
    2011-09
    Posts
    4

    Default Re: Drawing a row of four lines with Lisp.

    Yea that's almost perfect. I'm also curious why I failed so miserably.

    The only issue is I need the first line you draw to be the top-most or left-most line of the four and the fourth line to have a smaller width. Using pollar, when you draw from right to left it differs from what would happen if you draw from left to right.

    Thanks,
    -Brandon

    Code:
     
    (defun c:TesT (/ p1 p2 a ang d p3 p4 p5 p6 p7 p8)
    ;; Tharwat 17. Nov. 2011 ;;
      (setvar "cmdecho" 0)
      (while
        (and (setq p1 (getpoint "\n Start Point :"))
             (setq p2 (getpoint p1 "\n Next Point :"))
        )
         (setq a   0.5
               ang (angle p1 p2)
               d   (distance p1 p2)
               p3  (polar p1 (+ ang (* pi 1.5)) a)
               p4  (polar p3 (+ ang (* pi 1.5)) a)
               p5  (polar p4 (+ ang (* pi 1.5)) a)
               p6  (polar p3 ang d)
               p7  (polar p4 ang d)
               p8  (polar p5 ang d)
         )
         (command "_.pline" "_non" p1 "width" "1/32" "1/32" "_non" p2 "")
         (command "_.pline" "_non" p3 "width" "1/32" "1/32" "_non" p6 "")
         (command "_.pline" "_non" p4 "width" "1/32" "1/32" "_non" p7 "")
         (command "_.pline" "_non" p5 "width" "0" "0" "_non" p8 "")
      )
      (setvar "cmdecho" 1)
      (princ)
    )

  4. #4
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    478

    Default Re: Drawing a row of four lines with Lisp.

    Quote Originally Posted by Ko.Bach276562702 View Post
    The only issue is I need the first line you draw to be the top-most or left-most line of the four and the fourth line to have a smaller width. Using pollar, when you draw from right to left it differs from what would happen if you draw from left to right.
    You need to do some forms of conditions and when conditions meet somewhere , things should go somewhere . as you have did into your first post .

    If you stock with any problem , just ask .

    Good luck.

  5. #5
    Member
    Join Date
    2011-09
    Posts
    4

    Default Re: Drawing a row of four lines with Lisp.

    Okay here's the final product.

    Thanks,
    -Brandon

    Code:
     
    (defun c:TesT (/ p1 p2 a ang d p3 p4 p5 p6 p7 p8)
      (setvar "cmdecho" 0)
      (while
        (and (setq p1 (getpoint "\n Start Point :"))
             (setq p2 (getpoint p1 "\n Next Point :"))
        ) ;-while end
         (setq a   0.5
               ang (angle p1 p2)
               d   (distance p1 p2)
         ) ;-Setq end
      (cond ((and (> ang 1.5708) (< ang 4.71239)) ;- condition start
               (setq p3  (polar p1 (- ang (* pi 1.5)) a)
                     p4  (polar p3 (- ang (* pi 1.5)) a)
                     p5  (polar p4 (- ang (* pi 1.5)) a)
               );-Setq end
      ) ;- end fist condition
      (t
             (setq p3  (polar p1 (+ ang (* pi 1.5)) a)
                     p4  (polar p3 (+ ang (* pi 1.5)) a)
                     p5  (polar p4 (+ ang (* pi 1.5)) a)
             ) ;-Setq end
            ) ;- end else condition
     ) ;- end condtion
    
        (setq  p6  (polar p3 ang d)
               p7  (polar p4 ang d)
               p8  (polar p5 ang d)
        ) ;-setq end
    
    (princ ang)
         (command "_.pline" "_non" p1 "width" "1/32" "1/32" "_non" p2 "")
         (command "_.pline" "_non" p3 "width" "1/32" "1/32" "_non" p6 "")
         (command "_.pline" "_non" p4 "width" "1/32" "1/32" "_non" p7 "")
         (command "_.pline" "_non" p5 "width" "0" "0" "_non" p8 "")
      )
      (setvar "cmdecho" 1)
      (princ)
    )

  6. #6
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    478

    Default Re: Drawing a row of four lines with Lisp.

    It is good that you have the code working as you wanted and for deleting the author source code that accelerate the code .

  7. #7
    I could stop if I wanted to Lee Mac's Avatar
    Join Date
    2009-03
    Location
    London, England
    Posts
    280

    Default Re: Drawing a row of four lines with Lisp.

    One more:

    Code:
    (defun c:test ( / an p1 p2 )
        (while
            (and
                (setq p1 (getpoint "\nSpecify 1st Point: "))
                (setq p2 (getpoint "\nSpecify 2nd Point: " p1))
                (setq an ((if (< (car p1) (car p2)) - +) (angle p1 p2) (/ pi 2)))
            )
            (foreach wi '(0.03125 0.03125 0.03125 0.0)
                (entmake
                    (list
                       '(0 . "LWPOLYLINE")
                       '(100 . "AcDbEntity")
                       '(100 . "AcDbPolyline")
                       '(90 . 2)
                       '(70 . 0)
                        (cons 43 wi)
                        (cons 10 (trans p1 1 0))
                        (cons 10 (trans p2 1 0))
                    )
                )
                (setq p1 (polar p1 an 0.5)
                      p2 (polar p2 an 0.5)
                )
            )
        )
        (princ)
    )
    Lee Mac Programming

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

  8. #8
    Member
    Join Date
    2011-09
    Posts
    4

    Default Re: Drawing a row of four lines with Lisp.

    Quote Originally Posted by Lee Mac View Post
    One more:

    Code:
    (defun c:test ( / an p1 p2 )
        (while
            (and
                (setq p1 (getpoint "\nSpecify 1st Point: "))
                (setq p2 (getpoint "\nSpecify 2nd Point: " p1))
                (setq an ((if (< (car p1) (car p2)) - +) (angle p1 p2) (/ pi 2)))
            )
            (foreach wi '(0.03125 0.03125 0.03125 0.0)
                (entmake
                    (list
                       '(0 . "LWPOLYLINE")
                       '(100 . "AcDbEntity")
                       '(100 . "AcDbPolyline")
                       '(90 . 2)
                       '(70 . 0)
                        (cons 43 wi)
                        (cons 10 (trans p1 1 0))
                        (cons 10 (trans p2 1 0))
                    )
                )
                (setq p1 (polar p1 an 0.5)
                      p2 (polar p2 an 0.5)
                )
            )
        )
        (princ)
    )
    Nearly perfect, and much cleaner but that skinny line should always be on the right or bottom. When you draw from bottom to top it is the only time it doesn't perform right.

    I love seeing all the different ways to approach the same problem, thanks!
    -Brandon

  9. #9
    I could stop if I wanted to Lee Mac's Avatar
    Join Date
    2009-03
    Location
    London, England
    Posts
    280

    Default Re: Drawing a row of four lines with Lisp.

    Quote Originally Posted by Ko.Bach276562702 View Post
    Nearly perfect, and much cleaner but that skinny line should always be on the right or bottom.
    Gotcha.

    Code:
    (defun c:test ( / an p1 p2 )
        (while
            (and
                (setq p1 (getpoint "\nSpecify 1st Point: "))
                (setq p2 (getpoint "\nSpecify 2nd Point: " p1))
                (setq an (if (equal (car p1) (car p2) 1e-10) 0.0
                             ((if (< (car p1) (car p2)) - +) (angle p1 p2) (/ pi 2))
                         )
                )
            )
            (foreach wi '(0.03125 0.03125 0.03125 0.0)
                (entmake
                    (list
                       '(0 . "LWPOLYLINE")
                       '(100 . "AcDbEntity")
                       '(100 . "AcDbPolyline")
                       '(90 . 2)
                       '(70 . 0)
                        (cons 43 wi)
                        (cons 10 (trans p1 1 0))
                        (cons 10 (trans p2 1 0))
                    )
                )
                (setq p1 (polar p1 an 0.5)
                      p2 (polar p2 an 0.5)
                )
            )
        )
        (princ)
    )
    Lee Mac Programming

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

Similar Threads

  1. LISP to Help with Drawing Iso's
    By dean.dufrene in forum AutoLISP
    Replies: 15
    Last Post: 2013-05-03, 06:32 PM
  2. Need Lisp to Extend all lines
    By mikehaff in forum AutoLISP
    Replies: 10
    Last Post: 2011-12-20, 02:03 AM
  3. AutoCAD LISP for 3D Lines to 2D Lines
    By guala5 in forum AutoCAD General
    Replies: 3
    Last Post: 2009-03-24, 12:35 PM
  4. Lisp from a Drawing
    By burchd in forum AutoLISP
    Replies: 14
    Last Post: 2008-06-11, 04:50 AM
  5. Lisp question...where to add these lines
    By d_m_hopper in forum AutoLISP
    Replies: 4
    Last Post: 2007-08-27, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •