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