1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_ResultPrs.h"
23 #include <GeomAPI_PlanarEdges.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultCompSolid.h>
30 #include "ModuleBase_Tools.h"
31 #include "ModuleBase_BRepOwner.h"
33 #include <Events_InfoMessage.h>
34 #include <Events_Loop.h>
36 #include <BRep_Builder.hxx>
37 #include <Prs3d_Drawer.hxx>
39 #include <Prs3d_PointAspect.hxx>
40 #include <Prs3d_IsoAspect.hxx>
41 #include <TopoDS_Builder.hxx>
43 #include <SelectMgr_SequenceOfOwner.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <SelectMgr_SelectionManager.hxx>
46 #include <StdPrs_WFShape.hxx>
47 #include <StdSelect_BRepSelectionTool.hxx>
48 #include <AIS_InteractiveContext.hxx>
49 #include <AIS_Selection.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <Graphic3d_AspectMarker3d.hxx>
52 #include <TopExp_Explorer.hxx>
54 //*******************************************************************************************
56 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
60 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
61 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0)
63 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
64 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
66 Handle(Prs3d_Drawer) aDrawer = Attributes();
67 if (aDrawer->HasOwnPointAspect())
68 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
70 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
72 // Activate individual repaintng if this is a part of compsolid
73 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
74 SetAutoHilight(aCompSolid.get() == NULL);
76 ModuleBase_Tools::setPointBallHighlighting(this);
79 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
81 myAdditionalSelectionPriority = thePriority;
84 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
86 bool isModified = false;
88 // restore hidden shapes if there are not the shapes in parameter container
89 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
90 for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
92 if (!theShapes.Contains(aHiddenIt.Value()))
93 aVisibleSubShapes.Append(aHiddenIt.Value());
95 isModified = !aVisibleSubShapes.IsEmpty();
96 for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
98 myHiddenSubShapes.Remove(aVisibleIt.Value());
100 // append hidden shapes into internal container if there are not these shapes
101 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
104 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
107 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
108 myHiddenSubShapes.Append(aShapeIt.Value());
115 bool ModuleBase_ResultPrs::hasSubShapeVisible(const TopoDS_Shape& theShape)
117 int aNbOfHiddenSubShapes = myHiddenSubShapes.Size();
119 if (!myHiddenSubShapes.Contains(theShape))
120 aNbOfHiddenSubShapes++; // the shape to be hidden later
122 TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE);
123 bool aHasVisibleShape = false;
124 for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More() && !aHasVisibleShape;
127 aNbOfHiddenSubShapes--;
128 if (aNbOfHiddenSubShapes < 0)
129 aHasVisibleShape = true;
131 return aHasVisibleShape;
134 void ModuleBase_ResultPrs::Compute(
135 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
136 const Handle(Prs3d_Presentation)& thePresentation,
137 const Standard_Integer theMode)
139 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
140 bool aReadyToDisplay = aShapePtr.get();
141 if (aReadyToDisplay) {
142 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
143 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
144 if (!myOriginalShape.IsNull())
145 Set(myOriginalShape);
147 else { // convert shape into SHELL
149 BRep_Builder aShellBuilder;
150 aShellBuilder.MakeShell(aShell);
151 bool isEmptyShape = true;
152 for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
153 if (myHiddenSubShapes.Contains(anExp.Current()))
155 aShellBuilder.Add(aShell, anExp.Current());
156 isEmptyShape = false;
160 aReadyToDisplay = false;
163 // change deviation coefficient to provide more precise circle
164 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
165 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
167 if (!aReadyToDisplay) {
168 Events_InfoMessage("ModuleBase_ResultPrs",
169 "An empty AIS presentation: ModuleBase_ResultPrs").send();
170 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
171 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
175 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176 const Standard_Integer theMode)
178 if (appendVertexSelection(aSelection, theMode))
181 if (theMode > TopAbs_SHAPE) {
182 // In order to avoid using custom selection modes
183 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
184 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
189 // TODO: OCCT issue should be created for the COMPOUND processing
190 // before it is fixed, the next workaround in necessary
191 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
192 const TopoDS_Shape& aShape = Shape();
193 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
194 // do not activate in compound mode shapes which do not contain compounds
195 if (!aCompExp.More())
199 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
200 // Limit selection area only by actual object (Shape)
201 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
202 if (aCompSolid.get()) {
203 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
204 if (aShapePtr.get()) {
205 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
206 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
207 /// It is important to have priority for the shape of comp solid result less than priority
208 /// for the presentation shape which is a sub-result.
209 /// Reason is to select the sub-objects before: #1592
210 aPriority = aPriority - 1;
211 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
213 Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
214 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
215 aDeflection, myDrawer->HLRAngle(), 9, 500);
217 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
218 Handle(SelectMgr_EntityOwner) anOwner =
219 Handle(SelectMgr_EntityOwner)
220 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
226 //AIS_Shape::ComputeSelection(aSelection, 0);
228 AIS_Shape::ComputeSelection(aSelection, theMode);
230 if (myAdditionalSelectionPriority > 0) {
231 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
232 Handle(SelectBasics_EntityOwner) aBasicsOwner =
233 aSelection->Sensitive()->BaseSensitive()->OwnerId();
234 if (!aBasicsOwner.IsNull())
235 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
240 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
241 const Standard_Integer theMode)
243 if (Shape().ShapeType() == TopAbs_VERTEX) {
244 const TopoDS_Shape& aShape = Shape();
246 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
247 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
249 /// The cause of this method is the last parameter of BRep owner setting into True.
250 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
251 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
252 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
253 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
254 aDeflection, myDrawer->HLRAngle(), 9, 500);
256 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
257 Handle(SelectMgr_EntityOwner) anOwner =
258 Handle(SelectMgr_EntityOwner)
259 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
267 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
268 const SelectMgr_SequenceOfOwner& theOwners)
270 Handle(SelectMgr_EntityOwner) anOwner;
271 Handle(ModuleBase_BRepOwner) aCompOwner;
272 for (int i = 1; i <= theOwners.Length(); i++) {
273 anOwner = theOwners.Value(i);
274 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
275 if (aCompOwner.IsNull()) {
276 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
279 TopoDS_Shape aShape = aCompOwner->Shape();
280 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
281 aSelectionPrs->Clear();
283 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
285 aSelectionPrs->SetDisplayPriority(9);
286 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
287 aSelectionPrs->Display();
288 thePM->Color(this, GetContext()->SelectionStyle());
293 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
294 const Handle(Graphic3d_HighlightStyle)& theStyle,
295 const Handle(SelectMgr_EntityOwner)& theOwner)
297 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
301 TopoDS_Shape aShape = aOwner->Shape();
302 if (!aShape.IsNull()) {
303 thePM->Color(this, theStyle);
305 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
306 aHilightPrs->Clear();
308 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
309 aHilightPrs->Highlight(theStyle);
311 if (thePM->IsImmediateModeOn())
312 thePM->AddToImmediateList(aHilightPrs);