using System; using System.IO; using Rhino.Runtime.InProcess; using Rhino; using Rhino.DocObjects; using Rhino.Input; using Rhino.Commands; using Grasshopper.Kernel; using RhinoInside; using Grasshopper; namespace rhinoBatch { class Batch { static Batch() { RhinoInside.Resolver.Initialize(); } static string getPathOfFile(string fileName) { var assembly = typeof(Batch).Assembly; string dir = System.IO.Path.GetDirectoryName(assembly.Location); string filePath = System.IO.Path.Combine(dir, fileName); return filePath; } static dynamic connectGrasshopper(string pathOfFile) { dynamic gh = RhinoApp.GetPlugInObject("Grasshopper"); gh.RunHeadless(); GH_DocumentIO ghDoc = new GH_DocumentIO(); if (!ghDoc.Open(pathOfFile)) { Console.WriteLine("Grasshopper document loading failed."); return new { checkStatus = false }; } else { GH_Document gh_Doc = ghDoc.Document; gh_Doc.Enabled = true; return new { checkStatus = true, activeGhDocument = gh_Doc }; } } static void givePanelString(dynamic panel) { if (panel.UserText != "RhinoStart1.txt") { panel.UserText = "RhinoStart.txt"; panel.ExpireSolution(true); } } static void MakeItTrue(dynamic panel) { if (panel.UserText != "True") { panel.UserText = "True"; panel.ExpireSolution(true); } } static Grasshopper.Kernel.Special.GH_Panel panelInformation(dynamic pnl) { Grasshopper.Kernel.Special.GH_Panel panel = (Grasshopper.Kernel.Special.GH_Panel)pnl; return panel; } static dynamic getGrasshopperPanelObjects(dynamic activeDoc) { dynamic RhinoStartPanel = null; dynamic RhinoExportPanel = null; dynamic RhinoLastPanel = null; foreach (var obj in activeDoc.Objects) { if (obj.GetType().ToString() == "Grasshopper.Kernel.Special.GH_Panel") { if (obj.NickName == "RhinoStart") { RhinoStartPanel = obj; } else if (obj.NickName == "WriteBool") { RhinoLastPanel = obj; } else { Console.WriteLine("There is not any DesignFeature Panel"); } } } return new { RhinoStart = RhinoStartPanel, RhinoExport = RhinoExportPanel, RhinoLast = RhinoLastPanel}; } [System.STAThread] static void Main(string[] args) { using (new RhinoCore(new string[] { "/NOSPLASH" }, WindowStyle.Hidden)) { string txtPath = getPathOfFile("RhinoStart.txt"); string[] inputPar = System.IO.File.ReadAllLines(txtPath); string filePath = inputPar[0] + "\\" + "BatchProblem.gh"; dynamic ghConnection = connectGrasshopper(filePath); if (ghConnection.checkStatus == true) { dynamic panelSubObjects = getGrasshopperPanelObjects(ghConnection.activeGhDocument); dynamic RhinoStartWithPanel = panelInformation(panelSubObjects.RhinoStart); dynamic RhinoLastPanel = panelInformation(panelSubObjects.RhinoLast); givePanelString(RhinoStartWithPanel); bool loopCheck = false; while (loopCheck != true) { foreach (var data in RhinoLastPanel.VolatileData.AllData(true)) { if (data.ToString() == "True") { string fileRhino = System.IO.Path.Combine(inputPar[0], "delete.3dm"); string cmd = "_-Save " + fileRhino + " _Enter"; RhinoApp.RunScript(cmd, true); loopCheck = true; break; } } } } } } } }