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_ResultCompSolid.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 an unfree boundaries with different colors
75 Handle(Prs3d_Drawer) aDrawer = Attributes();
76 Handle(Prs3d_LineAspect) aFreeBndAspect = new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
77 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
79 if (aDrawer->HasOwnPointAspect())
80 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
82 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
84 // Activate individual repaintng if this is a part of compsolid
85 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
86 SetAutoHilight(aCompSolid.get() == NULL);
88 myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
89 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
90 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
91 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
93 ModuleBase_Tools::setPointBallHighlighting(this);
95 // Define colors for wireframe mode
96 setEdgesDefaultColor();
99 //********************************************************************
100 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
102 myAdditionalSelectionPriority = thePriority;
105 //********************************************************************
106 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
108 ViewerData_AISShape::SetColor(theColor);
109 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
110 setEdgesDefaultColor();
113 void ModuleBase_ResultPrs::setEdgesDefaultColor()
115 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
116 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
119 Handle(Prs3d_Drawer) aDrawer = Attributes();
120 Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
121 //anAspect->SetColor(Quantity_NOC_YELLOW);
122 //aDrawer->SetLineAspect(anAspect);
124 // - unfree boundaries color
125 anAspect = aDrawer->UnFreeBoundaryAspect();
126 anAspect->SetColor(Quantity_NOC_YELLOW);
127 aDrawer->SetUnFreeBoundaryAspect(anAspect);
128 aDrawer->SetUnFreeBoundaryDraw(true);
130 // - free boundaries color
131 anAspect = aDrawer->FreeBoundaryAspect();
132 anAspect->SetColor(Quantity_NOC_GREEN);
133 aDrawer->SetFreeBoundaryAspect(anAspect);
134 aDrawer->SetFreeBoundaryDraw(true);
136 // - standalone edges color
137 anAspect = aDrawer->WireAspect();
138 anAspect->SetColor(Quantity_NOC_RED);
139 aDrawer->SetWireAspect(anAspect);
144 //********************************************************************
145 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
147 bool isModified = false;
149 TopoDS_Compound aCompound;
150 BRep_Builder aBBuilder;
151 aBBuilder.MakeCompound (aCompound);
152 // restore hidden shapes if there are not the shapes in parameter container
153 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
154 for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
156 if (!theShapes.Contains(aHiddenIt.Value()))
157 aVisibleSubShapes.Append(aHiddenIt.Value());
159 aBBuilder.Add (aCompound, aHiddenIt.Value());
161 isModified = !aVisibleSubShapes.IsEmpty();
162 for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
164 myHiddenSubShapes.Remove(aVisibleIt.Value());
166 // append hidden shapes into internal container if there are not these shapes
167 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
170 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
173 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
174 myHiddenSubShapes.Append(aShapeIt.Value());
175 aBBuilder.Add (aCompound, aShapeIt.Value());
179 myHiddenCompound = aCompound;
183 //********************************************************************
184 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
186 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
189 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
190 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
193 if (theShape.IsSame(aShapeIt.Value()))
200 //********************************************************************
201 bool ModuleBase_ResultPrs::hasSubShapeVisible(
202 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
204 TopoDS_Compound aCompound;
205 BRep_Builder aBuilder;
206 aBuilder.MakeCompound (aCompound);
207 NCollection_List<TopoDS_Shape> aShapesToSkip;
208 aShapesToSkip.Append(myHiddenSubShapes);
209 for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
210 aShapesToSkip.Append(anIt.Value());
212 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
213 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
216 //********************************************************************
217 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
219 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
222 myTransparency = theTransparency;
223 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
227 //********************************************************************
228 void ModuleBase_ResultPrs::Compute(
229 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
230 const Handle(Prs3d_Presentation)& thePresentation,
231 const Standard_Integer theMode)
233 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
234 bool aReadyToDisplay = aShapePtr.get();
235 if (aReadyToDisplay) {
236 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
237 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
238 if (!myOriginalShape.IsNull())
239 Set(myOriginalShape);
241 else { // convert shape into SHELL
242 TopoDS_Compound aCompound;
243 BRep_Builder aBuilder;
244 aBuilder.MakeCompound (aCompound);
245 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
246 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
249 aReadyToDisplay = false;
252 // change deviation coefficient to provide more precise circle
253 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
254 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
256 // visualize hidden sub-shapes transparent
257 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
259 StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
260 aReadyToDisplay = true;
263 if (!aReadyToDisplay) {
264 Events_InfoMessage("ModuleBase_ResultPrs",
265 "An empty AIS presentation: ModuleBase_ResultPrs").send();
266 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
267 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
271 //********************************************************************
272 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
273 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
274 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
276 switch (theShape.ShapeType()) {
277 case TopAbs_COMPSOLID:
278 case TopAbs_COMPOUND: {
279 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
280 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
285 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
286 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
291 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
292 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
297 if (theHiddenSubShapes.Contains(theShape))
298 return; // remove hidden shape
299 theBuilder.Add(theCompound, theShape);
303 case TopAbs_VERTEX: {
304 theBuilder.Add(theCompound, theShape);
311 //********************************************************************
312 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
313 const Standard_Integer theMode)
315 if (appendVertexSelection(aSelection, theMode))
318 if (theMode > TopAbs_SHAPE) {
319 // In order to avoid using custom selection modes
320 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
321 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
326 // TODO: OCCT issue should be created for the COMPOUND processing
327 // before it is fixed, the next workaround in necessary
328 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
329 const TopoDS_Shape& aShape = Shape();
330 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
331 // do not activate in compound mode shapes which do not contain compounds
332 if (!aCompExp.More())
336 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
337 // // Limit selection area only by actual object (Shape)
338 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
339 // if (aCompSolid.get()) {
340 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
341 // if (aShapePtr.get()) {
342 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
343 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
344 // /// It is important to have priority for the shape of comp solid result less than priority
345 // /// for the presentation shape which is a sub-result.
346 // /// Reason is to select the sub-objects before: #1592
347 // aPriority = aPriority - 1;
348 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
350 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
351 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
352 // aDeflection, myDrawer->HLRAngle(), 9, 500);
354 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
355 // Handle(SelectMgr_EntityOwner) anOwner =
356 // Handle(SelectMgr_EntityOwner)
357 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
358 // anOwner->Set(this);
364 AIS_Shape::ComputeSelection(aSelection, theMode);
366 if (myAdditionalSelectionPriority > 0) {
367 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
368 Handle(SelectBasics_EntityOwner) aBasicsOwner =
369 aSelection->Sensitive()->BaseSensitive()->OwnerId();
370 if (!aBasicsOwner.IsNull())
371 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
376 //********************************************************************
377 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
378 const Standard_Integer theMode)
380 if (Shape().ShapeType() == TopAbs_VERTEX) {
381 const TopoDS_Shape& aShape = Shape();
383 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
384 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
386 /// The cause of this method is the last parameter of BRep owner setting into True.
387 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
388 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
389 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
390 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
391 aDeflection, myDrawer->HLRAngle(), 9, 500);
393 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
394 Handle(SelectMgr_EntityOwner) anOwner =
395 Handle(SelectMgr_EntityOwner)
396 ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
404 //********************************************************************
405 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
406 const SelectMgr_SequenceOfOwner& theOwners)
408 Handle(SelectMgr_EntityOwner) anOwner;
409 Handle(ModuleBase_BRepOwner) aCompOwner;
410 for (int i = 1; i <= theOwners.Length(); i++) {
411 anOwner = theOwners.Value(i);
412 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
413 if (aCompOwner.IsNull()) {
414 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
417 TopoDS_Shape aShape = aCompOwner->Shape();
418 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
419 aSelectionPrs->Clear();
421 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
423 aSelectionPrs->SetDisplayPriority(9);
424 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
425 aSelectionPrs->Display();
426 thePM->Color(this, GetContext()->SelectionStyle());
431 //********************************************************************
432 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
433 const Handle(Prs3d_Drawer)& theStyle,
434 const Handle(SelectMgr_EntityOwner)& theOwner)
436 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
440 TopoDS_Shape aShape = aOwner->Shape();
441 if (!aShape.IsNull()) {
442 thePM->Color(this, theStyle);
444 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
445 aHilightPrs->Clear();
447 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
448 aHilightPrs->Highlight(theStyle);
450 if (thePM->IsImmediateModeOn())
451 thePM->AddToImmediateList(aHilightPrs);