Getting a list of polyline vertices using LISP

Here is one way to generate a list of polyline vertices using lisp. This function requires one argument, a polyline entity. The polyline can be the old style POLYLINE entity (2D or 3D), or it can be an LWPOLYLINE entity.

(defun getcoords (ent)
  (vlax-safearray->list
    (vlax-variant-value
      (vlax-get-property
	(vlax-ename->vla-object ent)
	"Coordinates"
      )
    )
  )
)

Example:

(getcoords (car (entsel)))

This function will return a list of coordinates. If the polyline is a LWPOLYLINE entity, the list will be in the form of (x y x y x y x y), and the number of vertices can be determined by dividing the length of the list by 2.

If the polyline is a POLYLINE entity (2D or 3D), then the list will be in the form of (x y z x y z x y z), and the number of vertices can be determined by dividing the length of the list by 3.

A sample of the output is shown below:
(-3.65553 2.56916 -0.83241 5.477 3.9698 3.23796 6.50188 4.54649 10.0235 2.71455 14.8257 6.78553)

Incorrect output in some cases

Here is a sample output for a polyline with >270 vertices:

.4668 2.34008 -12.9724 4.87739 -14.7157 7.57328 -17.3307 7.57328 -19.5495 
8.04903 -21.6099 7.73187 -24.6211 6.85966 -27.1569 5.74959 -28.9794 4.40164 
-32.1492 3.84661 -33.8133 3.84661 -33.8133 0.912843 -37.4584 -0.0386474 
-38.0131 -4.39965 -39.8357 -7.72987 -39.8357 -12.0909 -40.7074 -17.3241 
-40.8659 -18.1283 -40.8902 -22.7159 -40.3112 -25.3325 -38.0924 -26.2839 
-37.2856 -28.2662 -36.5075 -30.9621 -35.3189 -33.103 -33.3378 -34.2131 -32.7831 
-35.2438 -31.8322 -37.0675 -29.8511 -41.0321 -27.3153 -41.0321 -24.6211 
-43.8866 -20.9759 -45.1552 -16.2213 -45.1552 -14.9534 -42.4593 -15.6666 
-41.9043 -20.9759 -39.922 -23.3532 -38.8119 -25.3343 -37.1468 -26.6022 -35.4024 
-27.7116 -33.0237 -29.2964 -30.8035 -30.2473 -28.3455 -31.8322 -28.3455 
-31.8322 -25.0946 -33.1001)2938 -14.4451 -18.9321 -16.6335 -17.9296 -17.3469 
-16.8101 -17.8835 -15.9681 -18.493 -15.0116 -19.8948 -14.2638 -21.2395 -13.1321 
-22.4336 -12.1271 -22.4336 -10.3735 -23.7874 -7.54426 -24.2808 -6.51317 
-25.2737 -4.36133 -27.1846 -3.37108 -28.954 -2.31011 -30.7233 -1.53205 -32.2096 
-0.612541 -33.5423 0.211974 -34.5452 1.15576 -35.8191 2.146 -37.1638 3.06552 
-39.2871 4.12649 -41.0707 5.01775 -43.1797 5.11674 -44.7367 5.18747 -44.7367 
4.19722 -44.9491 3.06552 -44.1705 3.06552 -43.4628 3.06552 -41.4103 1.65088 
-38.4378 0.589903 -34.9698 -0.824736 -32.8466 -2.38084 -31.2895 -3.65401 
-29.4494 -5.70523 -27.4677 -7.04914 -26.2645 -8.81743 -25.486 -10.6565 -24.6367 
-12.354 -23.6459 -13.6272 -22.1596 -14.9004 -20.6026 -16.5979 -19.2578 -17.8004 
-17.7008 -18.8614 -15.9113 -20.0807 -15.436 -21.4784 -13.3835 -23.8833 -11.4018 
-26.076 -12.1804 -27.2784 -13.1514 -27.5298 -14.4407 -27.5298 -15.0358 -26.836 
-15.0358 -26.1918 -15.0358 -26.1918 -14.7382 -25.0024 -14.7382 -23.7635 -16.573 
-22.475 -18.0606 -21.5334 -19.449 -20.6909 -20.1929 -19.898 -20.9367 -19.0555 
-22.0276 -18.1635 -23.664 -16.7263 -24.9037 -14.9423 -26.1 -13.2206 -27.1351 
-11.4733 -27.8104 -10.3332 -28.8706 -9.73875 -29.8128 -8.54937 -30.9037 
-7.16177 -32.2426 -6.61663 -32.8376 -7.65734 -31.598 -8.94583 -30.6062 -10.5812 
-29.664 -11.6219 -28.6227 -12.7122 -27.7797 -15.3883 -27.2343 -16.4785 -26.2425 
-17.8166 -25.5414 -18.7625 -23.664 -20.1953 -22.7218 -21.4343 -21.6805 -22.7723 
-20.3416 -24.259 -19.9449 -25.1015 -19.2011 -25.5971 -18.0606 -26.836 -18.0606 
-26.836 -16.3746 -27.6289 -15.0358 -28.1245 -12.8539 -28.7688 -11.1679 -29.0661 
-9.13486 -28.9174 -5.21745 -28.2732 -4.07695 -27.7776 -3.18437 -26.8856 
-2.49015 -25.6962 -2.39097 -24.3582 -2.2803 -22.8649 -2.19262 -21.6821 -2.53973 
-20.8892 -3.28355 -19.7989 -4.32488 -18.56 -4.52323 -17.9653 -6.06044 -16.429 
-7.3993 -14.6945 -9.03569 -13.5547 -9.97784 -12.8609 -11.9613 -11.9688 -13.449 
-11.4733 -13.449 -11.4733 -15.9779 -10.0361 -16.8209 -9.59008 -18.4573 -7.95469 
-19.3995 -6.86442 -19.9449 -5.42726 -20.1929 -4.38656 -20.1929 -3.14762 
-19.3995 -2.30515 -17.3664 -3.09807 -17.3168 -4.13877 -16.7713 -5.17948 
-13.0027 -7.50867 -12.0605 -8.30159 -10.077 -9.59008 -8.24229 -10.6803 -6.70507 
-12.0184 -5.91168 -12.96 -3.18437 -15.3883 -1.39922 -17.321 0.0388079 -20.344 
1.17932 -22.9706 0.980971 -24.259 0.137984 -25.6467 -1.15129 -26.6378 -1.94469 
-27.8767 -3.33314 -29.0166 -5.41581 -29.9086 -8.93651 -30.9988 -10.0274 
-30.4537 -14.8374 -30.2059 -16.4242 -30.3546 -18.2589 -29.9581 -19.4986 
-29.2643 -20.1929 -28.4219 -20.7383 -27.1334 -21.6805 -24.9033 -22.0323 
-24.0705 -23.7136 -22.7228 -24.7053 -21.0874 -24.7053 -22.029 -24.9532 -24.259 
-24.259 -26.7369 -24.3582 -27.8767 -22.6366 -29.6926 -21.2886 -30.3266 -21.0508 
-32.5454 -16.5312 -35.7943 -13.5974 -35.7943 -13.5974 -34.051 -11.853 -33.1001 
-9.15711 -34.5264 -8.91924 -36.666 -6.06476 -35.0811 -5.27185 -33.1793 -1.86234 
-33.1001 -0.831557 -34.7642 1.23001 -33.8925 2.81582 -30.1681 2.49866 -29.1379 
5.03597 -25.4928 5.35314 -22.4023 5.35314 -21.3721 3.76732 -18.9156 2.10221 
-17.0138 0.833553 -15.0327 -0.276518 -13.6856 -1.70376 -12.5762 -3.05171 
-10.8328 -3.92391 -9.80266 -6.14406 -8.45553 -7.57129 -6.87067 -10.9015 
-3.38398 -6.14406 -3.38398 -6.14406 -5.20657 -4.55824 -6.47445 -1.86234 
-7.98007 -0.435106 -9.72341 0.595682 -11

