OCC旋转,缩放和平移的实现

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.结果显示

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值