Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions GHScriptGPT/Scripts/CurrentEditor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Grasshopper.GUI.Script;
using Grasshopper.Kernel;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -52,15 +54,142 @@ public SouceCode GetCode_RunScript()

public void SetCode_RunScript(SouceCode code)
{
/*
GH_CodeBlock newCodeBlock = new GH_CodeBlock();
newCodeBlock.AddLines(code.CodeLines);
_codeBlocks[3] = newCodeBlock;
UpdateEditor();
*/
InsertBlock(code.CodeText);
}

private void UpdateEditor()
{
_editor.SetSourceCode(_codeBlocks);

}

private void InsertBlock(string text)
{

BindingFlags bind = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

// CodeEditor
PropertyInfo pInfo_CodeEditor = _editor.GetType().GetProperty("CodeEditor", bind);
object codeEditor = pInfo_CodeEditor.GetValue(_editor);
Type type_codeEditor = typeof(Grasshopper.GUI.GH_AlignWidgetSettingsUI).Assembly.GetType("Grasshopper.GUI.GH_WndProcOverridenCodeEditor");


string systemPath = Path.GetDirectoryName(typeof(Rhino.RhinoDoc).Assembly.Location);
string assemblyPath = Path.Combine(systemPath, "QWhale.Editor.dll");
Assembly assembly = Assembly.LoadFile(assemblyPath);


// Selection
PropertyInfo pInfo_Selection = type_codeEditor.GetProperty("Selection", bind);
object selection = pInfo_Selection.GetValue(codeEditor);
Type type_Selection = assembly.GetType("QWhale.Editor.Selection", true);

// TextSource
PropertyInfo pInfo_Source = type_codeEditor.GetProperty("Source", bind);
object textSource = pInfo_Source.GetValue(codeEditor);
Type type_TextSource = assembly.GetType("QWhale.Editor.TextSource.TextSource", true);

// Position
PropertyInfo pInfo_Position = type_TextSource.GetProperty("Position", bind);



List<Rectangle> mutableRecs = GetMutableBlockRec(_codeBlocks);
Rectangle rec = mutableRecs[1];

// Position Change
pInfo_Position.SetValue(textSource, new Point(rec.X, rec.Y));

// Set Selection
Type type_SelectionType = assembly.GetType("QWhale.Editor.SelectionType", true);
MethodInfo mInfo_SetSelection = type_Selection.GetMethod("SetSelection", new Type[]{ type_SelectionType, typeof(Rectangle)});
mInfo_SetSelection.Invoke(selection, new object[] { 1, rec });

// Set Text
MethodInfo mInfo_SetSelectedText = type_Selection.GetMethod("SetSelectedText", new Type[] { typeof(string), type_SelectionType, typeof(bool) });
mInfo_SetSelectedText.Invoke(selection, new object[] { text, 1, true });

// Smart Format
MethodInfo mInfo_SmartFormatDocument = type_Selection.GetMethod("SmartFormatDocument", bind);
mInfo_SmartFormatDocument.Invoke(selection, new object[] { });

// Clear
MethodInfo mInfo_Clear = type_Selection.GetMethod("Clear", bind);
mInfo_Clear.Invoke(selection, new object[] { });


}

private List<int[]> GetMutableIndex(bool[] isReadOnlys)
{
List<int[]> mutableIndexes = new List<int[]>();

List<int> startIndexes = new List<int>();
List<int> endIndexes = new List<int>();

int count = isReadOnlys.Length;
bool previous = false;

for (int i = 0; i < count; i++)
{
bool isReadOnly = isReadOnlys[i];
try
{
if (isReadOnly) continue;
if (isReadOnlys[i + 1]) endIndexes.Add(i);
if (!previous) continue;

startIndexes.Add(i);

}
finally
{
previous = isReadOnly;
}
}

for (int i = 0; i < startIndexes.Count; i++)
{
int[] start_end = new int[2];
start_end[0] = startIndexes[i];
start_end[1] = endIndexes[i];
mutableIndexes.Add(start_end);
}

return mutableIndexes;
}

private List<Rectangle> GetMutableBlockRec(GH_CodeBlocks codeBlocks)
{
List<Rectangle> recs = new List<Rectangle>();

string[] lines = new string[] { };
bool[] isReadOnlys = new bool[] { };

codeBlocks.GetAllLines(ref lines, ref isReadOnlys);

List<int[]> mutableIndexes = GetMutableIndex(isReadOnlys);

foreach (int[] start_end in mutableIndexes)
{
int start = start_end[0];
int end = start_end[1];

int height = end - start;

int width = lines[end].Length;

Rectangle rec = new Rectangle(0, start, width, height);
recs.Add(rec);
}

return recs;
}

public IEnumerable<string> GetErrors()
Expand Down