GH I/O iteration process builder

 

IO iteration process builder is a set of C# components that creates a loop around Grasshopper definition to automate import/export process.

 

incremental trigger

Incremental trigger component, when triggered, will return incremental integer from 0 to N. This component is thread based, meaning it will fire value and wait for the process to finish and expires itself to fire next value. Unlike widely used GH timer based counters, inadvertant and redundant triggering is suppressed which allows flawless iteration process.

View Code (C#) incremental trigger

private void RunScript(bool active, int count, ref object A)
{

if(active && num < count){

A = num;
num++;
//wait for threads to finish and fire again
Component.ExpireSolution(true);

}
else{num = 0;}

}

//int value declaired outside the RunScript method to store previous value
int num = 0;

 

string streamer

Along with ,net streamWriter functionality, in accordance with counter, string streamer can create, reset target folder upon triggering in realtime.

View Code (C#) string streamer

private void RunScript(bool write, bool purge, bool append, string rootDir, List<string> fileName, DataTree<string> contents, ref object A)
{

if(write){

WriteLine(rootDir, fileName, contents, append);

}
else{}
if(purge){

Purge(rootDir);

}

}

//WriteLine Method
static void WriteLine(string dir, List<string> fileName, DataTree<string> data, bool append){

//dir initialize
initializeDir(dir);
//Shortest List Count
int fileCount = 0;
if(fileName.Count <= data.BranchCount){

fileCount = fileName.Count;

}
else{

fileCount = data.BranchCount;

}
//filePaths
List<string> filePaths = new List<string>();
foreach(string fName in fileName){

string filePath = dir + @”/” + fName;
filePaths.Add(filePath);

}

//Write Start
for(int i = 0;i < fileCount;i++){

//if append==true, string will be appended on existing file
System.IO.StreamWriter txtWriter = new System.IO.StreamWriter(filePaths[i], append);
foreach(string line in data.Branch(i)){

txtWriter.WriteLine(line);

}
txtWriter.Close();

}

}
//Purge Method (delete target directory)
static void Purge(string dir){

if(Directory.Exists(dir)){

Directory.Delete(dir, true);

}
else{}

}

//InitializeDir Method (create directory if !Directory.Exist)
static void initializeDir(string dir){

if(!Directory.Exists(dir)){

Directory.CreateDirectory(dir);

}
else{}

}

Get files by extension

 

View Code (C#) get files

private void RunScript(bool openFolder, string extension, ref object A)
{

string ext = “.” + extension;

if(openFolder){

folderPath = new List<string>();
filePaths = new List<string>();
//open .NET  folder select dialog
System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
DialogResult result = folderBrowserDialog1.ShowDialog();

//get path
if (result == DialogResult.OK){

folderPath = Directory.GetFiles(folderBrowserDialog1.SelectedPath).ToList();

}
else{}
//get files with matching extension
foreach(string filePath in folderPath){

if(Path.GetExtension(filePath) == ext){

filePaths.Add(filePath);

}
else{}

}

}
A = filePaths;

}

//to use this component with ‘button’, value is declared outside the RunScript
List<string> folderPath = new List<string>();
List<string> filePaths = new List<string>();

Realtime 3dm writer

View Code (C#) realtime 3dm streamer
private void RunScript(bool write, bool append, string rootDir, List fileName, DataTree geometry)
{

if(write){

//make file paths
List filePaths = new List();
foreach(string fName in fileName){

string filePath = rootDir + @”/” + fName;
filePaths.Add(filePath);

}
//in case filename count and geometry branch count do not match
int shortestCount = geometry.BranchCount;
if(fileName.Count <= geometry.BranchCount){

shortestCount = fileName.Count;

}
else{}

//add geometries to file
for(int i = 0;i < shortestCount;i++){

string path = filePaths[i];
Rhino.FileIO.File3dm file = new Rhino.FileIO.File3dm();

//append option
//if append==true, geometries will be added onto existing files instead of making new blank document

if(append && File.Exists(path)){

file = Rhino.FileIO.File3dm.Read(path);

}
else{}

GH_Path ghPath = new GH_Path(i);

//add geometries by type
for(int j = 0;j < geometry.Branch(i).Count ;j++){

GeometryBase g = geometry.Branch(i)[j];
if(g is Brep){

ObjectAttributes objAttr = new ObjectAttributes();
Brep brp = new Brep();
brp = Brep.TryConvertBrep(g);
file.Objects.AddBrep(brp);
Print(“Brep added.”);

}
else if(g is Curve){

file.Objects.AddCurve((Curve) g);
Print(“Curve added.”);

}

else if(g is Rhino.Geometry.Point){

Rhino.Geometry.Point pt = (Rhino.Geometry.Point) g;
file.Objects.AddPoint(pt.Location);
Print(“Point added.”);

}
//unsupported types will be ignored (eg. mesh, textdot, text…)
else{

Print(“*** object conversion failed at ” + i + ” ***”);

}

}
//write file
file.Write(path, 0);

}

}
else{

//validity report only
//find shorter between fileNames and geometry branches
int shortestCount = geometry.BranchCount;
if(fileName.Count <= geometry.BranchCount){

shortestCount = fileName.Count;

}
else{}

for(int i = 0;i < shortestCount;i++){

GH_Path ghPath = new GH_Path(i);

//check geometry type for validity
for(int j = 0;j < geometry.Branch(i).Count ;j++){

GeometryBase g = geometry.Branch(i)[j];
if(g is Brep){

Print(“Object at ” + j + ” is valid.(brep)”);

}
else if(g is Curve){

Print(“Object at ” + j + ” is valid.(curve)”);

}
else if(g is Rhino.Geometry.Point){

Print(“Object at ” + j + ” is valid.(point)”);

}

else{

Print(“*** Object at ” + j + ” is not valid. ***”);

}

}

}

}

}

Leave a Reply