Question Author Mon, 02/28/2022 - 10:36 Hi everyone,
I want to have vertex, edge, face or full body selection on AIS_Shape according to selection mode. Actually, if the vertex that I want to go from is selected, I need to obtain the coordinates of the vertex. If edge is selected I need to calculate the length. Surface area even if face is selected...
I can highlight with my command.
myViewerWidget->getContext()->Activate(TopAbs_FACE, Standard_True);
But when the shape is clicked it returns me all of its vertices or surfaces.
My code in the mouse click event is as follows;
if (theEvent->button() == Qt::LeftButton) {
qDebug() << "Left click pressed.";
if(!myContext->DetectedOwner().IsNull()){
Handle(AIS_InteractiveObject) picked;
myContext->InitSelected();
picked = myContext->DetectedInteractive();
Handle(AIS_Shape) aShape=Handle(AIS_Shape)::DownCast(picked);
TopoDS_Shape topShape = aShape->Shape();
// Vertex
for(TopExp_Explorer vertEx(topShape, TopAbs_VERTEX); vertEx.More(); vertEx.Next()) {
TopoDS_Vertex aVertex = TopoDS::Vertex(vertEx.Current());
gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
qDebug() << "Vertex: " << aPnt.X() << " " << aPnt.Y() << " " << aPnt.Z();
}
// Face
for(TopExp_Explorer vertEx(topShape, TopAbs_FACE); vertEx.More(); vertEx.Next()) {
TopoDS_Face aVertex = TopoDS::Face(vertEx.Current());
GProp_GProps System;
BRepGProp::SurfaceProperties(aVertex, System);
Standard_Real Area = System.Mass();
qDebug() << "Area: " << Area;
}
}
}
How can I have only one corner or edge of any AIS_Shape that I want? What am I missing?
Thanks, Author.
01 Commenter-1 Mon, 02/28/2022 - 11:26 Your code retrieves the shape from AIS_Shape, which would be entire shape of this presentation. To get picking results you should work with Owner objects instead.
myContext->InitSelected();
Handle(SelectMgr_EntityOwner) aSelOwner = myContext->SelectedOwner();
Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast (aSelOwner);
if (!aBRepOwner.IsNull())
{
TopoDS_Shape aSubShape = aBRepOwner->Shape();
}02 Author Mon, 02/28/2022 - 13:16 Thanks for your answer Kirill. I know I'm tiring you but when I do as you say I can't catch any TopoDS_Shape.
My code:
Handle(SelectMgr_EntityOwner) aSelOwner = myContext->SelectedOwner();
Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast (aSelOwner);
if (!aBRepOwner.IsNull())
{
qDebug() << "Shape is live.";
TopoDS_Shape aSubShape = aBRepOwner->Shape();
for (TopExp_Explorer wExpl(aSubShape, TopAbs_VERTEX); wExpl.More(); wExpl.Next()) {
const TopoDS_Vertex &curVertex = TopoDS::Vertex(wExpl.Current());
const gp_Pnt curVertexPos = BRep_Tool::Pnt(curVertex);
qDebug() << "Vertex: " << curVertexPos.X() << " " << curVertexPos.Y() << " " << curVertexPos.Z();
}
}
This is how I set the viewer content. Could there be an issue with that?
// Make Box
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(3.0, 4.0, 5.0).Shape();
Handle(AIS_Shape) anAisBox = new AIS_Shape(aTopoBox);
anAisBox->SetColor(Quantity_NOC_AZURE);
myViewerWidget->getContext()->Display(anAisBox, Standard_True);
myViewerWidget->getContext()->Deactivate();
const int mode = AIS_Shape::SelectionMode(TopAbs_VERTEX);
myViewerWidget->getContext()->Activate(anAisBox, mode);
myViewerWidget->fitAll();
// Make Box