opencascade AIS_PointCloudOwner源码学习 原创

AIS_PointCloudOwner

前言

在这里插入图片描述
为了在 OpenCascade 中实现自定义的选中点高亮功能,可以创建一个自定义的 Entity Owner 类,用于处理点的选择和高亮显示。

方法

1

主构造函数
Standard_EXPORT AIS_PointCloudOwner(const Handle(AIS_PointCloud)& theOrigin);

2

析构函数
Standard_EXPORT virtual ~AIS_PointCloudOwner();

3

返回选中的点
注意!索引从0开始(相对于Graphic3d_ArrayOfPoints::Vertice()偏移-1)
const Handle(TColStd_HPackedMapOfInteger)& SelectedPoints() const { return mySelPoints; }

4

返回最后检测到的点
注意!索引从0开始(相对于Graphic3d_ArrayOfPoints::Vertice()偏移-1)
const Handle(TColStd_HPackedMapOfInteger)& DetectedPoints() const { return myDetPoints; }

5

始终更新动态高亮显示
Standard_EXPORT virtual Standard_Boolean IsForcedHilight() const Standard_OVERRIDE;

6

处理动态高亮显示
Standard_EXPORT virtual void HilightWithColor(const Handle(PrsMgr_PresentationManager)& thePrsMgr,
const Handle(Prs3d_Drawer)& theStyle,
const Standard_Integer theMode) Standard_OVERRIDE;

7

移除高亮显示
Standard_EXPORT virtual void Unhilight(const Handle(PrsMgr_PresentationManager)& thePrsMgr, const Standard_Integer theMode) Standard_OVERRIDE;

8

清除呈现
Standard_EXPORT virtual void Clear(const Handle(PrsMgr_PresentationManager)& thePrsMgr, const Standard_Integer theMode) Standard_OVERRIDE;

protected:
Handle(TColStd_HPackedMapOfInteger) myDetPoints; //!< 最后检测到的点
Handle(TColStd_HPackedMapOfInteger) mySelPoints; //!< 选中的点
};

示例

下面是一个示例,展示如何创建自定义的 SelectMgr_EntityOwner 子类以及如何使用它来实现选中点的高亮显示。

1. 创建自定义的 Entity Owner 类

首先,创建一个自定义的 Entity Owner 类,继承自 SelectMgr_EntityOwner

#include <SelectMgr_EntityOwner.hxx>
#include <AIS_PointCloud.hxx>
#include <Prs3d_Presentation.hxx>
#include <PrsMgr_PresentationManager.hxx>
#include <Graphic3d_ArrayOfPoints.hxx>
#include <V3d_View.hxx>

class CustomPointOwner : public SelectMgr_EntityOwner
{
public:
  CustomPointOwner(const Handle(AIS_PointCloud)& thePointCloud, const Standard_Integer theIndex)
    : SelectMgr_EntityOwner(thePointCloud),
      myPointCloud(thePointCloud),
      myIndex(theIndex)
  {
  }

  virtual void HilightWithColor(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
                                const Handle(Prs3d_Drawer)& theStyle,
                                const Handle(SelectMgr_Selection)& theSelection) override
  {
    // Create a new point array for highlighting
    Handle(Graphic3d_ArrayOfPoints) aHilightPoints = new Graphic3d_ArrayOfPoints(1);
    aHilightPoints->AddVertex(myPointCloud->GetPoints()->Vertice(myIndex));

    // Create a new presentation for highlighting
    Handle(Prs3d_Presentation) aPrs = new Prs3d_Presentation(thePrsMgr->StructureManager());
    aPrs->SetDisplayPriority(5); // Set high priority for highlighting
    aPrs->AddPrimitiveArray(aHilightPoints);

    // Apply the highlight color
    theStyle->SetColor(Quantity_NOC_YELLOW); // Use yellow color for highlighting
    theStyle->ShadingAspect()->Aspect()->SetColor(Quantity_NOC_YELLOW);
    theStyle->ShadingAspect()->Aspect()->SetTypeOfMarker(Aspect_TOM_STAR);

    // Display the highlight presentation
    thePrsMgr->SetPresentation(aPrs, theStyle);
  }

private:
  Handle(AIS_PointCloud) myPointCloud;
  Standard_Integer myIndex;
};
2. 使用自定义的 Entity Owner

然后,在创建和显示点云对象时,使用自定义的 CustomPointOwner 来处理选中点的高亮显示:

#include <AIS_PointCloud.hxx>
#include <Graphic3d_ArrayOfPoints.hxx>
#include <V3d_View.hxx>
#include <AIS_InteractiveContext.hxx>
#include <Quantity_Color.hxx>
#include <Prs3d_Drawer.hxx>
#include <SelectMgr_Selection.hxx>

void CreateAndDisplayPointCloud(const Handle(AIS_InteractiveContext)& aContext)
{
  // 创建点云对象
  Handle(AIS_PointCloud) aPointCloud = new AIS_PointCloud();

  // 创建点数组
  Handle(Graphic3d_ArrayOfPoints) aPoints = new Graphic3d_ArrayOfPoints(3);
  aPoints->AddVertex(gp_Pnt(0.0, 0.0, 0.0));
  aPoints->AddVertex(gp_Pnt(10.0, 0.0, 0.0));
  aPoints->AddVertex(gp_Pnt(0.0, 10.0, 0.0));

  // 设置点云对象的点
  aPointCloud->SetPoints(aPoints);

  // 设置点云对象的颜色
  aPointCloud->SetColor(Quantity_NOC_RED);

  // 将点云对象添加到上下文中
  aContext->Display(aPointCloud, Standard_True);

  // 为每个点创建自定义的 Entity Owner
  for (Standard_Integer i = 1; i <= aPoints->VertexNumber(); ++i)
  {
    Handle(CustomPointOwner) aPointOwner = new CustomPointOwner(aPointCloud, i);
    aContext->AddSelect(aPointOwner);
  }
}

int main()
{
  // 初始化OpenCascade的Viewer和Context
  Handle(Aspect_DisplayConnection) aDisplayConnection = new Aspect_DisplayConnection();
  Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);
  Handle(V3d_Viewer) aViewer = new V3d_Viewer(aGraphicDriver);
  Handle(V3d_View) aView = aViewer->CreateView();
  Handle(AIS_InteractiveContext) aContext = new AIS_InteractiveContext(aViewer);

  // 创建和显示点云
  CreateAndDisplayPointCloud(aContext);

  // 设置视图参数并开始交互
  aView->SetBackgroundColor(Quantity_NOC_BLACK);
  aView->MustBeResized();
  aView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.1, V3d_ZBUFFER);

  // 在此添加其他交互逻辑或视图更新逻辑
  aView->Redraw();

  return 0;
}

这个示例展示了如何在 OpenCascade 中使用自定义的 Entity Owner 类来高亮显示选中的点。通过继承 SelectMgr_EntityOwner 并重载 HilightWithColor 方法,我们可以自定义高亮显示的行为,使其只高亮显示选中的点。

posted @ 2024-07-29 09:15  一点灯  阅读(0)  评论(0编辑  收藏  举报  来源