Notice that there is a ")" character in the output. Also, with other lines, I find coordinates to be incorrect.

I have been using getcoords regularly to extract co-ordinates from polylines. I'd be very grateful if you could help me with this. An alternative will be useful too. Thanks for the good work.

If you will wblock out this

If you will wblock out this polyline and email me the drawing (of the polyline *only*), I will take a look as time permits.

Is there any update to this?

I have also noticed a slight error between the drawing database (10. codes) of polylines and the actual co-ordinate although ID always gives the correct result, just wondered if there was any reason for it.

Thanks

Dave.

I never received anything to

I never received anything to check. As far as discrepancies between DXF10 codes and what LIST returns, are you sure you are checking out to the maximum number of decimal places?

See also:
http://blog.jtbworld.com/2008/04/understanding-floating-point-precision....

inserting blocks at vertices

Hello,

I was wondering if you be able to help me. I would like a lisp which allows me to select multiple polylines, and then to insert a block I have made at each vertex of those polylines?

What I'm trying to do is to pick the polylines, then click and have it insert the block in place at the vertex of the polyline.

Many thanks,

Brian

Brian, take a look at the

Brian, take a look at the routine here:
http://www.cadforum.cz/cadforum_en/qaID.asp?tip=5384

Getcoord defun

Do you have a version of this defun that will work on blocks that inserted in to a drawings? If so can you email.

Thanks

 


All content is copyright © CAD PANACEA 2005-2011 unless otherwise noted.
All content of CAD PANACEA is solely my own personal thoughts and opinions and do not those of any other entity or person.
All comments posted to this blog are the sole responsibility of the person making the comment.

 

Google, as a third party vendor, uses cookies to serve ads on this site. Google's use of their cookies enables it to serve ads to users based on their visit to your sites and other sites on the Internet. You may opt out of the use of these cookies by visiting the Google ad and content network privacy policy.

free hit counters

Powered by Drupal, an open source content management system