Salome HOME
b600306b92cf5f5ed26f09d9dacf6e0ac6473950
[modules/shaper.git] / src / PartSet / PartSet_ResultSketchPrs.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "PartSet_ResultSketchPrs.h"
22 #include "PartSet_Tools.h"
23 #include "ModuleBase_Tools.h"
24
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <GeomAPI_PlanarEdges.h>
30
31 #include <Events_InfoMessage.h>
32 #include <Events_Loop.h>
33
34 #include <SketchPlugin_SketchEntity.h>
35
36 #include <SketcherPrs_Tools.h>
37
38 #include <Config_PropManager.h>
39
40 #include <BRep_Builder.hxx>
41 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d.hxx>
43 #include <Prs3d_PointAspect.hxx>
44 #include <TopoDS_Builder.hxx>
45 #include <SelectMgr_SelectionManager.hxx>
46 #include <StdPrs_WFShape.hxx>
47 #include <StdSelect_BRepSelectionTool.hxx>
48 #include <Graphic3d_AspectLine3d.hxx>
49 #include <Prs3d_LineAspect.hxx>
50 #include <Graphic3d_AspectMarker3d.hxx>
51
52 #define DEBUG_WIRE
53
54 #ifdef DEBUG_WIRE
55 #include <TopTools_IndexedMapOfShape.hxx>
56 #include <TopExp.hxx>
57 #endif
58
59 //*******************************************************************************************
60
61 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
62
63 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
64   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
65 {
66   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
67
68   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
69   Set(aShape);
70   Handle(Prs3d_Drawer) aDrawer = Attributes();
71   if (aDrawer->HasOwnPointAspect())
72     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
73   else
74     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
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(myResult, Attributes());
110   try {
111     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
112   }
113   catch (...) {
114     return;
115   }
116   if (!myAuxiliaryCompound.IsNull()) {
117     setAuxiliaryPresentationStyle(true);
118
119     Handle(Prs3d_Drawer) aDrawer = Attributes();
120     StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
121   }
122
123   if (!aReadyToDisplay) {
124     Events_InfoMessage("PartSet_ResultSketchPrs",
125                        "An empty AIS presentation: PartSet_ResultSketchPrs").send();
126     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
127     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
128   }
129 }
130
131 #ifdef DEBUG_WIRE
132 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
133 {
134   TopTools_IndexedMapOfShape aSubShapes;
135   TopExp::MapShapes (theShape, theType, aSubShapes);
136
137   Standard_Boolean
138     isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
139   int anExtent = aSubShapes.Extent();
140   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
141   {
142     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
143     int aValue = 0;
144   }
145 }
146 #endif
147
148 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
149                                             const Standard_Integer theMode)
150 {
151   int aMode = theMode;
152
153   if (aMode > 8 &&
154       aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
155       aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
156     // In order to avoid using custom selection modes
157     return;
158
159   bool aShapeIsChanged = false;
160   double aPrevDeviation = Attributes()->DeviationCoefficient();
161   if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
162       aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
163     aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
164                                                           : AIS_Shape::SelectionMode(TopAbs_WIRE);
165 #ifdef DEBUG_WIRE
166     const TopoDS_Shape& aShape = Shape();
167     debugInfo(aShape, TopAbs_VERTEX); // 24
168     debugInfo(aShape, TopAbs_EDGE); // 12
169     debugInfo(aShape, TopAbs_WIRE); // 0
170     debugInfo(aShape, TopAbs_FACE); // 0
171 #endif
172     BRep_Builder aBuilder;
173     TopoDS_Compound aComp;
174     aBuilder.MakeCompound(aComp);
175     aBuilder.Add(aComp, myOriginalShape);
176
177     for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
178       const TopoDS_Shape& aFace = anIt.Value();
179       aBuilder.Add(aComp, aFace);
180     }
181 #ifdef DEBUG_WIRE
182     debugInfo(aComp, TopAbs_VERTEX); // 24
183     debugInfo(aComp, TopAbs_EDGE); // 12
184     debugInfo(aComp, TopAbs_WIRE); // 4
185     debugInfo(aComp, TopAbs_FACE); // 2
186 #endif
187     Set(aComp);
188     double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection");
189     Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
190     aShapeIsChanged = true;
191   }
192   else
193     Set(myOriginalShape);
194
195   // append auxiliary compound to selection of edges/vertices
196   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
197       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
198
199     bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
200     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
201   }
202
203   AIS_Shape::ComputeSelection(aSelection, aMode);
204
205   if (aShapeIsChanged) {
206     Attributes()->SetDeviationCoefficient(aPrevDeviation);
207     Set(myOriginalShape);
208   }
209 }
210
211 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
212                                                    const TopoDS_Shape& theShape,
213                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
214 {
215   // POP protection against crash in low layers
216   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
217   try {
218     StdSelect_BRepSelectionTool::Load(theSelection,
219                                       this,
220                                       theShape,
221                                       theTypeOfSelection,
222                                       aDeflection,
223                                       myDrawer->HLRAngle(),
224                                       myDrawer->IsAutoTriangulation());
225   } catch ( Standard_Failure ) {
226   }
227 }
228
229 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
230 {
231   Standard_Integer aLineStyle = isAuxiliary?
232     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
233     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
234
235   Handle(Prs3d_Drawer) aDrawer = Attributes();
236
237   // set line style
238   Handle(Prs3d_LineAspect) aLineAspect;
239
240   Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
241   if (aDrawer->HasOwnLineAspect()) {
242     aLineAspect = aDrawer->LineAspect();
243   }
244   if (aDrawer->HasOwnWireAspect()) {
245     aLineAspect = aDrawer->WireAspect();
246   }
247   Quantity_Color aCurrentColor = aLineAspect->Aspect()->Color();
248   Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
249   Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
250   bool isChangedLineType = aType != aPrevLineType;
251   if (isChangedLineType) {
252     Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
253     aDrawer->SetLineAspect(aAspect);
254     aDrawer->SetWireAspect(aAspect);
255     SetAttributes(aDrawer);
256   }
257 }
258
259 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
260                                          TopoDS_Compound& theAuxiliaryCompound,
261                                          NCollection_List<TopoDS_Shape>& theFaceList)
262 {
263   //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
264   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
265   if (!aShapePtr)
266     return;
267
268   theFaceList.Clear();
269   ResultConstructionPtr aConstruction =
270     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
271   if (aConstruction.get()) {
272     int aFacesNum = aConstruction->facesNum();
273     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
274       std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
275       if (aFaceShape.get()) {
276         TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
277         theFaceList.Append(aFace);
278       }
279     }
280   }
281   theResultShape = aShapePtr->impl<TopoDS_Shape>();
282
283   /// find auxiliary shapes
284   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
285   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
286                                                                           (aResultFeature);
287   std::list<ResultPtr> anAuxiliaryResults;
288   for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
289     FeaturePtr aFeature = aSketchFeature->subFeature(i);
290     if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
291       std::list<ResultPtr> aResults = aFeature->results();
292       std::list<ResultPtr>::const_iterator aIt;
293       for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
294         ResultPtr aResult = *aIt;
295         if (aResult.get() && aResult->shape().get())
296           anAuxiliaryResults.push_back(aResult);
297       }
298     }
299     else if (PartSet_Tools::isIncludeIntoSketchResult(aFeature)) {
300       // Append not-edges shapes, e.g. center of a circle, an arc, a point feature.
301       // Issue #2535: do not show elements produced by Projection or Intersection features,
302       // which should not be included into the sketch result.
303       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
304       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
305       for (; aResIter != aRes.cend(); aResIter++) {
306         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
307             ModelAPI_ResultConstruction>(*aResIter);
308         if (aConstr) {
309           std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
310           if (aGeomShape.get()) {
311             const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
312             if (aShape.ShapeType() != TopAbs_EDGE)
313               anAuxiliaryResults.push_back(aConstr);
314           }
315         }
316       }
317     }
318   }
319
320   if (anAuxiliaryResults.size() > 0) {
321     BRep_Builder aBuilder;
322     //TopoDS_Compound aComp;
323     aBuilder.MakeCompound(theAuxiliaryCompound);
324     std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
325                                          aLast = anAuxiliaryResults.end();
326     for (; anIt != aLast; anIt++) {
327       ResultPtr aResult = *anIt;
328       if (aResult.get()) {
329         GeomShapePtr aGeomShape = aResult->shape();
330         if (aGeomShape.get()) {
331           const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
332           if (!aShape.IsNull())
333             aBuilder.Add(theAuxiliaryCompound, aShape);
334         }
335       }
336     }
337   }
338 }