文章目錄
- 一、檢測模型
- 二、選擇模型
- 項目倉庫
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);
......
}
程式效果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cswWNXFGMGdkYsBnMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5MzMyEzMxEDMyAjNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
更新:修改高亮風格
通過擷取并修改 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):
- 選擇視圖中由像素最大最小值(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);
- 多段線的選擇,或清除以前選擇的清單。
Standard_EXPORT AIS_StatusOfPick Select (const TColgp_Array1OfPnt2d& thePolyline,
const Handle(V3d_View)& theView,
const Standard_Boolean theToUpdateViewer);
- 通過MoveTo檢測到的對象進行選擇,或清除之前的選擇
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