Skip to content

Instantly share code, notes, and snippets.

@katakurashohei
Created November 20, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katakurashohei/20a3944066a315e4e0a17f1bdaabb558 to your computer and use it in GitHub Desktop.
Save katakurashohei/20a3944066a315e4e0a17f1bdaabb558 to your computer and use it in GitHub Desktop.
protected override void SolveInstance(IGH_DataAccess DA)
{
List<Brep> InputBreps = new List<Brep>();
string SavePath = null;
bool On = false;
DA.GetDataList(0, InputBreps);
DA.GetData(1, ref SavePath);
DA.GetData(2, ref On);
if(On)
{
ExportStl(InputBreps, SavePath);
}
IEnumerable<string> files = GetStlFiles(SavePath);
DA.SetDataList(0, files);
}
protected void ExportStl(List<Brep> breps, string path)
{
List<Guid> guidList = new List<Guid>();
Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;
for (int i = 0; i < breps.Count; i++)
{
if (breps[i] == null || !breps[i].IsValid)
{
this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No object to bake, or brep not valid, aborted.");
return;
}
System.Guid guid = ot.AddBrep(breps[i]);
guidList.Add(guid);
}
int nSelected = ot.Select(guidList);
if (nSelected != guidList.Count)
{
this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Not all objects could be selected, aborted.");
return;
}
string cmd = "-_Export " + path + "file.stl" + " _Enter";
Rhino.RhinoApp.RunScript(cmd, true);
ot.Delete(guidList, true);
}
protected IEnumerable<string> GetStlFiles(string path)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = di.GetFiles("*.stl", System.IO.SearchOption.AllDirectories);
IEnumerable<string> fullNames = files.Select(file => file.FullName);
return fullNames;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment