Export Contours per branch C#

Hi,

I want to export curves per branch to dxf via code, so that I have a file for each heightsection.
I got stuck looping through the tree.
Maybe someone can help me out.

private void RunScript(bool export, Brep brep, List plane, string path, string fileType, string filename, ref object A, ref object B)
{

// get contours
var contourTree = new DataTree<Curve>();
for (int i = 0; i < plane.Count; i++)
{
  Curve[] contours = Brep.CreateContourCurves(brep, plane[i]);
  contourTree.AddRange(contours, new GH_Path(i));
}

//get or create directory

if (!System.IO.Directory.Exists(path))
{
  System.IO.Directory.CreateDirectory(path);
}

//create filetype

if (!fileType.StartsWith("."))

{
  fileType = "." + fileType;
}

//declare attributes
ObjectAttributes attr = RhinoDocument.CreateDefaultAttributes();

var doc = Rhino.RhinoDoc.ActiveDoc;
if (doc == null) return;

doc.Objects.UnselectAll();

if (export)
{
  for (int i = 0; i < contourTree.BranchCount; i++)
  {


    var crvs = doc.Objects.AddCurve(contourTree.Branches[i], attr);
    doc.Objects.Select(crvs);
    Rhino.RhinoApp.RunScript("-_Export \n\"" + path + "\\" + filename + fileType + "\"\n _Enter", true);

  }
}


A = contourTree;

}

Thanks

file:
20180925_BakeExport.gh (9.6 KB)

Why create a tree at all? Why not just write your file from within the loop?

I assumed in a case that I have different contours at the same height it would be nessecary to export per branch to have for each plane just one file.

To be later able to isolate the export part and be able to feed in any produced tree, I was thinking it would be the best set it up like this.

Thanks for your time David!

I’m happy to help out with the code that isn’t working, but it just seemed like it was too complicated to begin with. If you have good reasons for it though then let’s figure out what went wrong with the tree approach.

1 Like

Hi David, and thanks again for your patience !
Your help is really apreciated!

I tried it in a nested for loop now and try to understand the tree thing later with simpler excercises:sweat_smile:

The problem now is the export between line 108 -117. I suspect it has to be in another loop since it generates multiple times the same.

I hope its not too inconvinient to ask again :slight_smile:
private void RunScript(bool export, List brep, List plane, string path, string fileType, string filename, ref object A, ref object B)
{

//get or create directory

if (!System.IO.Directory.Exists(path))
{
  System.IO.Directory.CreateDirectory(path);
}

//create filetype

if (!fileType.StartsWith("."))

{
  fileType = "." + fileType;
}


//declare attributes
ObjectAttributes attr = RhinoDocument.CreateDefaultAttributes();
var doc = Rhino.RhinoDoc.ActiveDoc;
if (doc == null) return;



var contours = new List<Curve>();

for (int i = 0; i < plane.Count; i++)
{
  for (int j = 0; j < brep.Count; j++)
  {
    //get the contours
    Curve[] crvs = Brep.CreateContourCurves(brep[j], plane[i]);
    contours.AddRange(crvs);
  }

  //filename like planeHeight
  string filenameHeight = filename + plane[i].Origin.Z.ToString();

  foreach (Curve c in contours)
  {
    var bakedC = doc.Objects.AddCurve(c, attr);
    doc.Objects.Select(bakedC);
    Rhino.RhinoApp.RunScript("-_Export \n\"" + path + "\\" + filenameHeight + fileType + "\"\n _Enter", true);
  }

  doc.Objects.UnselectAll();

}

A = contours;

}

file:20180927_BakeExport.gh (12.7 KB)

I figured it out. thanks for all