Trying to figure out how to replace the ParentSubD of selected edges

I wrote a little command that takes in a selection of SubD edges and a Curve - creates faces perpendicular to that curve from the Edges selected and joins it all together.

I have it all working fine but I am joining things to the original SubD, that I identified by getting the ParentSubD from the edges. Now when the command is done running, I add the joined SubD to the document and that works fine as well, but now I have 2 SubDs.. No huge issue but I would rather replace the original one.

doc.Objects.Replace seems the logical way to accomplish that but it requires the objRef of the original and although I have identified the SubD, I can’t seem to find a way to get its objRef.

Any thoughts on how to accomplish that?

Hi @robertVM,

Can you show us how you do this?

Thanks,

– Dale

Selecting the edges?

Here is the command - I demarked what every section does with comments. It is the last command, the replace one that is currently commented out, that I can’t seem to get to work.

Thanks you for looking at it:

//Select SubD Edges
var go = new GetObject();
go.SetCommandPrompt(“Select SubD edges”);

go.GeometryFilter = ObjectType.EdgeFilter;
go.SubObjectSelect = true;
go.EnablePreSelect(true, true);
go.DeselectAllBeforePostSelect = false;

// Limit to SubD
go.SetCustomGeometryFilter((rhinoObject, geometry, componentIndex) =>
{
return rhinoObject.ObjectType == ObjectType.SubD &&
componentIndex.ComponentIndexType == ComponentIndexType.SubdEdge;
});

go.GetMultiple(1, 0);

if (go.CommandResult() != Result.Success)
return go.CommandResult();

// Iterate through selected edges and add them to the edges list
RhinoList edges = new RhinoList();
for (int i = 0; i < go.ObjectCount; i++)
{
var objRef = go.Object(i);
var subdEdge = objRef.SubDEdge();

 if (subdEdge != null)
 {
    edges.Add(subdEdge);

 }

}

//Identify and set the parent SubD

SubD mama = new SubD();
if (edges.Count < 1) return Result.Failure;
else { mama = edges[0].ParentSubD;}

//get the curve to extend to:
go.SetCommandPrompt(“Select Curve to extend to”);
go.GeometryFilter = ObjectType.Curve;
go.SubObjectSelect = false;
go.SetCustomGeometryFilter((obj, geom, compIdx) =>
{
return geom.ObjectType == ObjectType.Curve;
});
go.GetMultiple(1, 1);
if (go.CommandResult() != Result.Success)
return go.CommandResult();
Curve c1 = go.Object(0).Curve();

//Create the SubDs by iterating and calling the function

SubD subdz = new SubD[edges.Count];

for (int i = 0; i < edges.Count; i++)
{
subdz[i] = SupportFunctions.CreateSubdFromEdgeToCurve(edges[i], c1);
}

//Join it all
List subD2join = new List();
subD2join.Add(mama);
for (int i = 0; i < subdz.Length; i++)
{
subD2join.Add(subdz[i]);
}

SubD joined = SubD.JoinSubDs(subD2join, doc.ModelAbsoluteTolerance, false);

doc.Objects.AddSubD(joined[0]);

//doc.Objects.Replace

return Result.Success;

There is your objref.

– Dale

Yeah - I figured that, and also that it was a scope thing so I tried declaring and initializing it before the for loop but that changed nothing, doc.Objects.Replace(objRef, joined[0]); doesn’t actually do anything. It doesn’t throw an error. I also checked to make sure that objRef is not null.

My thinking was that the object reference is to the edge and not the parent SubD and that this is why it doesn’t work for me and why I figured I need a way to get the ObjRef of the parent Subd. Are you saying they should be the same?

Thanks for looking at this,

Robert

In the end, there was a solution and I got everything to work fine.

A few notes for anyone that runs into the problem:

Dale is correct in the way to identify the ObjRef - I proved that by first unselecting all objects in my code ( doc.Objects.UnselectAll(); ) followed by selecting the original SubD by the reference ( doc.Objects.Select(objRef); )

Unfortunately using the reference to replace my original subD simply does not work - Neither does using the reference to delete it from the object table after adding the new subD.

What did end up working was using the doc.Objects.Select(objRef); as mentioned before. Once I had it as the only object selected, cycling through selected objects to delete them (short ride obviously, but an existing function I had) did the trick.

All solved and working fine. Thanks for the assist