Python Script to Extract Polyline X, Y Values

I am trying to extract out the coordinates (x,y) of all selected polylines in my file. I need to then apply to some filtering functions to the points list to simplify the shapes and reimport the points to recreate the simplified polylines. Does anyone have any advice how to retrieve this information?

Is this in Revit or AutoCAD?

It is in Civil 3D 2025.

Would a lsp that exports them to .xlsx or csv help?

Why Python and not using nodes?

That could be a work-around, but I would try to avoid anything that requires users to get out of Dynamo scripts for this. Extracting the polyline vertices is in the middle of other scripts, so keeping it Dynamo would make things run smoother.

I keep getting errors from nodes when I try to extract the points out that the polylines branch, the shapes I am trying to extract data from can we quite complex and I have built a separate script that will reduce the points in the shape, but I need the points extracted to run the other script.

Hm, maybe the points are very close to each other. Some nodes tend to round off coordinates which doesn’t happen in Python. Do you have an example drawing with such a polyline?

Attached is an example drawing with what could be real polylines I need to extract the data from. In this case, they boundaries need to be simplified to removed excess vertices (both duplicates, and unnecessary vertices) and I am trying to find/think of a way to simplify the shapes in some instances. Some polylines are already pretty simple, but some of the others get more complex. Screenshot below:

Daylight Boundaries_Example.dwg (3.4 MB)

You first need to define what “simplify” means. What counts as unnecessary? Is it only curves that turn inward or would small bump-outs need to be removed as well? Is there a size limit? At what scale does a group of excess vertices become part of the overall shape? All of these questions determine what kind of logic you need in order to “simplify” the shape. We can make assumptions, but that doesn’t help if they don’t match your requirements.

Understood, I think anytime the curves turn inward, then that creates unnecessary lines and I would like to remove them. That is where I am stuck, but I couldn’t think of how to verbalize that until you mentioned it your response - thank you! There really isn’t a size limit. Not quite sure what you are trying to explain to me with the scaling issue, but I have wrote a python script that removes the majority of the excess vertices while maintaining the same shape, but it does NOT remove the curves that turn inward.

Attached is my current dynamo script that has a couple python scripts in there to help remove the unnecessary vertices along the polyline, while retaining the original shape. The other problem with this script is that I have it flatten the list, so this works great for a single polyline, but in reality I need it to handle hundreds of polylines at once, but I am unsure how to do that at the moment.
_2_OPTIMA Boundary Creation_2025.dyn (38.7 KB)

I’d start by finding the “interior” of the boundary by getting something like the centroid of the closed surface. Then you should be able to do some math when checking the directionality of each curve to see which curves are interior to the rest.

Hi there,

This doesn’t include anything to do with the filtering (filtering points is non-trivial and there are tons of ways to do that for different use cases), but here’s some python code I quickly threw together to extract the point coordinates from selected polylines in a python node if you still want to go that route:

import sys
import clr

clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

from Autodesk.DesignScript.Geometry import Point
from Autodesk.Civil.ApplicationServices import *

dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

with adoc.LockDocument():

    with adoc.Database as db:

        with db.TransactionManager.StartTransaction() as t:
            
            errorReport = None
            
            try:
                
                polyLineList = []
                selectedPolylines = IN[0]
                
                for polyline in selectedPolylines:
                    
                    polyLinePoints = []
                    realObject = polyline.AcObject
 
                    if isinstance(realObject, Polyline):
                        
                        indexNum = int(realObject.EndParam)
                         
                        for i in range(indexNum+1):                            
                            point = realObject.GetPoint2dAt(i)                            
                            polyLinePoints += [[point.X,point.Y]]
                            
                        polyLineList += [polyLinePoints]
            
            except:
            
                import traceback
                errorReport = traceback.format_exc()
                
            t.Commit()

if errorReport == None:
    OUT = polyLineList
else:
    OUT = errorReport

You can use the select objects node to select the polylines you want to extract points from, as shown below:

Thoughts on why I am getting this from the output?

I like this, I will try to think on how to get the centroid of the shapes and then a way to determine whether a curve is projecting towards that point or not as a filter mechanism .

That’s just one option. You could also look for points that continue along the curve’s projected path by a given amount.

It looks like it’s pulling an invalid index from the polyline. Can you share the CAD file? I didn’t test this script very much.

Here’s the example dwg I used for testing, and here’s my dynamo file.

ExtractPoints.dyn (7.7 KB)
POINT EXTRACTION EXAMPLE.dwg (1.7 MB)

Here are my files
Polyline Python XY Extraction.dyn (12.7 KB)
Polyline Boundary XY Extract Test File.dwg (1.6 MB)

After looking at your file, it seems like it could be giving this in response because my polylines are closed? I noticed the polylines in your test .dwg weren’t closed, and it does work, but if I add a closed polyline to your test file, it gives the same output I shared previously. Thoughts on how to get this extraction to work for closed polylines?