Salome HOME
956e60cd990aed160edfe15d56f1aee473577e1c
[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
75 void PartSet_ResultSketchPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
76                                    const Handle(Prs3d_Presentation)& thePresentation, 
77                                    const Standard_Integer theMode)
78 {
79   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
80   if (!aShapePtr) {
81     Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
82     return;
83   }
84
85   myFacesList.clear();
86   ResultConstructionPtr aConstruction = 
87     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
88   if (aConstruction.get()) {
89     int aFacesNum = aConstruction->facesNum();
90     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
91       myFacesList.push_back(aConstruction->face(aFaceIndex));
92     }
93   }
94
95   myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
96   if (!myOriginalShape.IsNull()) {
97     Set(myOriginalShape);
98
99     // change deviation coefficient to provide more precise circle
100     ModuleBase_Tools::setDefaultDeviationCoefficient(myOriginalShape, Attributes());
101     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
102   }
103   else
104     Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
105
106
107   // create auxiliary shapes
108   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
109   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
110                                                                           (aResultFeature);
111   std::list<ResultPtr> anAuxiliaryResults;
112   for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
113     FeaturePtr aFeature = aSketchFeature->subFeature(i);
114     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
115       std::list<ResultPtr> aResults = aFeature->results();
116       std::list<ResultPtr>::const_iterator aIt;
117       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
118         ResultPtr aResult = *aIt;
119         if (aResult.get() && aResult->shape().get())
120           anAuxiliaryResults.push_back(aResult);
121       }
122     }
123   }
124   if (anAuxiliaryResults.size() > 0) {
125     std::vector<int> aColor;
126     aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color",
127                                          SKETCH_AUXILIARY_COLOR);
128     Standard_Real anAuxiliaryWidth = 1.;
129     Standard_Integer anAuxiliaryLineStyle = 3;
130
131     SetColor(Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB));
132
133     //thePresentation->Clear();
134     Handle(Prs3d_Drawer) aDrawer = Attributes();
135     setWidth(aDrawer, anAuxiliaryWidth);
136     // set line style
137     Handle(Prs3d_LineAspect) aLineAspect;
138
139     Aspect_TypeOfLine aType = (Aspect_TypeOfLine)anAuxiliaryLineStyle;
140     if (aDrawer->HasOwnLineAspect()) {
141       aLineAspect = aDrawer->LineAspect();
142     }
143     if (aDrawer->HasOwnWireAspect()) {
144       aLineAspect = aDrawer->WireAspect();
145     }
146     Quantity_Color aCurrentColor;
147     Aspect_TypeOfLine aCurrentType;
148     Standard_Real aCurrentWidth;
149     aLineAspect->Aspect()->Values(aCurrentColor, aCurrentType, aCurrentWidth);
150     bool isChanged = aType != aCurrentType;
151     if (isChanged) {
152       aLineAspect->SetTypeOfLine(aType);
153     }
154     // end of set line style
155
156     BRep_Builder aBuilder;
157     TopoDS_Compound aComp;
158     aBuilder.MakeCompound(aComp);
159
160     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
161                                          aLast = anAuxiliaryResults.end();
162     for (; anIt != aLast; anIt++) {
163       ResultPtr aResult = *anIt;
164       if (aResult.get()) {
165         GeomShapePtr aGeomShape = aResult->shape();
166         if (aGeomShape.get()) {
167           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
168           if (!aShape.IsNull())
169             aBuilder.Add(aComp, aShape);
170         }
171       }
172     }
173     myAuxiliaryCompound = aComp;
174     StdPrs_WFDeflectionShape::Add(thePresentation, aComp, aDrawer);
175   }
176 }
177
178 #ifdef DEBUG_WIRE
179 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
180 {
181   TopTools_IndexedMapOfShape aSubShapes;
182   TopExp::MapShapes (theShape, theType, aSubShapes);
183
184   Standard_Boolean isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
185   int anExtent = aSubShapes.Extent();
186   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
187   {
188     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
189     int aValue = 0;
190   }
191 }
192 #endif
193
194 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
195                                             const Standard_Integer aMode)
196 {
197   if (aMode > 8)
198     // In order to avoid using custom selection modes
199     return;
200
201   if (aMode == AIS_Shape::SelectionMode(TopAbs_FACE)) {
202 #ifdef DEBUG_WIRE
203     const TopoDS_Shape& aShape = Shape();
204     debugInfo(aShape, TopAbs_VERTEX); // 24
205     debugInfo(aShape, TopAbs_EDGE); // 12
206     debugInfo(aShape, TopAbs_WIRE); // 0
207     debugInfo(aShape, TopAbs_FACE); // 0
208 #endif
209     BRep_Builder aBuilder;
210     TopoDS_Compound aComp;
211     aBuilder.MakeCompound(aComp);
212     aBuilder.Add(aComp, myOriginalShape);
213     std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
214     for (aIt = myFacesList.cbegin(); aIt != myFacesList.cend(); ++aIt) {
215       TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
216       aBuilder.Add(aComp, aFace);
217       // for sketch presentation in the face mode wires should be selectable also
218       // accoring to #1343 Improvement of Extrusion and Revolution operations
219       appendShapeSelection(aSelection, aFace, TopAbs_WIRE);
220     }
221 #ifdef DEBUG_WIRE
222     debugInfo(aComp, TopAbs_VERTEX); // 24
223     debugInfo(aComp, TopAbs_EDGE); // 12
224     debugInfo(aComp, TopAbs_WIRE); // 4
225     debugInfo(aComp, TopAbs_FACE); // 2
226 #endif
227     Set(aComp);
228   } else
229     Set(myOriginalShape);
230
231   // append auxiliary compound to selection of edges/vertices
232   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
233       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
234
235         bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
236     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
237   }
238
239   AIS_Shape::ComputeSelection(aSelection, aMode);
240 }
241
242 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
243                                                    const TopoDS_Shape& theShape,
244                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
245 {
246   // POP protection against crash in low layers
247   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
248   try {
249     StdSelect_BRepSelectionTool::Load(theSelection,
250                                       this,
251                                       theShape,
252                                       theTypeOfSelection,
253                                       aDeflection,
254                                       myDrawer->HLRAngle(),
255                                       myDrawer->IsAutoTriangulation());
256   } catch ( Standard_Failure ) {
257   }
258 }