From 916cd9d11f7643ffdca1db94ba6d49cf581b2f66 Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 17 Oct 2014 16:16:02 +0400 Subject: [PATCH] Provide selection of external objects in plane of sketcher --- src/ModuleBase/ModuleBase_ViewerFilters.cpp | 46 ++++++++++++++++++++- src/ModuleBase/ModuleBase_ViewerFilters.h | 26 +++++++++++- src/PartSet/PartSet_Module.cpp | 33 ++++++++++++--- src/PartSet/PartSet_Module.h | 7 +++- 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/src/ModuleBase/ModuleBase_ViewerFilters.cpp b/src/ModuleBase/ModuleBase_ViewerFilters.cpp index 6626a1668..6d2209de5 100644 --- a/src/ModuleBase/ModuleBase_ViewerFilters.cpp +++ b/src/ModuleBase/ModuleBase_ViewerFilters.cpp @@ -10,6 +10,14 @@ #include #include +#include + +#include + +#include +#include +#include +#include IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter); IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter); @@ -33,4 +41,40 @@ Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(const Handle(SelectMgr_Ent } } return Standard_False; -} \ No newline at end of file +} + + +IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter); +IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter); + +Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const +{ + if (theOwner->HasSelectable()) { + Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner); + if (!aShapeOwner.IsNull()) { + TopoDS_Shape aShape = aShapeOwner->Shape(); + TopAbs_ShapeEnum aType = aShape.ShapeType(); + switch (aType) { + case TopAbs_VERTEX: + { + gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape)); + return myPlane.Distance(aPnt) < Precision::Confusion(); + } + case TopAbs_EDGE: + { + TopoDS_Edge aEdge = TopoDS::Edge(aShape); + Standard_Real aFirst, aLast; + Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aFirst, aLast); + gp_Pnt aFirstPnt = aCurve->Value(aFirst); + gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.); + gp_Pnt aLastPnt = aCurve->Value(aLast); + bool aD1 = myPlane.Distance(aFirstPnt) < Precision::Confusion(); + bool aD2 = myPlane.Distance(aMidPnt) < Precision::Confusion(); + bool aD3 = myPlane.Distance(aLastPnt) < Precision::Confusion(); + return aD1 && aD2 && aD3; + } + } + } + } + return Standard_False; +} diff --git a/src/ModuleBase/ModuleBase_ViewerFilters.h b/src/ModuleBase/ModuleBase_ViewerFilters.h index 55ad0b686..d26d3db3e 100644 --- a/src/ModuleBase/ModuleBase_ViewerFilters.h +++ b/src/ModuleBase/ModuleBase_ViewerFilters.h @@ -8,12 +8,18 @@ #include #include +#include class ModuleBase_IWorkshop; -DEFINE_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter); +/** +* A filter which provides filtering of selection in 3d viewer. +* Installing of this filter lets to select objects which belong to +* currently active document or to global document +*/ +DEFINE_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter); class ModuleBase_ShapeDocumentFilter: public SelectMgr_Filter { public: @@ -28,4 +34,22 @@ private: ModuleBase_IWorkshop* myWorkshop; }; +/** +* A filter which provides filtering of selection in 3d viewer. +* Installing of this filter lets to select of Vertexes and Edges which belongs to the given plane +*/ +DEFINE_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter); +class ModuleBase_ShapeInPlaneFilter: public SelectMgr_Filter +{ +public: + Standard_EXPORT ModuleBase_ShapeInPlaneFilter(const gp_Pln& thePane): + SelectMgr_Filter(), myPlane(thePane) {} + + Standard_EXPORT virtual Standard_Boolean IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const; + + DEFINE_STANDARD_RTTI(ModuleBase_ShapeInPlaneFilter) +private: + gp_Pln myPlane; +}; + #endif \ No newline at end of file diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index d1b54dcc1..a44256ea7 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include @@ -35,6 +37,7 @@ #include #include +#include #include #include @@ -153,7 +156,7 @@ void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation) PartSet_OperationSketch* aSketchOp = dynamic_cast(aPreviewOp); if (aSketchOp) { if (aSketchOp->isEditOperation()) { - setSketchingMode(); + setSketchingMode(getSketchPlane(aSketchOp->feature())); } else { aDisplayer->openLocalContext(); aDisplayer->activateObjectsOutOfContext(QIntList()); @@ -288,7 +291,10 @@ void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ) { myWorkshop->viewer()->setViewProjection(theX, theY, theZ); xWorkshop()->actionsMgr()->update(); - setSketchingMode(); + // Set working plane + ModuleBase_Operation* anOperation = myWorkshop->currentOperation(); + FeaturePtr aSketch = anOperation->feature(); + setSketchingMode(getSketchPlane(aSketch)); } void PartSet_Module::onFitAllView() @@ -359,7 +365,7 @@ void PartSet_Module::onSetSelection(const QList& theFeatures) aDisplayer->updateViewer(); } -void PartSet_Module::setSketchingMode() +void PartSet_Module::setSketchingMode(const gp_Pln& thePln) { XGUI_Displayer* aDisplayer = xWorkshop()->displayer(); if (!myPlaneFilter.IsNull()) { @@ -367,13 +373,16 @@ void PartSet_Module::setSketchingMode() myPlaneFilter.Nullify(); } QIntList aModes; - //aModes << TopAbs_VERTEX << TopAbs_EDGE; - //aModes << AIS_DSM_Text << AIS_DSM_Line; + // Clear standard selection modes aDisplayer->setSelectionModes(aModes); aDisplayer->openLocalContext(); // Get default selection modes aModes = sketchSelectionModes(ObjectPtr()); aDisplayer->activateObjectsOutOfContext(aModes); + + // Set filter + mySketchFilter = new ModuleBase_ShapeInPlaneFilter(thePln); + aDisplayer->addSelectionFilter(mySketchFilter); } void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode) @@ -587,3 +596,17 @@ QIntList PartSet_Module::sketchSelectionModes(ObjectPtr theFeature) aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE)); return aModes; } + + +gp_Pln PartSet_Module::getSketchPlane(FeaturePtr theSketch) const +{ + DataPtr aData = theSketch->data(); + boost::shared_ptr anOrigin = boost::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Sketch::ORIGIN_ID())); + boost::shared_ptr aNorm = boost::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Sketch::NORM_ID())); + gp_Pnt aOrig(anOrigin->x(), anOrigin->y(), anOrigin->z()); + gp_Dir aDir(aNorm->x(), aNorm->y(), aNorm->z()); + return gp_Pln(aOrig, aDir); +} + diff --git a/src/PartSet/PartSet_Module.h b/src/PartSet/PartSet_Module.h index 88f745776..79379ffc4 100644 --- a/src/PartSet/PartSet_Module.h +++ b/src/PartSet/PartSet_Module.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -115,7 +116,8 @@ Q_OBJECT void onSetSelection(const QList& theFeatures); /// SLOT, Defines Sketch editing mode - void setSketchingMode(); + /// \param thePln - plane of current sketch + void setSketchingMode(const gp_Pln& thePln); /// SLOT, to visualize the feature in another local context mode /// \param theFeature the feature to be put in another local context mode @@ -139,6 +141,8 @@ Q_OBJECT //! Edits the feature void editFeature(FeaturePtr theFeature); + gp_Pln getSketchPlane(FeaturePtr theSketch) const; + private: //XGUI_Workshop* myWorkshop; PartSet_Listener* myListener; @@ -146,6 +150,7 @@ Q_OBJECT std::map myFeaturesInFiles; Handle(StdSelect_FaceFilter) myPlaneFilter; + Handle(ModuleBase_ShapeInPlaneFilter) mySketchFilter; }; #endif -- 2.39.2