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