1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "ModuleBase_ResultPrs.h"
22 #include <GeomAPI_PlanarEdges.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_AttributeIntArray.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>();
73 // Set own free boundaries aspect in order to have free
74 // and unfree boundaries with different colors
75 Handle(Prs3d_Drawer) aDrawer = Attributes();
76 Handle(Prs3d_LineAspect) aFreeBndAspect =
77 new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
78 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
80 if (aDrawer->HasOwnPointAspect())
81 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
83 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
85 // Activate individual repaintng if this is a part of compsolid
86 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
87 SetAutoHilight(aResOwner.get() == NULL);
89 myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
90 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
91 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
92 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
94 ModuleBase_Tools::setPointBallHighlighting(this);
96 // Define colors for wireframe mode
97 setEdgesDefaultColor();
100 //********************************************************************
101 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
103 myAdditionalSelectionPriority = thePriority;
106 //********************************************************************
107 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
109 ViewerData_AISShape::SetColor(theColor);
110 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
111 setEdgesDefaultColor();
114 void ModuleBase_ResultPrs::setEdgesDefaultColor()
116 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
117 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
120 Handle(Prs3d_Drawer) aDrawer = Attributes();
121 Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
122 //anAspect->SetColor(Quantity_NOC_YELLOW);
123 //aDrawer->SetLineAspect(anAspect);
125 // - unfree boundaries color
126 anAspect = aDrawer->UnFreeBoundaryAspect();
127 anAspect->SetColor(Quantity_NOC_YELLOW);
128 aDrawer->SetUnFreeBoundaryAspect(anAspect);
129 aDrawer->SetUnFreeBoundaryDraw(true);
131 // - free boundaries color
132 anAspect = aDrawer->FreeBoundaryAspect();
133 anAspect->SetColor(Quantity_NOC_GREEN);
134 aDrawer->SetFreeBoundaryAspect(anAspect);
135 aDrawer->SetFreeBoundaryDraw(true);
137 // - standalone edges color
138 anAspect = aDrawer->WireAspect();
139 anAspect->SetColor(Quantity_NOC_RED);
140 aDrawer->SetWireAspect(anAspect);
145 //********************************************************************
146 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
148 bool isModified = false;
150 TopoDS_Compound aCompound;
151 BRep_Builder aBBuilder;
152 aBBuilder.MakeCompound (aCompound);
153 // restore hidden shapes if there are not the shapes in parameter container
154 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
155 for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
157 if (!theShapes.Contains(aHiddenIt.Value()))
158 aVisibleSubShapes.Append(aHiddenIt.Value());
160 aBBuilder.Add (aCompound, aHiddenIt.Value());
162 isModified = !aVisibleSubShapes.IsEmpty();
163 for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
165 myHiddenSubShapes.Remove(aVisibleIt.Value());
167 // append hidden shapes into internal container if there are not these shapes
168 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
171 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
174 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
175 myHiddenSubShapes.Append(aShapeIt.Value());
176 aBBuilder.Add (aCompound, aShapeIt.Value());
180 myHiddenCompound = aCompound;
184 //********************************************************************
185 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
187 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
190 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
191 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
194 if (theShape.IsSame(aShapeIt.Value()))
201 //********************************************************************
202 bool ModuleBase_ResultPrs::hasSubShapeVisible(
203 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
205 TopoDS_Compound aCompound;
206 BRep_Builder aBuilder;
207 aBuilder.MakeCompound (aCompound);
208 NCollection_List<TopoDS_Shape> aShapesToSkip;
209 aShapesToSkip.Append(myHiddenSubShapes);
210 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
211 aShapesToSkip.Append(anIt.Value());
213 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
214 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
217 //********************************************************************
218 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
220 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
223 myTransparency = theTransparency;
224 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
228 //********************************************************************
229 void ModuleBase_ResultPrs::Compute(
230 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
231 const Handle(Prs3d_Presentation)& thePresentation,
232 const Standard_Integer theMode)
234 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
235 bool aReadyToDisplay = aShapePtr.get();
236 if (aReadyToDisplay) {
237 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
238 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
239 if (!myOriginalShape.IsNull())
240 Set(myOriginalShape);
242 else { // convert shape into SHELL
243 TopoDS_Compound aCompound;
244 BRep_Builder aBuilder;
245 aBuilder.MakeCompound (aCompound);
246 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
247 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
250 aReadyToDisplay = false;
253 // change deviation coefficient to provide more precise circle
254 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
256 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
262 // visualize hidden sub-shapes transparent
263 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
265 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
266 aReadyToDisplay = true;
269 if (!aReadyToDisplay) {
270 Events_InfoMessage("ModuleBase_ResultPrs",
271 "An empty AIS presentation: ModuleBase_ResultPrs").send();
272 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
273 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
277 //********************************************************************
278 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
279 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
280 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
282 switch (theShape.ShapeType()) {
283 case TopAbs_COMPSOLID:
284 case TopAbs_COMPOUND: {
285 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
286 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
291 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
292 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
297 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
298 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
303 if (theHiddenSubShapes.Contains(theShape))
304 return; // remove hidden shape
305 theBuilder.Add(theCompound, theShape);
309 case TopAbs_VERTEX: {
310 theBuilder.Add(theCompound, theShape);
317 //********************************************************************
318 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
319 const Standard_Integer theMode)
321 if (appendVertexSelection(aSelection, theMode))
324 if (theMode > TopAbs_SHAPE) {
325 // In order to avoid using custom selection modes
326 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
327 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
332 // TODO: OCCT issue should be created for the COMPOUND processing
333 // before it is fixed, the next workaround in necessary
334 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
335 const TopoDS_Shape& aShape = Shape();
336 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
337 // do not activate in compound mode shapes which do not contain compounds
338 if (!aCompExp.More())
342 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
343 // // Limit selection area only by actual object (Shape)
344 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
345 // if (aCompSolid.get()) {
346 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
347 // if (aShapePtr.get()) {
348 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
349 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
350 // /// It is important to have priority for the shape of comp solid result less than priority
351 // /// for the presentation shape which is a sub-result.
352 // /// Reason is to select the sub-objects before: #1592
353 // aPriority = aPriority - 1;
354 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
356 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
357 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
358 // aDeflection, myDrawer->HLRAngle(), 9, 500);
360 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
361 // Handle(SelectMgr_EntityOwner) anOwner =
362 // Handle(SelectMgr_EntityOwner)
363 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
364 // anOwner->Set(this);
370 AIS_Shape::ComputeSelection(aSelection, theMode);
372 if (myAdditionalSelectionPriority > 0) {
373 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
374 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
377 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
378 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
379 if (!aBasicsOwner.IsNull())
380 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
385 //********************************************************************
386 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
387 const Standard_Integer theMode)
389 if (Shape().ShapeType() == TopAbs_VERTEX) {
390 const TopoDS_Shape& aShape = Shape();
392 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
393 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
395 /// The cause of this method is the last parameter of BRep owner setting into True.
396 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
397 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
398 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
399 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
400 aDeflection, myDrawer->HLRAngle(), 9, 500);
403 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
404 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
407 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
408 Handle(SelectMgr_EntityOwner) anOwner =
409 Handle(SelectMgr_EntityOwner)
410 ::DownCast(anEntity->BaseSensitive()->OwnerId());
419 //********************************************************************
420 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
421 const SelectMgr_SequenceOfOwner& theOwners)
423 Handle(SelectMgr_EntityOwner) anOwner;
424 Handle(ModuleBase_BRepOwner) aCompOwner;
425 for (int i = 1; i <= theOwners.Length(); i++) {
426 anOwner = theOwners.Value(i);
427 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
428 if (aCompOwner.IsNull()) {
429 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
432 TopoDS_Shape aShape = aCompOwner->Shape();
433 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
434 aSelectionPrs->Clear();
436 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
438 aSelectionPrs->SetDisplayPriority(9);
439 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
440 aSelectionPrs->Display();
441 thePM->Color(this, GetContext()->SelectionStyle());
446 //********************************************************************
447 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
448 const Handle(Prs3d_Drawer)& theStyle,
449 const Handle(SelectMgr_EntityOwner)& theOwner)
451 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
455 TopoDS_Shape aShape = aOwner->Shape();
456 if (!aShape.IsNull()) {
457 thePM->Color(this, theStyle);
459 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
460 aHilightPrs->Clear();
462 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
463 aHilightPrs->Highlight(theStyle);
465 if (thePM->IsImmediateModeOn())
466 thePM->AddToImmediateList(aHilightPrs);