public bool isCompleteLine(SilkwormLine sline) { bool Complete = true; if (sline.Flow == -1 && sline.Speed == -1) { Complete = false; } return(Complete); }
protected override void SolveInstance(IGH_DataAccess DA) { // Input Holders List <double> Flow = new List <double>(); List <double> Speed = new List <double>(); List <GH_ObjectWrapper> Movement = new List <GH_ObjectWrapper>(); GH_ObjectWrapper dObject = new GH_ObjectWrapper(); List <SilkwormLine> sMovement = new List <SilkwormLine>(); //Input if (!DA.GetData(3, ref dObject)) { } if (!DA.GetDataList(1, Speed)) { } if (!DA.GetDataList(2, Flow)) { } if (!DA.GetDataList(0, Movement)) { return; } //Fill Input with placeholders if empty if (Speed.Count < 1) { for (int j = 0; j < Movement.Count; j++) { Speed.Add(-1); } } if (Speed.Count == 1) { if (Movement.Count > 1) { for (int j = 0; j < Movement.Count; j++) { Speed.Add(Speed[0]); } } } if (Flow.Count < 1) { for (int k = 0; k < Movement.Count; k++) { Flow.Add(-1); } } if (Flow.Count == 1) { if (Movement.Count > 1) { for (int k = 0; k < Movement.Count; k++) { Flow.Add(Flow[0]); } } } #region Sort Geometric Input List <Curve> curves = new List <Curve>(); List <Line> lines = new List <Line>(); List <Point3d> points = new List <Point3d>(); foreach (GH_ObjectWrapper Goo in Movement) { if (Goo.Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Goo.Value, ref curve, GH_Conversion.Both); curves.Add(curve); continue; } if (Goo.Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Goo.Value, ref line, GH_Conversion.Both); lines.Add(line); continue; } if (Goo.Value is GH_Point) { Point3d point = new Point3d(); GH_Convert.ToPoint3d(Goo.Value, ref point, GH_Conversion.Both); points.Add(point); continue; } } #endregion #region Sort Numerical Input #endregion //Output Holder SilkwormMovement sModel = new SilkwormMovement(); //Convert Different Geometry types to Movements based on input parameters #region Catch Exceptions if (points.Count > 1 || curves.Count > 1) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Only one curve or point per movement"); } if (points.Count == 1 || curves.Count == 1) { if (Flow.Count > 1 || Speed.Count > 1) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Flow or Speed Values do not match length of Input"); } } #endregion #region if curve ////Make Silkworm Lines if (curves.Count > 0 && curves.Count < 2) { List <Line> sLines = new List <Line>(); Curve newcurve = curves[0]; SilkwormSegment segmented = new SilkwormSegment(newcurve); //Make lines from curves foreach (Curve curve in segmented.Segments) { Line line = new Line(curve.PointAtStart, curve.PointAtEnd); sLines.Add(line); } //Create Silkworm Line from each line and a single flow or speed value for (int i = 0; i < sLines.Count; i++) { SilkwormLine sLine = new SilkwormLine(Flow[0], Speed[0], sLines[i]); sMovement.Add(sLine); } //Add Custom Delimiter or Default Delimiter (depending if input is provided) if (dObject.Value is Delimiter) { Delimiter delimiter = (Delimiter)dObject.Value; sModel = new SilkwormMovement(sMovement, delimiter); } else { sModel = new SilkwormMovement(sMovement, new Delimiter()); } } #endregion #region if lines //Make Silkworm Lines if (lines.Count > 0) { #region More Error Catching if (Flow.Count > 1 && Flow.Count != lines.Count) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Flow Values do not match length of Line List"); } if (Speed.Count > 1 && Speed.Count != lines.Count) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Speed Values do not match length of Line List"); } #endregion //Create Silkworm Line from each line and a corresponding flow or speed value //(will create an incomplete movement if none are provided) if (Flow.Count == lines.Count && Speed.Count == lines.Count) { for (int i = 0; i < lines.Count; i++) { SilkwormLine sLine = new SilkwormLine(Flow[i], Speed[i], lines[i]); sMovement.Add(sLine); } } //Add Custom Delimiter or Default Delimiter (depending if input is provided) if (dObject.Value is Delimiter) { Delimiter delimiter = (Delimiter)dObject.Value; sModel = new SilkwormMovement(sMovement, delimiter); } else { sModel = new SilkwormMovement(sMovement, new Delimiter()); } } #endregion #region if point if (points.Count < 2 && points.Count > 0) { if (dObject.Value is Delimiter) { Delimiter delimiter = (Delimiter)dObject.Value; sModel = new SilkwormMovement(points[0], delimiter); } else { sModel = new SilkwormMovement(points[0], new Delimiter()); } } #endregion //Output DA.SetData(0, sModel); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { #region INPUTS // import silkwormSettings file List<string> silkwormSettings = new List<string>(); if (!DA.GetDataList(0, silkwormSettings)) return; // import raw Grasshopper Geometry to convert to Silkworm Movements List<GH_ObjectWrapper> MovementList = new List<GH_ObjectWrapper>(); if (!DA.GetDataList(1, MovementList)) { return; } int sorted = new int(); if (!DA.GetData(2, ref sorted)) { return; } #endregion #region Utilities //Utility to convert list of strings to Settings Dictionary SilkwormUtility sUtil = new SilkwormUtility(); Dictionary<string, string> Settings = sUtil.convertSettings(silkwormSettings); #endregion double layerHeight = double.Parse(Settings["layer_height"]); int layerHeightDP = CountDecimalPlaces(layerHeight); #region Output Holders //GCode Lines List<GH_String> sGCode =new List<GH_String>(); //Unsorted Movements List<SilkwormMovement> unMovements = new List<SilkwormMovement>(); //Movements sorted by Layer List<SilkwormMovement>[] sMovements = new List<SilkwormMovement>[0]; #endregion //Wrapper List for all types to Parse GH_ObjectWrapper[] Movement = MovementList.ToArray(); //Switch sorting routine by input switch (sorted) { case 1: #region Unsorted (Sort Order of Movements by z then x and y) if (sorted == 1) { //Parse #region Parse Input Type List<Brep> solids = new List<Brep>(); List<Mesh> meshes = new List<Mesh>(); //Incomplete Silkworm Movements List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); List<Brep> shapes = new List<Brep>(); List<Curve> curves = new List<Curve>(); List<Polyline> plines = new List<Polyline>(); List<Line> lines = new List<Line>(); for (int i = 0; i < Movement.Length; i++) { //Sort Types into Container Lists #region IfNull if ((Movement[i].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { incmovements.Add(aMovement); continue; } } #endregion #region Solids else if (Movement[i].Value is GH_Brep) { Brep solid = null; GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); solids.Add(solid); continue; } else if (Movement[i].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); meshes.Add(mesh); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[i].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); curves.Add(curve); continue; } #endregion #region SilkwormLine else if (Movement[i].Value is SilkwormLine) { List<SilkwormLine> slines = new List<SilkwormLine>(); slines.Add((SilkwormLine)Movement[i].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[i].Value)) { unMovements.Add(new SilkwormMovement(slines, new Delimiter())); } else { incmovements.Add(new SilkwormMovement(slines, new Delimiter())); } continue; } #endregion #region Line else if (Movement[i].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); lines.Add(line); continue; } #endregion #region Point else if (Movement[i].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } #endregion #endregion } //Build Movements from Incomplete Parts #region Build Movement #region List<Brep>Add to unMovements if (solids.Count > 0) { List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); } #endregion #region List<Mesh> Add to unMovements //TODO #endregion #region List<Shapes> Add to unMovements //TODO #endregion #region List<Curve> Add to unMovements foreach (Curve curve in curves) { SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); } #endregion #region List<Line> Add to unMovements foreach (Line line in lines) { List<Line> _lines = new List<Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); } #endregion #region List<IncompleteSilkwormMovement> Add to unMovements foreach (SilkwormMovement movement in incmovements) { unMovements.Add(completeMovement(Settings, movement)); ; continue; } #endregion #endregion } goto SortZ; #endregion break; case 2: #region Partially Sorted (Sort Movements by z only) if (sorted == 2) { for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { unMovements.Add(completeMovement(Settings, aMovement)); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List<Brep> solids = new List<Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List<SilkwormLine> s_lines = new List<SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { unMovements.Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List<Line> lines = new List<Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List<SilkwormLine> Movements = new List<SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; unMovements.Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List<Line> _lines = new List<Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto SortZ; #endregion break; case 3: #region Completely Sorted if (sorted == 3) { //Initialise sMovements sMovements = new List<SilkwormMovement>[1]; sMovements[0] = new List<SilkwormMovement>(); for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //Make sure it has a config field sMovements[0].Add(aMovement); continue; } else { sMovements[0].Add(completeMovement(Settings, aMovement)); //sMovements[0].Add(new SilkwormMovement(Movements, new Delimiter())); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List<Brep> solids = new List<Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); sMovements[0].AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); sMovements[0].Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List<SilkwormLine> s_lines = new List<SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { sMovements[0].Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List<Line> lines = new List<Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List<SilkwormLine> Movements = new List<SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; sMovements[0].Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List<Line> _lines = new List<Line>(); _lines.Add(line); sMovements[0].Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto Compiler; #endregion break; default: break; } #region ss ////Parse //#region Parse Input Type //List<Brep> solids = new List<Brep>(); //List<Mesh> meshes = new List<Mesh>(); ////Incomplete Silkworm Movements //List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); //List<Brep> shapes = new List<Brep>(); //List<Curve> curves = new List<Curve>(); //List<Polyline> plines = new List<Polyline>(); //List<Line> lines = new List<Line>(); //for (int i = 0; i < Movement.Length; i++) //{ ////Sort Types into Container Lists // #region IfNull // if ((Movement[i].Value == null)) // { continue; } // #endregion //#region SilkwormMovement //if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) //{ // SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; // if(aMovement.complete) // { // unMovements.Add(aMovement); // continue; // } // else // { // incmovements.Add(aMovement); // continue; // } //} //#endregion //#region Solids //if (Movement[i].Value is GH_Brep) //{ // Brep solid = null; // GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); // solids.Add(solid); // continue; //} //if (Movement[i].Value is GH_Mesh) //{ // Mesh mesh = null; // GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); // meshes.Add(mesh); // continue; //} //#endregion //#region Regions ////TODO //#endregion //#region Curves //if (Movement[i].Value is GH_Curve) //{ // Curve curve = null; // GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); // curves.Add(curve); // continue; //} //#endregion //#region SilkwormLine //if (Movement[i].Value is SilkwormLine) //{ // List<SilkwormLine> slines = new List<SilkwormLine>(); // slines.Add((SilkwormLine)Movement[i].Value); // //Check if complete // if (isCompleteLine((SilkwormLine)Movement[i].Value)) // { // unMovements.Add(new SilkwormMovement(slines, true)); // } // else // { // incmovements.Add(new SilkwormMovement(slines, true)); // } // continue; //} //#endregion //#region Line // if (Movement[i].Value is GH_Line) //{ // Line line = new Line(); // GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); // lines.Add(line); //} //#endregion //#region Point // if (Movement[i].Value is GH_Point) //{ // //TODO // continue; //} //#endregion //else //{ // AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); // continue; //} //#endregion //} ////Build Movements from Incomplete Parts //#region Build Movement //#region List<Brep>Add to unMovements // //List<Brep> solidS = new List<Brep>(); // //solidS.Add((Brep)solid); // List<Polyline>[] rPerimeter = new List<Polyline>[0]; // List<Polyline>[] rInfill = new List<Polyline>[0]; // //Slice, Skin and Fill Breps (TODO: Add Bridge Finder) // SilkwormSkein skein = new SilkwormSkein(Settings, solids); // if (skein.validPos) // { // rPerimeter = skein.plinePerimeter; // rInfill = skein.plineInfill; // } // List<SilkwormMovement> solidMovements = new List<SilkwormMovement>(); // //Add to ORGANISED List of Movements (i.e. Model) // for (int b = 0; b <= rPerimeter.GetUpperBound(0); b++) // { // foreach (Polyline pline in rPerimeter[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // foreach (Polyline pline in rInfill[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // } // unMovements.AddRange(solidMovements); //#endregion //#region List<Mesh> Add to unMovements ////TODO //#endregion //#region List<Shapes> Add to unMovements ////TODO //#endregion //#region List<Curve> Add to unMovements //foreach (Curve curve in curves) //{ // SilkwormMovement movement = new SilkwormMovement(Settings, curve, true); // unMovements.Add(movement); //} //#endregion //#region List<Line> Add to unMovements //foreach (Line line in lines) //{ // List<Line> _lines = new List<Line>(); // _lines.Add(line); // unMovements.Add(new SilkwormMovement(Settings, _lines, true)); //} //#endregion //#region List<IncompleteSilkwormMovement> Add to unMovements //foreach (SilkwormMovement movement in incmovements) // { // List<Line> lines = new List<Line>(); // SilkwormLine[] s_Movement = new SilkwormLine[movement.Count]; // foreach (SilkwormLine line in movement) // { // lines.Add(line.Curve); // } // List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines,true).sMovement; // List<SilkwormLine> Movements = new List<SilkwormLine>(); // //Complete Movements // for (int j = 0; j < sLines.Count; j++) // { // if (s_Movement[j].Flow == -1) // { // s_Movement[j].Flow = sLines[j].Flow; // } // if (s_Movement[j].Speed == -1) // { // s_Movement[j].Speed = sLines[j].Speed; // } // Movements.Add(s_Movement[j]); // } // //Add Configuration // movement.Configuration = Settings; // unMovements.Add(new SilkwormMovement(Movements, true)); // continue; //} //#endregion //#endregion #endregion //Sort Unorganised Movements into Layered Model SortZ: #region Build Model List<double> uniqueZ = new List<double>(); //Find Unique ZValues uniqueZ.AddRange(FindUniqueZValues(unMovements, layerHeightDP)); //Sort List of Unique Z Levels uniqueZ.Sort(); //Make Dictionary from List of Unique Z Levels Dictionary<double, int> ZLevel = new Dictionary<double, int>(); for (int d = 0; d < uniqueZ.Count; d++) { ZLevel.Add(uniqueZ[d], d); } //Initialise Array of Lists sMovements = new List<SilkwormMovement>[uniqueZ.Count]; for (int a = 0; a < sMovements.Length; a++) { sMovements[a] = new List<SilkwormMovement>(); } //Sort Silkworm Movements into correct layers in Array of Lists foreach (SilkwormMovement movement in unMovements) { sMovements[ZLevel[Math.Round(movement.ZDomain.T0, layerHeightDP)]].Add(movement); } #endregion goto Compiler; //Compile Model to GCode Compiler: #region GCode Compiler #region HEADER //Add Custom Commands at Start string header = Settings["start_gcode"]; //Char[] splitChar = new Char[] {'\\','\n', 'n'}; string[] splitChar = new string[] { "\\n" }; string[] parts = header.Split(splitChar,StringSplitOptions.RemoveEmptyEntries); if (parts != null) { for (int i = 0; i < parts.Length; i++) { sGCode.Add(new GH_String(parts[i])); } } else { sGCode.Add(new GH_String(header)); } if (int.Parse(Settings["absolute_extrudersteps"]) == 1) //if true use absolute distances for extrusion, otherwise use relative { sGCode.Add(new GH_String("M82 ; use absolute distances for extrusion")); } sGCode.Add(new GH_String("G90 ; use absolute coordinates")); sGCode.Add(new GH_String("G21 ; set units to millimeters")); sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Set Temperature double temp = double.Parse(Settings["temperature"]); sGCode.Add(new GH_String("M104 S" + temp + " ; set temperature")); sGCode.Add(new GH_String("M109 S" + temp + " ; wait for temperature to be reached")); //Extrude a bit of plastic before start sGCode.Add(new GH_String("G1 Z0.0 F360 E1")); #endregion for (int z = 0; z <= sMovements.GetUpperBound(0); z++) { foreach (SilkwormMovement movement in sMovements[z]) { sGCode.AddRange(movement.ToGCode()); } } #region FOOTER sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Add Custom Commands at End string footer = Settings["end_gcode"]; string[] fparts = footer.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (fparts != null) { for (int i = 0; i < fparts.Length; i++) { sGCode.Add(new GH_String(fparts[i])); } } else { sGCode.Add(new GH_String(footer)); } #endregion ////Convert Array of Movements to List //List<SilkwormMovement> outMovements = new List<SilkwormMovement>(); //for (int l = 0; l < sMovements.GetUpperBound(0); l++) //{ // outMovements.AddRange(sMovements[l]); //} #endregion #region Silkworm Model List<SilkwormMovement> s_Model = new List<SilkwormMovement>(); for (int p = 0; p <= sMovements.GetUpperBound(0); p++) { for (int s = 0; s < sMovements[p].Count; s++) { s_Model.Add(sMovements[p][s]); } } List<SilkwormModel> sModel = new List<SilkwormModel>(); sModel.Add(new SilkwormModel(Settings, s_Model)); #endregion //Output GCode and Model #region OUTPUTS DA.SetDataList(0, sGCode); DA.SetDataList(1, sModel); #endregion }
public bool isCompleteLine(SilkwormLine sline) { bool Complete = true; if (sline.Flow == -1 && sline.Speed == -1) { Complete = false; } return Complete; }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { #region INPUTS // import silkwormSettings file List <string> silkwormSettings = new List <string>(); if (!DA.GetDataList(0, silkwormSettings)) { return; } // import raw Grasshopper Geometry to convert to Silkworm Movements List <GH_ObjectWrapper> MovementList = new List <GH_ObjectWrapper>(); if (!DA.GetDataList(1, MovementList)) { return; } int sorted = new int(); if (!DA.GetData(2, ref sorted)) { return; } #endregion #region Utilities //Utility to convert list of strings to Settings Dictionary SilkwormUtility sUtil = new SilkwormUtility(); Dictionary <string, string> Settings = sUtil.convertSettings(silkwormSettings); #endregion double layerHeight = double.Parse(Settings["layer_height"]); int layerHeightDP = CountDecimalPlaces(layerHeight); #region Output Holders //GCode Lines List <GH_String> sGCode = new List <GH_String>(); //Unsorted Movements List <SilkwormMovement> unMovements = new List <SilkwormMovement>(); //Movements sorted by Layer List <SilkwormMovement>[] sMovements = new List <SilkwormMovement> [0]; #endregion //Wrapper List for all types to Parse GH_ObjectWrapper[] Movement = MovementList.ToArray(); //Switch sorting routine by input switch (sorted) { case 1: #region Unsorted (Sort Order of Movements by z then x and y) if (sorted == 1) { //Parse #region Parse Input Type List <Brep> solids = new List <Brep>(); List <Mesh> meshes = new List <Mesh>(); //Incomplete Silkworm Movements List <SilkwormMovement> incmovements = new List <SilkwormMovement>(); List <Brep> shapes = new List <Brep>(); List <Curve> curves = new List <Curve>(); List <Polyline> plines = new List <Polyline>(); List <Line> lines = new List <Line>(); for (int i = 0; i < Movement.Length; i++) { //Sort Types into Container Lists #region IfNull if ((Movement[i].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { incmovements.Add(aMovement); continue; } } #endregion #region Solids else if (Movement[i].Value is GH_Brep) { Brep solid = null; GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); solids.Add(solid); continue; } else if (Movement[i].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); meshes.Add(mesh); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[i].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); curves.Add(curve); continue; } #endregion #region SilkwormLine else if (Movement[i].Value is SilkwormLine) { List <SilkwormLine> slines = new List <SilkwormLine>(); slines.Add((SilkwormLine)Movement[i].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[i].Value)) { unMovements.Add(new SilkwormMovement(slines, new Delimiter())); } else { incmovements.Add(new SilkwormMovement(slines, new Delimiter())); } continue; } #endregion #region Line else if (Movement[i].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); lines.Add(line); continue; } #endregion #region Point else if (Movement[i].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } #endregion #endregion } //Build Movements from Incomplete Parts #region Build Movement #region List<Brep>Add to unMovements if (solids.Count > 0) { List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); } #endregion #region List<Mesh> Add to unMovements //TODO #endregion #region List<Shapes> Add to unMovements //TODO #endregion #region List<Curve> Add to unMovements foreach (Curve curve in curves) { SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); } #endregion #region List<Line> Add to unMovements foreach (Line line in lines) { List <Line> _lines = new List <Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); } #endregion #region List<IncompleteSilkwormMovement> Add to unMovements foreach (SilkwormMovement movement in incmovements) { unMovements.Add(completeMovement(Settings, movement));; continue; } #endregion #endregion } goto SortZ; #endregion break; case 2: #region Partially Sorted (Sort Movements by z only) if (sorted == 2) { for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { unMovements.Add(completeMovement(Settings, aMovement)); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List <Brep> solids = new List <Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List <SilkwormLine> s_lines = new List <SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { unMovements.Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List <Line> lines = new List <Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List <SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List <SilkwormLine> Movements = new List <SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; unMovements.Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List <Line> _lines = new List <Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto SortZ; #endregion break; case 3: #region Completely Sorted if (sorted == 3) { //Initialise sMovements sMovements = new List <SilkwormMovement> [1]; sMovements[0] = new List <SilkwormMovement>(); for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //Make sure it has a config field sMovements[0].Add(aMovement); continue; } else { sMovements[0].Add(completeMovement(Settings, aMovement)); //sMovements[0].Add(new SilkwormMovement(Movements, new Delimiter())); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List <Brep> solids = new List <Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); sMovements[0].AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); sMovements[0].Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List <SilkwormLine> s_lines = new List <SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { sMovements[0].Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List <Line> lines = new List <Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List <SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List <SilkwormLine> Movements = new List <SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; sMovements[0].Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List <Line> _lines = new List <Line>(); _lines.Add(line); sMovements[0].Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto Compiler; #endregion break; default: break; } #region ss ////Parse //#region Parse Input Type //List<Brep> solids = new List<Brep>(); //List<Mesh> meshes = new List<Mesh>(); ////Incomplete Silkworm Movements //List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); //List<Brep> shapes = new List<Brep>(); //List<Curve> curves = new List<Curve>(); //List<Polyline> plines = new List<Polyline>(); //List<Line> lines = new List<Line>(); //for (int i = 0; i < Movement.Length; i++) //{ ////Sort Types into Container Lists // #region IfNull // if ((Movement[i].Value == null)) // { continue; } // #endregion //#region SilkwormMovement //if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) //{ // SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; // if(aMovement.complete) // { // unMovements.Add(aMovement); // continue; // } // else // { // incmovements.Add(aMovement); // continue; // } //} //#endregion //#region Solids //if (Movement[i].Value is GH_Brep) //{ // Brep solid = null; // GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); // solids.Add(solid); // continue; //} //if (Movement[i].Value is GH_Mesh) //{ // Mesh mesh = null; // GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); // meshes.Add(mesh); // continue; //} //#endregion //#region Regions ////TODO //#endregion //#region Curves //if (Movement[i].Value is GH_Curve) //{ // Curve curve = null; // GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); // curves.Add(curve); // continue; //} //#endregion //#region SilkwormLine //if (Movement[i].Value is SilkwormLine) //{ // List<SilkwormLine> slines = new List<SilkwormLine>(); // slines.Add((SilkwormLine)Movement[i].Value); // //Check if complete // if (isCompleteLine((SilkwormLine)Movement[i].Value)) // { // unMovements.Add(new SilkwormMovement(slines, true)); // } // else // { // incmovements.Add(new SilkwormMovement(slines, true)); // } // continue; //} //#endregion //#region Line // if (Movement[i].Value is GH_Line) //{ // Line line = new Line(); // GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); // lines.Add(line); //} //#endregion //#region Point // if (Movement[i].Value is GH_Point) //{ // //TODO // continue; //} //#endregion //else //{ // AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); // continue; //} //#endregion //} ////Build Movements from Incomplete Parts //#region Build Movement //#region List<Brep>Add to unMovements // //List<Brep> solidS = new List<Brep>(); // //solidS.Add((Brep)solid); // List<Polyline>[] rPerimeter = new List<Polyline>[0]; // List<Polyline>[] rInfill = new List<Polyline>[0]; // //Slice, Skin and Fill Breps (TODO: Add Bridge Finder) // SilkwormSkein skein = new SilkwormSkein(Settings, solids); // if (skein.validPos) // { // rPerimeter = skein.plinePerimeter; // rInfill = skein.plineInfill; // } // List<SilkwormMovement> solidMovements = new List<SilkwormMovement>(); // //Add to ORGANISED List of Movements (i.e. Model) // for (int b = 0; b <= rPerimeter.GetUpperBound(0); b++) // { // foreach (Polyline pline in rPerimeter[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // foreach (Polyline pline in rInfill[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // } // unMovements.AddRange(solidMovements); //#endregion //#region List<Mesh> Add to unMovements ////TODO //#endregion //#region List<Shapes> Add to unMovements ////TODO //#endregion //#region List<Curve> Add to unMovements //foreach (Curve curve in curves) //{ // SilkwormMovement movement = new SilkwormMovement(Settings, curve, true); // unMovements.Add(movement); //} //#endregion //#region List<Line> Add to unMovements //foreach (Line line in lines) //{ // List<Line> _lines = new List<Line>(); // _lines.Add(line); // unMovements.Add(new SilkwormMovement(Settings, _lines, true)); //} //#endregion //#region List<IncompleteSilkwormMovement> Add to unMovements //foreach (SilkwormMovement movement in incmovements) // { // List<Line> lines = new List<Line>(); // SilkwormLine[] s_Movement = new SilkwormLine[movement.Count]; // foreach (SilkwormLine line in movement) // { // lines.Add(line.Curve); // } // List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines,true).sMovement; // List<SilkwormLine> Movements = new List<SilkwormLine>(); // //Complete Movements // for (int j = 0; j < sLines.Count; j++) // { // if (s_Movement[j].Flow == -1) // { // s_Movement[j].Flow = sLines[j].Flow; // } // if (s_Movement[j].Speed == -1) // { // s_Movement[j].Speed = sLines[j].Speed; // } // Movements.Add(s_Movement[j]); // } // //Add Configuration // movement.Configuration = Settings; // unMovements.Add(new SilkwormMovement(Movements, true)); // continue; //} //#endregion //#endregion #endregion //Sort Unorganised Movements into Layered Model SortZ: #region Build Model List <double> uniqueZ = new List <double>(); //Find Unique ZValues uniqueZ.AddRange(FindUniqueZValues(unMovements, layerHeightDP)); //Sort List of Unique Z Levels uniqueZ.Sort(); //Make Dictionary from List of Unique Z Levels Dictionary <double, int> ZLevel = new Dictionary <double, int>(); for (int d = 0; d < uniqueZ.Count; d++) { ZLevel.Add(uniqueZ[d], d); } //Initialise Array of Lists sMovements = new List <SilkwormMovement> [uniqueZ.Count]; for (int a = 0; a < sMovements.Length; a++) { sMovements[a] = new List <SilkwormMovement>(); } //Sort Silkworm Movements into correct layers in Array of Lists foreach (SilkwormMovement movement in unMovements) { sMovements[ZLevel[Math.Round(movement.ZDomain.T0, layerHeightDP)]].Add(movement); } #endregion goto Compiler; //Compile Model to GCode Compiler: #region GCode Compiler #region HEADER //Add Custom Commands at Start string header = Settings["start_gcode"]; //Char[] splitChar = new Char[] {'\\','\n', 'n'}; string[] splitChar = new string[] { "\\n" }; string[] parts = header.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (parts != null) { for (int i = 0; i < parts.Length; i++) { sGCode.Add(new GH_String(parts[i])); } } else { sGCode.Add(new GH_String(header)); } if (int.Parse(Settings["absolute_extrudersteps"]) == 1) //if true use absolute distances for extrusion, otherwise use relative { sGCode.Add(new GH_String("M82 ; use absolute distances for extrusion")); } sGCode.Add(new GH_String("G90 ; use absolute coordinates")); sGCode.Add(new GH_String("G21 ; set units to millimeters")); sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Set Temperature double temp = double.Parse(Settings["temperature"]); sGCode.Add(new GH_String("M104 S" + temp + " ; set temperature")); sGCode.Add(new GH_String("M109 S" + temp + " ; wait for temperature to be reached")); //Extrude a bit of plastic before start sGCode.Add(new GH_String("G1 Z0.0 F360 E1")); #endregion for (int z = 0; z <= sMovements.GetUpperBound(0); z++) { foreach (SilkwormMovement movement in sMovements[z]) { sGCode.AddRange(movement.ToGCode()); } } #region FOOTER sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Add Custom Commands at End string footer = Settings["end_gcode"]; string[] fparts = footer.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (fparts != null) { for (int i = 0; i < fparts.Length; i++) { sGCode.Add(new GH_String(fparts[i])); } } else { sGCode.Add(new GH_String(footer)); } #endregion ////Convert Array of Movements to List //List<SilkwormMovement> outMovements = new List<SilkwormMovement>(); //for (int l = 0; l < sMovements.GetUpperBound(0); l++) //{ // outMovements.AddRange(sMovements[l]); //} #endregion #region Silkworm Model List <SilkwormMovement> s_Model = new List <SilkwormMovement>(); for (int p = 0; p <= sMovements.GetUpperBound(0); p++) { for (int s = 0; s < sMovements[p].Count; s++) { s_Model.Add(sMovements[p][s]); } } List <SilkwormModel> sModel = new List <SilkwormModel>(); sModel.Add(new SilkwormModel(Settings, s_Model)); #endregion //Output GCode and Model #region OUTPUTS DA.SetDataList(0, sGCode); DA.SetDataList(1, sModel); #endregion }