1、通过AIS_InteractiveContext的函数访问当前选择的图形

hAISContext->InitSelected();
hAISContext->MoreSelected();
hAISContext->NextSelected();
hAISContext->SelectedShape();

其中hAISContext->SelectedShape()通过StdSelect_BrepOwner来获取TopoDS_Shape,具体的参看源码即可

OCC笔记:选择TopoDS_Shape顶点、边、面等等_Standard

2、示例

  • 绘制一个box,并激活box的顶点、边、面的选择模式
//create box for test
	BRepPrimAPI_MakeBox mkBox(gp_Pnt(0, 0, 0), 10, 20, 30);
	TopoDS_Shape aShpae = mkBox.Shape();
	Handle(AIS_Shape) hBoxShape = new AIS_Shape(aShpae);
	myAISContext->Display(hBoxShape, Standard_True);

	myAISContext->Deactivate(hBoxShape);
	myAISContext->Activate(hBoxShape, AIS_Shape::SelectionMode(TopAbs_FACE));
	myAISContext->Activate(hBoxShape, AIS_Shape::SelectionMode(TopAbs_EDGE));
	myAISContext->Activate(hBoxShape, AIS_Shape::SelectionMode(TopAbs_VERTEX));
  • 选择的处理
void COCTView::OnBtnTestSelect()
{
	auto hAISContext = GetDocument()->GetAISContext();
	if (!hAISContext)
	{
		return;
	}

	for (hAISContext->InitSelected(); hAISContext->MoreSelected();
		hAISContext->NextSelected())
	{
		TopoDS_Shape aShape = hAISContext->SelectedShape();
		if (aShape.IsNull()) continue;

		switch (aShape.ShapeType())
		{
		case TopAbs_ShapeEnum::TopAbs_VERTEX:
		{
			TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
			AfxMessageBox(_T("选中一个点"));
			break;
		}
		case TopAbs_ShapeEnum::TopAbs_EDGE:
		{
			AfxMessageBox(_T("选中一条边"));
			break;
		}
		case TopAbs_ShapeEnum::TopAbs_FACE:
		{
			AfxMessageBox(_T("选中一个面"));
			break;
		}
		//其他略
		}
	}
}

3、演示

OCC笔记:选择TopoDS_Shape顶点、边、面等等_occ选择_02