コード例 #1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            bool enabled = false;

            if (DA.GetData <bool>(0, ref enabled))
            {
                GH_Document document = this.OnPingDocument();
                if (document == null)
                {
                    base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid Document");
                    return;
                }
                string filePath = document.FilePath;
                if (string.IsNullOrEmpty(filePath))
                {
                    base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Document not saved yet!");
                    return;
                }
                string        directory     = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
                DirectoryInfo directoryInfo = Directory.CreateDirectory(directory);
                if (directoryInfo == null)
                {
                    directoryInfo = new DirectoryInfo(directory);
                }
                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    file.Delete();                     // Should ignore git files
                }
                foreach (GH_Component component in Grasshopper.Instances.ActiveCanvas.Document.Objects.OfType <GH_Component>())
                {
                    GH_Archive archieve = new GH_Archive();
                    archieve.AppendObject(component, "Component");
                    archieve.WriteToFile(Path.Combine(directory, component.InstanceGuid.ToString("N") + ".ghx"), true, false);
                }
            }
        }
コード例 #2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            myMatrix      = constructMatrixFromSliders((GH_Component)Component);
            INSTANCECOUNT = myMatrix.Count;
            if (INSTANCECOUNT > 4242)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "There are " + INSTANCECOUNT + " instances. That's quite a lot. Try reducing the parameter space!\nSpeckle WILL work but you might have a long wait ahead.");
            }
            this.Message = "# Instances:\n" + myMatrix.Count;

            if (!SOLVE)
            {
                // part of some obscure ritual code
                geometries = new List <List <System.Object> >();
                kvpairs    = new List <SuperKVP>();

                myProperties = new List <SuperProperty>();

                foreach (IGH_Param param in Component.Params.Input[1].Sources)
                {
                    SuperProperty myProperty = new SuperProperty(getPanelName(param));
                    myProperties.Add(myProperty);
                }

                EMERGENCY_BREAK = false;


                List <System.Object> inputObjects = new List <System.Object>();

                this.Message = "# Instances:\n" + myMatrix.Count;

                // critical - if not set we don't know where to sthap
                INSTANCECOUNT = myMatrix.Count;
            }
            else
            {
                // sanity checks
                if (EMERGENCY_BREAK)
                {
                    return;
                }

                // running through the iterations - so store and save

                List <System.Object> geoms = new List <System.Object>();
                List <string>        guids = new List <string>();

                foreach (IGH_Param param in Component.Params.Input[2].Sources)
                {
                    foreach (Object myObj in param.VolatileData.AllData(true))
                    {
                        geoms.Add(myObj);                         // these are the object geometries
                        guids.Add(param.InstanceGuid.ToString()); // these are the guids of the parent componenets
                    }
                }

                string path = Path.Combine(FOLDERLOCATION, currentInstanceName + ".json");


                writeFile(JsonConvert.SerializeObject(translateGeometry(geoms, guids, currentInstanceName, Component), Newtonsoft.Json.Formatting.None), path);

                // get the key value pairs nicely wrapped up
                SuperKVP myKVP = getCurrentKVP(currentInstanceName);
                kvpairs.Add(myKVP);

                this.Message = currentCount + " / " + INSTANCECOUNT;
                string[] splitvals = myKVP.values.Split(',');

                int counter = 0;

                foreach (SuperProperty prop in myProperties)
                {
                    prop.addValue(splitvals[counter]);
                    counter++;
                }

                currentCount++;

                this.Message = currentCount + "\n---\n" + INSTANCECOUNT;
                //that means we have calculated all the required instances
                if (currentCount == INSTANCECOUNT)
                {
                    SOLVE = false;
                    string pathh = "";

                    // write the static geom file

                    List <Object> staticGeo = new List <Object>();

                    foreach (IGH_Param param in Component.Params.Input[3].Sources)
                    {
                        foreach (Object myObj in param.VolatileData.AllData(true))
                        {
                            staticGeo.Add(myObj); // these are the object geometries
                        }
                    }
                    pathh = Path.Combine(FOLDERLOCATION, "static.json");
                    writeFile(JsonConvert.SerializeObject(translateGeometry(staticGeo, new List <string>(), "staticGeo", Component), Newtonsoft.Json.Formatting.None), pathh);


                    OUTFILE            = new System.Dynamic.ExpandoObject();
                    OUTFILE.meta       = "ParametricSpaceExporter";
                    OUTFILE.parameters = new List <dynamic>();
                    OUTFILE.propNames  = new List <string>();
                    OUTFILE.kvpairs    = kvpairs;

                    foreach (IGH_Param param in Component.Params.Input[1].Sources)
                    {
                        //Print(getPanelNameAndVal(param));
                        var myprop = getPanelNameAndVal(param);
                        if (myprop != null)
                        {
                            string[] pops = myprop.Split(',');
                            OUTFILE.propNames.Add(pops[0]);
                        }
                    }

                    // populate the sliders
                    int k = 0;
                    foreach (List <double> mySliderVars in sliderValues)
                    {
                        dynamic Slider = new System.Dynamic.ExpandoObject();
                        Slider.name   = sliderNames.ElementAt(k++);
                        Slider.values = mySliderVars;
                        OUTFILE.parameters.Add(Slider);
                    }

                    OUTFILE.properties = myProperties;

                    pathh = Path.Combine(FOLDERLOCATION, "params.json");

                    writeFile(JsonConvert.SerializeObject(OUTFILE, Newtonsoft.Json.Formatting.None), pathh);

                    // copy/write the gh defintion in the folder

                    if (allowDefExport)
                    {
                        string     ghSavePath = Path.Combine(FOLDERLOCATION, "def.ghx");
                        GH_Archive myArchive  = new GH_Archive();
                        myArchive.Path = ghSavePath;
                        myArchive.AppendObject(GrasshopperDocument, "Definition");
                        myArchive.WriteToFile(ghSavePath, true, false);
                    }

                    // zip things up
                    string startPath = FOLDERLOCATION;

                    using (ZipFile zip = new ZipFile())
                    {
                        zip.AddDirectory(@startPath);
                        zip.Save(startPath + @"\" + GHDEFNAME + ".zip");
                    }

                    // delete the garbage data

                    System.IO.DirectoryInfo myDir = new DirectoryInfo(FOLDERLOCATION);

                    foreach (FileInfo file in myDir.GetFiles())
                    {
                        if (!(file.Extension == ".zip"))
                        {
                            file.Delete();
                        }
                    }

                    // open an explorer window to the location of the archive.
                    Process.Start("explorer.exe", FOLDERLOCATION);

                    PATHISSET      = false;
                    FOLDERLOCATION = "";

                    Component.ExpireSolution(true);
                }
            }
        }