Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 38cf73f

Browse files
committed
changes and fixes for post-Grasshopper 0.9.61 full compatibility. Includes:
- Grasshopper.dll is automatically added as a reference for scripting - addition and removal of Inputs and Outputs is supported in the native Grasshopper ZUI way - code input and out output stopped working. This is fixed - code input and out output are now removable also via + and - ZUI signs - several IO issues in deprecated (old) components were fixed. Also old components should keep on working properly - GH_IO.dll deprecated iteration over chunks was removed - registration of input parameters now via the Grasshopper SKD-suggested way - floating editor could remain linked to a deleted component. This is fixed
1 parent fa25e40 commit 38cf73f

18 files changed

+685
-676
lines changed

Component/DocStringUtils.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55

66
namespace GhPython.Component
77
{
8-
/*
9-
# based on DocStrings as defined in
10-
# http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Comments
11-
""""""
12-
Title: Arithmetic Series
13-
Description: Computes the Sum of an Arithmetic Progression, or the
14-
sum of all numbers from F to L, included.
15-
Args:
16-
F: the first number included in the series.
17-
L: the last number included in the series.
18-
Returns:
19-
S: If F > L, then sum of all numbers [F,L].
20-
If F = L, then 0.
21-
If F < L, then sum of all numbers (L,F).
22-
K: Not used.
23-
Help:
24-
See also the Gauss elementary school story:
25-
http://mathworld.wolfram.com/ArithmeticSeries.html
26-
""""""
27-
*/
8+
/*
9+
# based on DocStrings as defined in
10+
# http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Comments
11+
""""""
12+
Title: Arithmetic Series
13+
Description: Computes the Sum of an Arithmetic Progression, or the
14+
sum of all numbers from F to L, included.
15+
Args:
16+
F: the first number included in the series.
17+
L: the last number included in the series.
18+
Returns:
19+
S: If F > L, then sum of all numbers [F,L].
20+
If F = L, then 0.
21+
If F < L, then sum of all numbers (L,F).
22+
K: Not used.
23+
Help:
24+
See also the Gauss elementary school story:
25+
http://mathworld.wolfram.com/ArithmeticSeries.html
26+
""""""
27+
*/
2828
class DocStringUtils
2929
{
3030
public static bool FindApplyDocString(string code, ScriptingAncestorComponent component)
@@ -55,7 +55,8 @@ public static bool FindApplyDocString(string code, ScriptingAncestorComponent co
5555
int endSeparator = line.IndexOf(_docStringSeparator);
5656
if (endSeparator != -1) line = line.Substring(0, endSeparator);
5757

58-
if (IsEmptyLine(line)) {
58+
if (IsEmptyLine(line))
59+
{
5960
if (endSeparator != -1) break;
6061
continue;
6162
}
@@ -141,7 +142,7 @@ private static void Send(string variable, ref StringBuilder result, KeywordType
141142
{
142143
if (variable != null)
143144
{
144-
switch(type)
145+
switch (type)
145146
{
146147
case KeywordType.Description:
147148
component.Description = result.ToString();
@@ -153,7 +154,7 @@ private static void Send(string variable, ref StringBuilder result, KeywordType
153154
FindAndDescribe(component.Params.Output, variable, result.ToString());
154155
break;
155156
case KeywordType.Help:
156-
component.SpecialPythonHelpContent = result.ToString();
157+
component.AdditionalHelpFromDocStrings = result.ToString();
157158
break;
158159
}
159160
result = new StringBuilder();

Component/PyUpgrader.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,11 @@ public IGH_DocumentObject Upgrade(IGH_DocumentObject target, GH_Document documen
1414

1515
ZuiPythonComponent component_new = new ZuiPythonComponent();
1616

17-
bool show_code_input = false;
18-
if (component_OBSOLETE.CodeInputVisible)
19-
{
20-
// see if the "code" input on the old component really has anything
21-
// hooked up to it. If not, don't show the input
22-
show_code_input = component_OBSOLETE.Params.Input[0].SourceCount > 0;
23-
}
24-
component_new.CodeInputVisible = show_code_input;
25-
26-
component_new.HideCodeOutput = component_OBSOLETE.HideCodeOutput;
17+
component_new.HiddenCodeInput = component_OBSOLETE.HiddenCodeInput;
18+
component_new.HiddenOutOutput = component_OBSOLETE.HiddenOutOutput;
2719

28-
if (component_new.HideCodeOutput)
29-
component_new.Params.Output.RemoveAt(0);
30-
31-
if (!component_new.CodeInputVisible)
32-
component_new.CodeInput = component_OBSOLETE.CodeInput;
33-
34-
component_OBSOLETE.Dispose();
20+
if (!component_new.HiddenCodeInput)
21+
component_new.Code = component_OBSOLETE.Code;
3522

3623
if (GH_UpgradeUtil.SwapComponents(component_OBSOLETE, component_new))
3724
{
@@ -55,7 +42,8 @@ public IGH_DocumentObject Upgrade(IGH_DocumentObject target, GH_Document documen
5542
}
5643
}
5744

58-
component_new.CodeInputVisible = show_code_input;
45+
component_OBSOLETE.Dispose();
46+
5947
return component_new;
6048
}
6149
return null;
@@ -73,7 +61,7 @@ public Guid UpgradeTo
7361

7462
public DateTime Version
7563
{
76-
get { return new DateTime(2012, 3, 19, 21, 0, 0); }
64+
get { return new DateTime(2013, 10, 14, 0, 0, 0); }
7765
}
7866
}
7967
}

Component/PythonComponent.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class PythonComponent_OBSOLETE : ScriptingAncestorComponent, IGH_VarParam
1414
{
1515
protected override void AddDefaultInput(GH_Component.GH_InputParamManager inputManager)
1616
{
17-
inputManager.RegisterParam(ConstructVariable(GH_VarParamSide.Input, "x"));
18-
inputManager.RegisterParam(ConstructVariable(GH_VarParamSide.Input, "y"));
17+
inputManager.AddParameter(ConstructVariable(GH_VarParamSide.Input, "x"));
18+
inputManager.AddParameter(ConstructVariable(GH_VarParamSide.Input, "y"));
1919
}
2020

2121
protected override void AddDefaultOutput(GH_Component.GH_OutputParamManager outputManager)
@@ -88,7 +88,7 @@ public void SetDoc(object sender, EventArgs e)
8888
{
8989
try
9090
{
91-
Component.CheckAndSetupActions();
91+
Component.CheckIfSetupActionsAreNecessary();
9292

9393
Component.DocStorageMode = NewDocStorage;
9494
Component.SetScriptTransientGlobals();
@@ -181,13 +181,13 @@ public bool IsOutputVariable
181181

182182
public bool IsVariableParam(GH_VarParamEventArgs e)
183183
{
184-
return e.Index > (!CodeInputVisible ? -1 : 0);
184+
return e.Index > (!HiddenCodeInput ? -1 : 0);
185185
}
186186

187187
public void ManagerConstructed(GH_VarParamSide side, Grasshopper.GUI.GH_VariableParameterManager manager)
188188
{
189189
string pool = (side == GH_VarParamSide.Input) ? "xyzuvw" : "abcdef";
190-
manager.NameConstructor = new GH_CharPatternParamNameConstructor(pool, 4);
190+
manager.NameConstructor = new Grasshopper.Kernel.GH_StringPattern(pool, 4);
191191
}
192192

193193
public void ParametersModified(GH_VarParamSide side)

Component/PythonComponentAttributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void OpenEditor()
2525
var attachedComp = this.Owner as ScriptingAncestorComponent;
2626
if (attachedComp != null && !attachedComp.Locked)
2727
{
28-
attachedComp.CheckAndSetupActions();
28+
attachedComp.CheckIfSetupActionsAreNecessary();
2929

3030
if (m_form == null || m_form.IsDisposed)
3131
m_form = new PythonScriptForm(attachedComp);

Component/PythonEnvironment.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
22
using Grasshopper.Kernel;
33
using Rhino.Runtime;
4+
using System.Reflection;
5+
using System.Linq;
6+
using System.Collections.Generic;
7+
using System.Collections;
48

59
namespace GhPython.Component
610
{
@@ -21,6 +25,19 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
2125
LocalScope = scopeField.GetValue(script);
2226
}
2327

28+
var intellisenseField = scriptType.GetField("m_intellisense",
29+
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField);
30+
if (intellisenseField != null)
31+
{
32+
Intellisense = intellisenseField.GetValue(script);
33+
if (Intellisense != null)
34+
{
35+
var intellisenseType = Intellisense.GetType();
36+
var scopeProperty = intellisenseType.GetProperty("Scope");
37+
IntellisenseScope = scopeProperty.GetValue(Intellisense, null);
38+
}
39+
}
40+
2441
var baseType = scriptType.BaseType;
2542
if (baseType != null && baseType != typeof (object))
2643
{
@@ -29,8 +46,16 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
2946
{
3047
var engineInfo = hostType.GetProperty("Engine");
3148
if (engineInfo != null)
49+
{
3250
Engine = engineInfo.GetValue(null, null);
3351

52+
if (Engine != null)
53+
{
54+
var runtimeInfo = Engine.GetType().GetProperty("Runtime");
55+
Runtime = runtimeInfo.GetValue(Engine, null);
56+
}
57+
}
58+
3459
var scopeInfo = hostType.GetProperty("Scope", System.Reflection.BindingFlags.NonPublic |
3560
System.Reflection.BindingFlags.GetProperty |
3661
System.Reflection.BindingFlags.Static);
@@ -39,7 +64,6 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
3964
}
4065
}
4166
}
42-
4367
}
4468

4569
public GH_Component Component { get; internal set; }
@@ -50,6 +74,60 @@ internal PythonEnvironment(Grasshopper.Kernel.GH_Component component, PythonScri
5074

5175
public object ScriptScope { get; internal set; }
5276

77+
public object Intellisense { get; internal set; }
78+
79+
public object IntellisenseScope { get; internal set; }
80+
5381
public object Engine { get; internal set; }
82+
83+
public object Runtime { get; internal set; }
84+
85+
public void LoadAssembly(System.Reflection.Assembly assembly)
86+
{
87+
FunctionalityLoad(assembly);
88+
89+
// now intellisense
90+
if (IntellisenseScope == null) return;
91+
92+
// we really want to get intellisense right away. No matter what
93+
// so, we first make it cache, then add to it
94+
95+
var intellisenseType = Intellisense.GetType();
96+
var m = intellisenseType.GetMethod("GetModuleList", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod);
97+
m.Invoke(Intellisense, null);
98+
99+
var ex_m_autocomplete_modules = intellisenseType.GetField("m_autocomplete_modules",
100+
BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
101+
102+
if (ex_m_autocomplete_modules == null) return;
103+
var list = ex_m_autocomplete_modules.GetValue(Intellisense) as IList;
104+
105+
if (list == null) return;
106+
foreach (var namesp in GetToplevelNamespacesForAssembly(assembly))
107+
{
108+
list.Add(namesp);
109+
}
110+
}
111+
112+
private void FunctionalityLoad(System.Reflection.Assembly assembly)
113+
{
114+
var runtime = Runtime as dynamic;
115+
runtime.LoadAssembly(assembly);
116+
}
117+
118+
private static IEnumerable<string> GetToplevelNamespacesForAssembly(Assembly assembly)
119+
{
120+
return assembly.GetTypes().Select(GetTopLevelNamespace)
121+
.Where(s => !string.IsNullOrEmpty(s)).Distinct();
122+
}
123+
124+
// question by David here:
125+
// http://stackoverflow.com/questions/1549198/finding-all-namespaces-in-an-assembly-using-reflection-dotnet
126+
static string GetTopLevelNamespace(Type t)
127+
{
128+
string ns = t.Namespace ?? "";
129+
int firstDot = ns.IndexOf('.');
130+
return firstDot == -1 ? ns : ns.Substring(0, firstDot);
131+
}
54132
}
55133
}

0 commit comments

Comments
 (0)