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 <AIS_ColoredDrawer.hxx>
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_Selection.hxx>
39 #include <BOPTools_AlgoTools3D.hxx>
40 #include <BRep_Builder.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d_PointAspect.hxx>
45 #include <Prs3d_IsoAspect.hxx>
46 #include <Prs3d_ShadingAspect.hxx>
47 #include <SelectMgr_SequenceOfOwner.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <SelectMgr_SelectionManager.hxx>
50 #include <StdPrs_WFShape.hxx>
51 #include <StdPrs_ShadedShape.hxx>
52 #include <StdSelect_BRepSelectionTool.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <TopoDS_Builder.hxx>
58 //*******************************************************************************************
60 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
64 //********************************************************************
65 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
66 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
69 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
70 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
72 Handle(Prs3d_Drawer) aDrawer = Attributes();
73 if (aDrawer->HasOwnPointAspect())
74 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
76 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
78 // Activate individual repaintng if this is a part of compsolid
79 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
80 SetAutoHilight(aCompSolid.get() == NULL);
82 myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
83 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
84 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
85 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
87 ModuleBase_Tools::setPointBallHighlighting(this);
90 //********************************************************************
91 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
93 myAdditionalSelectionPriority = thePriority;
96 //********************************************************************
97 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
99 ViewerData_AISShape::SetColor(theColor);
100 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
103 //********************************************************************
104 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
106 bool isModified = false;
108 TopoDS_Compound aCompound;
109 BRep_Builder aBBuilder;
110 aBBuilder.MakeCompound (aCompound);
111 // restore hidden shapes if there are not the shapes in parameter container
112 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
113 for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
115 if (!theShapes.Contains(aHiddenIt.Value()))
116 aVisibleSubShapes.Append(aHiddenIt.Value());
118 aBBuilder.Add (aCompound, aHiddenIt.Value());
120 isModified = !aVisibleSubShapes.IsEmpty();
121 for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
123 myHiddenSubShapes.Remove(aVisibleIt.Value());
125 // append hidden shapes into internal container if there are not these shapes
126 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
129 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
132 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
133 myHiddenSubShapes.Append(aShapeIt.Value());
134 aBBuilder.Add (aCompound, aShapeIt.Value());
138 myHiddenCompound = aCompound;
142 //********************************************************************
143 bool ModuleBase_ResultPrs::hasSubShapeVisible(
144 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
146 TopoDS_Compound aCompound;
147 BRep_Builder aBuilder;
148 aBuilder.MakeCompound (aCompound);
149 NCollection_List<TopoDS_Shape> aShapesToSkip;
150 aShapesToSkip.Append(myHiddenSubShapes);
151 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
152 aShapesToSkip.Append(anIt.Value());
154 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
155 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
158 //********************************************************************
159 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
161 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
164 myTransparency = theTransparency;
165 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
169 //********************************************************************
170 void ModuleBase_ResultPrs::Compute(
171 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
172 const Handle(Prs3d_Presentation)& thePresentation,
173 const Standard_Integer theMode)
175 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
176 bool aReadyToDisplay = aShapePtr.get();
177 if (aReadyToDisplay) {
178 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
179 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
180 if (!myOriginalShape.IsNull())
181 Set(myOriginalShape);
183 else { // convert shape into SHELL
184 TopoDS_Compound aCompound;
185 BRep_Builder aBuilder;
186 aBuilder.MakeCompound (aCompound);
187 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
188 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
191 aReadyToDisplay = false;
194 // change deviation coefficient to provide more precise circle
195 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
196 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
198 // visualize hidden sub-shapes transparent
199 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
201 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
202 aReadyToDisplay = true;
205 if (!aReadyToDisplay) {
206 Events_InfoMessage("ModuleBase_ResultPrs",
207 "An empty AIS presentation: ModuleBase_ResultPrs").send();
208 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
209 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
213 //********************************************************************
214 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
215 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
216 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
218 switch (theShape.ShapeType()) {
219 case TopAbs_COMPOUND: {
220 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
221 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
226 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
227 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
232 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
233 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
238 if (theHiddenSubShapes.Contains(theShape))
239 return; // remove hidden shape
240 theBuilder.Add(theCompound, theShape);
244 case TopAbs_VERTEX: {
245 theBuilder.Add(theCompound, theShape);
252 //********************************************************************
253 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
254 const Standard_Integer theMode)
256 if (appendVertexSelection(aSelection, theMode))
259 if (theMode > TopAbs_SHAPE) {
260 // In order to avoid using custom selection modes
261 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
262 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
267 // TODO: OCCT issue should be created for the COMPOUND processing
268 // before it is fixed, the next workaround in necessary
269 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
270 const TopoDS_Shape& aShape = Shape();
271 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
272 // do not activate in compound mode shapes which do not contain compounds
273 if (!aCompExp.More())
277 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
278 // Limit selection area only by actual object (Shape)
279 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
280 if (aCompSolid.get()) {
281 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
282 if (aShapePtr.get()) {
283 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
284 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
285 /// It is important to have priority for the shape of comp solid result less than priority
286 /// for the presentation shape which is a sub-result.
287 /// Reason is to select the sub-objects before: #1592
288 aPriority = aPriority - 1;
289 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
291 Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
292 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
293 aDeflection, myDrawer->HLRAngle(), 9, 500);
295 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
296 Handle(SelectMgr_EntityOwner) anOwner =
297 Handle(SelectMgr_EntityOwner)
298 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
304 //AIS_Shape::ComputeSelection(aSelection, 0);
306 AIS_Shape::ComputeSelection(aSelection, theMode);
308 if (myAdditionalSelectionPriority > 0) {
309 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
310 Handle(SelectBasics_EntityOwner) aBasicsOwner =
311 aSelection->Sensitive()->BaseSensitive()->OwnerId();
312 if (!aBasicsOwner.IsNull())
313 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
318 //********************************************************************
319 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
320 const Standard_Integer theMode)
322 if (Shape().ShapeType() == TopAbs_VERTEX) {
323 const TopoDS_Shape& aShape = Shape();
325 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
326 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
328 /// The cause of this method is the last parameter of BRep owner setting into True.
329 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
330 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
331 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
332 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
333 aDeflection, myDrawer->HLRAngle(), 9, 500);
335 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
336 Handle(SelectMgr_EntityOwner) anOwner =
337 Handle(SelectMgr_EntityOwner)
338 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
346 //********************************************************************
347 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
348 const SelectMgr_SequenceOfOwner& theOwners)
350 Handle(SelectMgr_EntityOwner) anOwner;
351 Handle(ModuleBase_BRepOwner) aCompOwner;
352 for (int i = 1; i <= theOwners.Length(); i++) {
353 anOwner = theOwners.Value(i);
354 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
355 if (aCompOwner.IsNull()) {
356 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
359 TopoDS_Shape aShape = aCompOwner->Shape();
360 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
361 aSelectionPrs->Clear();
363 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
365 aSelectionPrs->SetDisplayPriority(9);
366 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
367 aSelectionPrs->Display();
368 thePM->Color(this, GetContext()->SelectionStyle());
373 //********************************************************************
374 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
375 const Handle(Graphic3d_HighlightStyle)& theStyle,
376 const Handle(SelectMgr_EntityOwner)& theOwner)
378 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
382 TopoDS_Shape aShape = aOwner->Shape();
383 if (!aShape.IsNull()) {
384 thePM->Color(this, theStyle);
386 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
387 aHilightPrs->Clear();
389 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
390 aHilightPrs->Highlight(theStyle);
392 if (thePM->IsImmediateModeOn())
393 thePM->AddToImmediateList(aHilightPrs);