]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ResultPrs.cpp
Salome HOME
Issue #2999: Provide initialization of deviation coefficient for all presentations
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ResultPrs.h"
21 #include "ModuleBase_IViewer.h"
22
23 #include <GeomAPI_PlanarEdges.h>
24
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>
30
31 #include "ModuleBase_Tools.h"
32 #include "ModuleBase_BRepOwner.h"
33
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
36
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>
44 #include <Prs3d.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>
57 #include <TopoDS.hxx>
58 #include <TopoDS_Builder.hxx>
59
60 //*******************************************************************************************
61
62 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
63
64
65
66 //********************************************************************
67 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
68   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
69   myTransparency(1), myIsSubstituted(false)
70 {
71   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
72   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
73   Set(aShape);
74
75   // Activate individual repaintng if this is a part of compsolid
76   ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
77   SetAutoHilight(aResOwner.get() == NULL);
78
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->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
83   aDrawer->SetUnFreeBoundaryAspect(
84     new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
85   aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
86   aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
87
88   Quantity_Color aColor;
89   Color(aColor);
90
91   std::vector<int> aIsoValues;
92   bool isIsoVisible;
93   ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
94   if (isIsoVisible) {
95     if (aIsoValues.size() == 0) {
96       aIsoValues.push_back(1);
97       aIsoValues.push_back(1);
98     }
99   }
100   else {
101     aIsoValues.push_back(0);
102     aIsoValues.push_back(0);
103   }
104   myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
105   myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
106   aDrawer->SetUIsoAspect(myUIsoAspect);
107   aDrawer->SetVIsoAspect(myVIsoAspect);
108
109   if (aDrawer->HasOwnPointAspect())
110     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
111   else
112     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
113
114   aDrawer = DynamicHilightAttributes();
115   if (aDrawer.IsNull()) {
116     if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
117       aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
118       aDrawer->SetUIsoAspect(myUIsoAspect);
119       aDrawer->SetVIsoAspect(myVIsoAspect);
120       SetDynamicHilightAttributes(aDrawer);
121     }
122   }
123   //if (!aDrawer.IsNull())
124   //  aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
125
126   myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
127   Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
128   aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
129   aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
130   myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
131
132   ModuleBase_Tools::setPointBallHighlighting(this);
133
134   // Define colors for wireframe mode
135   setEdgesDefaultColor();
136 }
137
138 //********************************************************************
139 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
140 {
141   myAdditionalSelectionPriority = thePriority;
142 }
143
144 //********************************************************************
145 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
146 {
147   ViewerData_AISShape::SetColor(theColor);
148   myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
149   setEdgesDefaultColor();
150   myUIsoAspect->SetColor(theColor);
151   myVIsoAspect->SetColor(theColor);
152 }
153
154 void ModuleBase_ResultPrs::setEdgesDefaultColor()
155 {
156   if (myResult.get()) {
157     AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
158     bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
159
160     Handle(Prs3d_Drawer) aDrawer = Attributes();
161     aDrawer->SetFaceBoundaryDraw(Standard_True);
162     aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
163
164     if (!aHasColor) {
165       aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
166       aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
167       aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
168
169       aDrawer->SetUnFreeBoundaryDraw(Standard_True);
170       aDrawer->SetFreeBoundaryDraw(Standard_True);
171       aDrawer->SetWireDraw(Standard_True);
172     }
173   }
174 }
175
176
177 //********************************************************************
178 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
179 {
180   bool isModified = false;
181
182   TopoDS_Compound aCompound;
183   BRep_Builder aBBuilder;
184   aBBuilder.MakeCompound (aCompound);
185
186   myHiddenSubShapes = theShapes;
187   collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
188   myVisibleCompound = aCompound;
189
190   aBBuilder.MakeCompound (aCompound);
191   TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
192   for (; aIt.More(); aIt.Next()) {
193     aBBuilder.Add(aCompound, aIt.Value());
194   }
195   myHiddenCompound = aCompound;
196 }
197
198 //********************************************************************
199 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
200 {
201   if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
202     return false;
203
204   // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
205   TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
206   for (; aShapeIt.More(); aShapeIt.Next()) {
207     if (theShape.IsSame(aShapeIt.Value()))
208       return true;
209   }
210
211   return true;
212 }
213
214 //********************************************************************
215 bool ModuleBase_ResultPrs::hasSubShapeVisible(
216   const TopoDS_ListOfShape& theShapesToSkip)
217 {
218   TopoDS_Compound aCompound;
219   BRep_Builder aBuilder;
220   aBuilder.MakeCompound (aCompound);
221   TopoDS_ListOfShape aShapesToSkip;
222   TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
223   aShapesToSkip.Append(aHiddenCopy);
224   for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
225     aShapesToSkip.Append(anIt.Value());
226
227   collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
228   return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
229 }
230
231 //********************************************************************
232 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
233 {
234   if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
235     return false;
236
237   myTransparency = theTransparency;
238   myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
239   return true;
240 }
241
242 //********************************************************************
243 void ModuleBase_ResultPrs::Compute(
244           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
245           const Handle(Prs3d_Presentation)& thePresentation,
246           const Standard_Integer theMode)
247 {
248   std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
249   bool aReadyToDisplay = aShapePtr.get();
250   if (aReadyToDisplay) {
251     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
252     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
253       if (!myOriginalShape.IsNull()) {
254         Set(myOriginalShape);
255         myIsSubstituted = false;
256       }
257     }
258     else { // convert shape into SHELL
259       bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
260       Set(myVisibleCompound);
261       myIsSubstituted = true;
262       if (isEmptyShape)
263         aReadyToDisplay = false;
264     }
265   }
266   // change deviation coefficient to provide more precise circle
267   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
268   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
269   try {
270     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
271   }
272   catch (...) {
273     return;
274   }
275
276   // visualize hidden sub-shapes transparent
277   if (myResult.get()) {
278     if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
279     {
280       StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
281       aReadyToDisplay = true;
282     }
283
284     if (!aReadyToDisplay) {
285       Events_InfoMessage("ModuleBase_ResultPrs",
286         "An empty AIS presentation: ModuleBase_ResultPrs").send();
287       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
288       ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
289     }
290   }
291 }
292
293 //********************************************************************
294 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
295   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
296   const TopoDS_ListOfShape& theHiddenSubShapes)
297 {
298   switch (theShape.ShapeType()) {
299     case TopAbs_COMPSOLID:
300     case TopAbs_COMPOUND: {
301       for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
302         collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
303     }
304     break;
305     case TopAbs_SOLID:
306     case TopAbs_SHELL: {
307       for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
308         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
309       }
310     }
311     break;
312     case TopAbs_WIRE: {
313       for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
314         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
315       }
316     }
317     break;
318     case TopAbs_FACE: {
319       if (theHiddenSubShapes.Contains(theShape))
320         return; // remove hidden shape
321       theBuilder.Add(theCompound, theShape);
322     }
323     break;
324     case TopAbs_EDGE:
325     case TopAbs_VERTEX: {
326       theBuilder.Add(theCompound, theShape);
327     }
328     default:
329       break;
330   }
331 }
332
333 //********************************************************************
334 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
335                                             const Standard_Integer theMode)
336 {
337   if (appendVertexSelection(aSelection, theMode))
338     return;
339
340   if (theMode > TopAbs_SHAPE) {
341     // In order to avoid using custom selection modes
342     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
343       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
344     }
345     return;
346   }
347
348   // TODO: OCCT issue should be created for the COMPOUND processing
349   // before it is fixed, the next workaround in necessary
350   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
351     const TopoDS_Shape& aShape = Shape();
352     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
353     // do not activate in compound mode shapes which do not contain compounds
354     if (!aCompExp.More())
355       return;
356   }
357
358   // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
359   //  // Limit selection area only by actual object (Shape)
360   //  ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
361   //  if (aCompSolid.get()) {
362   //    std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
363   //    if (aShapePtr.get()) {
364   //      TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
365   //   int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
366   //     /// It is important to have priority for the shape of comp solid result less than priority
367   //      /// for the presentation shape which is a sub-result.
368   //      /// Reason is to select the sub-objects before: #1592
369   //      aPriority = aPriority - 1;
370   //      double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
371
372   //      Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
373   //      StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
374   //        aDeflection, myDrawer->HLRAngle(), 9, 500);
375
376   //      for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
377   //        Handle(SelectMgr_EntityOwner) anOwner =
378   //          Handle(SelectMgr_EntityOwner)
379   //          ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
380   //        anOwner->Set(this);
381   //      }
382   //      return;
383   //    }
384   //  }
385   //}
386   AIS_Shape::ComputeSelection(aSelection, theMode);
387
388   if (myAdditionalSelectionPriority > 0) {
389     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
390     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
391          anIt.More();
392          anIt.Next()) {
393       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
394       Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
395       if (!aBasicsOwner.IsNull())
396         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
397     }
398   }
399 }
400
401 //********************************************************************
402 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
403                                                  const Standard_Integer theMode)
404 {
405   if (Shape().ShapeType() == TopAbs_VERTEX) {
406     const TopoDS_Shape& aShape = Shape();
407
408     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
409     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
410
411     /// The cause of this method is the last parameter of BRep owner setting into True.
412     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
413     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
414     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
415     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
416                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
417
418
419     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
420     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
421          anIt.More();
422          anIt.Next()) {
423       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
424       Handle(SelectMgr_EntityOwner) anOwner =
425         Handle(SelectMgr_EntityOwner)
426         ::DownCast(anEntity->BaseSensitive()->OwnerId());
427       anOwner->Set(this);
428     }
429
430     return true;
431   }
432   return false;
433 }
434
435 //********************************************************************
436 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
437                                            const SelectMgr_SequenceOfOwner& theOwners)
438 {
439   Handle(SelectMgr_EntityOwner) anOwner;
440   Handle(ModuleBase_BRepOwner) aCompOwner;
441   for (int i = 1; i <= theOwners.Length(); i++) {
442     anOwner = theOwners.Value(i);
443     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
444     if (aCompOwner.IsNull()) {
445       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
446     }
447     else {
448       TopoDS_Shape aShape = aCompOwner->Shape();
449       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
450       aSelectionPrs->Clear();
451
452       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
453
454       aSelectionPrs->SetDisplayPriority(9);
455       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
456       aSelectionPrs->Display();
457       thePM->Color(this, GetContext()->SelectionStyle());
458     }
459   }
460 }
461
462 //********************************************************************
463 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
464                                                  const Handle(Prs3d_Drawer)& theStyle,
465                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
466 {
467   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
468   if (aOwner.IsNull())
469     return;
470
471   TopoDS_Shape aShape = aOwner->Shape();
472   if (!aShape.IsNull()) {
473     thePM->Color(this, theStyle);
474
475     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
476     aHilightPrs->Clear();
477
478     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
479     aHilightPrs->Highlight(theStyle);
480
481     if (thePM->IsImmediateModeOn())
482       thePM->AddToImmediateList(aHilightPrs);
483   }
484 }
485
486
487 //********************************************************************
488 void ModuleBase_ResultPrs::updateIsoLines()
489 {
490   std::vector<int> aIsoValues;
491   bool isIsoVisible;
492   ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
493   if (isIsoVisible) {
494     if (aIsoValues.size() == 0) {
495       aIsoValues.push_back(1);
496       aIsoValues.push_back(1);
497     }
498   }
499   else {
500     if (aIsoValues.size() == 0) {
501       aIsoValues.push_back(0);
502       aIsoValues.push_back(0);
503     }
504     else {
505       aIsoValues[0] = 0;
506       aIsoValues[1] = 0;
507     }
508   }
509   myUIsoAspect->SetNumber(aIsoValues[0]);
510   myVIsoAspect->SetNumber(aIsoValues[1]);
511 }