Results 1 to 9 of 9

Thread: get coordinates of first and last point of a polyline

  1. #1
    Member
    Join Date
    2007-05
    Posts
    38
    Login to Give a bone
    0

    Default get coordinates of first and last point of a polyline

    How can i assign the coordinates of a drawn polyline to variables (firstPoint and lastPoint)? I've tried some stuff, but cannot get it to work..

    Code:
    (defun c:test (/)
      (command "pline")
      (while (= (getvar "CMDNAMES") "PLINE")
        (command pause)
      )
      (setq firstPoint (cdr (assoc 10 (reverse (car (entlast))))) ) ;first point of polyline
      (setq lastPoint (cdr (assoc 10 (car (entlast)))) ) ;last point of polyline
    );defun

  2. #2
    I could stop if I wanted to tyshofner's Avatar
    Join Date
    2004-03
    Location
    Dallas, Tx
    Posts
    229
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    Hello, try this out:

    Code:
     
    (defun c:test (/)
     (command "pline")
     (while (= (getvar "CMDNAMES") "PLINE")
      (command pause)
     )
     (setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)))))
     (setq firstpoint (nth 0 vertex_lst))
     (setq lastpoint  (nth (- (length vertex_lst) 1) vertex_lst))
     )
    );defun
    Ty

  3. #3
    Member
    Join Date
    2007-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    thanks.. that works like a charm!
    now to figure out what it actually means

  4. #4
    I could stop if I wanted to tyshofner's Avatar
    Join Date
    2004-03
    Location
    Dallas, Tx
    Posts
    229
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    The syntax of the "mapcar" function can be somewhat hard to understand. I probaly should have used a more easily understandable function, but I'll try to explain. "Mapcar" will apply a function to each element of a list. I'll break it down piece by piece:
    Code:
     
    (setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)) ) ) )
    BLUE:
    The portion in blue returns the entity list. The entity list is a list of dotted pairs.

    RED:
    the "lamda" function is essentially just like a "defun" but it can be used inside other functions (here it is used inside mapcar). So if written as it's own routine it would look like this:
    Code:
     
    (defun (x) 
    (if (= (car x) 10)
    (cdr x)
    )
    )
    This function looks at the the first element of a list, if it equals 10, the function returns the rest of the list with the first element stripped out.

    GREEN:
    This is the "mapcar" function. All it does is apply the "lambda" function to each element of the entity list obtained in the BLUE portion of the code. It then returns a list of the results.

    ORANGE:
    Here I use the "vl-remove" function to remove the "nil" results from the list returned by the "mapcar" function.

    So, first I grab the entity list (BLUE). Then use a combanation of the "mapcar" and "lambda" functions to parse the entity list (MAPCAR, GREEN) according to my conditions (LAMBDA, RED). This returns a list of the vertexes.

    Then I use the "nth" function to extract the desired points from the list.

    I hope I have explained it well enough. Probably the easiest way to see what is happening is to open the lisp in VLIDE and use the inspect tool to step through each portion of the code. Also search the help files for "mapcar".

    This task could be done a few different ways, I only used "mapcar" because well, I like to use it.

    Before I discovered "mapcar" and "lambda" I used to do this:
    Code:
    (defun c:test (/)
     (command "pline")
     (while (= (getvar "CMDNAMES") "PLINE")
      (command pause)
     )
     (setq ent (entget (entlast)))
     (setq vertex_lst nil)
     (foreach dp ent
      (if (= (car dp) 10)
       (setq vertex_lst (append vertex_lst (list (cdr dp))))
      )
     )
     (setq firstpoint (nth 0 vertex_lst))
     (setq lastpoint (nth (- (length vertex_lst) 1) vertex_lst))
    );defun
    This does exactly the same thing, only using the "foreach" function. You can see though it is essentially the same code. About the only difference is I have to make sure the "vertex_lst" variable is "nil" before I build the list and I it won't give any "nil" results so I don't have to remove them.

    Ty
    Last edited by tyshofner; 2007-06-07 at 03:26 PM.

  5. #5
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    Here is an other way to do it
    Code:
    (if (setq Ent (entsel))
      (progn
        (setq VL-Obj (vlax-ename->vla-object (car Ent )))
        (setq StartPoint (vlax-curve-getStartPoint VL-Obj ))
        (setq EndPoint (vlax-curve-getEndPoint VL-Obj ))
      )
      (princ "..no object selected" )
    )
    : ) Happy Computing !

    kennet

  6. #6
    Member
    Join Date
    2007-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    Quote Originally Posted by tyshofner
    The syntax of the "mapcar" function can be somewhat hard to understand. I probaly should have used a more easily understandable function, but I'll try to explain. "Mapcar" will apply a function to each element of a list. I'll break it down piece by piece:
    Ty
    thanks for taking time to explain. tomorrow i will print out some stuff about the functions you described, and see if I get them You never know when they'll come in handy again ..

  7. #7
    Member
    Join Date
    2007-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    Quote Originally Posted by kennet.sjoberg
    Here is an other way to do it
    : ) Happy Computing !

    kennet
    thanks! will look into that as well

  8. #8
    All AUGI, all the time CAB2k's Avatar
    Join Date
    2016-01
    Location
    Brandon, Florida
    Posts
    687
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    One more variant:
    Code:
    (and (setq Ent (car (entsel)))
         (setq StartPoint (vlax-curve-getstartpoint Ent))
         (setq EndPoint (vlax-curve-getendpoint Ent))
    )

  9. #9
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: get coordinates of first and last point of a polyline

    Quote Originally Posted by wbreedveld
    thanks! will look into that as well
    ok, you may need to run (vl-load-com) if you not already done it.
    ---------
    And MAPCAR and LAMBDA . . is a long story to read from this LINK

    : ) Happy Computing !

    kennet

Similar Threads

  1. Project Coordinates, Shared Coordinates, and the new Survey Point
    By Wes Macaulay in forum Revit - Platform
    Replies: 59
    Last Post: 2012-07-05, 06:20 PM
  2. Replies: 0
    Last Post: 2011-12-03, 09:13 AM
  3. 3D Polyline coordinates
    By lmitsou in forum AutoLISP
    Replies: 5
    Last Post: 2008-07-17, 08:49 PM
  4. Replies: 0
    Last Post: 2007-09-30, 02:56 PM
  5. Replies: 9
    Last Post: 2007-02-21, 10:43 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
  •