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