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