]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_ResultSketchPrs.cpp
Salome HOME
ec87b9dc4aa25a307b2d6e29692d5327976896ea
[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   {
87     std::vector<int> aColor;
88     aColor = Config_PropManager::color("Visualization", "result_construction_color",
89                                        ModelAPI_ResultConstruction::DEFAULT_COLOR());
90     Standard_Real anAuxiliaryWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
91     Standard_Integer anAuxiliaryLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
92
93     Handle(Prs3d_Drawer) aDrawer = Attributes();
94     setColor(aDrawer, Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB));
95
96     //thePresentation->Clear();
97     setWidth(aDrawer, anAuxiliaryWidth);
98     // set line style
99     Handle(Prs3d_LineAspect) aLineAspect;
100
101     Aspect_TypeOfLine aType = (Aspect_TypeOfLine)anAuxiliaryLineStyle;
102     if (aDrawer->HasOwnLineAspect()) {
103       aLineAspect = aDrawer->LineAspect();
104     }
105     if (aDrawer->HasOwnWireAspect()) {
106       aLineAspect = aDrawer->WireAspect();
107     }
108     Quantity_Color aCurrentColor;
109     Aspect_TypeOfLine aPrevLineType;
110     Standard_Real aCurrentWidth;
111     aLineAspect->Aspect()->Values(aCurrentColor, aPrevLineType, aCurrentWidth);
112     bool isChangedLineType = aType != aPrevLineType;
113     if (isChangedLineType) {
114       aLineAspect->SetTypeOfLine(aType);
115     }
116     // end of set line style
117   }
118
119   myFacesList.clear();
120   ResultConstructionPtr aConstruction = 
121     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
122   if (aConstruction.get()) {
123     int aFacesNum = aConstruction->facesNum();
124     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
125       myFacesList.push_back(aConstruction->face(aFaceIndex));
126     }
127   }
128
129   myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
130   if (!myOriginalShape.IsNull()) {
131     Set(myOriginalShape);
132
133     // change deviation coefficient to provide more precise circle
134     ModuleBase_Tools::setDefaultDeviationCoefficient(myOriginalShape, Attributes());
135     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
136   }
137   else
138     Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
139
140
141   // create auxiliary shapes
142   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
143   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
144                                                                           (aResultFeature);
145   std::list<ResultPtr> anAuxiliaryResults;
146   /// append auxiliary shapes
147   for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
148     FeaturePtr aFeature = aSketchFeature->subFeature(i);
149     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
150       std::list<ResultPtr> aResults = aFeature->results();
151       std::list<ResultPtr>::const_iterator aIt;
152       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
153         ResultPtr aResult = *aIt;
154         if (aResult.get() && aResult->shape().get())
155           anAuxiliaryResults.push_back(aResult);
156       }
157     }
158     else {
159     /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
160       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
161       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
162       for (; aResIter != aRes.cend(); aResIter++) {
163         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
164             ModelAPI_ResultConstruction>(*aResIter);
165         if (aConstr) {
166           std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
167           if (aGeomShape.get()) {
168             const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
169             if (aShape.ShapeType() != TopAbs_EDGE)
170               anAuxiliaryResults.push_back(aConstr);
171           }
172         }
173       }
174     }
175   }
176
177   if (anAuxiliaryResults.size() > 0) {
178     Quantity_Color aPrevColor;
179     Color(aPrevColor);
180     Standard_Real aPrevWidth = Width();
181
182     std::vector<int> aColor;
183     aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color",
184                                          SKETCH_AUXILIARY_COLOR);
185     Standard_Real anAuxiliaryWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
186     Standard_Integer anAuxiliaryLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
187
188     Handle(Prs3d_Drawer) aDrawer = Attributes();
189     setColor(aDrawer, Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB));
190
191     //thePresentation->Clear();
192     setWidth(aDrawer, anAuxiliaryWidth);
193     // set line style
194     Handle(Prs3d_LineAspect) aLineAspect;
195
196     Aspect_TypeOfLine aType = (Aspect_TypeOfLine)anAuxiliaryLineStyle;
197     if (aDrawer->HasOwnLineAspect()) {
198       aLineAspect = aDrawer->LineAspect();
199     }
200     if (aDrawer->HasOwnWireAspect()) {
201       aLineAspect = aDrawer->WireAspect();
202     }
203     Quantity_Color aCurrentColor;
204     Aspect_TypeOfLine aPrevLineType;
205     Standard_Real aCurrentWidth;
206     aLineAspect->Aspect()->Values(aCurrentColor, aPrevLineType, aCurrentWidth);
207     bool isChangedLineType = aType != aPrevLineType;
208     if (isChangedLineType) {
209       aLineAspect->SetTypeOfLine(aType);
210     }
211     // end of set line style
212
213     BRep_Builder aBuilder;
214     TopoDS_Compound aComp;
215     aBuilder.MakeCompound(aComp);
216
217     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
218                                          aLast = anAuxiliaryResults.end();
219     for (; anIt != aLast; anIt++) {
220       ResultPtr aResult = *anIt;
221       if (aResult.get()) {
222         GeomShapePtr aGeomShape = aResult->shape();
223         if (aGeomShape.get()) {
224           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
225           if (!aShape.IsNull())
226             aBuilder.Add(aComp, aShape);
227         }
228       }
229     }
230     myAuxiliaryCompound = aComp;
231     StdPrs_WFDeflectionShape::Add(thePresentation, aComp, aDrawer);
232
233     // restore presentation properties
234     //SetColor(aPrevColor);
235     //setWidth(aDrawer, aPrevWidth);
236     //if (isChangedLineType) {
237     //  aLineAspect->SetTypeOfLine(aPrevLineType);
238     //}
239   }
240 }
241
242 #ifdef DEBUG_WIRE
243 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
244 {
245   TopTools_IndexedMapOfShape aSubShapes;
246   TopExp::MapShapes (theShape, theType, aSubShapes);
247
248   Standard_Boolean isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
249   int anExtent = aSubShapes.Extent();
250   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
251   {
252     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
253     int aValue = 0;
254   }
255 }
256 #endif
257
258 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
259                                             const Standard_Integer aMode)
260 {
261   if (aMode > 8)
262     // In order to avoid using custom selection modes
263     return;
264
265   if (aMode == AIS_Shape::SelectionMode(TopAbs_FACE)) {
266 #ifdef DEBUG_WIRE
267     const TopoDS_Shape& aShape = Shape();
268     debugInfo(aShape, TopAbs_VERTEX); // 24
269     debugInfo(aShape, TopAbs_EDGE); // 12
270     debugInfo(aShape, TopAbs_WIRE); // 0
271     debugInfo(aShape, TopAbs_FACE); // 0
272 #endif
273     BRep_Builder aBuilder;
274     TopoDS_Compound aComp;
275     aBuilder.MakeCompound(aComp);
276     aBuilder.Add(aComp, myOriginalShape);
277     std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
278     for (aIt = myFacesList.cbegin(); aIt != myFacesList.cend(); ++aIt) {
279       TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
280       aBuilder.Add(aComp, aFace);
281       // for sketch presentation in the face mode wires should be selectable also
282       // accoring to #1343 Improvement of Extrusion and Revolution operations
283       appendShapeSelection(aSelection, aFace, TopAbs_WIRE);
284     }
285 #ifdef DEBUG_WIRE
286     debugInfo(aComp, TopAbs_VERTEX); // 24
287     debugInfo(aComp, TopAbs_EDGE); // 12
288     debugInfo(aComp, TopAbs_WIRE); // 4
289     debugInfo(aComp, TopAbs_FACE); // 2
290 #endif
291     Set(aComp);
292   } else
293     Set(myOriginalShape);
294
295   // append auxiliary compound to selection of edges/vertices
296   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
297       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
298
299         bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
300     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
301   }
302
303   AIS_Shape::ComputeSelection(aSelection, aMode);
304 }
305
306 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
307                                                    const TopoDS_Shape& theShape,
308                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
309 {
310   // POP protection against crash in low layers
311   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
312   try {
313     StdSelect_BRepSelectionTool::Load(theSelection,
314                                       this,
315                                       theShape,
316                                       theTypeOfSelection,
317                                       aDeflection,
318                                       myDrawer->HLRAngle(),
319                                       myDrawer->IsAutoTriangulation());
320   } catch ( Standard_Failure ) {
321   }
322 }