Clearing Mesh From Rhino

I’ve created a sizeable GH component in C# to run through Galapagos and while the results look promising I’d like to tidy up the last bit of a strange Mesh behaviour before outputting nice renders.

For the most part the geometry and cost function can be calculated with a simple analytical model and is working as expected. However, when I toggle the Render output the RhinoList<Mesh> from Render gives very strange artifacts that, of course, can’t be baked in any reasonable fashion. I would like to write some sort of clearing function like can be done in three.js but I haven’t found the right combination of Rhinocommon methods yet and am looking for suggestions.

The 2 main approaches I’ve tried so far from my searches about the problem are:

  1. Find all the meshes in the active doc. But there doesn’t seem to be anything there to work with when I checked ot.Count with Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects so there were no objects to Delete.
  2. I looped through the RhinoList applying Mesh.Dispose() to each but that didn’t have any affect.

Hi, it’s hard to say without more detailled information as to how your script operates, but it seems to me that your “Render” output is outputting all the iterations generated by galapagos. Is that correct ? If so, a solution would be to clear the render mesh list every iteration to keep only the last elements, or the “best” elements depending on how your code functions. The method to clear the list is simply .Clear() if that’s what you were looking for. Hope that helps, but as I said it’s hard to say without slightly more information.

The Galapagos solver is only using the Cost output from the component which is a double. The Render output of the component outputs a `RhinoList’ where I’ve neatly composed everything within the component.

My goal was not to render a Mesh for every iteration. I was hoping that once the solver had run I could then initiate the lowest cost solution and toggle the Render? input to output the mesh and bake it. Then I’d have a good baked mesh to use for other presentations in the project. I’ve tried several different configurations but I’m going to try the .Clear() method. Not sure why it’s not coming up in my searches of the problem.

  protected override void SolveInstance(IGH_DataAccess DA)
    {
        // Declare Solver Variables
        bool run_toggle = false;
        bool geometry_toggle = false;
        double cost = 0.0;
        RhinoList<Mesh> render = new RhinoList<Mesh>();
        SetupTemplate setup = new SetupTemplate(DA);
        DesignTemplate design = null;

        // Use the DA object to retrieve the data inside the first input parameter.
        if (!DA.GetData(0, ref run_toggle)) { return; }
        if (!DA.GetData(1, ref geometry_toggle)) { return; }

        // Execute Main Functions
        if (run_toggle is true)
        {
            // Create Test Build
            GeometryEngine ge = new GeometryEngine(ref setup);
            design = ge.Main_Geometry_Function();

            // Calculate Cost
            CostFunction cf = new CostFunction(ref setup, ref design);
            cost = cf.Main_Cost_Function();

            //Execute Rendered output
            if (geometry_toggle is true)
            {
                RenderEngine re = new RenderEngine(ref setup, ref design);
                render = re.Main_Rendering_Function();
            }
            else if (geometry_toggle is false)
            {
                // https://developer.rhino3d.com/api/RhinoCommon/html/Overload_Rhino_DocObjects_Tables_ObjectTable_Delete.htm
                Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;
                Console.WriteLine($"\nDoc Object  {ot.Count}");
            }
        } else
        {
            design = new DesignTemplate();
            render = new RhinoList<Mesh>();
        }

        // Use the DA object to assign a new String to the first output parameter.
        DA.SetData(0, design.Roof);
        DA.SetDataList(1, design.Columns);
        DA.SetData(2, design.MidTierPlane);
        DA.SetData(3, cost);
        DA.SetDataList(4, render); 
    }

It took a couple of tries to figure out where the Clear() method needed to go but it’s inside the class if that helps anyone in need of a solution in the future. Can’t really explain why though since the list itself is newly initialized by the solver every time? or at least so I assumed?

    public static RhinoList<Mesh> render_ouput = new RhinoList<Mesh>();

    public RhinoList<Mesh> Main_Rendering_Function()
    {
        render_ouput.Clear();

        // Create closed columns
        render_ouput.AddRange(RenderColumnMesh());

        // Create roof plane
        render_ouput.AddRange(RenderRoofMesh());

        // Create mid tier
        render_ouput.AddRange(RenderMidTierMesh());

        // Create foundation
        render_ouput.AddRange(RenderFoundation());

        return render_ouput;
    }

Great ! The reason is that every iteration you called on the Main_Rendering_Function(), which only executed the code that was in it, that’s why it kept adding elements to your list without clearing the old ones. In this case, initializing your render_output list in your function rather than as a public variable would have also worked if I’m not mistaken.