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