1.首先在occview视图类里创建动作枚举类型和一些变量,
protected:
enum CurrentAction{
CurrentAction_Nothing,//
CurrentAction_Panning,//平移,按住鼠标左键和右键并拖动进行平移操作
CurrentAction_Zooming,//缩放,滚动滚轮进行缩放
CurrentAction_Rotation//旋转,按住鼠标中间并拖动进行旋转
};
private:
Standard_Integer m_x;//记录平移坐标X
Standard_Integer m_y;//记录平移坐标Y
CurrentAction m_curremt_mode;//记录三维场景转换模式
2.在cpp文件中重写鼠标事件
void OccView::wheelEvent(QWheelEvent *event)//滚轮事件实现缩放
{
m_view->StartZoomAtPoint(event->pos().x(),event->pos().y());
m_view->ZoomAtPoint(0,0,event->angleDelta().y(),0);
}
void OccView::mousePressEvent(QMouseEvent *event)//鼠标案件事件,左键和右键一起按下设置为平移模式,中间建按下设置为旋转模式
{
if(event->buttons()&Qt::LeftButton&&event->buttons()&Qt::RightButton)//event->buttons()实时更新按钮并判断;如果不加会导致左键之后,不管再按哪个键一直是平移
{
m_curremt_mode=CurrentAction_Panning;
m_x=event->pos().x();
m_y=event->pos().y();
}else if(event->buttons()&Qt::MidButton)
{
m_curremt_mode=CurrentAction_Rotation;
}
}
void OccView::mouseReleaseEvent(QMouseEvent* )//鼠标释放事件,将模式重新初始化
{
m_curremt_mode=CurrentAction_Nothing;
}
void OccView::mouseMoveEvent(QMouseEvent *event)//鼠标移动事件,平移和旋转的具体实现
{
switch (m_curremt_mode) {
case CurrentAction_Panning:
m_view->Pan(event->pos().x()-m_x,event->pos().y()-m_y);//平移
m_x=event->pos().x();//更新x坐标作为下次平移起点
m_y=event->pos().y();//更新y坐标作为下次平移起点
break;
case CurrentAction_Rotation:
m_view->Rotation(event->pos().x(),event->pos().y());//旋转的实现以鼠标点与原点差值作为参数,具体可以查看occ帮助文档
break;
default:
break;
}
m_context->MoveTo(event->pos().x(),event->pos().y(),m_view,Standard_True);
}
3.创建一个Box来验证
需要在occview里写一个接口来获得它的view和context;很简单这里就不写代码了
void MainWindow::on_actionbox_triggered()
{
TopoDS_Shape box=BRepPrimAPI_MakeBox(10.0,10.0,10.0).Shape();
Handle(AIS_Shape) aisBox=new AIS_Shape(box);
occView->GetInteractiveContext()->Display(aisBox,Standard_True);
occView->GetView()->FitAll();
}
4.结果显示