Hi!
I am trying to create floors from linked DWG. I have those floors' contours drawn with polylines in AutoCAD. I've created this method, generating a dictionary with all the rooms' CurveLoops:
private Dictionary<string, IList<CurveLoop>> ExtractRoomContours(Document doc, string targetLayer, string roomType)
{
Dictionary<string, IList<CurveLoop>> roomContours = new Dictionary<string, IList<CurveLoop>>();
// find all ImportInstances (linked DWGs) in the document
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(ImportInstance)).WhereElementIsNotElementType();
ImportInstance importInstance = collector.First() as ImportInstance;
// extract the geometry
GeometryElement geomElement = importInstance.get_Geometry(new Options());
foreach (GeometryObject geomObj in geomElement)
{
if (geomObj is GeometryInstance geomInstance)
{
GeometryElement geomInstanceElement = geomInstance.GetInstanceGeometry();
foreach (GeometryObject geomSubObj in geomInstanceElement)
{
// process curves
if (geomSubObj is PolyLine polyLine)
{
GraphicsStyle graphicsStyle = doc.GetElement(polyLine.GraphicsStyleId) as GraphicsStyle;
if (graphicsStyle?.GraphicsStyleCategory.Name == targetLayer)
{
roomContours.Add(roomType, CreateCurveLoopFromPolyLine(polyLine));
TaskDialog.Show("Test", $"File: {importInstance.Name} | Polyline: {polyLine.GetOutline().GetDiagonalLength()}");
}
}
}
}
}
return roomContours;
}
It is calling another method, meant specifically to generate CurveLoops from polylines:
private IList<CurveLoop> CreateCurveLoopFromPolyLine(PolyLine polyline)
{
try
{
IList<XYZ> polyPoints = polyline.GetCoordinates();
CurveLoop curveLoop = new CurveLoop();
IList<CurveLoop> curveLoopList = new List<CurveLoop>() as IList<CurveLoop>;
for (int i = 0; i < polyPoints.Count - 2; i++)
{
Line midSedment = Line.CreateBound(polyPoints[i], polyPoints[i+1]);
curveLoop.Append(midSedment);
}
Line endSegment = Line.CreateBound(polyPoints.Last(), polyPoints.First());
curveLoop.Append(endSegment);
curveLoopList.Add(curveLoop);
return curveLoopList;
}
catch (Exception e)
{
TaskDialog.Show("Warning", e.Message);
return null;
}
}
The problem is it is failing to generate 2 out of 3 very simple polylines (just basic rectangles with 4 points each). This is the Exception I get:
![danailmomchilov_0-1736716132515.png danailmomchilov_0-1736716132515.png]()
I am just not sure as to the order in which polylines' points are presented in the API and I suspect the error might be coming from this line:
Line endSegment = Line.CreateBound(polyPoints.Last(), polyPoints.First());
Is it possible that those two points are identical in a closed rectangle? However, it seems unlikely, as the code is actually generating one of the CurveLoops successfully. Any ideas on what I might be doing wrong?