forked from mdjcad/SampleCsCommands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleCsAutomateGrasshopper.cs
97 lines (83 loc) · 2.68 KB
/
SampleCsAutomateGrasshopper.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
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Rhino;
using Rhino.Commands;
namespace SampleCsCommands
{
[System.Runtime.InteropServices.Guid("cb825773-b04f-49df-9c12-5e9134696854")]
public class SampleCsAutomateGrasshopper : Command
{
/// <summary>
/// Grasshopper's plug-in Guid
/// </summary>
private readonly Guid m_gh_guid = new Guid("b45a29b1-4343-4035-989e-044e8580d9cf");
/// <summary>
/// Constructor
/// </summary>
public SampleCsAutomateGrasshopper()
{
}
/// <summary>
/// EnglishName override
/// </summary>
public override string EnglishName
{
get { return "SampleCsAutomateGrasshopper"; }
}
/// <summary>
/// RunCommand override
/// </summary>
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Verify that Grasshopper is installed
string gh_path = Rhino.PlugIns.PlugIn.PathFromId(m_gh_guid);
if (string.IsNullOrEmpty(gh_path) && File.Exists(gh_path))
{
RhinoApp.WriteLine("Grasshopper not installed.");
return Result.Cancel;
}
// Prompt the user for a Grasshopper solution to open
string filename = string.Empty;
if (mode == RunMode.Interactive)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = @"Grasshopper Files (*.gh;*.ghx)|*.gh; *.ghx||";
dialog.DefaultExt = "gh";
DialogResult rc = dialog.ShowDialog();
if (rc != DialogResult.OK)
return Result.Cancel;
filename = dialog.FileName;
}
else
{
Result rc = Rhino.Input.RhinoGet.GetString("Grasshopper file to open", false, ref filename);
if (rc != Result.Success)
return rc;
}
// Verify Grasshopper file
if (string.IsNullOrEmpty(filename) || !File.Exists(filename))
return Result.Failure;
try
{
// Try getting the Grasshopper COM object
object gh_object = RhinoApp.GetPlugInObject(m_gh_guid);
// Create a parameters array
object[] parameters = new object[1];
parameters[0] = filename;
// Invoke the OpenDocument method
gh_object.GetType().InvokeMember("OpenDocument", BindingFlags.InvokeMethod, null, gh_object, parameters);
// You can use the folloiwng to dump the names and types of
// all of the members to the command line...
//foreach (MemberInfo member in gh_object.GetType().GetMembers())
// RhinoApp.WriteLine("{0} -- {1}", member.MemberType, member);
}
catch (Exception ex)
{
RhinoApp.WriteLine(ex.Message);
}
return Result.Success;
}
}
}