通用应用BIMBase API实现一个两点画线功能了解BIMBase基本工具。
BIMBase API提供了两种实现两点画线功能的机制。一是通过IBPPointCommand
提供的接口实现,一是在IBPFunctionCommand
中通过类ADS/ARX的拾取点接口实现。
- 在
IBPPointCommand
实现IBPPointCommand
是BIMBase API提供的一种交互机制,IBPPointCommand
类中对于工具启动、左键点击、右键点击、鼠标移动等都提供了虚接口,通过实现这些虚接口即可实现相应的功能,如下代码所示。
[BPExternalCommandAttribute(name = "ExlLayoutLineDemo")]
public class ExLayoutLine : IBPPointCommand
{
GePoint3d m_ptS;
int m_nFlag;
public ExLayoutLine()
{
m_nFlag = 0;
}
private BPGraphics getGraphic(GePoint3d sPoint, GePoint3d ePoint)
{
BPViewport vp = BPApplication.singleton().activeDocument.viewManager.getActivedViewport();
BPModel model = vp.targetModel;
BPGraphics graphic = new BPGraphics(model);
GeSegment3d line = new GeSegment3d(sPoint, ePoint);
graphic.addCurve(IGeCurveBase.createSegment(line));
return graphic;
}
public override bool onLeftClick(BPCommandContext context)
{
if (m_nFlag == 0)
{
m_nFlag = 1;
m_ptS = context.mousePoint;
}
else if (m_nFlag == 1)
{
m_nFlag = 0;
BPGraphics graphic = getGraphic(m_ptS, context.mousePoint);
graphic.save();
}
return true;
}
public override bool onRightClick(BPCommandContext context)
{
callExitTool();
return true;
}
public override void onDynamicDraw(BPCommandContext context)
{
if (m_nFlag == 0) return;
BPViewport vp = BPApplication.singleton().activeDocument.viewManager.getActivedViewport();
BPGraphics graphic = getGraphic(m_ptS, context.mousePoint);
vp.dynamicDraw(graphic);
}
}
代码2-5 在IBPPointCommand实现两点画线
该例子通过重写IBPPointCommand
类中onLeftClick接口实现在第一次左键点击时确认线段的第一个点,第二次左键点击时得到线段的第二个点,并用第一个点和第二个点构造线段并保存到工程;重写onDynamicDraw接口,通过得到的动态点与第一个点构造线段并动态显示,实现线段的动态预览。
- 在
IBPFunctionCommand
实现
通过BIMBase API提供的拾取点接口,可在IBPFunctionCommand中实现两点画线功能。
public class AdsCommand : IBPFunctionCommand
{
public override void onExcute(BPCommandContext context)
{
BPDocument doc = BPApplication.singleton().activeDocument;
BPModel model = doc.modelManager.activeModel;
BPGraphics graphic = new BPGraphics();
GePoint3d ptBase = GePoint3d.createByZero();
GePoint3d pt = GePoint3d.createByZero();
BPAdsFun.GetPoint(null, "Select first point", ref ptBase);
BPAdsFun.GetPoint(ptBase, "Select second point", ref pt);
graphic.addCurve(IGeCurveBase.createSegment(new GeSegment3d(ptBase, pt)));
graphic.save();
base.onExcute(context);
}
}
代码2-6 在IBPFunctionCommand实现两点画线
该例子通过BIMBase API提供的GetPoint接口实现交互,获取线段的端点坐标,构造线段并保存到工程。