1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_ResultPrs.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),
70 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
71 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
74 // Set own free boundaries aspect in order to have free
75 // and unfree boundaries with different colors
76 Handle(Prs3d_Drawer) aDrawer = Attributes();
77 Handle(Prs3d_LineAspect) aFreeBndAspect =
78 new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
79 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
81 if (aDrawer->HasOwnPointAspect())
82 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
84 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
86 // Activate individual repaintng if this is a part of compsolid
87 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
88 SetAutoHilight(aResOwner.get() == NULL);
90 myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
91 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
92 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
93 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
95 ModuleBase_Tools::setPointBallHighlighting(this);
97 // Define colors for wireframe mode
98 setEdgesDefaultColor();
101 //********************************************************************
102 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
104 myAdditionalSelectionPriority = thePriority;
107 //********************************************************************
108 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
110 ViewerData_AISShape::SetColor(theColor);
111 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
112 setEdgesDefaultColor();
115 void ModuleBase_ResultPrs::setEdgesDefaultColor()
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);
146 //********************************************************************
147 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
149 bool isModified = false;
151 TopoDS_Compound aCompound;
152 BRep_Builder aBBuilder;
153 aBBuilder.MakeCompound (aCompound);
154 // restore hidden shapes if there are not the shapes in parameter container
155 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
156 for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
158 if (!theShapes.Contains(aHiddenIt.Value()))
159 aVisibleSubShapes.Append(aHiddenIt.Value());
161 aBBuilder.Add (aCompound, aHiddenIt.Value());
163 isModified = !aVisibleSubShapes.IsEmpty();
164 for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
166 myHiddenSubShapes.Remove(aVisibleIt.Value());
168 // append hidden shapes into internal container if there are not these shapes
169 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
172 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
175 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
176 myHiddenSubShapes.Append(aShapeIt.Value());
177 aBBuilder.Add (aCompound, aShapeIt.Value());
181 myHiddenCompound = aCompound;
185 //********************************************************************
186 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
188 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
191 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
192 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
195 if (theShape.IsSame(aShapeIt.Value()))
202 //********************************************************************
203 bool ModuleBase_ResultPrs::hasSubShapeVisible(
204 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
206 TopoDS_Compound aCompound;
207 BRep_Builder aBuilder;
208 aBuilder.MakeCompound (aCompound);
209 NCollection_List<TopoDS_Shape> aShapesToSkip;
210 aShapesToSkip.Append(myHiddenSubShapes);
211 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
212 aShapesToSkip.Append(anIt.Value());
214 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
215 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
218 //********************************************************************
219 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
221 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
224 myTransparency = theTransparency;
225 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
229 //********************************************************************
230 void ModuleBase_ResultPrs::Compute(
231 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
232 const Handle(Prs3d_Presentation)& thePresentation,
233 const Standard_Integer theMode)
235 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
236 bool aReadyToDisplay = aShapePtr.get();
237 if (aReadyToDisplay) {
238 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
239 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
240 if (!myOriginalShape.IsNull())
241 Set(myOriginalShape);
243 else { // convert shape into SHELL
244 TopoDS_Compound aCompound;
245 BRep_Builder aBuilder;
246 aBuilder.MakeCompound (aCompound);
247 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
248 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
251 aReadyToDisplay = false;
254 // change deviation coefficient to provide more precise circle
255 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
257 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
263 // visualize hidden sub-shapes transparent
264 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
266 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
267 aReadyToDisplay = true;
270 if (!aReadyToDisplay) {
271 Events_InfoMessage("ModuleBase_ResultPrs",
272 "An empty AIS presentation: ModuleBase_ResultPrs").send();
273 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
274 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
278 //********************************************************************
279 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
280 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
281 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
283 switch (theShape.ShapeType()) {
284 case TopAbs_COMPSOLID:
285 case TopAbs_COMPOUND: {
286 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
287 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
292 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
293 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
298 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
299 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
304 if (theHiddenSubShapes.Contains(theShape))
305 return; // remove hidden shape
306 theBuilder.Add(theCompound, theShape);
310 case TopAbs_VERTEX: {
311 theBuilder.Add(theCompound, theShape);
318 //********************************************************************
319 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
320 const Standard_Integer theMode)
322 if (appendVertexSelection(aSelection, theMode))
325 if (theMode > TopAbs_SHAPE) {
326 // In order to avoid using custom selection modes
327 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
328 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
333 // TODO: OCCT issue should be created for the COMPOUND processing
334 // before it is fixed, the next workaround in necessary
335 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
336 const TopoDS_Shape& aShape = Shape();
337 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
338 // do not activate in compound mode shapes which do not contain compounds
339 if (!aCompExp.More())
343 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
344 // // Limit selection area only by actual object (Shape)
345 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
346 // if (aCompSolid.get()) {
347 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
348 // if (aShapePtr.get()) {
349 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
350 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
351 // /// It is important to have priority for the shape of comp solid result less than priority
352 // /// for the presentation shape which is a sub-result.
353 // /// Reason is to select the sub-objects before: #1592
354 // aPriority = aPriority - 1;
355 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
357 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
358 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
359 // aDeflection, myDrawer->HLRAngle(), 9, 500);
361 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
362 // Handle(SelectMgr_EntityOwner) anOwner =
363 // Handle(SelectMgr_EntityOwner)
364 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
365 // anOwner->Set(this);
371 AIS_Shape::ComputeSelection(aSelection, theMode);
373 if (myAdditionalSelectionPriority > 0) {
374 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
375 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
378 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
379 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
380 if (!aBasicsOwner.IsNull())
381 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
386 //********************************************************************
387 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
388 const Standard_Integer theMode)
390 if (Shape().ShapeType() == TopAbs_VERTEX) {
391 const TopoDS_Shape& aShape = Shape();
393 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
394 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
396 /// The cause of this method is the last parameter of BRep owner setting into True.
397 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
398 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
399 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
400 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
401 aDeflection, myDrawer->HLRAngle(), 9, 500);
404 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
405 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
408 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
409 Handle(SelectMgr_EntityOwner) anOwner =
410 Handle(SelectMgr_EntityOwner)
411 ::DownCast(anEntity->BaseSensitive()->OwnerId());
420 //********************************************************************
421 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
422 const SelectMgr_SequenceOfOwner& theOwners)
424 Handle(SelectMgr_EntityOwner) anOwner;
425 Handle(ModuleBase_BRepOwner) aCompOwner;
426 for (int i = 1; i <= theOwners.Length(); i++) {
427 anOwner = theOwners.Value(i);
428 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
429 if (aCompOwner.IsNull()) {
430 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
433 TopoDS_Shape aShape = aCompOwner->Shape();
434 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
435 aSelectionPrs->Clear();
437 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
439 aSelectionPrs->SetDisplayPriority(9);
440 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
441 aSelectionPrs->Display();
442 thePM->Color(this, GetContext()->SelectionStyle());
447 //********************************************************************
448 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
449 const Handle(Prs3d_Drawer)& theStyle,
450 const Handle(SelectMgr_EntityOwner)& theOwner)
452 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
456 TopoDS_Shape aShape = aOwner->Shape();
457 if (!aShape.IsNull()) {
458 thePM->Color(this, theStyle);
460 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
461 aHilightPrs->Clear();
463 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
464 aHilightPrs->Highlight(theStyle);
466 if (thePM->IsImmediateModeOn())
467 thePM->AddToImmediateList(aHilightPrs);