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::isSubShapeHidden(const TopoDS_Shape& theShape)
145 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
148 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
149 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
152 if (theShape.IsSame(aShapeIt.Value()))
159 //********************************************************************
160 bool ModuleBase_ResultPrs::hasSubShapeVisible(
161 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
163 TopoDS_Compound aCompound;
164 BRep_Builder aBuilder;
165 aBuilder.MakeCompound (aCompound);
166 NCollection_List<TopoDS_Shape> aShapesToSkip;
167 aShapesToSkip.Append(myHiddenSubShapes);
168 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
169 aShapesToSkip.Append(anIt.Value());
171 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
172 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
175 //********************************************************************
176 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
178 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
181 myTransparency = theTransparency;
182 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
186 //********************************************************************
187 void ModuleBase_ResultPrs::Compute(
188 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
189 const Handle(Prs3d_Presentation)& thePresentation,
190 const Standard_Integer theMode)
192 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
193 bool aReadyToDisplay = aShapePtr.get();
194 if (aReadyToDisplay) {
195 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
196 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
197 if (!myOriginalShape.IsNull())
198 Set(myOriginalShape);
200 else { // convert shape into SHELL
201 TopoDS_Compound aCompound;
202 BRep_Builder aBuilder;
203 aBuilder.MakeCompound (aCompound);
204 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
205 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
208 aReadyToDisplay = false;
211 // change deviation coefficient to provide more precise circle
212 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
213 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
215 // visualize hidden sub-shapes transparent
216 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
218 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
219 aReadyToDisplay = true;
222 if (!aReadyToDisplay) {
223 Events_InfoMessage("ModuleBase_ResultPrs",
224 "An empty AIS presentation: ModuleBase_ResultPrs").send();
225 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
226 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
230 //********************************************************************
231 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
232 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
233 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
235 switch (theShape.ShapeType()) {
236 case TopAbs_COMPOUND: {
237 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
238 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
243 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
244 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
249 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
250 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
255 if (theHiddenSubShapes.Contains(theShape))
256 return; // remove hidden shape
257 theBuilder.Add(theCompound, theShape);
261 case TopAbs_VERTEX: {
262 theBuilder.Add(theCompound, theShape);
269 //********************************************************************
270 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
271 const Standard_Integer theMode)
273 if (appendVertexSelection(aSelection, theMode))
276 if (theMode > TopAbs_SHAPE) {
277 // In order to avoid using custom selection modes
278 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
279 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
284 // TODO: OCCT issue should be created for the COMPOUND processing
285 // before it is fixed, the next workaround in necessary
286 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
287 const TopoDS_Shape& aShape = Shape();
288 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
289 // do not activate in compound mode shapes which do not contain compounds
290 if (!aCompExp.More())
294 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
295 // Limit selection area only by actual object (Shape)
296 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
297 if (aCompSolid.get()) {
298 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
299 if (aShapePtr.get()) {
300 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
301 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
302 /// It is important to have priority for the shape of comp solid result less than priority
303 /// for the presentation shape which is a sub-result.
304 /// Reason is to select the sub-objects before: #1592
305 aPriority = aPriority - 1;
306 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
308 Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
309 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
310 aDeflection, myDrawer->HLRAngle(), 9, 500);
312 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
313 Handle(SelectMgr_EntityOwner) anOwner =
314 Handle(SelectMgr_EntityOwner)
315 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
321 //AIS_Shape::ComputeSelection(aSelection, 0);
323 AIS_Shape::ComputeSelection(aSelection, theMode);
325 if (myAdditionalSelectionPriority > 0) {
326 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
327 Handle(SelectBasics_EntityOwner) aBasicsOwner =
328 aSelection->Sensitive()->BaseSensitive()->OwnerId();
329 if (!aBasicsOwner.IsNull())
330 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
335 //********************************************************************
336 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
337 const Standard_Integer theMode)
339 if (Shape().ShapeType() == TopAbs_VERTEX) {
340 const TopoDS_Shape& aShape = Shape();
342 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
343 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
345 /// The cause of this method is the last parameter of BRep owner setting into True.
346 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
347 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
348 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
349 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
350 aDeflection, myDrawer->HLRAngle(), 9, 500);
352 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
353 Handle(SelectMgr_EntityOwner) anOwner =
354 Handle(SelectMgr_EntityOwner)
355 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
363 //********************************************************************
364 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
365 const SelectMgr_SequenceOfOwner& theOwners)
367 Handle(SelectMgr_EntityOwner) anOwner;
368 Handle(ModuleBase_BRepOwner) aCompOwner;
369 for (int i = 1; i <= theOwners.Length(); i++) {
370 anOwner = theOwners.Value(i);
371 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
372 if (aCompOwner.IsNull()) {
373 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
376 TopoDS_Shape aShape = aCompOwner->Shape();
377 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
378 aSelectionPrs->Clear();
380 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
382 aSelectionPrs->SetDisplayPriority(9);
383 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
384 aSelectionPrs->Display();
385 thePM->Color(this, GetContext()->SelectionStyle());
390 //********************************************************************
391 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
392 const Handle(Prs3d_Drawer)& theStyle,
393 const Handle(SelectMgr_EntityOwner)& theOwner)
395 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
399 TopoDS_Shape aShape = aOwner->Shape();
400 if (!aShape.IsNull()) {
401 thePM->Color(this, theStyle);
403 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
404 aHilightPrs->Clear();
406 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
407 aHilightPrs->Highlight(theStyle);
409 if (thePM->IsImmediateModeOn())
410 thePM->AddToImmediateList(aHilightPrs);