Salome HOME
Update copyrights
[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
75   // Activate individual repaintng if this is a part of compsolid
76   ResultBodyPtr anOwner = ModelAPI_Tools::bodyOwner(myResult);
77   SetAutoHilight(anOwner.get() == NULL);
78
79   ModuleBase_Tools::setPointBallHighlighting(this);
80 }
81
82 void PartSet_ResultSketchPrs::Compute(
83                       const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
84                       const Handle(Prs3d_Presentation)& thePresentation,
85                       const Standard_Integer theMode)
86 {
87   thePresentation->Clear();
88
89   TopoDS_Shape aResultShape;
90   TopoDS_Compound anAuxiliaryCompound;
91   NCollection_List<TopoDS_Shape> aFaceList;
92   fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
93
94   bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
95
96   if (aReadyToDisplay) {
97     if (!aResultShape.IsNull()) {
98       myOriginalShape = aResultShape;
99       if (!myOriginalShape.IsNull())
100         Set(myOriginalShape);
101     }
102     myAuxiliaryCompound = anAuxiliaryCompound;
103   }
104
105   setAuxiliaryPresentationStyle(false);
106
107   // change deviation coefficient to provide more precise circle
108   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
109   try {
110     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
111   }
112   catch (...) {
113     return;
114   }
115   if (!myAuxiliaryCompound.IsNull()) {
116     setAuxiliaryPresentationStyle(true);
117
118     Handle(Prs3d_Drawer) aDrawer = Attributes();
119     StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
120   }
121
122   if (!aReadyToDisplay) {
123     Events_InfoMessage("PartSet_ResultSketchPrs",
124                        "An empty AIS presentation: PartSet_ResultSketchPrs").send();
125     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
126     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
127   }
128 }
129
130 #ifdef DEBUG_WIRE
131 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
132 {
133   TopTools_IndexedMapOfShape aSubShapes;
134   TopExp::MapShapes (theShape, theType, aSubShapes);
135
136   Standard_Boolean
137     isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
138   int anExtent = aSubShapes.Extent();
139   for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
140   {
141     const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
142     int aValue = 0;
143   }
144 }
145 #endif
146
147 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
148                                             const Standard_Integer theMode)
149 {
150   int aMode = theMode;
151
152   if (aMode > 8 &&
153       aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
154       aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
155     // In order to avoid using custom selection modes
156     return;
157
158   bool aShapeIsChanged = false;
159   double aPrevDeviation = Attributes()->DeviationCoefficient();
160   if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
161       aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
162     aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
163                                                           : AIS_Shape::SelectionMode(TopAbs_WIRE);
164 #ifdef DEBUG_WIRE
165     const TopoDS_Shape& aShape = Shape();
166     debugInfo(aShape, TopAbs_VERTEX); // 24
167     debugInfo(aShape, TopAbs_EDGE); // 12
168     debugInfo(aShape, TopAbs_WIRE); // 0
169     debugInfo(aShape, TopAbs_FACE); // 0
170 #endif
171     BRep_Builder aBuilder;
172     TopoDS_Compound aComp;
173     aBuilder.MakeCompound(aComp);
174     aBuilder.Add(aComp, myOriginalShape);
175
176     for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
177       const TopoDS_Shape& aFace = anIt.Value();
178       aBuilder.Add(aComp, aFace);
179     }
180 #ifdef DEBUG_WIRE
181     debugInfo(aComp, TopAbs_VERTEX); // 24
182     debugInfo(aComp, TopAbs_EDGE); // 12
183     debugInfo(aComp, TopAbs_WIRE); // 4
184     debugInfo(aComp, TopAbs_FACE); // 2
185 #endif
186     Set(aComp);
187     double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection");
188     Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
189     aShapeIsChanged = true;
190   }
191   else
192     Set(myOriginalShape);
193
194   // append auxiliary compound to selection of edges/vertices
195   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
196       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
197
198     bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
199     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
200   }
201
202   AIS_Shape::ComputeSelection(aSelection, aMode);
203
204   if (aShapeIsChanged) {
205     Attributes()->SetDeviationCoefficient(aPrevDeviation);
206     Set(myOriginalShape);
207   }
208 }
209
210 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
211                                                    const TopoDS_Shape& theShape,
212                                                    const TopAbs_ShapeEnum& theTypeOfSelection)
213 {
214   // POP protection against crash in low layers
215   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
216   try {
217     StdSelect_BRepSelectionTool::Load(theSelection,
218                                       this,
219                                       theShape,
220                                       theTypeOfSelection,
221                                       aDeflection,
222                                       myDrawer->HLRAngle(),
223                                       myDrawer->IsAutoTriangulation());
224   } catch ( Standard_Failure ) {
225   }
226 }
227
228 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
229 {
230   Standard_Integer aLineStyle = isAuxiliary?
231     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
232     SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
233
234   Handle(Prs3d_Drawer) aDrawer = Attributes();
235
236   // set line style
237   Handle(Prs3d_LineAspect) aLineAspect;
238
239   Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
240   if (aDrawer->HasOwnLineAspect()) {
241     aLineAspect = aDrawer->LineAspect();
242   }
243   if (aDrawer->HasOwnWireAspect()) {
244     aLineAspect = aDrawer->WireAspect();
245   }
246   Quantity_Color aCurrentColor = aLineAspect->Aspect()->Color();
247   Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
248   Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
249   bool isChangedLineType = aType != aPrevLineType;
250   if (isChangedLineType) {
251     Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
252     aDrawer->SetLineAspect(aAspect);
253     aDrawer->SetWireAspect(aAspect);
254     SetAttributes(aDrawer);
255   }
256 }
257
258 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
259                                          TopoDS_Compound& theAuxiliaryCompound,
260                                          NCollection_List<TopoDS_Shape>& theFaceList)
261 {
262   //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
263   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
264   if (!aShapePtr)
265     return;
266
267   theFaceList.Clear();
268   ResultConstructionPtr aConstruction =
269     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
270   if (aConstruction.get()) {
271     int aFacesNum = aConstruction->facesNum();
272     for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
273       std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
274       if (aFaceShape.get()) {
275         TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
276         theFaceList.Append(aFace);
277       }
278     }
279   }
280   theResultShape = aShapePtr->impl<TopoDS_Shape>();
281
282   /// find auxiliary shapes
283   FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
284   CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
285                                                                           (aResultFeature);
286   std::list<ResultPtr> anAuxiliaryResults;
287   int aNumberOfSubs = aSketchFeature->numberOfSubs();
288   for (int i = 0; i < aNumberOfSubs; 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 }