Пример #1
0
        public static void LoadDoc(string file)
        {
            var io = new GH_DocumentIO();

            if (!io.Open(file))
            {
                throw new Exception("读取文档失败");
            }

            GH_DocumentServer server = Instances.DocumentServer;

            var doc = io.Document;

            if (server == null)
            {
                throw new Exception("读取文档失败");
            }

            server.AddDocument(doc);

            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                throw new Exception("读取文档失败");
            }

            activeCanvas.Document            = doc;
            activeCanvas.Document.IsModified = false;
            activeCanvas.Refresh();
        }
Пример #2
0
        public static void ChangeObjects(IEnumerable <IGH_Param> items, IGH_Param newObject)
        {
            foreach (IGH_Param item in items)
            {
                //get the input it is connected to
                if (item.Recipients.Count == 0)
                {
                    return;
                }
                var parrent = item.Recipients[0];

                GH_DocumentIO docIO = new GH_DocumentIO();
                docIO.Document = new GH_Document();

                //get active GH doc
                GH_Document doc = item.OnPingDocument();
                if (doc == null)
                {
                    return;
                }
                if (docIO.Document == null)
                {
                    return;
                }

                Component.AddObject(docIO, newObject, parrent, item.Attributes.Pivot);
                Component.MergeDocuments(docIO, doc, $"Create {newObject.Name}");

                doc.RemoveObject(item, false);
                parrent.AddSource(newObject);
            }
        }
Пример #3
0
        public GGitViewer(string filepath)
        {
            _filepath = filepath;
            GH_DocumentIO doc = new GH_DocumentIO();

            doc.Open(filepath);
            document = doc.Document;
            InitializeComponent();
            ListInit();
        }
Пример #4
0
        /// <summary>
        /// Creates a value list pre-populated with possible accent colors and adds it to the Grasshopper Document, located near the component pivot.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CreateRasterList(object sender, System.EventArgs e)
        {
            string source = sender.ToString();

            source = source.Replace("Create ", "");
            source = source.Replace(" Source List", "");

            GH_DocumentIO docIO = new GH_DocumentIO();

            docIO.Document = new GH_Document();

            ///Initialize object
            GH_ValueList vl = new GH_ValueList();

            ///Clear default contents
            vl.ListItems.Clear();

            foreach (var service in rasterJson["REST Raster"])
            {
                if (service["source"].ToString() == source)
                {
                    GH_ValueListItem vi = new GH_ValueListItem(service["service"].ToString(), String.Format("\"{0}\"", service["url"].ToString()));
                    vl.ListItems.Add(vi);
                }
            }

            ///Set component nickname
            vl.NickName = source;

            ///Get active GH doc
            GH_Document doc = OnPingDocument();

            if (docIO.Document == null)
            {
                return;
            }

            ///Place the object
            docIO.Document.AddObject(vl, false, 1);

            ///Get the pivot of the "URL" param
            PointF currPivot = Params.Input[4].Attributes.Pivot;

            ///Set the pivot of the new object
            vl.Attributes.Pivot = new PointF(currPivot.X - 400, currPivot.Y - 11);

            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable <IGH_DocumentObject> objs = docIO.Document.Objects;

            doc.DeselectAll();
            doc.UndoUtil.RecordAddObjectEvent("Create REST Raster Source List", objs);
            doc.MergeDocument(docIO.Document);
        }
Пример #5
0
        private void LoadGhDocument()
        {
            if (content.type != TaskContentType.file)
            {
                return;
            }

            string file = content.value;

            if (!File.Exists(file))
            {
                ErrorEvent(this, "Work file is not exist!");
                return;
            }
            GH_DocumentIO io = new GH_DocumentIO();

            io.Open(file);

            GH_Document doc = GH_Document.DuplicateDocument(io.Document);

            if (doc == null)
            {
                ErrorEvent(this, "Cannot read this file!");
                return;
            }

            GH_DocumentServer server = Instances.DocumentServer;

            if (server == null)
            {
                ErrorEvent(this, "No Document Server exist!");
                return;
            }

            server.AddDocument(doc);

            doc.Properties.ProjectFileName = ID.ToString();

            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                ErrorEvent(this, "No Active Canvas exist!");
                return;
            }

            activeCanvas.Document            = doc;
            activeCanvas.Document.IsModified = false;
            activeCanvas.Refresh();

            // SolutionEndCnt = 0;
            doc.SolutionEnd += Doc_SolutionEnd;
            UpdateData(true);
        }
