FeatureLine to Polyline 2D

Nice code sample @zachri.jensen :grinning: , if obj.Closed is True we don’t need to reduce obj.PointsCount by 1. Something like this works if obj is closed or not. See lines 35 to 39. Also if obj.Closed is True I’d also recommend closing the Polyline instance being create, see lines 73 to 75.

import clr

clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd') 

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

from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil import *

def extract_basecurve(featureLines):
	
	output=[]
	obj=[]
	
	if not featureLines:
		return
	
	if not isinstance(featureLines,list):
		featureLines = [featureLines]
	
	adoc = Application.DocumentManager.MdiActiveDocument
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				for featureLine in featureLines: 
					obj = featureLine.InternalDBObject
					if isinstance(obj, FeatureLine):
						# Get bulge values
						bulges=[]
						if obj.Closed is True:
						    count = obj.PointsCount  
						else:
						    count = obj.PointsCount - 1
						for i in range(count):
							bulges.Add(obj.GetBulge(i))
						bulges.append(0)
						
						# Get all points
						pntsAll=obj.GetPoints(FeatureLinePointType.AllPoints)
						lstPntsAll=[]
						for i in pntsAll:
							lstPntsAll.append(Point2d(i.X,i.Y))
							
						# Get PI points
						pntsPI=obj.GetPoints(FeatureLinePointType.PIPoint)
						lstPntsPI=[]
						for j in pntsPI:
							lstPntsPI.append(Point2d(j.X,j.Y))
						
						# Get indices of PI points
						index=[]
						for pt in lstPntsPI:
							index.append(lstPntsAll.index(pt))
						
						# Create filtered list of bulge values
						filterBulge=[]
						for k in index:
							filterBulge.append(bulges[k])							
						
						# Polyline constructor
						pl = Polyline()
						bt = t.GetObject(db.BlockTableId, OpenMode.ForRead)
						btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
						
						# Add vertices
						for l in range(0,len(lstPntsPI)):
							pl.AddVertexAt(l,lstPntsPI[l],filterBulge[l],0,0)
						# Close the polyline where applicable
						if obj.Closed is True:
						    pl.Closed = True
						# Add entity to database
						btr.AppendEntity(pl)
						t.AddNewlyCreatedDBObject(pl, True)
						output.append(pl)						
				t.Commit()			
		return output
	
OUT = [extract_basecurve(x) for x in IN[0]]
2 Likes