基于OpenCASCADE自制三维建模软件(九)选择物体

 

OpenCASECADE提供模型选择的方法,在AIS_InteractiveContext中,MoveTo方法让鼠标检测到的模型高亮,Select方法实现选择模型,ShiftSelect方法实现多个模型的选择。

一、检测模型

MoveTo方法的定义(AIS_InteractiveContext.hxx):

  Standard_EXPORT AIS_StatusOfDetection MoveTo (const Standard_Integer  theXPix,
                                                const Standard_Integer  theYPix,
                                                const Handle(V3d_View)& theView,
                                                const Standard_Boolean  theToRedrawOnUpdate);

MoveTo方法将鼠标位置(theXPix,theYPix以像素为单位)传递给AIS_InteractiveContex 。这是由视图theView完成的,它将这个位置传递给主查看器并更新它,最后使鼠标检测到的模型实现高亮。

在程序中mouseMoveEvent事件加入MoveTo方法,实现鼠标移动同时检测模型,并且让模型高亮:

void C3DWidget::mouseMoveEvent(QMouseEvent *event)
{
    // 鼠标移动到模型时,模型高亮显示
    m_context->MoveTo(event->x(),event->y(),m_view,true);
	......
}

程序效果:
在这里插入图片描述
更新:修改高亮风格
通过获取并修改 AIS_InteractiveContext 对象的 SelectionStyle ,可以配置检测到模型时的高亮风格

        // 设置模型高亮的风格
        Handle(Prs3d_Drawer) t_hilight_style = m_context->HighlightStyle(); // 获取高亮风格
        t_hilight_style->SetMethod(Aspect_TOHM_COLOR);  // 颜色显示方式
        t_hilight_style->SetColor(Quantity_NOC_LIGHTYELLOW);    // 设置高亮颜色
        t_hilight_style->SetDisplayMode(1); // 整体高亮
        t_hilight_style->SetTransparency(0.2f); // 设置透明度

鼠标移动到模型的位置,模型高亮显示淡黄色:
在这里插入图片描述

二、选择模型

Select方法有三种形式(AIS_InteractiveContext.hxx):

  1. 选择视图中由像素最大最小值(XPMin、YPMin、XPMax和YPMax)定义的边框中找到的所选模型。检测到的对象被传递给主查看器,然后更新主查看器。
  Standard_EXPORT AIS_StatusOfPick Select (const Standard_Integer  theXPMin,
                                           const Standard_Integer  theYPMin,
                                           const Standard_Integer  theXPMax,
                                           const Standard_Integer  theYPMax,
                                           const Handle(V3d_View)& theView,
                                           const Standard_Boolean  theToUpdateViewer);
  1. 多段线的选择,或清除以前选择的列表。
  Standard_EXPORT AIS_StatusOfPick Select (const TColgp_Array1OfPnt2d& thePolyline,
                                           const Handle(V3d_View)&     theView,
                                           const Standard_Boolean      theToUpdateViewer);
  1. 通过MoveTo检测到的对象进行选择,或清除之前的选择
  Standard_EXPORT AIS_StatusOfPick Select (const Standard_Boolean theToUpdateViewer);

ShiftSelect方法将最后检测到的添加到先前选择的列表中,如果最后检测到的模型已被选择,则将取消该模型的选择。类似地ShiftSelect方法也有三种形式(AIS_InteractiveContext.hxx):

  Standard_EXPORT AIS_StatusOfPick ShiftSelect (const Standard_Integer  theXPMin,
                                                const Standard_Integer  theYPMin,
                                                const Standard_Integer  theXPMax,
                                                const Standard_Integer  theYPMax,
                                                const Handle(V3d_View)& theView,
                                                const Standard_Boolean  theToUpdateViewer);
                                                
  Standard_EXPORT void FitSelected (const Handle(V3d_View)& theView,
                                    const Standard_Real     theMargin,
                                    const Standard_Boolean  theToUpdate);
                                    
  Standard_EXPORT void FitSelected (const Handle(V3d_View)& theView);

在程序中mousePressEvent事件加入Select和ShiftSelect方法,实现鼠标对模型选择或取消选择:

void C3DWidget::mousePressEvent(QMouseEvent *event)
{
	......
    else if(event->buttons() & Qt::LeftButton)  //鼠标左键选择模型
    {
        // 按下Shift键点击鼠标左键实现多选
        if(qApp->keyboardModifiers() == Qt::ShiftModifier)
        {
            m_context->ShiftSelect(true);
        }
        else
        {
            m_context->Select(true);    // 单选模型
        }
    }
    ......
}

程序效果:
在这里插入图片描述
更新:修改选择风格
通过获取并修改 AIS_InteractiveContext 对象的 SelectionStyle,可以配置模型选择后的显示风格

        // 设置选择模型的风格
        Handle(Prs3d_Drawer) t_select_style = m_context->SelectionStyle();  // 获取选择风格
        t_select_style->SetMethod(Aspect_TOHM_COLOR);  // 颜色显示方式
        t_select_style->SetColor(Quantity_NOC_LIGHTSEAGREEN);   // 设置选择后颜色
        t_select_style->SetDisplayMode(1); // 整体高亮
        t_select_style->SetTransparency(0.4f); // 设置透明度

模型被点击选择后,显示浅海绿色,并有一定的透明度
在这里插入图片描述

项目仓库

https://github.com/Jelatine/JellyCAD

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容