Пример #6
0
        private static void MergeDocuments(GH_DocumentIO docIO, GH_Document doc, string name = "Merge")
        {
            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable <IGH_DocumentObject> objs = docIO.Document.Objects;

            doc.DeselectAll();
            doc.UndoUtil.RecordAddObjectEvent(name, objs);
            doc.MergeDocument(docIO.Document);
            //doc.ScheduleSolution(10);
        }
Пример #7
0
 protected override void BeforeSolveInstance()
 {
     if (packagePath != null)
     {
         GH_DocumentIO io = new GH_DocumentIO();
         io.Open(packagePath);
         m_document = io.Document;
         if (!inputAdjusted)
         {
             AdjustPackageInput();
         }
         if (!outputAdjusted)
         {
             AdjustPackageOutput();
         }
         SetPackageInput();
     }
 }
Пример #8
0
        // private methods to magee the placement of ne objects
        private static void AddObject(GH_DocumentIO docIO, IGH_Param Object, IGH_Param param, PointF location = new PointF())
        {
            // place the object
            docIO.Document.AddObject(Object, false, 1);

            //get the pivot of the "accent" param
            System.Drawing.PointF currPivot = param.Attributes.Pivot;

            if (location == new PointF())
            {
                Object.Attributes.Pivot = new System.Drawing.PointF(currPivot.X - 120, currPivot.Y - 11);
            }
            //set the pivot of the new object
            else
            {
                Object.Attributes.Pivot = location;
            }
        }
        /// <summary>
        /// Creates a value list pre-populated with possible accent colors and adds it to the Grasshopper Document, located near the component pivot.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void createAccentList(object sender, System.EventArgs e)
        {
            GH_DocumentIO docIO = new GH_DocumentIO();

            docIO.Document = new GH_Document();

            //initialize object
            GH_ValueList vl = new GH_ValueList();

            //clear default contents
            vl.ListItems.Clear();
            //add all the accent colors as both "Keys" and values
            foreach (string color in ACCENT_COLORS)
            {
                GH_ValueListItem vi = new GH_ValueListItem(color, String.Format("\"{0}\"", color));
                vl.ListItems.Add(vi);
            }
            //set component nickname
            vl.NickName = "Accent Colors";
            //get active GH doc
            GH_Document doc = OnPingDocument();

            if (docIO.Document == null)
            {
                return;
            }
            // place the object
            docIO.Document.AddObject(vl, false, 1);
            //get the pivot of the "accent" param
            PointF currPivot = Params.Input[3].Attributes.Pivot;

            //set the pivot of the new object
            vl.Attributes.Pivot = new PointF(currPivot.X - 120, currPivot.Y - 11);

            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable <IGH_DocumentObject> objs = docIO.Document.Objects;

            doc.DeselectAll();
            doc.UndoUtil.RecordAddObjectEvent("Create Accent List", objs);
            doc.MergeDocument(docIO.Document);
            //doc.ScheduleSolution(10);
        }
Пример #10
0
        private void LoadGhDocument(string file)
        {
            if (!System.IO.File.Exists(file))
            {
                return;
            }
            GH_DocumentIO io = new GH_DocumentIO();

            io.Open(file);

            GH_Document doc = GH_Document.DuplicateDocument(io.Document);

            if (doc == null)
            {
                return;
            }

            GH_DocumentServer server = Instances.DocumentServer;

            if (server == null)
            {
                return;
            }

            server.AddDocument(doc);

            doc.Properties.ProjectFileName = ID.ToString();

            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                return;
            }

            activeCanvas.Document            = doc;
            activeCanvas.Document.IsModified = false;
            activeCanvas.Refresh();

            doc.SolutionEnd += Doc_SolutionEnd;
        }
