在 Open Cascade 中如果想对某个模型进行调整,如移动、旋转、缩放,可以使用自带的模型操纵器(AIS_Manipulator)组件。
代码示例
第一步:创建模型操纵器
// 先创建一个长方体
gp_Ax2 boxPos;
boxPos.SetLocation(gp_Pnt(10.0, 10.0, 10.0));
TopoDS_Shape boxShape = BRepPrimAPI_MakeBox(boxPos, 15, 20, 30).Shape();
Handle(AIS_Shape) aisBoxShape = new AIS_Shape(boxShape);
aisBoxShape->SetColor(Quantity_NOC_ORANGE2);
m_context->Display(aisBoxShape, Standard_False);
// 再创建一个模型操纵器
Handle(AIS_Manipulator) aManipulator = new AIS_Manipulator();
// 可以用 SetPart 禁用或启用某些轴的平移、旋转或缩放的可视部分
aManipulator->SetPart(0, AIS_ManipulatorMode::AIS_MM_Scaling, Standard_False); // 禁用了 X 轴的缩放
aManipulator->SetPart(1, AIS_ManipulatorMode::AIS_MM_Rotation, Standard_False); // 禁用了 Y 轴的旋转
// 将操纵器附在创建的长方体上
aManipulator->Attach(aisBoxShape);
// 启用指定的操纵模式
aManipulator->EnableMode(AIS_ManipulatorMode::AIS_MM_Translation); // 启用移动
aManipulator->EnableMode(AIS_ManipulatorMode::AIS_MM_Rotation); // 启用旋转
aManipulator->EnableMode(AIS_ManipulatorMode::AIS_MM_Scaling); // 启用缩放
// 激活操纵器
aManipulator->SetModeActivationOnDetection(Standard_True);
m_context->UpdateCurrentViewer();
此时运行,可以看到模型操纵器已经显示在界面中了。但是还无法响应操作。
第二步:事件绑定
// 在 mouse press 事件中加入如下代码:
if (aManipulator->HasActiveMode())
{
aManipulator->StartTransform(anXPix, anYPix, aV3dView); // 初始化转换,记录起始位置
}
// ...
// 在 mouse move 事件中加入如下代码:
if (aManipulator->HasActiveMode())
{
aManipulator->Transform(anXPix, anYPix, aV3dView); // 应用鼠标从起始位置开始移动而产生的变换
aV3dView->Redraw();
}
// ...
// 在 mouse release 事件中加入如下代码:
aManipulator->StopTransform(Standard_True); // 重置起始变换参数(函数参数为 Standard_False 则撤销本次的变换)
第三步:停止模型操纵器
aManipulator->DeactivateCurrentMode();
第四步:将模型操纵器与模型分离
aManipulator->Detach();