Grasshopper

algorithmic modeling for Rhino

C#: Method to split the separate lines of a list of string using indices?

Hi Guys,

Is there a simple method to separate the lines of a string similarly to what is describe here. The ini file that I am using already has separated lines but  when importing it using the code below, it does not separate the lines with indices.

What kind of data is directly separated with indices by GH?

Many thanks,
Arthur

      public void ShowSettingsGui()
                {
                    var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
                    if (dialog.ShowDialog() != DialogResult.OK) return;
                    m_settings = File.ReadAllText(dialog.FileName);
                    ExpireSolution(true);
                }

                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: 1851

Replies to This Discussion

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks a lot David, I have tried the code below and a simpler way using 

m_settings = File.ReadAllLines(dialog.FileName); but I get

{0} 0. System.String[]as output of my component in both cases instead of the actual lines.

string m_settings_temp;
string[] m_settings;
public void ShowSettingsGui()
{
var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
if (dialog.ShowDialog() != DialogResult.OK) return;

m_settings_temp = File.ReadAllText(dialog.FileName);
m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
}

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);
}

 

 I have also tried to add:

for (var i = 0; i < m_settings.Length; i ++)
{
DA.SetData(i, m_settings[i]);
}

 

but I am getting the error message on GH below:

 

If it can help I get the following exception in VS:

System.Exception occurred
  Message=Unknown file
  Source=Grasshopper
  StackTrace:
       at Grasshopper.Global_Proc.ASSERT(Guid assert_id, String message, Exception exception) in C:\dev\Grasshopper\1.0\root\src\GH_GlobalProc.vb:line 98
  InnerException:

m_settings is an array of strings, so you need to assign lists:

 

DA.SetDataList(0, m_settings);

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service