Пример #11
0
        private void CreateAccentList(string[] values, string nick, int inputParam, int offsetX, int offsetY)
        {
            GH_DocumentIO docIO = new GH_DocumentIO();

            docIO.Document = new GH_Document();

            GH_ValueList vl = new GH_ValueList();

            vl.ListItems.Clear();

            foreach (string item in values)
            {
                GH_ValueListItem vi = new GH_ValueListItem(item, String.Format("\"{0}\"", item));
                vl.ListItems.Add(vi);
            }

            vl.NickName = nick;
            GH_Document doc = OnPingDocument();

            if (docIO.Document == null)
            {
                return;
            }

            docIO.Document.AddObject(vl, false, inputParam);
            PointF currPivot = Params.Input[inputParam].Attributes.Pivot;

            vl.Attributes.Pivot = new PointF(currPivot.X - offsetX, currPivot.Y + offsetY);

            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable <IGH_DocumentObject> objs = docIO.Document.Objects;

            doc.MergeDocument(docIO.Document);
            Component.Params.Input[inputParam].AddSource(vl);
            doc.DeselectAll();
        }
Пример #12
0
        // This is the method that actually does the work.
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Creating this component in the grasshopperdocument
            IGH_Component Component           = this;
            GH_Document   GrasshopperDocument = this.OnPingDocument();

            // Creating input parameters
            //object component = null;
            int    nbCopy    = 0;
            string groupName = null;
            bool   trigger   = false;

            // Getting the data from Grasshopper
            //DA.GetData<object>(0, ref component);
            DA.GetData <string>(1, ref groupName);
            DA.GetData <int>(2, ref nbCopy);
            DA.GetData <bool>(3, ref trigger);

            // If the botton is pressed it will proceed
            if (!trigger)
            {
                return;
            }

            Grasshopper.Kernel.IGH_Param         selNumsInput = Component.Params.Input[0];
            IList <Grasshopper.Kernel.IGH_Param> sources      = selNumsInput.Sources;

            if (!sources.Any())
            {
                return;
            }
            IGH_DocumentObject comp = sources[0].Attributes.GetTopLevel.DocObject;

            // Gets component attributes like the bounds of the component which is used to shift
            // the next one and get the size of the panels
            IGH_Attributes att    = comp.Attributes;
            RectangleF     bounds = att.Bounds;
            int            vShift = (int)Math.Round(bounds.Height) + 10;
            int            vStart = 30 + vShift;

            List <Guid> objectsToCopy = new List <Guid>();

            objectsToCopy.Add(comp.InstanceGuid);

            // Creating a Grasshopper Group g and assignning a nickname and color to it.
            // Adding group g to the GrasshopperDocument
            Grasshopper.Kernel.Special.GH_Group g = new Grasshopper.Kernel.Special.GH_Group();
            g.NickName = groupName;
            g.Colour   = Grasshopper.GUI.Canvas.GH_Skin.group_back;
            GrasshopperDocument.AddObject(g, false);
            List <IGH_Component> components = new List <IGH_Component>();

            // For-loop used to duplicate component and to assign properties to it (size, datalist...)
            for (int i = 0; i < nbCopy; i++)
            {
                GH_DocumentIO documentIO = new GH_DocumentIO(GrasshopperDocument);
                documentIO.Copy(GH_ClipboardType.System, objectsToCopy);
                documentIO.Paste(GH_ClipboardType.System);

                documentIO.Document.TranslateObjects(new Size(0, vStart + i * vShift), false);
                documentIO.Document.SelectAll();
                documentIO.Document.MutateAllIds();

                GrasshopperDocument.DeselectAll();
                GrasshopperDocument.MergeDocument(documentIO.Document);
                g.AddObject(documentIO.Document.Objects[0].InstanceGuid);
            }
            GrasshopperDocument.DeselectAll();
        }
