Hello, i try to compile this code but i got an error, how i can fix it
Hi, Iâm not so sure if this leads to success if you do it like this. But potentially you could inject that somehow and run it. Where did you get that approach from?
The error raises because Script_Instance is a derivative of the abstract base class âGH_ScriptInstanceâ, but it has not all required members implemented. Abstract base classes can force to implement some members if they are defined abstract
public abstract class MyAbstractClass
{
public abstract void DoSomething();
}
public class MyConcreteClass : MyAbstractClass
{
// if this is missing CS0534
public override void DoSomething()
{
// simulate work
Thread.Sleep(4000);
}
}
Thanks , i will check this
I saw an example in microsoft website about compiling cs file and load it in ironpython as dll.
I thought i can use the same method with this code but i am not sure about that.
It is relating to rhinoinside which is very complicated and no clear tutorial available.
Make sure you are using the correct compiler. Modern C# code examples are using âCoreâ which is not the same as using the âFramework 4.xâ.
Core uses the âRoslynâ compiler, and this is like compiling for a different language. With the same compiler, IronPython and C# should match. I have done a similar thing here: HowTo: Run C# Script in Rhino using VS Code
Grasshopperâs default C# code is abbreviated. Many implementation details are omitted. If you really need to write C# component outside Grasshopper, use âExport Sourceâ in the right-click menu of C# component. But generally itâs not a good practice afaik and I guess the C# component approach wonât fulfill your objective.
Thank you All
This solve the problem
I need to test the dll with Ironpython , maybe it work or not
I load the dll in Ironpython but donât understand how to use it like the original script to receive data (point)
the original code
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(ref object Receive_pt)
{
Receive(Component);
Receive_pt = pt;
}
// <Custom additional code>
Point3d pt;
bool registered = false;
IGH_Component comp = null;
void Receive(IGH_Component component)
{
if(!registered)
{
Rhino.Runtime.HostUtils.RegisterNamedCallback("ToAnsys", RiRhino);
comp = component;
registered = true;
}
}
void RiRhino(object sender, Rhino.Runtime.NamedParametersEventArgs args)
{
Point3d values;
if (args.TryGetPoint("point", out values))
{
pt = values;
}
comp.ExpireSolution(true);
}
The new code exported
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(ref object Receive_pt)
{
Receive(Component);
Receive_pt = pt;
}
// <Custom additional code>
Point3d pt;
bool registered = false;
IGH_Component comp = null;
void Receive(IGH_Component component)
{
if(!registered)
{
Rhino.Runtime.HostUtils.RegisterNamedCallback("ToAnsys", RiRhino);
comp = component;
registered = true;
}
}
void RiRhino(object sender, Rhino.Runtime.NamedParametersEventArgs args)
{
Point3d values;
if (args.TryGetPoint("point", out values))
{
pt = values;
}
comp.ExpireSolution(true);
}
// </Custom additional code>
private List<string> __err = new List<string>(); //Do not modify this list directly.
private List<string> __out = new List<string>(); //Do not modify this list directly.
private RhinoDoc doc = RhinoDoc.ActiveDoc; //Legacy field.
private IGH_ActiveObject owner; //Legacy field.
private int runCount; //Legacy field.
public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List<object> inputs, IGH_DataAccess DA)
{
//Prepare for a new run...
//1. Reset lists
this.__out.Clear();
this.__err.Clear();
this.Component = owner;
this.Iteration = iteration;
this.GrasshopperDocument = owner.OnPingDocument();
this.RhinoDocument = rhinoDocument as Rhino.RhinoDoc;
this.owner = this.Component;
this.runCount = this.Iteration;
this. doc = this.RhinoDocument;
//2. Assign input parameters
//3. Declare output parameters
object Receive_pt = null;
//4. Invoke RunScript
RunScript(ref Receive_pt);
try
{
//5. Assign output parameters to component...
if (Receive_pt != null)
{
if (GH_Format.TreatAsCollection(Receive_pt))
{
IEnumerable __enum_Receive_pt = (IEnumerable)(Receive_pt);
DA.SetDataList(0, __enum_Receive_pt);
}
else
{
if (Receive_pt is Grasshopper.Kernel.Data.IGH_DataTree)
{
//merge tree
DA.SetDataTree(0, (Grasshopper.Kernel.Data.IGH_DataTree)(Receive_pt));
}
else
{
//assign direct
DA.SetData(0, Receive_pt);
}
}
}
else
{
DA.SetData(0, null);
}
}
catch (Exception ex)
{
this.__err.Add(string.Format("Script exception: {0}", ex.Message));
}
finally
{
//Add errors and messages...
if (owner.Params.Output.Count > 0)
{
if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
{
List<string> __errors_plus_messages = new List<string>();
if (this.__err != null) { __errors_plus_messages.AddRange(this.__err); }
if (this.__out != null) { __errors_plus_messages.AddRange(this.__out); }
if (__errors_plus_messages.Count > 0)
DA.SetDataList(0, __errors_plus_messages);
}
}
}
}
}

