From 29139022cc1edba82d909cae50dc0e4d70a24d3a Mon Sep 17 00:00:00 2001 From: apl Date: Wed, 12 Oct 2016 18:59:41 +0300 Subject: [PATCH] Picking point on the shape --- src/MeasureGUI/MeasureGUI_AnnotationDlg.cxx | 138 ++++++++++++++++++-- src/MeasureGUI/MeasureGUI_AnnotationDlg.h | 1 + 2 files changed, 131 insertions(+), 8 deletions(-) mode change 100755 => 100644 src/MeasureGUI/MeasureGUI_AnnotationDlg.h diff --git a/src/MeasureGUI/MeasureGUI_AnnotationDlg.cxx b/src/MeasureGUI/MeasureGUI_AnnotationDlg.cxx index 7a03a65f8..ba25c563e 100644 --- a/src/MeasureGUI/MeasureGUI_AnnotationDlg.cxx +++ b/src/MeasureGUI/MeasureGUI_AnnotationDlg.cxx @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -66,12 +67,24 @@ #include #include #include +#include #include #include +#include #include #include +#include +#include +#include +#include +#include +#include + +#include + +#include #include #include #include @@ -79,6 +92,8 @@ #include #include +#include + #include #include @@ -420,9 +435,12 @@ void MeasureGUI_AnnotationDlg::SelectionIntoArgument() activateSelection(); if ( !myShape->_is_nil() ) { - TopoDS_Shape aShape; - GEOMBase::GetShape( myShape.get(), aShape ); - anAttachPoint = getAttachPoint( aShape ); + if ( !getPickedPoint( anAttachPoint ) ) + { + TopoDS_Shape aShape; + GEOMBase::GetShape( myShape.get(), aShape ); + anAttachPoint = getAttachPoint( aShape ); + } } } else if ( myEditCurrentArgument == mySubShapeName ) { if ( !myShape->_is_nil() ) { @@ -446,7 +464,12 @@ void MeasureGUI_AnnotationDlg::SelectionIntoArgument() aSubShape = aCurrentSubShape; } } - anAttachPoint = getAttachPoint( aSubShape ); + + if ( !getPickedPoint( anAttachPoint ) ) + { + anAttachPoint = getAttachPoint( aSubShape ); + } + myAnnotationProperties.ShapeIndex = aSubShapeIndex; } } @@ -671,14 +694,113 @@ void MeasureGUI_AnnotationDlg::redisplayPreview() } } +//================================================================================= +// function : getPickedPoint +// purpose : finds picked point in active viewer on the selected shape +//================================================================================= +bool MeasureGUI_AnnotationDlg::getPickedPoint( gp_Pnt& thePnt ) +{ + const SUIT_ViewWindow* anActiveView = GEOMBase_Helper::getActiveView(); + if ( !anActiveView ) + return false; + + const OCCViewer_ViewWindow* anOccView = qobject_cast( anActiveView ); + if ( !anOccView ) + return false; + + OCCViewer_ViewManager* aVM = ( OCCViewer_ViewManager* )anOccView->getViewManager(); + OCCViewer_Viewer* aViewer = aVM->getOCCViewer(); + + Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext(); + Handle(SelectMgr_ViewerSelector) aSelector; + if ( anAISContext->HasOpenedContext() ) + aSelector = anAISContext->LocalSelector(); + else + aSelector = anAISContext->MainSelector(); + + if ( aSelector->NbPicked() < 1 ) + return false; + + thePnt = aSelector->PickedPoint( 1 ); + return true; +} + //================================================================================= // function : getAttachPoint -// purpose : finds a point on shape to attach annotation object +// purpose : computes default attachment point on the shape //================================================================================= gp_Pnt MeasureGUI_AnnotationDlg::getAttachPoint( const TopoDS_Shape& theShape ) { - gp_Pnt aPoint = gp_Pnt( 0, 0, 0 ); + TopoDS_Shape aAttachShape; + if ( theShape.ShapeType() == TopAbs_COMPOUND ) + { + QStack< NCollection_Handle > aItStack; + aItStack.push( NCollection_Handle( new TopoDS_Iterator( theShape ) ) ); + while ( aAttachShape.IsNull() && !aItStack.empty() ) + { + NCollection_Handle anIt = aItStack.top(); + if ( !anIt->More() ) + { + aItStack.pop(); + } + else + { + const TopoDS_Shape& aShape = anIt->Value(); + if ( aShape.ShapeType() != TopAbs_COMPSOLID ) + { + aAttachShape = aShape; + } + else + { + aItStack.push( NCollection_Handle( new TopoDS_Iterator( aShape ) ) ); + } + } + } + } + else + { + aAttachShape = theShape; + } - return aPoint; -} + if ( aAttachShape.ShapeType() == TopAbs_COMPSOLID + || aAttachShape.ShapeType() == TopAbs_SOLID + || aAttachShape.ShapeType() == TopAbs_SHELL ) + { + Bnd_Box aBox; + BRepBndLib::Add( aAttachShape, aBox ); + const gp_Pnt aMin = aBox.CornerMin(); + const gp_Pnt aMax = aBox.CornerMax(); + return gp_Pnt( aMin.X() + aMax.X() / 2.0, + aMin.Y() + aMax.Y() / 2.0, + aMin.Z() + aMax.Z() / 2.0 ); + } + else if ( aAttachShape.ShapeType() == TopAbs_FACE ) + { + BRepAdaptor_Surface aFace( TopoDS::Face( aAttachShape ) ); + const Standard_Real aU1 = aFace.FirstUParameter(); + const Standard_Real aU2 = aFace.LastUParameter(); + const Standard_Real aV1 = aFace.FirstVParameter(); + const Standard_Real aV2 = aFace.LastVParameter(); + return aFace.Value( ( aU1 + aU2 ) / 2.0, ( aV1 + aV2 ) / 2.0 ); + } + else if ( aAttachShape.ShapeType() == TopAbs_WIRE ) + { + BRepAdaptor_CompCurve aWire( TopoDS::Wire( aAttachShape ) ); + const Standard_Real aP1 = aWire.FirstParameter(); + const Standard_Real aP2 = aWire.LastParameter(); + return aWire.Value( ( aP1 + aP2 ) / 2.0 ); + } + else if ( aAttachShape.ShapeType() == TopAbs_EDGE ) + { + BRepAdaptor_Curve aEdge( TopoDS::Edge( aAttachShape ) ); + const Standard_Real aP1 = aEdge.FirstParameter(); + const Standard_Real aP2 = aEdge.LastParameter(); + return aEdge.Value( ( aP1 + aP2 ) / 2.0 ); + } + else if ( aAttachShape.ShapeType() == TopAbs_VERTEX ) + { + return BRep_Tool::Pnt( TopoDS::Vertex( aAttachShape ) ); + } + return gp_Pnt( 0.0, 0.0, 0.0 ); +} diff --git a/src/MeasureGUI/MeasureGUI_AnnotationDlg.h b/src/MeasureGUI/MeasureGUI_AnnotationDlg.h old mode 100755 new mode 100644 index 91e567f48..c07d3ed02 --- a/src/MeasureGUI/MeasureGUI_AnnotationDlg.h +++ b/src/MeasureGUI/MeasureGUI_AnnotationDlg.h @@ -89,6 +89,7 @@ private: void activateSelectionArgument( QPushButton* theSelectionButton ); void activateSelection(); TopAbs_ShapeEnum getShapeType() const; + bool getPickedPoint(gp_Pnt& thePnt); gp_Pnt getAttachPoint( const TopoDS_Shape& theShape ); private: -- 2.39.2