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