Salome HOME
Porting to SALOME_8.2.0
[modules/shaper.git] / src / PartSet / PartSet_ResultSketchPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_ResultSketchPrs.h
4 // Created:     25 March 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_ResultSketchPrs.h"
8 #include "PartSet_Tools.h"
9 #include "ModuleBase_Tools.h"
10
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Tools.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <GeomAPI_PlanarEdges.h>
15
16 #include <Events_InfoMessage.h>
17 #include <Events_Loop.h>
18
19 #include <SketchPlugin_SketchEntity.h>
20
21 #include <SketcherPrs_Tools.h>
22
23 #include <Config_PropManager.h>
24
25 #include <BRep_Builder.hxx>
26 #include <Prs3d_Drawer.hxx>
27 #include <Prs3d.hxx>
28 #include <Prs3d_PointAspect.hxx>
29 #include <TopoDS_Builder.hxx>
30 #include <SelectMgr_SelectionManager.hxx>
31 #include <StdPrs_WFShape.hxx>
32 #include <StdSelect_BRepSelectionTool.hxx>
33 #include <Graphic3d_AspectLine3d.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Graphic3d_AspectMarker3d.hxx>
36
37 #define DEBUG_WIRE
38
39 #ifdef DEBUG_WIRE
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopExp.hxx>
42 #endif
43
44 //*******************************************************************************************
45
46 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
47
48 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
49   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
50 {
51   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
52
53   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
54   Set(aShape);
55   Handle(Prs3d_Drawer) aDrawer = Attributes();
56   if (aDrawer->HasOwnPointAspect())
57     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
58   else
59     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
60
61   // Activate individual repaintng if this is a part of compsolid
62   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
63   SetAutoHilight(aCompSolid.get() == NULL);
64
65   ModuleBase_Tools::setPointBallHighlighting(this);
66 }
67
68 void PartSet_ResultSketchPrs::Compute(
69                       const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
70                       const Handle(Prs3d_Presentation)& thePresentation,
71                       const Standard_Integer theMode)
72 {
73   thePresentation->Clear();
74
75   TopoDS_Shape aResultShape;
76   TopoDS_Compound anAuxiliaryCompound;
77   NCollection_List<TopoDS_Shape> aFaceList;
78   fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
79
80   bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
81
82   if (aReadyToDisplay) {
83     if (!aResultShape.IsNull()) {
84       myOriginalShape = aResultShape;
85       if (!myOriginalShape.IsNull())
86         Set(myOriginalShape);
87     }
88     myAuxiliaryCompound = anAuxiliaryCompound;
89   }
90
91   setAuxiliaryPresentationStyle(false);
92
93   // change deviation coefficient to provide more precise circle
94   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
95   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
96
97   if (!myAuxiliaryCompound.IsNull()) {
98     setAuxiliaryPresentationStyle(true);
99
100     Handle(Prs3d_Drawer) aDrawer = Attributes();
101     StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
102   }
103
104   if (!aReadyToDisplay) {
105     Events_InfoMessage("PartSet_ResultSketchPrs",
106                        "An empty AIS presentation: PartSet_ResultSketchPrs").send();
107     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
108     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
109   }
110 }
111
112 #ifdef DEBUG_WIRE
113 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
114 {
115   TopTools_IndexedMapOfShape aSubShapes;
116   TopExp::MapShapes (theShape, theType, aSubShapes);
117
118   Standard_Boolean
119     isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
120   int anExtent = aSubShapes.Extent();
121   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
122   {
123     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
124     int aValue = 0;
125   }
126 }
127 #endif
128
129 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
130                                             const Standard_Integer theMode)
131 {
132   int aMode = theMode;
133
134   if (aMode > 8 &&
135       aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
136       aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
137     // In order to avoid using custom selection modes
138     return;
139
140   bool aShapeIsChanged = false;
141   double aPrevDeviation = Attributes()->DeviationCoefficient();
142   if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
143       aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
144     aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
145                                                           : AIS_Shape::SelectionMode(TopAbs_WIRE);
146 #ifdef DEBUG_WIRE
147     const TopoDS_Shape& aShape = Shape();
148     debugInfo(aShape, TopAbs_VERTEX); // 24
149     debugInfo(aShape, TopAbs_EDGE); // 12
150     debugInfo(aShape, TopAbs_WIRE); // 0
151     debugInfo(aShape, TopAbs_FACE); // 0
152 #endif
153     BRep_Builder aBuilder;
154     TopoDS_Compound aComp;
155     aBuilder.MakeCompound(aComp);
156     aBuilder.Add(aComp, myOriginalShape);
157
158     for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
159       const TopoDS_Shape& aFace = anIt.Value();
160       aBuilder.Add(aComp, aFace);
161     }
162 #ifdef DEBUG_WIRE
163     debugInfo(aComp, TopAbs_VERTEX); // 24
164     debugInfo(aComp, TopAbs_EDGE); // 12
165     debugInfo(aComp, TopAbs_WIRE); // 4
166     debugInfo(aComp, TopAbs_FACE); // 2
167 #endif
168     Set(aComp);
169     double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection",
170                                                          ModelAPI_ResultBody::DEFAULT_DEFLECTION());
171     Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
172     aShapeIsChanged = true;
173   }
174   else
175     Set(myOriginalShape);
176
177   // append auxiliary compound to selection of edges/vertices
178   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
179       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
180
181     bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
182     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
183   }
184
185   AIS_Shape::ComputeSelection(aSelection, aMode);
186
187   if (aShapeIsChanged) {
188     Attributes()->SetDeviationCoefficient(aPrevDeviation);
189     Set(myOriginalShape);
190   }
191 }
192
193 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
194                                                    const TopoDS_Shape& theShape,
195                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
196 {
197   // POP protection against crash in low layers
198   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
199   try {
200     StdSelect_BRepSelectionTool::Load(theSelection,
201                                       this,
202                                       theShape,
203                                       theTypeOfSelection,
204                                       aDeflection,
205                                       myDrawer->HLRAngle(),
206                                       myDrawer->IsAutoTriangulation());
207   } catch ( Standard_Failure ) {
208   }
209 }
210
211 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
212 {
213   Standard_Integer aLineStyle = isAuxiliary?
214     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
215     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
216
217   Handle(Prs3d_Drawer) aDrawer = Attributes();
218
219   // set line style
220   Handle(Prs3d_LineAspect) aLineAspect;
221
222   Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
223   if (aDrawer->HasOwnLineAspect()) {
224     aLineAspect = aDrawer->LineAspect();
225   }
226   if (aDrawer->HasOwnWireAspect()) {
227     aLineAspect = aDrawer->WireAspect();
228   }
229   Quantity_Color aCurrentColor;
230   Aspect_TypeOfLine aPrevLineType;
231   Standard_Real aCurrentWidth;
232   aLineAspect->Aspect()->Values(aCurrentColor, aPrevLineType, aCurrentWidth);
233   bool isChangedLineType = aType != aPrevLineType;
234   if (isChangedLineType) {
235     aLineAspect->SetTypeOfLine(aType);
236   }
237 }
238
239 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
240                                          TopoDS_Compound& theAuxiliaryCompound,
241                                          NCollection_List<TopoDS_Shape>& theFaceList)
242 {
243   //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
244   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
245   if (!aShapePtr)
246     return;
247
248   theFaceList.Clear();
249   ResultConstructionPtr aConstruction =
250     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
251   if (aConstruction.get()) {
252     int aFacesNum = aConstruction->facesNum();
253     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
254       std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
255       if (aFaceShape.get()) {
256         TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
257         theFaceList.Append(aFace);
258       }
259     }
260   }
261   theResultShape = aShapePtr->impl<TopoDS_Shape>();
262
263   /// find auxiliary shapes
264   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
265   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
266                                                                           (aResultFeature);
267   std::list<ResultPtr> anAuxiliaryResults;
268   for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
269     FeaturePtr aFeature = aSketchFeature->subFeature(i);
270     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
271       std::list<ResultPtr> aResults = aFeature->results();
272       std::list<ResultPtr>::const_iterator aIt;
273       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
274         ResultPtr aResult = *aIt;
275         if (aResult.get() && aResult->shape().get())
276           anAuxiliaryResults.push_back(aResult);
277       }
278     }
279     else {
280     /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
281       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
282       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
283       for (; aResIter != aRes.cend(); aResIter++) {
284         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
285             ModelAPI_ResultConstruction>(*aResIter);
286         if (aConstr) {
287           std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
288           if (aGeomShape.get()) {
289             const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
290             if (aShape.ShapeType() != TopAbs_EDGE)
291               anAuxiliaryResults.push_back(aConstr);
292           }
293         }
294       }
295     }
296   }
297
298   if (anAuxiliaryResults.size() > 0) {
299     BRep_Builder aBuilder;
300     //TopoDS_Compound aComp;
301     aBuilder.MakeCompound(theAuxiliaryCompound);
302     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
303                                          aLast = anAuxiliaryResults.end();
304     for (; anIt != aLast; anIt++) {
305       ResultPtr aResult = *anIt;
306       if (aResult.get()) {
307         GeomShapePtr aGeomShape = aResult->shape();
308         if (aGeomShape.get()) {
309           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
310           if (!aShape.IsNull())
311             aBuilder.Add(theAuxiliaryCompound, aShape);
312         }
313       }
314     }
315   }
316 }