Export both curves and meshes into a 3DM file?

is there a way to get the script to export both curves and meshes into a 3DM file?

I’ve tried modifying the C# but I get stuck on how to add both meshes and curves together as a single list… (I am a complete novice at C#). My attempt so far:

  private void RunScript(Mesh x, Curve y, string z, ref object A)
  {
    Export3dm(x, y, z);


  }

  // <Custom additional code> 
  protected void Export3dm(Mesh mesh, Curve crv, string path)
  {
    List<Guid> AList = new List<Guid>();
    List<Guid> BList = new List<Guid>();
    Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;

    System.Guid A = ot.AddMesh(mesh);
    AList.Add(A);
    System.Guid B = ot.AddCurve(crv);
    BList.Add(B);

    List<Guid> CList = new List<Guid>(AList.Add(BList));

    int nSelected = ot.Select(CList);

    string cmd = "-_Export " + "\"" + path + ".3dm" + "\"" + " _Enter" + " _Enter";
    Rhino.RhinoApp.RunScript(cmd, false);

    ot.Delete(CList, true);
  }

Moved to a new topic…

Use File3dm instead of ActiveDoc.

I’m not exactly sure what you’re trying to achieve here, but for now I can’t see why to have 2 different lists, you’re only using it within the script. The creation of 2 lists which you later merge seems very unneccessary to me. I have not tested this, but something like that should do the trick:

// <Custom additional code> 
protected void Export3dm(List<Mesh> meshs, List<Curve> crvs, string path)
{
  List<Guid> guids= new List<Guid>();
  Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;

  foreach (var mesh in meshs)
  { 
      guids.Add(ot.AddMesh(mesh));
  }
  foreach (var crv in crvs) 
  {  
      guids.Add(ot.AddCurve(crv));
  }

  int nSelected = ot.Select(guids);

  string cmd = "-_Export " + "\"" + path + ".3dm" + "\"" + " _Enter" + " _Enter";
  Rhino.RhinoApp.RunScript(cmd, false);

  ot.Delete(guids, true);
}

On a side note, it really helps the readability of your code giving your variables proper names (and not just A,B,C, x,y,z). It makes debugging difficult on the long run.

Thank you @dsonntag !

I will give this a try.
I was trying the .Add() method because it’s what showed up in my search attempts online on stack overflow. I thought maybe adding two lists together might work…

Would you have any recommendations for websites to learn more about C# for Rhino grasshopper?

Much appreciated!

Hi @dsonntag

I tried implementing the code but am unsure if I am missing some steps before the additional code step.
I am getting these errors:
Error (CS1502): The best overloaded method match for ‘Script_Instance.Export3dm(System.Collections.Generic.List<Rhino.Geometry.Mesh>, System.Collections.Generic.List<Rhino.Geometry.Curve>, string)’ has some invalid arguments (line 57)
Error (CS1503): Argument 1: cannot convert from ‘Rhino.Geometry.Mesh’ to ‘System.Collections.Generic.List<Rhino.Geometry.Mesh>’ (line 57)
Error (CS1503): Argument 2: cannot convert from ‘Rhino.Geometry.Curve’ to ‘System.Collections.Generic.List<Rhino.Geometry.Curve>’ (line 57)

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(Mesh x, Curve y, string z, ref object A)
  {
    Export3dm(x, y, z);
  }

  // <Custom additional code> 
  protected void Export3dm(List<Mesh> meshs, List<Curve> crvs, string path)
  {
    List<Guid> guids = new List<Guid>();
    Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;

    foreach (var mesh in meshs)
    {
      guids.Add(ot.AddMesh(mesh));
    }
    foreach (var crv in crvs)
    {
      guids.Add(ot.AddCurve(crv));
    }

    int nSelected = ot.Select(guids);

    string cmd = "-_Export " + "\"" + path + ".3dm" + "\"" + " _Enter" + " _Enter";
    Rhino.RhinoApp.RunScript(cmd, false);

    ot.Delete(guids, true);
  }
  // </Custom additional code> 
}

Yeah, as the error message says: the method expects a list of meshes but gets a mesh instead, same for the curves. It seemed to make more sense for what you intended to do?
Either way, you can fix this easily by changing the inputs of the C# script component to list access. Righclick on the inputs x and y and change from item to list. Then maybe press F5 to recompute.

As for C#: you can check out the C# workshops by a colleague from our university, I think they’re pretty good.

1 Like