Salome HOME
Issue #2071 Fatal error when Create box
[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 <ModelAPI_ResultCompSolid.h>
15 #include <GeomAPI_PlanarEdges.h>
16
17 #include <Events_InfoMessage.h>
18 #include <Events_Loop.h>
19
20 #include <SketchPlugin_SketchEntity.h>
21
22 #include <SketcherPrs_Tools.h>
23
24 #include <Config_PropManager.h>
25
26 #include <BRep_Builder.hxx>
27 #include <Prs3d_Drawer.hxx>
28 #include <Prs3d.hxx>
29 #include <Prs3d_PointAspect.hxx>
30 #include <TopoDS_Builder.hxx>
31 #include <SelectMgr_SelectionManager.hxx>
32 #include <StdPrs_WFShape.hxx>
33 #include <StdSelect_BRepSelectionTool.hxx>
34 #include <Graphic3d_AspectLine3d.hxx>
35 #include <Prs3d_LineAspect.hxx>
36 #include <Graphic3d_AspectMarker3d.hxx>
37
38 #define DEBUG_WIRE
39
40 #ifdef DEBUG_WIRE
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TopExp.hxx>
43 #endif
44
45 //*******************************************************************************************
46
47 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
48
49 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
50   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
51 {
52   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
53
54   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
55   Set(aShape);
56   Handle(Prs3d_Drawer) aDrawer = Attributes();
57   if (aDrawer->HasOwnPointAspect())
58     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
59   else
60     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
61
62   // Activate individual repaintng if this is a part of compsolid
63   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
64   SetAutoHilight(aCompSolid.get() == NULL);
65
66   ModuleBase_Tools::setPointBallHighlighting(this);
67 }
68
69 void PartSet_ResultSketchPrs::Compute(
70                       const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
71                       const Handle(Prs3d_Presentation)& thePresentation,
72                       const Standard_Integer theMode)
73 {
74   thePresentation->Clear();
75
76   TopoDS_Shape aResultShape;
77   TopoDS_Compound anAuxiliaryCompound;
78   NCollection_List<TopoDS_Shape> aFaceList;
79   fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
80
81   bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
82
83   if (aReadyToDisplay) {
84     if (!aResultShape.IsNull()) {
85       myOriginalShape = aResultShape;
86       if (!myOriginalShape.IsNull())
87         Set(myOriginalShape);
88     }
89     myAuxiliaryCompound = anAuxiliaryCompound;
90   }
91
92   setAuxiliaryPresentationStyle(false);
93
94   // change deviation coefficient to provide more precise circle
95   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
96   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
97
98   if (!myAuxiliaryCompound.IsNull()) {
99     setAuxiliaryPresentationStyle(true);
100
101     Handle(Prs3d_Drawer) aDrawer = Attributes();
102     StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
103   }
104
105   if (!aReadyToDisplay) {
106     Events_InfoMessage("PartSet_ResultSketchPrs",
107                        "An empty AIS presentation: PartSet_ResultSketchPrs").send();
108     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
109     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
110   }
111 }
112
113 #ifdef DEBUG_WIRE
114 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
115 {
116   TopTools_IndexedMapOfShape aSubShapes;
117   TopExp::MapShapes (theShape, theType, aSubShapes);
118
119   Standard_Boolean
120     isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
121   int anExtent = aSubShapes.Extent();
122   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
123   {
124     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
125     int aValue = 0;
126   }
127 }
128 #endif
129
130 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
131                                             const Standard_Integer theMode)
132 {
133   int aMode = theMode;
134
135   if (aMode > 8 &&
136       aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
137       aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
138     // In order to avoid using custom selection modes
139     return;
140
141   bool aShapeIsChanged = false;
142   double aPrevDeviation = Attributes()->DeviationCoefficient();
143   if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
144       aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
145     aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
146                                                           : AIS_Shape::SelectionMode(TopAbs_WIRE);
147 #ifdef DEBUG_WIRE
148     const TopoDS_Shape& aShape = Shape();
149     debugInfo(aShape, TopAbs_VERTEX); // 24
150     debugInfo(aShape, TopAbs_EDGE); // 12
151     debugInfo(aShape, TopAbs_WIRE); // 0
152     debugInfo(aShape, TopAbs_FACE); // 0
153 #endif
154     BRep_Builder aBuilder;
155     TopoDS_Compound aComp;
156     aBuilder.MakeCompound(aComp);
157     aBuilder.Add(aComp, myOriginalShape);
158
159     for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
160       const TopoDS_Shape& aFace = anIt.Value();
161       aBuilder.Add(aComp, aFace);
162     }
163 #ifdef DEBUG_WIRE
164     debugInfo(aComp, TopAbs_VERTEX); // 24
165     debugInfo(aComp, TopAbs_EDGE); // 12
166     debugInfo(aComp, TopAbs_WIRE); // 4
167     debugInfo(aComp, TopAbs_FACE); // 2
168 #endif
169     Set(aComp);
170     double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_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 = aLineAspect->Aspect()->Color();
230   Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
231   Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
232   bool isChangedLineType = aType != aPrevLineType;
233   if (isChangedLineType) {
234     Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
235     aDrawer->SetLineAspect(aAspect);
236     aDrawer->SetWireAspect(aAspect);
237     SetAttributes(aDrawer);
238   }
239 }
240
241 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
242                                          TopoDS_Compound& theAuxiliaryCompound,
243                                          NCollection_List<TopoDS_Shape>& theFaceList)
244 {
245   //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
246   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
247   if (!aShapePtr)
248     return;
249
250   theFaceList.Clear();
251   ResultConstructionPtr aConstruction =
252     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
253   if (aConstruction.get()) {
254     int aFacesNum = aConstruction->facesNum();
255     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
256       std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
257       if (aFaceShape.get()) {
258         TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
259         theFaceList.Append(aFace);
260       }
261     }
262   }
263   theResultShape = aShapePtr->impl<TopoDS_Shape>();
264
265   /// find auxiliary shapes
266   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
267   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
268                                                                           (aResultFeature);
269   std::list<ResultPtr> anAuxiliaryResults;
270   for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
271     FeaturePtr aFeature = aSketchFeature->subFeature(i);
272     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
273       std::list<ResultPtr> aResults = aFeature->results();
274       std::list<ResultPtr>::const_iterator aIt;
275       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
276         ResultPtr aResult = *aIt;
277         if (aResult.get() && aResult->shape().get())
278           anAuxiliaryResults.push_back(aResult);
279       }
280     }
281     else {
282     /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
283       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
284       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
285       for (; aResIter != aRes.cend(); aResIter++) {
286         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
287             ModelAPI_ResultConstruction>(*aResIter);
288         if (aConstr) {
289           std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
290           if (aGeomShape.get()) {
291             const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
292             if (aShape.ShapeType() != TopAbs_EDGE)
293               anAuxiliaryResults.push_back(aConstr);
294           }
295         }
296       }
297     }
298   }
299
300   if (anAuxiliaryResults.size() > 0) {
301     BRep_Builder aBuilder;
302     //TopoDS_Compound aComp;
303     aBuilder.MakeCompound(theAuxiliaryCompound);
304     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
305                                          aLast = anAuxiliaryResults.end();
306     for (; anIt != aLast; anIt++) {
307       ResultPtr aResult = *anIt;
308       if (aResult.get()) {
309         GeomShapePtr aGeomShape = aResult->shape();
310         if (aGeomShape.get()) {
311           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
312           if (!aShape.IsNull())
313             aBuilder.Add(theAuxiliaryCompound, aShape);
314         }
315       }
316     }
317   }
318 }