在 Open Cascade 中如果想對某個模型進行調整,如移動、旋轉、縮放,可以使用自帶的模型操縱器(AIS_Manipulator)組件。
代碼示例
第一步:創(chuàng)建模型操縱器
// 先創(chuàng)建一個長方體
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);
// 再創(chuàng)建一個模型操縱器
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 軸的旋轉
// 將操縱器附在創(chuàng)建的長方體上
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();
此時運行,可以看到模型操縱器已經(jīng)顯示在界面中了。但是還無法響應操作。

模型操縱器效果
第二步:事件綁定
// 在 mouse press 事件中加入如下代碼:
if (aManipulator->HasActiveMode())
{
aManipulator->StartTransform(anXPix, anYPix, aV3dView); // 初始化轉換,記錄起始位置
}
// ...
// 在 mouse move 事件中加入如下代碼:
if (aManipulator->HasActiveMode())
{
aManipulator->Transform(anXPix, anYPix, aV3dView); // 應用鼠標從起始位置開始移動而產(chǎn)生的變換
aV3dView->Redraw();
}
// ...
// 在 mouse release 事件中加入如下代碼:
aManipulator->StopTransform(Standard_True); // 重置起始變換參數(shù)(函數(shù)參數(shù)為 Standard_False 則撤銷本次的變換)
第三步:停止模型操縱器
aManipulator->DeactivateCurrentMode();
第四步:將模型操縱器與模型分離
aManipulator->Detach();
效果演示

效果演示