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 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
150 bool isModified = false;
152 TopoDS_Compound aCompound;
153 BRep_Builder aBBuilder;
154 aBBuilder.MakeCompound (aCompound);
156 myHiddenSubShapes = theShapes;
157 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
158 myVisibleCompound = aCompound;
160 aBBuilder.MakeCompound (aCompound);
161 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
162 for (; aIt.More(); aIt.Next()) {
163 aBBuilder.Add(aCompound, aIt.Value());
165 myHiddenCompound = aCompound;
168 //********************************************************************
169 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
171 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
174 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
175 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
176 for (; aShapeIt.More(); aShapeIt.Next()) {
177 if (theShape.IsSame(aShapeIt.Value()))
184 //********************************************************************
185 bool ModuleBase_ResultPrs::hasSubShapeVisible(
186 const TopoDS_ListOfShape& theShapesToSkip)
188 TopoDS_Compound aCompound;
189 BRep_Builder aBuilder;
190 aBuilder.MakeCompound (aCompound);
191 TopoDS_ListOfShape aShapesToSkip;
192 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
193 aShapesToSkip.Append(aHiddenCopy);
194 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
195 aShapesToSkip.Append(anIt.Value());
197 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
198 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
201 //********************************************************************
202 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
204 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
207 myTransparency = theTransparency;
208 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
212 //********************************************************************
213 void ModuleBase_ResultPrs::Compute(
214 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
215 const Handle(Prs3d_Presentation)& thePresentation,
216 const Standard_Integer theMode)
218 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
219 bool aReadyToDisplay = aShapePtr.get();
220 if (aReadyToDisplay) {
221 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
222 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
223 if (!myOriginalShape.IsNull()) {
224 Set(myOriginalShape);
225 myIsSubstituted = false;
228 else { // convert shape into SHELL
229 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
230 Set(myVisibleCompound);
231 myIsSubstituted = true;
233 aReadyToDisplay = false;
237 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
243 // visualize hidden sub-shapes transparent
244 if (myResult.get()) {
245 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
247 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
248 aReadyToDisplay = true;
251 if (!aReadyToDisplay) {
252 Events_InfoMessage("ModuleBase_ResultPrs",
253 "An empty AIS presentation: ModuleBase_ResultPrs").send();
254 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
255 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
260 //********************************************************************
261 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
262 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
263 const TopoDS_ListOfShape& theHiddenSubShapes)
265 switch (theShape.ShapeType()) {
266 case TopAbs_COMPSOLID:
267 case TopAbs_COMPOUND: {
268 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
269 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
274 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
275 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
280 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
281 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
286 if (theHiddenSubShapes.Contains(theShape))
287 return; // remove hidden shape
288 theBuilder.Add(theCompound, theShape);
292 case TopAbs_VERTEX: {
293 theBuilder.Add(theCompound, theShape);
300 //********************************************************************
301 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
302 const Standard_Integer theMode)
304 if (appendVertexSelection(aSelection, theMode))
307 if (theMode > TopAbs_SHAPE) {
308 // In order to avoid using custom selection modes
309 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
310 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
315 // TODO: OCCT issue should be created for the COMPOUND processing
316 // before it is fixed, the next workaround in necessary
317 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
318 const TopoDS_Shape& aShape = Shape();
319 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
320 // do not activate in compound mode shapes which do not contain compounds
321 if (!aCompExp.More())
325 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
326 // // Limit selection area only by actual object (Shape)
327 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
328 // if (aCompSolid.get()) {
329 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
330 // if (aShapePtr.get()) {
331 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
332 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
333 // /// It is important to have priority for the shape of comp solid result less than priority
334 // /// for the presentation shape which is a sub-result.
335 // /// Reason is to select the sub-objects before: #1592
336 // aPriority = aPriority - 1;
337 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
339 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
340 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
341 // aDeflection, myDrawer->HLRAngle(), 9, 500);
343 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
344 // Handle(SelectMgr_EntityOwner) anOwner =
345 // Handle(SelectMgr_EntityOwner)
346 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
347 // anOwner->Set(this);
353 AIS_Shape::ComputeSelection(aSelection, theMode);
355 if (myAdditionalSelectionPriority > 0) {
356 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
357 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
360 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
361 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
362 if (!aBasicsOwner.IsNull())
363 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
368 //********************************************************************
369 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
370 const Standard_Integer theMode)
372 if (Shape().ShapeType() == TopAbs_VERTEX) {
373 const TopoDS_Shape& aShape = Shape();
375 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
376 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
378 /// The cause of this method is the last parameter of BRep owner setting into True.
379 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
380 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
381 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
382 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
383 aDeflection, myDrawer->HLRAngle(), 9, 500);
386 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
387 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
390 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
391 Handle(SelectMgr_EntityOwner) anOwner =
392 Handle(SelectMgr_EntityOwner)
393 ::DownCast(anEntity->BaseSensitive()->OwnerId());
402 //********************************************************************
403 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
404 const SelectMgr_SequenceOfOwner& theOwners)
406 Handle(SelectMgr_EntityOwner) anOwner;
407 Handle(ModuleBase_BRepOwner) aCompOwner;
408 for (int i = 1; i <= theOwners.Length(); i++) {
409 anOwner = theOwners.Value(i);
410 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
411 if (aCompOwner.IsNull()) {
412 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
415 TopoDS_Shape aShape = aCompOwner->Shape();
416 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
417 aSelectionPrs->Clear();
419 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
421 aSelectionPrs->SetDisplayPriority(9);
422 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
423 aSelectionPrs->Display();
424 thePM->Color(this, GetContext()->SelectionStyle());
429 //********************************************************************
430 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
431 const Handle(Prs3d_Drawer)& theStyle,
432 const Handle(SelectMgr_EntityOwner)& theOwner)
434 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
438 TopoDS_Shape aShape = aOwner->Shape();
439 if (!aShape.IsNull()) {
440 thePM->Color(this, theStyle);
442 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
443 aHilightPrs->Clear();
445 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
446 aHilightPrs->Highlight(theStyle);
448 if (thePM->IsImmediateModeOn())
449 thePM->AddToImmediateList(aHilightPrs);