// #! csharp
using System;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.DocObjects;
public class AddLineCommand : Command
{
public override string EnglishName => "AddLineExample";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Prompt for the first point
Point3d point1;
var point1Result = RhinoGet.GetPoint("Select first point", true, out point1);
if (point1Result != Result.Success)
return Result.Cancel;
// Prompt for the second point
Point3d point2;
var point2Result = RhinoGet.GetPoint("Select second point", true, out point2);
if (point2Result != Result.Success)
return Result.Cancel;
// Create a line from the two points
Line line = new Line(point1, point2);
// Add the line to the document
doc.Objects.AddLine(line);
doc.Views.Redraw(); // Redraw the view to show the line
// Return success
return Result.Success;
}
public Result RunCommandFromScript()
{
return RunCommand(RhinoDoc.ActiveDoc,RunMode.Interactive);
}
}
var myCommand = new AddLineCommand();
var result = myCommand.RunCommandFromScript();
how do you test small code snippets ?
In former Times I had a huge “testStuffPlug-in”.
but I like the power of c# scripts:
I started to add / use a public “RunCommandFromScript” Function that can call the protected RunCommand Method.
Initializing an Instance of AddLineCommand and call RunCommandFromScript are just 2 lines that make the hole command function without any plug-in architecture / loading / …
Does this approach violate against anything ?
Do I miss something ? are there nicer approaches to have working code fragments - that might be used later …
If you are writing a C# script, you can simply write the command logic in the global scope of the script and run it in the script editor. This script can later be added to a project and published as a Rhino plugin
// #! csharp
using System;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.DocObjects;
RhinoDoc doc = __rhino_doc__;
Point3d point1;
var point1Result = RhinoGet.GetPoint("Select first point", true, out point1);
if (Result.Success == point1Result)
{
// Prompt for the second point
Point3d point2;
var point2Result = RhinoGet.GetPoint("Select second point", true, out point2);
if (Result.Success == point2Result)
{
// Create a line from the two points
Line line = new Line(point1, point2);
// Add the line to the document
doc.Objects.AddLine(line);
doc.Views.Redraw(); // Redraw the view to show the lineĂŹ
}
}
i wanted a fast method to take some samples for example from dale’ s github and get them running.
for example this.
extend them
with 4 lines to get them running and see if it fits for my problem…
then go on with my complex c# project in visual studio…
one more question: the c# scripts in script editor - each script runs in its own scope ?
let s say i want to have one script that starts an display conduit, and another that disables it. how to set up a scenario for development like this in script-Editor ?
thanks
tom
For that the proper way to do it is to create a C# dll (shared library) that both scripts can reference and access the shared functionality.
A quick and dirty way of doing this (that is similar to using scriptcontext.sticky dictionary is to use the data dictionary that is available on AppDomain in dotnet
First script that sets up the shared api instance:
// #! csharp
using System;
using System.Reflection;
public class SharedAPI
{
public void DoSomething()
{
Rhino.RhinoApp.WriteLine("It Works!");
}
}
var myapi = new SharedAPI();
AppDomain.CurrentDomain.SetData("my-api", myapi);
Consumer script that gains access to the shared api and calls a method on it
// #! csharp
using System;
using System.Reflection;
if (AppDomain.CurrentDomain.GetData("my-api") is object apiObject)
{
dynamic api = apiObject;
api.DoSomething();
}
what s the advantage of this pattern instead of adding an Object to
RhinoDoc.ActiveDoc.RuntimeData
?
I have another problem with (un) subscribing Rhino.Idle Event …
do I miss something or is this the amount of complexity where a full plug-in in Visual Studio is the better choice ?