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 <Prs3d_PlaneAspect.hxx>
49 #include <SelectMgr_SequenceOfOwner.hxx>
50 #include <SelectMgr_EntityOwner.hxx>
51 #include <SelectMgr_SelectionManager.hxx>
52 #include <StdPrs_WFShape.hxx>
53 #include <StdPrs_ShadedShape.hxx>
54 #include <StdSelect_BRepSelectionTool.hxx>
55 #include <TColStd_ListIteratorOfListOfInteger.hxx>
56 #include <TopExp_Explorer.hxx>
58 #include <TopoDS_Builder.hxx>
60 //*******************************************************************************************
62 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
66 //********************************************************************
67 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
68 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
69 myTransparency(1), myIsSubstituted(false)
71 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
72 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
75 // Activate individual repaintng if this is a part of compsolid
76 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
77 SetAutoHilight(aResOwner.get() == NULL);
79 // Set own free boundaries aspect in order to have free
80 // and unfree boundaries with different colors
81 Handle(Prs3d_Drawer) aDrawer = Attributes();
82 aDrawer->SetUnFreeBoundaryAspect(
83 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
84 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
85 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
87 Quantity_Color aColor;
90 std::vector<int> aIsoValues;
91 ModelAPI_Tools::getIsoLines(myResult, aIsoValues);
92 if (aIsoValues.size() == 0) {
93 aIsoValues.push_back(1);
94 aIsoValues.push_back(1);
96 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
97 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
98 aDrawer->SetUIsoAspect(myUIsoAspect);
99 aDrawer->SetVIsoAspect(myVIsoAspect);
101 if (aDrawer->HasOwnPointAspect())
102 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
104 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
106 aDrawer = DynamicHilightAttributes();
107 if (aDrawer.IsNull()) {
108 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
109 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
110 aDrawer->SetUIsoAspect(myUIsoAspect);
111 aDrawer->SetVIsoAspect(myVIsoAspect);
112 SetDynamicHilightAttributes(aDrawer);
116 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
117 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
118 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
119 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
120 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
122 ModuleBase_Tools::setPointBallHighlighting(this);
124 // Define colors for wireframe mode
125 setEdgesDefaultColor();
128 //********************************************************************
129 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
131 myAdditionalSelectionPriority = thePriority;
134 //********************************************************************
135 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
137 ViewerData_AISShape::SetColor(theColor);
138 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
139 setEdgesDefaultColor();
140 myUIsoAspect->SetColor(theColor);
141 myVIsoAspect->SetColor(theColor);
144 void ModuleBase_ResultPrs::setEdgesDefaultColor()
146 if (myResult.get()) {
147 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
148 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
150 Handle(Prs3d_Drawer) aDrawer = Attributes();
151 aDrawer->SetFaceBoundaryDraw(Standard_True);
152 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
155 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
156 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
157 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
159 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
160 aDrawer->SetFreeBoundaryDraw(Standard_True);
161 aDrawer->SetWireDraw(Standard_True);
167 //********************************************************************
168 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
170 bool isModified = false;
172 TopoDS_Compound aCompound;
173 BRep_Builder aBBuilder;
174 aBBuilder.MakeCompound (aCompound);
176 myHiddenSubShapes = theShapes;
177 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
178 myVisibleCompound = aCompound;
180 aBBuilder.MakeCompound (aCompound);
181 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
182 for (; aIt.More(); aIt.Next()) {
183 aBBuilder.Add(aCompound, aIt.Value());
185 myHiddenCompound = aCompound;
188 //********************************************************************
189 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
191 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
194 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
195 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
196 for (; aShapeIt.More(); aShapeIt.Next()) {
197 if (theShape.IsSame(aShapeIt.Value()))
204 //********************************************************************
205 bool ModuleBase_ResultPrs::hasSubShapeVisible(
206 const TopoDS_ListOfShape& theShapesToSkip)
208 TopoDS_Compound aCompound;
209 BRep_Builder aBuilder;
210 aBuilder.MakeCompound (aCompound);
211 TopoDS_ListOfShape aShapesToSkip;
212 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
213 aShapesToSkip.Append(aHiddenCopy);
214 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
215 aShapesToSkip.Append(anIt.Value());
217 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
218 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
221 //********************************************************************
222 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
224 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
227 myTransparency = theTransparency;
228 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
232 //********************************************************************
233 void ModuleBase_ResultPrs::Compute(
234 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
235 const Handle(Prs3d_Presentation)& thePresentation,
236 const Standard_Integer theMode)
238 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
239 bool aReadyToDisplay = aShapePtr.get();
240 if (aReadyToDisplay) {
241 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
242 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
243 if (!myOriginalShape.IsNull()) {
244 Set(myOriginalShape);
245 myIsSubstituted = false;
248 else { // convert shape into SHELL
249 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
250 Set(myVisibleCompound);
251 myIsSubstituted = true;
253 aReadyToDisplay = false;
257 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
263 // visualize hidden sub-shapes transparent
264 if (myResult.get()) {
265 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
267 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
268 aReadyToDisplay = true;
271 if (!aReadyToDisplay) {
272 Events_InfoMessage("ModuleBase_ResultPrs",
273 "An empty AIS presentation: ModuleBase_ResultPrs").send();
274 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
275 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
280 //********************************************************************
281 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
282 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
283 const TopoDS_ListOfShape& theHiddenSubShapes)
285 switch (theShape.ShapeType()) {
286 case TopAbs_COMPSOLID:
287 case TopAbs_COMPOUND: {
288 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
289 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
294 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
295 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
300 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
301 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
306 if (theHiddenSubShapes.Contains(theShape))
307 return; // remove hidden shape
308 theBuilder.Add(theCompound, theShape);
312 case TopAbs_VERTEX: {
313 theBuilder.Add(theCompound, theShape);
320 //********************************************************************
321 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
322 const Standard_Integer theMode)
324 if (appendVertexSelection(aSelection, theMode))
327 if (theMode > TopAbs_SHAPE) {
328 // In order to avoid using custom selection modes
329 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
330 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
335 // TODO: OCCT issue should be created for the COMPOUND processing
336 // before it is fixed, the next workaround in necessary
337 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
338 const TopoDS_Shape& aShape = Shape();
339 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
340 // do not activate in compound mode shapes which do not contain compounds
341 if (!aCompExp.More())
345 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
346 // // Limit selection area only by actual object (Shape)
347 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
348 // if (aCompSolid.get()) {
349 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
350 // if (aShapePtr.get()) {
351 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
352 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
353 // /// It is important to have priority for the shape of comp solid result less than priority
354 // /// for the presentation shape which is a sub-result.
355 // /// Reason is to select the sub-objects before: #1592
356 // aPriority = aPriority - 1;
357 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
359 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
360 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
361 // aDeflection, myDrawer->HLRAngle(), 9, 500);
363 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
364 // Handle(SelectMgr_EntityOwner) anOwner =
365 // Handle(SelectMgr_EntityOwner)
366 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
367 // anOwner->Set(this);
373 AIS_Shape::ComputeSelection(aSelection, theMode);
375 if (myAdditionalSelectionPriority > 0) {
376 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
377 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
380 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
381 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
382 if (!aBasicsOwner.IsNull())
383 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
388 //********************************************************************
389 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
390 const Standard_Integer theMode)
392 if (Shape().ShapeType() == TopAbs_VERTEX) {
393 const TopoDS_Shape& aShape = Shape();
395 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
396 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
398 /// The cause of this method is the last parameter of BRep owner setting into True.
399 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
400 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
401 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
402 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
403 aDeflection, myDrawer->HLRAngle(), 9, 500);
406 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
407 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
410 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
411 Handle(SelectMgr_EntityOwner) anOwner =
412 Handle(SelectMgr_EntityOwner)
413 ::DownCast(anEntity->BaseSensitive()->OwnerId());
422 //********************************************************************
423 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
424 const SelectMgr_SequenceOfOwner& theOwners)
426 Handle(SelectMgr_EntityOwner) anOwner;
427 Handle(ModuleBase_BRepOwner) aCompOwner;
428 for (int i = 1; i <= theOwners.Length(); i++) {
429 anOwner = theOwners.Value(i);
430 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
431 if (aCompOwner.IsNull()) {
432 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
435 TopoDS_Shape aShape = aCompOwner->Shape();
436 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
437 aSelectionPrs->Clear();
439 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
441 aSelectionPrs->SetDisplayPriority(9);
442 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
443 aSelectionPrs->Display();
444 thePM->Color(this, GetContext()->SelectionStyle());
449 //********************************************************************
450 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
451 const Handle(Prs3d_Drawer)& theStyle,
452 const Handle(SelectMgr_EntityOwner)& theOwner)
454 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
458 TopoDS_Shape aShape = aOwner->Shape();
459 if (!aShape.IsNull()) {
460 thePM->Color(this, theStyle);
462 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
463 aHilightPrs->Clear();
465 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
466 aHilightPrs->Highlight(theStyle);
468 if (thePM->IsImmediateModeOn())
469 thePM->AddToImmediateList(aHilightPrs);