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"
21 #include "ModuleBase_IViewer.h"
23 #include <GeomAPI_PlanarEdges.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_AttributeIntArray.h>
31 #include "ModuleBase_Tools.h"
32 #include "ModuleBase_BRepOwner.h"
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
37 #include <AIS_ColoredDrawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <AIS_Selection.hxx>
40 #include <BOPTools_AlgoTools3D.hxx>
41 #include <BRep_Builder.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Prs3d_Drawer.hxx>
45 #include <Prs3d_PointAspect.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <SelectMgr_SequenceOfOwner.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdSelect_BRepSelectionTool.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <TopoDS_Builder.hxx>
59 //*******************************************************************************************
61 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
65 //********************************************************************
66 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
67 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
68 myTransparency(1), myIsSubstituted(false)
70 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
71 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
74 // Activate individual repaintng if this is a part of compsolid
75 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
76 SetAutoHilight(aResOwner.get() == NULL);
78 // Set own free boundaries aspect in order to have free
79 // and unfree boundaries with different colors
80 Handle(Prs3d_Drawer) aDrawer = Attributes();
81 Handle(Prs3d_LineAspect) aFreeBndAspect =
82 new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
83 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
85 aDrawer->VIsoAspect()->SetNumber(0);
86 aDrawer->UIsoAspect()->SetNumber(0);
88 if (aDrawer->HasOwnPointAspect())
89 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
91 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
93 aDrawer = DynamicHilightAttributes();
94 if (aDrawer.IsNull()) {
95 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
96 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
97 aDrawer->VIsoAspect()->SetNumber(0);
98 aDrawer->UIsoAspect()->SetNumber(0);
99 SetDynamicHilightAttributes(aDrawer);
102 aDrawer->VIsoAspect()->SetNumber(0);
103 aDrawer->UIsoAspect()->SetNumber(0);
105 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
106 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
107 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
108 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
110 ModuleBase_Tools::setPointBallHighlighting(this);
112 // Define colors for wireframe mode
113 setEdgesDefaultColor();
116 //********************************************************************
117 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
119 myAdditionalSelectionPriority = thePriority;
122 //********************************************************************
123 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
125 ViewerData_AISShape::SetColor(theColor);
126 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
127 setEdgesDefaultColor();
130 void ModuleBase_ResultPrs::setEdgesDefaultColor()
132 if (myResult.get()) {
133 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
134 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
137 Handle(Prs3d_Drawer) aDrawer = Attributes();
138 Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
139 //anAspect->SetColor(Quantity_NOC_YELLOW);
140 //aDrawer->SetLineAspect(anAspect);
142 // - unfree boundaries color
143 anAspect = aDrawer->UnFreeBoundaryAspect();
144 anAspect->SetColor(Quantity_NOC_YELLOW);
145 aDrawer->SetUnFreeBoundaryAspect(anAspect);
146 aDrawer->SetUnFreeBoundaryDraw(true);
148 // - free boundaries color
149 anAspect = aDrawer->FreeBoundaryAspect();
150 anAspect->SetColor(Quantity_NOC_GREEN);
151 aDrawer->SetFreeBoundaryAspect(anAspect);
152 aDrawer->SetFreeBoundaryDraw(true);
154 // - standalone edges color
155 anAspect = aDrawer->WireAspect();
156 anAspect->SetColor(Quantity_NOC_RED);
157 aDrawer->SetWireAspect(anAspect);
163 //********************************************************************
164 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
166 bool isModified = false;
168 TopoDS_Compound aCompound;
169 BRep_Builder aBBuilder;
170 aBBuilder.MakeCompound (aCompound);
172 myHiddenSubShapes = theShapes;
173 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
174 myVisibleCompound = aCompound;
176 aBBuilder.MakeCompound (aCompound);
177 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
178 for (; aIt.More(); aIt.Next()) {
179 aBBuilder.Add(aCompound, aIt.Value());
181 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 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
192 for (; aShapeIt.More(); aShapeIt.Next()) {
193 if (theShape.IsSame(aShapeIt.Value()))
200 //********************************************************************
201 bool ModuleBase_ResultPrs::hasSubShapeVisible(
202 const TopoDS_ListOfShape& theShapesToSkip)
204 TopoDS_Compound aCompound;
205 BRep_Builder aBuilder;
206 aBuilder.MakeCompound (aCompound);
207 TopoDS_ListOfShape aShapesToSkip;
208 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
209 aShapesToSkip.Append(aHiddenCopy);
210 for (TopoDS_ListOfShape::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 = myResult->shape();
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);
241 myIsSubstituted = false;
244 else { // convert shape into SHELL
245 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
246 Set(myVisibleCompound);
247 myIsSubstituted = true;
249 aReadyToDisplay = false;
253 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
259 // visualize hidden sub-shapes transparent
260 if (myResult.get()) {
261 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
263 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
264 aReadyToDisplay = true;
267 if (!aReadyToDisplay) {
268 Events_InfoMessage("ModuleBase_ResultPrs",
269 "An empty AIS presentation: ModuleBase_ResultPrs").send();
270 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
271 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
276 //********************************************************************
277 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
278 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
279 const TopoDS_ListOfShape& theHiddenSubShapes)
281 switch (theShape.ShapeType()) {
282 case TopAbs_COMPSOLID:
283 case TopAbs_COMPOUND: {
284 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
285 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
290 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
291 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
296 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
297 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
302 if (theHiddenSubShapes.Contains(theShape))
303 return; // remove hidden shape
304 theBuilder.Add(theCompound, theShape);
308 case TopAbs_VERTEX: {
309 theBuilder.Add(theCompound, theShape);
316 //********************************************************************
317 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
318 const Standard_Integer theMode)
320 if (appendVertexSelection(aSelection, theMode))
323 if (theMode > TopAbs_SHAPE) {
324 // In order to avoid using custom selection modes
325 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
326 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
331 // TODO: OCCT issue should be created for the COMPOUND processing
332 // before it is fixed, the next workaround in necessary
333 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
334 const TopoDS_Shape& aShape = Shape();
335 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
336 // do not activate in compound mode shapes which do not contain compounds
337 if (!aCompExp.More())
341 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
342 // // Limit selection area only by actual object (Shape)
343 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
344 // if (aCompSolid.get()) {
345 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
346 // if (aShapePtr.get()) {
347 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
348 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
349 // /// It is important to have priority for the shape of comp solid result less than priority
350 // /// for the presentation shape which is a sub-result.
351 // /// Reason is to select the sub-objects before: #1592
352 // aPriority = aPriority - 1;
353 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
355 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
356 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
357 // aDeflection, myDrawer->HLRAngle(), 9, 500);
359 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
360 // Handle(SelectMgr_EntityOwner) anOwner =
361 // Handle(SelectMgr_EntityOwner)
362 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
363 // anOwner->Set(this);
369 AIS_Shape::ComputeSelection(aSelection, theMode);
371 if (myAdditionalSelectionPriority > 0) {
372 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
373 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
376 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
377 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
378 if (!aBasicsOwner.IsNull())
379 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
384 //********************************************************************
385 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
386 const Standard_Integer theMode)
388 if (Shape().ShapeType() == TopAbs_VERTEX) {
389 const TopoDS_Shape& aShape = Shape();
391 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
392 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
394 /// The cause of this method is the last parameter of BRep owner setting into True.
395 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
396 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
397 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
398 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
399 aDeflection, myDrawer->HLRAngle(), 9, 500);
402 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
403 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
406 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
407 Handle(SelectMgr_EntityOwner) anOwner =
408 Handle(SelectMgr_EntityOwner)
409 ::DownCast(anEntity->BaseSensitive()->OwnerId());
418 //********************************************************************
419 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
420 const SelectMgr_SequenceOfOwner& theOwners)
422 Handle(SelectMgr_EntityOwner) anOwner;
423 Handle(ModuleBase_BRepOwner) aCompOwner;
424 for (int i = 1; i <= theOwners.Length(); i++) {
425 anOwner = theOwners.Value(i);
426 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
427 if (aCompOwner.IsNull()) {
428 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
431 TopoDS_Shape aShape = aCompOwner->Shape();
432 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
433 aSelectionPrs->Clear();
435 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
437 aSelectionPrs->SetDisplayPriority(9);
438 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
439 aSelectionPrs->Display();
440 thePM->Color(this, GetContext()->SelectionStyle());
445 //********************************************************************
446 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
447 const Handle(Prs3d_Drawer)& theStyle,
448 const Handle(SelectMgr_EntityOwner)& theOwner)
450 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
454 TopoDS_Shape aShape = aOwner->Shape();
455 if (!aShape.IsNull()) {
456 thePM->Color(this, theStyle);
458 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
459 aHilightPrs->Clear();
461 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
462 aHilightPrs->Highlight(theStyle);
464 if (thePM->IsImmediateModeOn())
465 thePM->AddToImmediateList(aHilightPrs);