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 // Activate individual repaintng if this is a part of compsolid
74 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
75 SetAutoHilight(aResOwner.get() == NULL);
77 // Set own free boundaries aspect in order to have free
78 // and unfree boundaries with different colors
79 Handle(Prs3d_Drawer) aDrawer = Attributes();
80 Handle(Prs3d_LineAspect) aFreeBndAspect =
81 new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
82 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
84 if (aDrawer->HasOwnPointAspect())
85 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
87 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
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 if (myResult.get()) {
117 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
118 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
121 Handle(Prs3d_Drawer) aDrawer = Attributes();
122 Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
123 //anAspect->SetColor(Quantity_NOC_YELLOW);
124 //aDrawer->SetLineAspect(anAspect);
126 // - unfree boundaries color
127 anAspect = aDrawer->UnFreeBoundaryAspect();
128 anAspect->SetColor(Quantity_NOC_YELLOW);
129 aDrawer->SetUnFreeBoundaryAspect(anAspect);
130 aDrawer->SetUnFreeBoundaryDraw(true);
132 // - free boundaries color
133 anAspect = aDrawer->FreeBoundaryAspect();
134 anAspect->SetColor(Quantity_NOC_GREEN);
135 aDrawer->SetFreeBoundaryAspect(anAspect);
136 aDrawer->SetFreeBoundaryDraw(true);
138 // - standalone edges color
139 anAspect = aDrawer->WireAspect();
140 anAspect->SetColor(Quantity_NOC_RED);
141 aDrawer->SetWireAspect(anAspect);
147 //********************************************************************
148 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
150 bool isModified = false;
152 TopoDS_Compound aCompound;
153 BRep_Builder aBBuilder;
154 aBBuilder.MakeCompound (aCompound);
156 TopoDS_Compound aShownComp;
158 aShownComp = TopoDS::Compound(Shape());
160 // restore hidden shapes if there are not the shapes in parameter container
161 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
162 NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes);
163 for (; aHiddenIt.More(); aHiddenIt.Next()) {
164 if (!theShapes.Contains(aHiddenIt.Value())) {
165 aVisibleSubShapes.Append(aHiddenIt.Value());
168 aBBuilder.Add(aCompound, aHiddenIt.Value());
169 if (!aShownComp.IsNull())
170 aBBuilder.Remove(aShownComp, aHiddenIt.Value());
173 isModified = !aVisibleSubShapes.IsEmpty();
174 NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes);
175 for (; aVisibleIt.More(); aVisibleIt.Next()) {
176 if (myHiddenSubShapes.Contains(aVisibleIt.Value())) {
177 myHiddenSubShapes.Remove(aVisibleIt.Value());
178 if (!aShownComp.IsNull())
179 aBBuilder.Add(aShownComp, aVisibleIt.Value());
182 // append hidden shapes into internal container if there are not these shapes
183 NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes);
184 for (; aShapeIt.More(); aShapeIt.Next()) {
185 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
188 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
189 myHiddenSubShapes.Append(aShapeIt.Value());
190 aBBuilder.Add (aCompound, aShapeIt.Value());
191 if (!aShownComp.IsNull())
192 aBBuilder.Remove(aShownComp, aShapeIt.Value());
196 myHiddenCompound = aCompound;
200 //********************************************************************
201 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
203 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
206 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
207 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
210 if (theShape.IsSame(aShapeIt.Value()))
217 //********************************************************************
218 bool ModuleBase_ResultPrs::hasSubShapeVisible(
219 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
221 TopoDS_Compound aCompound;
222 BRep_Builder aBuilder;
223 aBuilder.MakeCompound (aCompound);
224 NCollection_List<TopoDS_Shape> aShapesToSkip;
225 aShapesToSkip.Append(myHiddenSubShapes);
226 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
227 aShapesToSkip.Append(anIt.Value());
229 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
230 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
233 //********************************************************************
234 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
236 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
239 myTransparency = theTransparency;
240 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
244 //********************************************************************
245 void ModuleBase_ResultPrs::Compute(
246 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
247 const Handle(Prs3d_Presentation)& thePresentation,
248 const Standard_Integer theMode)
250 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
251 bool aReadyToDisplay = aShapePtr.get();
252 if (aReadyToDisplay) {
253 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
254 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
255 if (!myOriginalShape.IsNull()) {
256 Set(myOriginalShape);
257 myIsSubstituted = false;
260 else { // convert shape into SHELL
261 TopoDS_Compound aCompound;
262 if (!myIsSubstituted) {
263 BRep_Builder aBuilder;
264 aBuilder.MakeCompound(aCompound);
265 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
268 aCompound = TopoDS::Compound(Shape());
270 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
272 myIsSubstituted = true;
274 aReadyToDisplay = false;
278 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
284 // visualize hidden sub-shapes transparent
285 if (myResult.get()) {
286 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
288 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
289 aReadyToDisplay = true;
292 if (!aReadyToDisplay) {
293 Events_InfoMessage("ModuleBase_ResultPrs",
294 "An empty AIS presentation: ModuleBase_ResultPrs").send();
295 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
296 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
301 //********************************************************************
302 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
303 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
304 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
306 switch (theShape.ShapeType()) {
307 case TopAbs_COMPSOLID:
308 case TopAbs_COMPOUND: {
309 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
310 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
315 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
316 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
321 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
322 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
327 if (theHiddenSubShapes.Contains(theShape))
328 return; // remove hidden shape
329 theBuilder.Add(theCompound, theShape);
333 case TopAbs_VERTEX: {
334 theBuilder.Add(theCompound, theShape);
341 //********************************************************************
342 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
343 const Standard_Integer theMode)
345 if (appendVertexSelection(aSelection, theMode))
348 if (theMode > TopAbs_SHAPE) {
349 // In order to avoid using custom selection modes
350 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
351 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
356 // TODO: OCCT issue should be created for the COMPOUND processing
357 // before it is fixed, the next workaround in necessary
358 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
359 const TopoDS_Shape& aShape = Shape();
360 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
361 // do not activate in compound mode shapes which do not contain compounds
362 if (!aCompExp.More())
366 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
367 // // Limit selection area only by actual object (Shape)
368 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
369 // if (aCompSolid.get()) {
370 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
371 // if (aShapePtr.get()) {
372 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
373 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
374 // /// It is important to have priority for the shape of comp solid result less than priority
375 // /// for the presentation shape which is a sub-result.
376 // /// Reason is to select the sub-objects before: #1592
377 // aPriority = aPriority - 1;
378 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
380 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
381 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
382 // aDeflection, myDrawer->HLRAngle(), 9, 500);
384 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
385 // Handle(SelectMgr_EntityOwner) anOwner =
386 // Handle(SelectMgr_EntityOwner)
387 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
388 // anOwner->Set(this);
394 AIS_Shape::ComputeSelection(aSelection, theMode);
396 if (myAdditionalSelectionPriority > 0) {
397 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
398 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
401 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
402 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
403 if (!aBasicsOwner.IsNull())
404 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
409 //********************************************************************
410 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
411 const Standard_Integer theMode)
413 if (Shape().ShapeType() == TopAbs_VERTEX) {
414 const TopoDS_Shape& aShape = Shape();
416 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
417 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
419 /// The cause of this method is the last parameter of BRep owner setting into True.
420 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
421 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
422 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
423 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
424 aDeflection, myDrawer->HLRAngle(), 9, 500);
427 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
428 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
431 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
432 Handle(SelectMgr_EntityOwner) anOwner =
433 Handle(SelectMgr_EntityOwner)
434 ::DownCast(anEntity->BaseSensitive()->OwnerId());
443 //********************************************************************
444 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
445 const SelectMgr_SequenceOfOwner& theOwners)
447 Handle(SelectMgr_EntityOwner) anOwner;
448 Handle(ModuleBase_BRepOwner) aCompOwner;
449 for (int i = 1; i <= theOwners.Length(); i++) {
450 anOwner = theOwners.Value(i);
451 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
452 if (aCompOwner.IsNull()) {
453 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
456 TopoDS_Shape aShape = aCompOwner->Shape();
457 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
458 aSelectionPrs->Clear();
460 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
462 aSelectionPrs->SetDisplayPriority(9);
463 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
464 aSelectionPrs->Display();
465 thePM->Color(this, GetContext()->SelectionStyle());
470 //********************************************************************
471 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
472 const Handle(Prs3d_Drawer)& theStyle,
473 const Handle(SelectMgr_EntityOwner)& theOwner)
475 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
479 TopoDS_Shape aShape = aOwner->Shape();
480 if (!aShape.IsNull()) {
481 thePM->Color(this, theStyle);
483 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
484 aHilightPrs->Clear();
486 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
487 aHilightPrs->Highlight(theStyle);
489 if (thePM->IsImmediateModeOn())
490 thePM->AddToImmediateList(aHilightPrs);