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