Author Topic: Explode entity and keep xdata  (Read 318 times)

0 Members and 1 Guest are viewing this topic.

themethodman

  • Mosquito
  • Posts: 9
Explode entity and keep xdata
« 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.

« Last Edit: December 14, 2020, 11:42:59 PM by themethodman »

themethodman

  • Mosquito
  • Posts: 9
Re: Explode entity and keep xdata
« Reply #1 on: December 14, 2020, 11:51:05 PM »
First attempt, this gets stuck in an endless loop  :idiot2:

Code: [Select]
                    //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();
                    }

n.yuan

  • Bull Frog
  • Posts: 297
Re: Explode entity and keep xdata
« Reply #2 on: December 16, 2020, 09:37:50 AM »
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


Methodman

  • Mosquito
  • Posts: 4
Re: Explode entity and keep xdata
« Reply #3 on: December 16, 2020, 06:33:47 PM »
That is actually a super straight forward approach. Donít know why I didnít think of it. Thanks!!