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