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),
67 myTransparency(1), myIsSubstituted(false)
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);
154 TopoDS_Compound aShownComp;
156 aShownComp = TopoDS::Compound(Shape());
158 // restore hidden shapes if there are not the shapes in parameter container
159 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
160 NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes);
161 for (; aHiddenIt.More(); aHiddenIt.Next()) {
162 if (!theShapes.Contains(aHiddenIt.Value())) {
163 aVisibleSubShapes.Append(aHiddenIt.Value());
166 aBBuilder.Add(aCompound, aHiddenIt.Value());
167 if (!aShownComp.IsNull())
168 aBBuilder.Remove(aShownComp, aHiddenIt.Value());
171 isModified = !aVisibleSubShapes.IsEmpty();
172 NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes);
173 for (; aVisibleIt.More(); aVisibleIt.Next()) {
174 if (myHiddenSubShapes.Contains(aVisibleIt.Value())) {
175 myHiddenSubShapes.Remove(aVisibleIt.Value());
176 if (!aShownComp.IsNull())
177 aBBuilder.Add(aShownComp, aVisibleIt.Value());
180 // append hidden shapes into internal container if there are not these shapes
181 NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes);
182 for (; aShapeIt.More(); aShapeIt.Next()) {
183 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
186 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
187 myHiddenSubShapes.Append(aShapeIt.Value());
188 aBBuilder.Add (aCompound, aShapeIt.Value());
189 if (!aShownComp.IsNull())
190 aBBuilder.Remove(aShownComp, aShapeIt.Value());
194 myHiddenCompound = aCompound;
198 //********************************************************************
199 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
201 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
204 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
205 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
208 if (theShape.IsSame(aShapeIt.Value()))
215 //********************************************************************
216 bool ModuleBase_ResultPrs::hasSubShapeVisible(
217 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
219 TopoDS_Compound aCompound;
220 BRep_Builder aBuilder;
221 aBuilder.MakeCompound (aCompound);
222 NCollection_List<TopoDS_Shape> aShapesToSkip;
223 aShapesToSkip.Append(myHiddenSubShapes);
224 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
225 aShapesToSkip.Append(anIt.Value());
227 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
228 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
231 //********************************************************************
232 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
234 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
237 myTransparency = theTransparency;
238 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
242 //********************************************************************
243 void ModuleBase_ResultPrs::Compute(
244 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
245 const Handle(Prs3d_Presentation)& thePresentation,
246 const Standard_Integer theMode)
248 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
249 bool aReadyToDisplay = aShapePtr.get();
250 if (aReadyToDisplay) {
251 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
252 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
253 if (!myOriginalShape.IsNull()) {
254 Set(myOriginalShape);
255 myIsSubstituted = false;
258 else { // convert shape into SHELL
259 TopoDS_Compound aCompound;
260 if (!myIsSubstituted) {
261 BRep_Builder aBuilder;
262 aBuilder.MakeCompound(aCompound);
263 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
266 aCompound = TopoDS::Compound(Shape());
268 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
270 myIsSubstituted = true;
272 aReadyToDisplay = false;
275 // change deviation coefficient to provide more precise circle
276 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
278 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
284 // visualize hidden sub-shapes transparent
285 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
287 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
288 aReadyToDisplay = true;
291 if (!aReadyToDisplay) {
292 Events_InfoMessage("ModuleBase_ResultPrs",
293 "An empty AIS presentation: ModuleBase_ResultPrs").send();
294 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
295 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
299 //********************************************************************
300 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
301 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
302 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
304 switch (theShape.ShapeType()) {
305 case TopAbs_COMPSOLID:
306 case TopAbs_COMPOUND: {
307 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
308 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
313 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
314 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
319 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
320 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
325 if (theHiddenSubShapes.Contains(theShape))
326 return; // remove hidden shape
327 theBuilder.Add(theCompound, theShape);
331 case TopAbs_VERTEX: {
332 theBuilder.Add(theCompound, theShape);
339 //********************************************************************
340 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
341 const Standard_Integer theMode)
343 if (appendVertexSelection(aSelection, theMode))
346 if (theMode > TopAbs_SHAPE) {
347 // In order to avoid using custom selection modes
348 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
349 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
354 // TODO: OCCT issue should be created for the COMPOUND processing
355 // before it is fixed, the next workaround in necessary
356 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
357 const TopoDS_Shape& aShape = Shape();
358 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
359 // do not activate in compound mode shapes which do not contain compounds
360 if (!aCompExp.More())
364 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
365 // // Limit selection area only by actual object (Shape)
366 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
367 // if (aCompSolid.get()) {
368 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
369 // if (aShapePtr.get()) {
370 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
371 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
372 // /// It is important to have priority for the shape of comp solid result less than priority
373 // /// for the presentation shape which is a sub-result.
374 // /// Reason is to select the sub-objects before: #1592
375 // aPriority = aPriority - 1;
376 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
378 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
379 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
380 // aDeflection, myDrawer->HLRAngle(), 9, 500);
382 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
383 // Handle(SelectMgr_EntityOwner) anOwner =
384 // Handle(SelectMgr_EntityOwner)
385 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
386 // anOwner->Set(this);
392 AIS_Shape::ComputeSelection(aSelection, theMode);
394 if (myAdditionalSelectionPriority > 0) {
395 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
396 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
399 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
400 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
401 if (!aBasicsOwner.IsNull())
402 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
407 //********************************************************************
408 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
409 const Standard_Integer theMode)
411 if (Shape().ShapeType() == TopAbs_VERTEX) {
412 const TopoDS_Shape& aShape = Shape();
414 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
415 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
417 /// The cause of this method is the last parameter of BRep owner setting into True.
418 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
419 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
420 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
421 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
422 aDeflection, myDrawer->HLRAngle(), 9, 500);
425 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
426 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
429 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
430 Handle(SelectMgr_EntityOwner) anOwner =
431 Handle(SelectMgr_EntityOwner)
432 ::DownCast(anEntity->BaseSensitive()->OwnerId());
441 //********************************************************************
442 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
443 const SelectMgr_SequenceOfOwner& theOwners)
445 Handle(SelectMgr_EntityOwner) anOwner;
446 Handle(ModuleBase_BRepOwner) aCompOwner;
447 for (int i = 1; i <= theOwners.Length(); i++) {
448 anOwner = theOwners.Value(i);
449 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
450 if (aCompOwner.IsNull()) {
451 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
454 TopoDS_Shape aShape = aCompOwner->Shape();
455 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
456 aSelectionPrs->Clear();
458 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
460 aSelectionPrs->SetDisplayPriority(9);
461 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
462 aSelectionPrs->Display();
463 thePM->Color(this, GetContext()->SelectionStyle());
468 //********************************************************************
469 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
470 const Handle(Prs3d_Drawer)& theStyle,
471 const Handle(SelectMgr_EntityOwner)& theOwner)
473 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
477 TopoDS_Shape aShape = aOwner->Shape();
478 if (!aShape.IsNull()) {
479 thePM->Color(this, theStyle);
481 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
482 aHilightPrs->Clear();
484 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
485 aHilightPrs->Highlight(theStyle);
487 if (thePM->IsImmediateModeOn())
488 thePM->AddToImmediateList(aHilightPrs);