TheSwamp
Code Red => .NET => Topic started by: themethodman on December 14, 2020, 11:39:51 PM
-
Does anyone have a snippet that will loop through modelspace, and for each entity, explode, and pass on the xdata of the parent entity to the exploded child entities?
Did a search, couldn't find anything that worked.
-
First attempt, this gets stuck in an endless loop :idiot2:
//Loop through entities in model space
foreach (ObjectId objectId in wModelSpace)
{
var curve = (Curve)tr.GetObject(objectId, OpenMode.ForRead);
double extdia = ExtensionMethods.PipeExtDiaForObject(curve, regAppName);
DBObjectCollection tempDBObjects = new DBObjectCollection();
curve.Explode(tempDBObjects);
foreach (Curve obj in tempDBObjects)
{
Polyline polyObj = ExtensionMethods.ConvertToPolyline(obj);
polyObj.SetDatabaseDefaults();
ms.AppendEntity(polyObj);
tr.AddNewlyCreatedDBObject(polyObj, true);
ExtensionMethods.SetPipeExtDiaOnObject(tr, polyObj, (float)extdia, regAppName);
}
//erase original curve
var originalCurve = (Entity)tr.GetObject(curve.ObjectId, OpenMode.ForWrite);
originalCurve.Erase();
}
-
The issue with your code is that while looping through ModelSpace for each Curve, your code also explodes the Curve and adds the exploded new entities into ModelSpace, thus the seemingly endless loops. What you need to do is to do it in 2 steps: getting a list of target entities (Curve, in your case); then explode (and append the new entities into ModelSpace).
The code would be:
var curveIds=wModelSpace.Case<ObjectId>(); //Assume you are 100% sure all entities in ModelSpace is Curve!
foreach (var id in curveIds)
{
Curve curve=(Curve)tran.GetObject(.....)
// Do your explode/ZData handling worke
}
Or:
var curves=new List<Curve>();
foreach (ObjectId id in wModelSpace)
{
// Make sure only Curve is targeted
var curve=tran.GetObject(id, OpenMode.ForRead) as Curve;
if (curve!=null) curves.Add(curve);
}
if (curves.Count>0)
{
foreach (var c in curves)
{
// Do your explode/Xdata handling work
}
}
HTH
-
That is actually a super straight forward approach. Don’t know why I didn’t think of it. Thanks!!