在Rhino实现拾取功能,我们先创建一个Commands;
该Commands名称为"ceshi",命令没有被实现的时候会在Rhino命令行输出“未实现ceshi命令。 ”提示。
using System.Windows.Forms;
using Rhino;
using Rhino.Commands;
using RhinoWindows;
namespace CowForRhinoWinForms.Commands
{
[System.Runtime.InteropServices.Guid("GUID")]
public class CowForRhinoModalFormCommand : Command
{
public override string EnglishName
{
get { return "ceshi"; }
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
var rc = Result.Cancel;
if (mode == RunMode.Interactive)
{
//将在这里写拾取对象功能
}
else
{
var msg = string.Format("未实现 {0} 命令。 ", EnglishName);
RhinoApp.WriteLine(msg);
}
return rc;
}
}
}
拾取对象由Rhino.Input.Custom命名空间下的类来实现对应的拾取操作。例如GetPoint类是拾取Point的,GetLine类是拾取Line的等等。我们用GetLine做一个应用:
GetLine g = new GetLine();
g.SetCommandPrompt("选择一个直线");
g.Get();
if (g.CommandResult() == Result.Success)
{
Line line = g.Line();
// 这里添加更多命令
.....
// 这里添加更多命令
RhinoApp.WriteLine("直线拾取成功!");
}
else
{
RhinoApp.WriteLine("直线拾取不成功!");
}
将上面代码添加到if内,实现拾取一个直线的功能,完成拾取操作后将直线赋给line 然后在命令行输出"直线拾取成功!"提示,没有拾取正确类型的对象或取消操作时会在命令行输出"直线拾取成功!"提示。示例中并没有在拾取line后继续实现其他功能,大家可以根据自己的需求添加相应操作,比如获取line的长度、起始点、终点等信息,将它们输出到rhino命令行。
上面的功能完整代码如下:
using System.Windows.Forms;
using Rhino;
using Rhino.Commands;
using RhinoWindows;
namespace CowForRhinoWinForms.Commands
{
[System.Runtime.InteropServices.Guid("GUID")]
public class CowForRhinoModalFormCommand : Command
{
public override string EnglishName
{
get { return "ceshi"; }
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
var rc = Result.Cancel;
if (mode == RunMode.Interactive)
{
//将在这里写拾取对象功能
GetLine g = new GetLine();
g.SetCommandPrompt("选择一个直线");
g.Get();
if (g.CommandResult() == Result.Success)
{
Line line = g.Line();
// 这里添加更多命令
.....
// 这里添加更多命令
RhinoApp.WriteLine("直线拾取成功!");
}
else
{
RhinoApp.WriteLine("直线拾取不成功!");
}
}
else
{
var msg = string.Format("未实现 {0} 命令。 ", EnglishName);
RhinoApp.WriteLine(msg);
}
return rc;
}
}
}
扫码加入群聊,一起寻找Rhino开发乐趣!