Пример #13
0
        static public void SetValueList(GH_Document doc, GH_Component comp, int InputIndex, List <KeyValuePair <string, string> > valuePairs, string name)
        {
            if (valuePairs.Count == 0)
            {
                return;
            }

            GH_DocumentIO docIO = new GH_DocumentIO();

            docIO.Document = new GH_Document();

            if (docIO.Document == null)
            {
                return;
            }
            doc.MergeDocument(docIO.Document);

            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable <IGH_DocumentObject> objs = docIO.Document.Objects;

            doc.DeselectAll();
            doc.UndoUtil.RecordAddObjectEvent("Create Accent List", objs);
            doc.MergeDocument(docIO.Document);

            doc.ScheduleSolution(10, chanegValuelist);

            void chanegValuelist(GH_Document document)
            {
                IList <IGH_Param> sources = comp.Params.Input[InputIndex].Sources;
                int inputs = sources.Count;

                // If nothing has been conected create a new component
                if (inputs == 0)
                {
                    //instantiate  new value list and clear it
                    GH_ValueList vl = new GH_ValueList();
                    vl.ListItems.Clear();
                    vl.NickName = name;
                    vl.Name     = name;

                    //Create values for list and populate it
                    for (int i = 0; i < valuePairs.Count; ++i)
                    {
                        var item = new GH_ValueListItem(valuePairs[i].Key, valuePairs[i].Value);
                        vl.ListItems.Add(item);
                    }

                    //Add value list to the document
                    document.AddObject(vl, false, 1);

                    //get the pivot of the "accent" param
                    System.Drawing.PointF currPivot = comp.Params.Input[InputIndex].Attributes.Pivot;
                    //set the pivot of the new object
                    vl.Attributes.Pivot = new System.Drawing.PointF(currPivot.X - 210, currPivot.Y - 11);

                    // Connect to input
                    comp.Params.Input[InputIndex].AddSource(vl);
                }

                // If inputs exist replace the existing ones
                else
                {
                    for (int i = 0; i < inputs; ++i)
                    {
                        if (sources[i].Name == "Value List" | sources[i].Name == name)
                        {
                            //instantiate  new value list and clear it
                            GH_ValueList vl = new GH_ValueList();
                            vl.ListItems.Clear();
                            vl.NickName = name;
                            vl.Name     = name;

                            //Create values for list and populate it
                            for (int j = 0; j < valuePairs.Count; ++j)
                            {
                                var item = new GH_ValueListItem(valuePairs[j].Key, valuePairs[j].Value);
                                vl.ListItems.Add(item);
                            }

                            document.AddObject(vl, false, 1);
                            //set the pivot of the new object
                            vl.Attributes.Pivot = sources[i].Attributes.Pivot;

                            var currentSource = sources[i];
                            comp.Params.Input[InputIndex].RemoveSource(sources[i]);

                            currentSource.IsolateObject();
                            document.RemoveObject(currentSource, false);

                            //Connect new vl
                            comp.Params.Input[InputIndex].AddSource(vl);
                        }
                        else
                        {
                            //Do nothing if it dosent mach any of the above
                        }
                    }
                }
            }
        }
Пример #14
0
        /// <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)
        {
            string D = "";

            if (!DA.GetData(0, ref D))
            {
                return;
            }
            string F = "";

            if (!DA.GetData(1, ref F))
            {
                return;
            }
            bool S = false;

            DA.GetData(2, ref S);

            GH_Document ghDoc = null;// = Grasshopper.Instances.ActiveCanvas.Document;

            try
            {
                ghDoc = OnPingDocument();
            }
            catch
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Document not found");
            }

            if (ghDoc == null || !ghDoc.IsFilePathDefined)
            {
                return;
            }

            GH_DocumentIO docIO = new GH_DocumentIO(GH_Document.DuplicateDocument(Grasshopper.Instances.ActiveCanvas.Document));
            string        path, file;

            int nameLen = ghDoc.DisplayName.TrimEnd('*').Length + 3; // +3 accounts for the '.gh' extension missing here
            int fileLen = ghDoc.FilePath.TrimEnd('*').Length;

            if (D == "" || D == null)
            {
                path = ghDoc.FilePath.Substring(0, fileLen - nameLen);
            }
            else
            {
                path = D;
            }
            if (F == "" || F == null)
            {
                file = ghDoc.DisplayName.TrimEnd('*') + ".gh";
            }
            else
            {
                file = F + ".gh";
            }

            docIO.Document.FilePath = path + file;

            if (S)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                docIO.Save();
            }

            DA.SetData(0, docIO.Document.FilePath);
        }