Grasshopper

algorithmic modeling for Rhino

C#: No value is stored in the output of this custom mouse-reponsive component

Hi Guys,

The following code compiles with no errors and loads on Rhino5. However no value is stored in the output at all: The S should give a warning when no file is loaded and show the file path if the file is loaded (the m_settings variable should store that path). What am I doing wrong?

        public class SettingsComponentAttributes : GH_ComponentAttributes
            {   
                  public SettingsComponentAttributes(IGH_Component SettingsComponent) : base(SettingsComponent) {}
          
         public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e)
               {
                   ((SettingsComponent)Owner).ShowSettingsGui();
                    return GH_ObjectResponse.Handled;
               }  
            }   
   
       public class SettingsComponent : GH_Component
           {
                public SettingsComponent(): base("LoadSettings", "LoadSettings", "Loading ini", "Extra", "") { }

                public override void CreateAttributes()
                {
                    m_attributes = new SettingsComponentAttributes(this);
                }
                private string m_settings;

                public void ShowSettingsGui()
                {
                    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
                    ofd.Multiselect = true;
                    ofd.Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        string[] filePath = ofd.FileNames;
                        string[] safeFilePath = ofd.SafeFileNames;
                    }
       
                    m_settings = Path.GetDirectoryName(ofd.FileName);
                }

                protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
                {
                    pManager.Register_StringParam("DataPath", "S", "SettingsFile");
                }

                protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
                { }

                protected override void SolveInstance(IGH_DataAccess DA)
                {
                    if (m_settings == null)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
                        return;
                    }
                    DA.SetData(0, m_settings);
                }

              

        

Views: 345

Replies to This Discussion

When you change the state of your component, you have to start a new solution. After:

m_settings = Path.GetDirectoryName(ofd.FileName);

 

add a call to ExpireSolution(true);

 

Also, if you press F5 without adding that code, does it add the settings then?

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks so much David, it's working finally!

The F5 debugging part is not really working though, this is what I get although i tried to set all the paths to my rhino folders (and after building the project).

I meant F5 in Grasshopper. Which starts a new solution for all components, not just the ones that were tagged as expired.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service