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