forked from ProjectSilkworm/Silkworm
/
LoadSettings.cs
156 lines (104 loc) · 4.4 KB
/
LoadSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using Grasshopper.Kernel.Attributes;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Rhino.Geometry;
using GH_IO.Serialization;
namespace Silkworm_LoadSettings
{
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", "Silkworm", "I/O") { }
public override void CreateAttributes()
{
m_attributes = new SettingsComponentAttributes(this);
}
string m_settings_temp;
private 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);
ExpireSolution(true);
}
public override bool Write(GH_IWriter writer)
{
if (m_settings != null && m_settings.Length > 0)
{
writer.SetInt32("StringCount", m_settings.Length);
for (int i = 0; i < m_settings.Length; i++)
{
writer.SetString("String", i, m_settings[i]);
}
}
return base.Write(writer);
}
public override bool Read(GH_IReader reader)
{
m_settings = null;
int count = 0;
reader.TryGetInt32("StringCount", ref count);
if (count > 0)
{
System.Array.Resize(ref m_settings, count);
for (int i = 0; i < count; i++)
{
string line = null;
reader.TryGetString("String", i, ref line);
m_settings[i] = line;
}
}
return base.Read(reader);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
if (m_settings == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
return;
}
else
{
DA.SetDataList(0, m_settings);
}
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.Register_StringParam("Dictionary", "D", "SettingsFile");
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{ }
public override Guid ComponentGuid
{
get { return new Guid("{35ee73e3-d7f4-4eec-970e-87b35b804dd3}"); }
}
protected override System.Drawing.Bitmap Icon
{
get
{
return null;
}
}
}
}