See the top rated post in this thread. Click here

Results 1 to 9 of 9

Thread: AutoLisp - Draw polyline between middle of inserted points

  1. #1
    Member
    Join Date
    2012-07
    Posts
    4
    Login to Give a bone
    0

    Default AutoLisp - Draw polyline between middle of inserted points

    Hello everyone,

    I'm having problems modifying/ creatig a lisp that does:
    - draw pline
    - p1 = first point = mid of (p1.1,p1.2)
    - p2 = second point = mid of (p2.1,p2.2)
    - p3 = third pint = mid of (p3.1,p3.2)
    - and so on

    What I have at the moment is:

    Code:
    (defun c:pl-mid    ()
      (setq p1-1 (getpoint "\n Insert p1-1:"))
      (setq p1-2 (getpoint "\n insert p1-2:"))
      (setq p2-1 (getpoint "\n insert p2-1:"))
      (setq p2-2 (getpoint "\n insert p2-2:"))
      (setq    px1-1 (car p1-1)
        py1-1 (cadr p1-1)
        px1-2 (car p1-2)
        py1-2 (cadr p1-2)
        px2-1 (car p2-1)
        py2-1 (cadr p2-1)
        px2-2 (car p2-2)
        py2-2 (cadr p2-2)
      )
      (setq p1x-mid (/ (+ px1-1 px1-2) 2))
      (setq p1y-mid (/ (+ py1-1 py1-2) 2))
      (setq p2x-mid (/ (+ px2-1 px2-2) 2))
      (setq p2y-mid (/ (+ py2-1 py2-2) 2))
      (command "pline"
           (list p1x-mid p1y-mid 0.0)
           (list p2x-mid p2y-mid 0.0)
           ""
      )
    )
    What I don't manage to figure out is how to implement a continuous imput of points from wich to compute the middle distance as the next point of the polyline.
    Thanks in advance for any imput.

  2. #2
    All AUGI, all the time
    Join Date
    2010-10
    Posts
    535
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Quote Originally Posted by robertdq885441 View Post
    Hello everyone,

    What I don't manage to figure out is how to implement a continuous imput of points from wich to compute the middle distance as the next point of the polyline.
    Thanks in advance for any imput.
    Middle of the last pline created? Not sure if this is your intent:

    Code:
    (defun c:pl-mid	(/ _mid p1-1 p1-2 p2-1 p2-2 mp1 mp2)  
      (defun _mid (1p 2p)
        (mapcar (function (lambda (a b) (* (+ a b) 0.5))) 1p 2p)
      )
      (if (setq p1-1 (getpoint "\n Insert p1-1:"))
        (while (and
    	     (setq p1-2 (getpoint p1-1 "\n insert p1-2:"))
    	     (setq p2-1 (getpoint p1-2 "\n insert p2-1:"))
    	     (setq p2-2 (getpoint p2-1 "\n insert p2-2:"))
    	   )
          (command "pline"
    	       "_non"
    	       (setq mp1 (_mid p1-1 p1-2))
    	       "_non"
    	       (setq mp2 (_mid p2-1 p2-2))
    	       ""
          )
          (setq p1-1 (_mid mp1 mp2))
        )
      )
      (princ)
    )

  3. #3
    All AUGI, all the time
    Join Date
    2015-10
    Location
    Belgrade, Serbia, Europe
    Posts
    564
    Login to Give a bone
    1

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Code:
    (defun c:pl-mid ( / p1 p2 mp mpl clf )
      (setq p1 (getpoint "\nPick 1st reference point: "))
      (while (and 
               (if (null p1)
                 (progn
                   (initget 128)
                   (setq p1 (getpoint "\nPick 1st reference point (enter to exit - \"C\" (close)): "))
                 )
                 t
               )
               (if (not (or (eq p1 "C") (eq p1 "c")))
                 (progn
                   (setq p2 (getpoint "\nPick 2nd reference point (enter to exit): "))
                   (if (and p1 p2) (setq mp (mapcar '/ (mapcar '+ p1 p2) '(2. 2. 2.))))
                   (if (and p1 p2) (setq mpl (cons mp mpl)))
                   (setq p1 nil)
                   (if p2 t nil)
                 )
                 (progn
                   (setq mpl (cons p1 mpl))
                   nil
                 )
               )
               (if (and (not (or (eq (car mpl) "C") (eq (car mpl) "c"))) (> (length mpl) 1)) (progn (grdraw (car mpl) (cadr mpl) 1 1) t) t)
             )
      )
      (if (or (eq (car mpl) "C") (eq (car mpl) "c")) (setq mpl (cdr mpl) clf t))
      (entmake (append '((0 . "LWPOLYLINE") (100 . "AcDbEntity") (100 . "AcDbPolyline"))
                        (list (cons 90 (length mpl)))
                        (if clf '((70 . 1)) '((70 . 0)))
                        (list (cons 38 (caddr (trans (car mpl) 1 (trans '(0.0 0.0 1.0) 1 0 t)))))
                        (mapcar '(lambda (x) (cons 10 (trans x 1 (trans '(0.0 0.0 1.0) 1 0 t)))) (reverse mpl))
                        (list (cons 210 (trans '(0.0 0.0 1.0) 1 0 t)))
               )
      )
      (command "_.regen")
      (princ)
    )
    Last edited by marko_ribar; 2013-11-16 at 01:36 PM.

  4. #4
    Member
    Join Date
    2012-07
    Posts
    4
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    marko_ribar thank you, it's exactly what I was looking for. It seems so complicated for such a "simple thing" in my opinion
    I tryied to modify the code pbejse posted into something like:
    Code:
    (defun c:pl-mid ()
      (defun _mid (p1 p2)
        (mapcar (function (lambda (a b) (* (/ a b) 2))) 1p 2p)
        (setq p1-1 (getpoint "\n insert p1-1:"))
        (setq p1-2 (getpoint "\n insert p1-2:")))
        (command "pline"
    	     (setq mp (_mid p1-1 p1-2))
    	     ""
        )
     (princ)
    )
    but with no luck. i know I need a "while" loop somewere but I can't figure it out. (you do learn something new everyday )

    pbejse thanks to you i had to look into what mapcar and lambda mean and do.

  5. #5
    All AUGI, all the time
    Join Date
    2010-10
    Posts
    535
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Quote Originally Posted by robertdq885441 View Post
    but with no luck. i know I need a "while" loop somewere but I can't figure it out. (you do learn something new everyday )

    pbejse thanks to you i had to look into what mapcar and lambda mean and do.
    You are welcome, but i'm still not sure of your intent

    This?

    Code:
    (defun c:pl-mid	( / _mid p1-1 p1-2 mp)
      (defun _mid (1p 2p)
        (mapcar (function (lambda (a b) (* (+ a b) 0.5))) 1p 2p)
      )
      (if (and (setq p1-1 (getpoint "\n insert p1-1:"))
    	   (setq p1-2 (getpoint p1-1 "\n insert p1-2:"))
    	   (setq mp (_mid p1-1 p1-2))
          )
        (while
          (and (setq p2-1 (getpoint mp "\n insert p2-1:"))
    	   (setq p2-2 (getpoint p2-1 "\n insert p2-2:"))
          )
           (command "pline" "_non" mp "_non" (setq mp (_mid p2-1 p2-2)) "")
        )
      )
      (princ)
    )
    or

    Code:
    (defun c:pl-mid2 ( / _mid p1-1 p1-2 mp)
      (defun _mid (1p 2p)
        (mapcar (function (lambda (a b) (* (+ a b) 0.5))) 1p 2p)
      )
      (if (and (setq p1-1 (getpoint "\n insert p1-1:"))
    	   (setq p1-2 (getpoint p1-1 "\n insert p1-2:"))
    	   (setq mp (_mid p1-1 p1-2))
          )
      (progn
          (command "_Pline" mp)
          (while (> (getvar "CMDACTIVE") 0)
    	(if (and (setq p2-1 (getpoint mp "\n insert p2-1:"))
    	   (setq p2-2 (getpoint p2-1 "\n insert p2-2:"))
    	)
                (command (setq mp (_mid p2-1 p2-2)))
    	  (command "")
              )
    	)
            )
        )
      (princ)
    )
    Makes me wonder why not just use _m2p osnap when selecting the two other points and do away with the lisp code?

    Curios is all
    Last edited by pbejse; 2013-11-15 at 06:40 AM.

  6. #6
    Member
    Join Date
    2012-07
    Posts
    4
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    So my problem is that I have some topographic surveys of some roads with points taken only for the edge of the road. I need to draw a polyline that will represent the center line of the road. Each vertex of this polyline will be situated at the mid distance between the edge of the road. I have attached an example.

    The problem with the last 2 versions you posted is that I end with a lot of polylines instead of one

    I tried to take you're code line by line hoping it will give me a starting point but with no luck. I didn't knew what "_non" does, I remember reading about it somewhere but I forgot were and I have so little time at the moment .

    P.S. Next time I'l try to be more explicit and post when not under the effect of
    Attached Files Attached Files

  7. #7
    All AUGI, all the time
    Join Date
    2010-10
    Posts
    535
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Quote Originally Posted by robertdq885441 View Post

    The problem with the last 2 versions you posted is that I end with a lot of polylines instead of one
    The second code will only generate one polyline (defun c:pl-mid2 ()

    Anyhoo, can't open the file though stuck in 2009 and i'm using my netbook where DwgTrueView is not installed. can you save the file to a lower version so i can have a look now rather than later?

  8. #8
    Member
    Join Date
    2012-07
    Posts
    4
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Sure, I saved it in 2004 format.

    I didn't saw that the command for the second routine was pl-mid2. This one work's great thank you.
    Attached Files Attached Files

  9. #9
    All AUGI, all the time
    Join Date
    2010-10
    Posts
    535
    Login to Give a bone
    0

    Default Re: AutoLisp - Draw polyline between middle of inserted points

    Quote Originally Posted by robertdq885441 View Post
    Sure, I saved it in 2004 format.

    I didn't saw that the command for the second routine was pl-mid2. This one work's great thank you.
    Glad you had it sorted robertdq885441, play around with the code to include setting the preferred layer at the start of the routine and setting it back after the program is done

    HINT: (Setvar/Getvar)
    Also try to include an *error* sub for completeness

    Cheers <--- save me a mug next time you step out for more
    Last edited by pbejse; 2013-11-15 at 03:31 PM.

Similar Threads

  1. Draw Polyline from list of points
    By LSElite in forum AutoLISP
    Replies: 1
    Last Post: 2015-04-21, 03:53 AM
  2. 2013: z value at intersection points in 3d polyline and polyline
    By jaychandran in forum AutoCAD Civil 3D - General
    Replies: 1
    Last Post: 2013-10-30, 05:20 PM
  3. How to draw a 3d polyline from a list of points?
    By GreyHippo in forum AutoLISP
    Replies: 2
    Last Post: 2006-12-15, 06:35 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
  •