How to perserve data in gh component?

unhandled

(Andrealu2012) #1


i make a custom component(by c#),and it can be input some data in it in a small winform.like the picture…
but every time i restart the rhino,the data stored in it is disapppears…because i dont know how to perserve data in gh file.
of course i can use a data file(may be hidden in C:…) to store file…but it is not so elegant…
can someone has some idea of how to perserve data in gh custom component ?Thank you.


(David Rutten) #2

You must override the Read() and Write() methods on your component and include your values in the archive. Be sure to call the base class Read and Write, otherwise you’ll break IO.


(Andrealu2012) #3

thank you david ,it works,before 2days,i used static class to store the transport value,it have a lot of problem,cannot be serialize,cannot have multi different component in one gh file.now i change into publice member refereto this post:


and it works.
this is my final code
hope it is helpful for other people

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rhino.Geometry;
using Rhino;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Rhino.Display;
using System.Drawing;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;

using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
using Grasshopper.Kernel.Attributes;

namespace testf4goo
{
    public class TestWinFormAttributes:GH_ComponentAttributes
    {
        public TestWinFormAttributes(TestWinForm owner) : base(owner) { }
        public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender,GH_CanvasMouseEvent e)
        {
            
            Form1 f1 = new Form1(this.Owner as TestWinForm);

            Grasshopper.GUI.GH_WindowsFormUtil.CenterFormOnCursor(f1, true);
            f1.ShowDialog();
            
            
            Owner.ExpireSolution(true);
            
            
            return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled;

        }
        public override void ExpireLayout()
        {
            base.ExpireLayout();
        }

    }



    public class TestWinForm : GH_Component
    {
        public double aaa;
        /// <summary>
        /// Initializes a new instance of the TestWinForm class.
        /// </summary>
        /// 
        public TestWinForm()
          : base("TestWinForm", "WF",
              "Description",
              "Test", "Test")
        {
        }
        public override void CreateAttributes()
        {
            // base.CreateAttributes();
            m_attributes = new TestWinFormAttributes(this);

        }
        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddNumberParameter("Input", "I", "inputNum", GH_ParamAccess.item,0);

        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddNumberParameter("Output", "O", "outputNum", GH_ParamAccess.item);
        }

        

        /// <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)
        {
            double A=0;
            DA.GetData("Input", ref A);
            //double B = 0;
            double C = aaa+A;

            Message = $"result is :{C}";
            DA.SetData("Output", C);

        }




        public override bool Write(GH_IWriter writer)
        {
            writer.SetDouble("fromWinForm", this.aaa);
            return base.Write(writer);
           // return this.Write(writer);

        }

        public override bool Read(GH_IReader reader)
        {
            this.aaa = reader.GetDouble("fromWinForm");
            return base.Read(reader);
        }

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                //You can add image files to your project resources and access them like this:
                // return Resources.IconForThisComponent;
                return null;
            }
        }

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid
        {

            get { return new Guid("9F8A0403-01EA-469D-B91F-02165B952503"); }
        }
    }
}

//===================
form1 code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Rhino.Geometry;
using Rhino;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Rhino.Display;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;

using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
using Grasshopper.Kernel.Attributes;

namespace testf4goo
{
    public partial class Form1 : Form
    {
        TestWinForm comp;
        public Form1()
        {
            InitializeComponent();

            this.ControlBox = false;
           
        }
        public Form1(TestWinForm owner) : this()
        { comp = owner; }


        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = System.Convert.ToString(comp.aaa);

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            //the instance of TestWinForm's data member = textBox1.Text;
            //  Button B = (Button)sender;
            //B = System.Convert.ToDouble( textBox1.Text);
            // this.
            //aaa =System.Convert.ToDouble(textBox1.Text);

            comp.aaa = System.Convert.ToDouble(textBox1.Text);


            comp.ExpireSolution(true);

            this.Close();
        }
    }
}

(David Rutten) #4

You can wrap any code you post in discourse in triple ticks to format it correctly.

```
string code = “Would look correctly” + " like this.";
```

results in:

string code = "Would look correctly" + " like this.";

(Andrealu2012) #5
Thank you,David,i have already fix the problem.