Skip to content

Commit 55261b3

Browse files
authored
Merge pull request #4 from mitevpi/web-view-2
Creation Components
2 parents d20a36d + 72a9903 commit 55261b3

27 files changed

+2624
-460
lines changed

GHUI/BuildHtmlUiComponent.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.IO;
3+
using Grasshopper.Kernel;
4+
5+
namespace GHUI
6+
{
7+
public class BuildHtmlUiComponent : GH_Component
8+
{
9+
private string _oldString = null;
10+
11+
/// <summary>
12+
/// Component for building a Vue.js UI into a HTML file within
13+
/// Grasshopper.
14+
/// </summary>
15+
public BuildHtmlUiComponent()
16+
: base("Build HTML UI", "HTML UI",
17+
"Build a Web UI to a HTML file.",
18+
"UI", "Main")
19+
{
20+
}
21+
22+
protected override void RegisterInputParams(GH_InputParamManager pManager)
23+
{
24+
pManager.AddTextParameter("HTML Path", "path", "Where to create the Vue HTML interface.",
25+
GH_ParamAccess.item);
26+
pManager.AddTextParameter("HTML String", "html", "The HTML string to write to a file.",
27+
GH_ParamAccess.item);
28+
}
29+
30+
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
31+
{
32+
pManager.AddBooleanParameter("Success", "out", "Status of HTML file creation.", GH_ParamAccess.item);
33+
pManager.AddTextParameter("Path", "path", "The path at which the HTML file has been written.", GH_ParamAccess.item);
34+
}
35+
36+
protected override void SolveInstance(IGH_DataAccess da)
37+
{
38+
// get input from gh component inputs
39+
string path = null;
40+
string htmlString = null;
41+
42+
if (!da.GetData(0, ref path)) return;
43+
if (!da.GetData(1, ref htmlString)) return;
44+
45+
// if the html string is the same, do nothing
46+
if (_oldString == htmlString)
47+
{
48+
}
49+
// otherwise try writing to the html file and inform the user
50+
// of the successful write
51+
else
52+
{
53+
try
54+
{
55+
File.WriteAllText(path, htmlString);
56+
_oldString = htmlString;
57+
}
58+
catch (Exception e)
59+
{
60+
da.SetData(0, false);
61+
da.SetData(1, "");
62+
}
63+
}
64+
65+
// set status message and path output if component executed successfully
66+
da.SetData(0, true);
67+
da.SetData(1, path);
68+
69+
GH_Document doc = OnPingDocument();
70+
doc?.ScheduleSolution(500, ScheduleCallback);
71+
}
72+
73+
74+
private void ScheduleCallback(GH_Document document)
75+
{
76+
ExpireSolution(false);
77+
}
78+
79+
protected override System.Drawing.Bitmap Icon => Properties.Resources.web_window;
80+
81+
public override Guid ComponentGuid => new Guid("1c7a41f6-2e46-4a7b-a67d-b7621dc312b4");
82+
}
83+
}

GHUI/BuildSliderComponent.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using Grasshopper.Kernel;
4+
5+
namespace GHUI
6+
{
7+
public class BuildSliderComponent : GH_Component
8+
{
9+
private string _oldString = null;
10+
11+
/// <summary>
12+
/// Component for building a Vue.js UI into a HTML file within
13+
/// Grasshopper.
14+
/// </summary>
15+
public BuildSliderComponent()
16+
: base("Create Slider", "Slider",
17+
"Create a HTML Slider Input.",
18+
"UI", "Create")
19+
{
20+
}
21+
22+
protected override void RegisterInputParams(GH_InputParamManager pManager)
23+
{
24+
pManager.AddTextParameter("Name", "name", "The name of the slider input component.", GH_ParamAccess.item,
25+
"slider");
26+
pManager.AddTextParameter("ID", "id", "The id of the slider input component.", GH_ParamAccess.item,
27+
"slider");
28+
pManager.AddNumberParameter("Value", "val", "The starting value of the slider input component.",
29+
GH_ParamAccess.item, 50);
30+
pManager.AddNumberParameter("Min", "min", "The min value of the slider input component.",
31+
GH_ParamAccess.item, 0);
32+
pManager.AddNumberParameter("Max", "max", "The max value of the slider input component.",
33+
GH_ParamAccess.item, 100);
34+
}
35+
36+
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
37+
{
38+
pManager.AddTextParameter("HTML", "html", "The HTML code for the created slider input.",
39+
GH_ParamAccess.list);
40+
}
41+
42+
protected override void SolveInstance(IGH_DataAccess da)
43+
{
44+
// get input from gh component inputs
45+
string name = null;
46+
string id = null;
47+
double value = 50;
48+
double min = 0;
49+
double max = 100;
50+
51+
da.GetData(0, ref name);
52+
da.GetData(1, ref id);
53+
da.GetData(2, ref value);
54+
da.GetData(3, ref min);
55+
da.GetData(4, ref max);
56+
57+
// create a valid HTML string from the inputs for our slider
58+
string sliderString =
59+
$"<input type='range' id='{id}' name='{name}' value='{value}' min='{min}' max='{max}'>";
60+
61+
da.SetData(0, sliderString);
62+
63+
GH_Document doc = OnPingDocument();
64+
doc?.ScheduleSolution(500, ScheduleCallback);
65+
}
66+
67+
68+
private void ScheduleCallback(GH_Document document)
69+
{
70+
ExpireSolution(false);
71+
}
72+
73+
protected override System.Drawing.Bitmap Icon => Properties.Resources.slider;
74+
75+
public override Guid ComponentGuid => new Guid("1c2a47f6-2e46-2a7b-a17d-b2629dc312b3");
76+
}
77+
}

GHUI/Classes/Models/DomInputModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GHUI.Classes.Models
2+
{
3+
public class DomInputModel
4+
{
5+
public string id;
6+
public string value;
7+
public string name;
8+
public string max;
9+
public string min;
10+
public string type;
11+
public string isChecked;
12+
}
13+
}

0 commit comments

Comments
 (0)