]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_ResultSketchPrs.cpp
Salome HOME
Issue #2999: Provide initialization of deviation coefficient for all presentations
[modules/shaper.git] / src / PartSet / PartSet_ResultSketchPrs.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_ResultSketchPrs.h"
21 #include "PartSet_Tools.h"
22 #include "ModuleBase_Tools.h"
23
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <GeomAPI_PlanarEdges.h>
29
30 #include <Events_InfoMessage.h>
31 #include <Events_Loop.h>
32
33 #include <SketchPlugin_SketchEntity.h>
34
35 #include <SketcherPrs_Tools.h>
36
37 #include <Config_PropManager.h>
38
39 #include <BRep_Builder.hxx>
40 #include <Prs3d_Drawer.hxx>
41 #include <Prs3d.hxx>
42 #include <Prs3d_PointAspect.hxx>
43 #include <TopoDS_Builder.hxx>
44 #include <SelectMgr_SelectionManager.hxx>
45 #include <StdPrs_WFShape.hxx>
46 #include <StdSelect_BRepSelectionTool.hxx>
47 #include <Graphic3d_AspectLine3d.hxx>
48 #include <Prs3d_LineAspect.hxx>
49 #include <Graphic3d_AspectMarker3d.hxx>
50
51 #define DEBUG_WIRE
52
53 #ifdef DEBUG_WIRE
54 #include <TopTools_IndexedMapOfShape.hxx>
55 #include <TopExp.hxx>
56 #endif
57
58 //*******************************************************************************************
59
60 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
61
62 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
63   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
64 {
65   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
66
67   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
68   Set(aShape);
69   Handle(Prs3d_Drawer) aDrawer = Attributes();
70   if (aDrawer->HasOwnPointAspect())
71     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
72   else
73     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
74   //aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
75
76   // Activate individual repaintng if this is a part of compsolid
77   ResultBodyPtr anOwner = ModelAPI_Tools::bodyOwner(myResult);
78   SetAutoHilight(anOwner.get() == NULL);
79
80   ModuleBase_Tools::setPointBallHighlighting(this);
81 }
82
83 void PartSet_ResultSketchPrs::Compute(
84                       const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85                       const Handle(Prs3d_Presentation)& thePresentation,
86                       const Standard_Integer theMode)
87 {
88   thePresentation->Clear();
89
90   TopoDS_Shape aResultShape;
91   TopoDS_Compound anAuxiliaryCompound;
92   NCollection_List<TopoDS_Shape> aFaceList;
93   fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
94
95   bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
96
97   if (aReadyToDisplay) {
98     if (!aResultShape.IsNull()) {
99       myOriginalShape = aResultShape;
100       if (!myOriginalShape.IsNull())
101         Set(myOriginalShape);
102     }
103     myAuxiliaryCompound = anAuxiliaryCompound;
104   }
105
106   setAuxiliaryPresentationStyle(false);
107
108   // change deviation coefficient to provide more precise circle
109   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
110   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
111   try {
112     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
113   }
114   catch (...) {
115     return;
116   }
117   if (!myAuxiliaryCompound.IsNull()) {
118     setAuxiliaryPresentationStyle(true);
119
120     Handle(Prs3d_Drawer) aDrawer = Attributes();
121     StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
122   }
123
124   if (!aReadyToDisplay) {
125     Events_InfoMessage("PartSet_ResultSketchPrs",
126                        "An empty AIS presentation: PartSet_ResultSketchPrs").send();
127     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
128     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
129   }
130 }
131
132 #ifdef DEBUG_WIRE
133 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
134 {
135   TopTools_IndexedMapOfShape aSubShapes;
136   TopExp::MapShapes (theShape, theType, aSubShapes);
137
138   Standard_Boolean
139     isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
140   int anExtent = aSubShapes.Extent();
141   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
142   {
143     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
144     int aValue = 0;
145   }
146 }
147 #endif
148
149 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
150                                             const Standard_Integer theMode)
151 {
152   int aMode = theMode;
153
154   if (aMode > 8 &&
155       aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
156       aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
157     // In order to avoid using custom selection modes
158     return;
159
160   bool aShapeIsChanged = false;
161   double aPrevDeviation = Attributes()->DeviationCoefficient();
162   if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
163       aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
164     aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
165                                                           : AIS_Shape::SelectionMode(TopAbs_WIRE);
166 #ifdef DEBUG_WIRE
167     const TopoDS_Shape& aShape = Shape();
168     debugInfo(aShape, TopAbs_VERTEX); // 24
169     debugInfo(aShape, TopAbs_EDGE); // 12
170     debugInfo(aShape, TopAbs_WIRE); // 0
171     debugInfo(aShape, TopAbs_FACE); // 0
172 #endif
173     BRep_Builder aBuilder;
174     TopoDS_Compound aComp;
175     aBuilder.MakeCompound(aComp);
176     aBuilder.Add(aComp, myOriginalShape);
177
178     for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
179       const TopoDS_Shape& aFace = anIt.Value();
180       aBuilder.Add(aComp, aFace);
181     }
182 #ifdef DEBUG_WIRE
183     debugInfo(aComp, TopAbs_VERTEX); // 24
184     debugInfo(aComp, TopAbs_EDGE); // 12
185     debugInfo(aComp, TopAbs_WIRE); // 4
186     debugInfo(aComp, TopAbs_FACE); // 2
187 #endif
188     Set(aComp);
189     double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection");
190     Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
191     aShapeIsChanged = true;
192   }
193   else
194     Set(myOriginalShape);
195
196   // append auxiliary compound to selection of edges/vertices
197   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
198       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
199
200     bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
201     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
202   }
203
204   AIS_Shape::ComputeSelection(aSelection, aMode);
205
206   if (aShapeIsChanged) {
207     Attributes()->SetDeviationCoefficient(aPrevDeviation);
208     Set(myOriginalShape);
209   }
210 }
211
212 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
213                                                    const TopoDS_Shape& theShape,
214                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
215 {
216   // POP protection against crash in low layers
217   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
218   try {
219     StdSelect_BRepSelectionTool::Load(theSelection,
220                                       this,
221                                       theShape,
222                                       theTypeOfSelection,
223                                       aDeflection,
224                                       myDrawer->HLRAngle(),
225                                       myDrawer->IsAutoTriangulation());
226   } catch ( Standard_Failure ) {
227   }
228 }
229
230 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
231 {
232   Standard_Integer aLineStyle = isAuxiliary?
233     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
234     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
235
236   Handle(Prs3d_Drawer) aDrawer = Attributes();
237
238   // set line style
239   Handle(Prs3d_LineAspect) aLineAspect;
240
241   Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
242   if (aDrawer->HasOwnLineAspect()) {
243     aLineAspect = aDrawer->LineAspect();
244   }
245   if (aDrawer->HasOwnWireAspect()) {
246     aLineAspect = aDrawer->WireAspect();
247   }
248   Quantity_Color aCurrentColor = aLineAspect->Aspect()->Color();
249   Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
250   Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
251   bool isChangedLineType = aType != aPrevLineType;
252   if (isChangedLineType) {
253     Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
254     aDrawer->SetLineAspect(aAspect);
255     aDrawer->SetWireAspect(aAspect);
256     SetAttributes(aDrawer);
257   }
258 }
259
260 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
261                                          TopoDS_Compound& theAuxiliaryCompound,
262                                          NCollection_List<TopoDS_Shape>& theFaceList)
263 {
264   //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
265   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
266   if (!aShapePtr)
267     return;
268
269   theFaceList.Clear();
270   ResultConstructionPtr aConstruction =
271     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
272   if (aConstruction.get()) {
273     int aFacesNum = aConstruction->facesNum();
274     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
275       std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
276       if (aFaceShape.get()) {
277         TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
278         theFaceList.Append(aFace);
279       }
280     }
281   }
282   theResultShape = aShapePtr->impl<TopoDS_Shape>();
283
284   /// find auxiliary shapes
285   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
286   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
287                                                                           (aResultFeature);
288   std::list<ResultPtr> anAuxiliaryResults;
289   int aNumberOfSubs = aSketchFeature->numberOfSubs();
290   for (int i = 0; i < aNumberOfSubs; i++) {
291     FeaturePtr aFeature = aSketchFeature->subFeature(i);
292     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
293       std::list<ResultPtr> aResults = aFeature->results();
294       std::list<ResultPtr>::const_iterator aIt;
295       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
296         ResultPtr aResult = *aIt;
297         if (aResult.get() && aResult->shape().get())
298           anAuxiliaryResults.push_back(aResult);
299       }
300     }
301     else if (PartSet_Tools::isIncludeIntoSketchResult(aFeature)) {
302       // Append not-edges shapes, e.g. center of a circle, an arc, a point feature.
303       // Issue #2535: do not show elements produced by Projection or Intersection features,
304       // which should not be included into the sketch result.
305       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
306       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
307       for (; aResIter != aRes.cend(); aResIter++) {
308         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
309             ModelAPI_ResultConstruction>(*aResIter);
310         if (aConstr) {
311           std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
312           if (aGeomShape.get()) {
313             const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
314             if (aShape.ShapeType() != TopAbs_EDGE)
315               anAuxiliaryResults.push_back(aConstr);
316           }
317         }
318       }
319     }
320   }
321
322   if (anAuxiliaryResults.size() > 0) {
323     BRep_Builder aBuilder;
324     //TopoDS_Compound aComp;
325     aBuilder.MakeCompound(theAuxiliaryCompound);
326     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
327                                          aLast = anAuxiliaryResults.end();
328     for (; anIt != aLast; anIt++) {
329       ResultPtr aResult = *anIt;
330       if (aResult.get()) {
331         GeomShapePtr aGeomShape = aResult->shape();
332         if (aGeomShape.get()) {
333           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
334           if (!aShape.IsNull())
335             aBuilder.Add(theAuxiliaryCompound, aShape);
336         }
337       }
338     }
339   }
340 }