Hello.
I recently started developing Rhino plug-in.
I would like to implement a command that allows the user to select multiple objects interactively, but it is difficult.
Below is the sample code of the Command class.
As the execution result, only “Select base object A” is output, and “Select target object B” is not output.
For some reason, the variables pointA and pointB contain exactly the same value.
using Rhino;
using Rhino.Commands;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.Geometry;
public class GetRelativeCoordinatesCommand : Command
{
public GetRelativeCoordinatesCommand()
{
Instance = this;
}
public static GetRelativeCoordinatesCommand Instance { get; private set; }
public override string EnglishName => "GetRelativeCoordinates";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
ObjRef objARef = null;
using (var getObject = new GetObject())
{
getObject.SetCommandPrompt("Select base object A");
getObject.SubObjectSelect = false;
getObject.Get();
if (getObject.CommandResult() != Result.Success)
return getObject.CommandResult();
objARef = getObject.Object(0);
}
if (objARef == null)
return Result.Failure;
ObjRef objBRef = null;
using (var getObject = new GetObject())
{
getObject.SetCommandPrompt("Select target object B");
getObject.SubObjectSelect = false;
getObject.Get();
if (getObject.CommandResult() != Result.Success)
return getObject.CommandResult();
objBRef = getObject.Object(0);
}
if (objBRef == null)
return Result.Failure;
var pointA = objARef.Geometry().GetBoundingBox(true).Center;
var pointB = objBRef.Geometry().GetBoundingBox(true).Center;
var relativeVector = pointB - pointA;
RhinoApp.WriteLine($"Relative Coordinates from A to B: X: {relativeVector.X}, Y: {relativeVector.Y}, Z: {relativeVector.Z}");
return Result.Success;
}
Environment
- .NET7
- Windows11
- Rhino8