Salome HOME
Get rid of compilation warnings. Part III. Eliminate rest of warnings.
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Tools.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 "SketcherPrs_Tools.h"
21
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Arc.h>
27
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_Events.h>
33
34 #include <ModelGeomAlgo_Point2D.h>
35
36 #include <Events_InfoMessage.h>
37
38 #include <GeomDataAPI_Point2D.h>
39 #include <GeomAPI_Lin2d.h>
40
41 #include <TopoDS.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
44
45 #include <Prs3d_DimensionAspect.hxx>
46
47 #include <BRep_Tool.hxx>
48 #include <Precision.hxx>
49
50 #include <AIS_Dimension.hxx>
51
52 namespace SketcherPrs_Tools {
53
54 static ParameterStyle MyStyle = ParameterValue;
55
56 void setParameterStyle(ParameterStyle theStyle)
57 {
58   MyStyle = theStyle;
59 }
60
61 ParameterStyle parameterStyle()
62 {
63   return MyStyle;
64 }
65
66 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
67 {
68   AttributePtr anAttribute;
69   if (theFeature) {
70     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
71     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
72       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
73       if (!anAttr->isObject())
74         anAttribute = anAttr->attr();
75     }
76   }
77   return anAttribute;
78 }
79
80 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
81 {
82   ObjectPtr anObject;
83   if (theFeature) {
84     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
85     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
86       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
87       if (anAttr.get())
88         anObject = anAttr->object();
89     }
90   }
91   return anObject;
92 }
93
94
95 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
96 {
97   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
98   if (aRes.get() != NULL && aRes->data()->isValid()) {
99     /// essential check as it is called in openGl thread
100     return aRes->shape();
101   }
102   return std::shared_ptr<GeomAPI_Shape>();
103 }
104
105
106 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
107                                         const std::string& theAttribute)
108 {
109   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
110     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theAttribute));
111   if (!aPointAttr.get() || !aPointAttr->isInitialized()) {
112     aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
113                theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
114   }
115   if (aPointAttr.get() != NULL)
116     return aPointAttr->pnt();
117   return std::shared_ptr<GeomAPI_Pnt2d>();
118 }
119
120 //*************************************************************************************
121 /// Find an attribute of the given object which corresponds to a vetrex
122 /// defined by a shape
123 /// \param theObject an object
124 /// \param theShape a vertex
125 /// \param thePlane a projection plane (sketcher plane)
126 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
127                                     const TopoDS_Shape& theShape,
128                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
129 {
130   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
131
132   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
133   if (anObjectFeature) {
134     if (theShape.ShapeType() == TopAbs_VERTEX) {
135       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
136       if (!aShapeVertex.IsNull())  {
137         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
138         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
139             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
140
141         // find the given point in the feature attributes
142         std::list<AttributePtr> anObjectAttiributes =
143           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
144         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
145                                                 aLast = anObjectAttiributes.end();
146         for (; anIt != aLast && !aGeomPoint; anIt++) {
147           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
148             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
149
150           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
151                                                                        anAttributePoint->y());
152           if (anAttributePnt.get() &&
153               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
154             aGeomPoint = anAttributePoint;
155             break;
156           }
157         }
158       }
159     }
160   }
161   return aGeomPoint;
162 }
163
164 //*************************************************************************************
165 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
166                                                      const std::string& theAttribute,
167                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
168 {
169   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
170
171   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
172     return aPointAttr;
173
174   FeaturePtr aFeature;
175   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
176       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
177   if (anAttr) {
178     if (anAttr->isObject()) {
179       ObjectPtr anObject = anAttr->object();
180       aFeature = ModelAPI_Feature::feature(anObject);
181       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
182         // Attribute refers to a point
183         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
185       }
186       else {
187         // if the attribute refers on another object
188         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
189         if (aRes.get()) {
190           GeomShapePtr aShape = aRes->shape();
191           if (aShape.get()) {
192             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
193             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
194           }
195         }
196       }
197     }
198     else if (anAttr->attr()) {
199       // If attribute is a point
200       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
201     }
202   }
203   return aPointAttr;
204 }
205
206 //*************************************************************************************
207 std::list<ResultPtr> getFreePoints(const CompositeFeaturePtr& theSketch)
208 {
209   std::list<ResultPtr> aFreePoints;
210   if (!theSketch)
211     return aFreePoints;
212
213   AttributeRefListPtr aFeatures = theSketch->reflist(SketchPlugin_Sketch::FEATURES_ID());
214   if (!aFeatures)
215     return aFreePoints;
216   std::list<ObjectPtr> anObjects = aFeatures->list();
217   for (std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
218        anObjIt != anObjects.end(); ++anObjIt) {
219     FeaturePtr aCurrent = ModelAPI_Feature::feature(*anObjIt);
220     if (aCurrent && aCurrent->getKind() == SketchPlugin_Point::ID()) {
221       // check point is not referred by any constraints: the feature and result of point
222       bool aIsFree = true;
223       for(int aKind = 0; aIsFree && aKind < 2; aKind++) { // 0 for feature, 1 for result
224         ObjectPtr aReferenced = aCurrent;
225         if (aKind == 1) {
226           if (!aCurrent->results().empty())
227             aReferenced = aCurrent->firstResult();
228           else
229             break;
230         }
231         const std::set<AttributePtr>& aRefs = aReferenced->data()->refsToMe();
232         std::set<AttributePtr>::iterator aRIt = aRefs.begin();
233         for (; aRIt != aRefs.end(); ++aRIt) {
234           FeaturePtr aRefFeat = ModelAPI_Feature::feature((*aRIt)->owner());
235           std::shared_ptr<SketchPlugin_Constraint> aRefConstr =
236               std::dynamic_pointer_cast<SketchPlugin_Constraint>(aRefFeat);
237           if (aRefConstr) {
238             aIsFree = false;
239             break;
240           }
241         }
242       }
243       if (aIsFree)
244         aFreePoints.push_back(aCurrent->lastResult());
245     }
246   }
247   return aFreePoints;
248 }
249
250 //*************************************************************************************
251 FeaturePtr getFeatureLine(DataPtr theData,
252                           const std::string& theAttribute)
253 {
254   FeaturePtr aLine;
255   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
256     return aLine;
257
258   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
259     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
260   if (anAttr) {
261     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
262     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
263       return aFeature;
264     }
265   }
266   return aLine;
267 }
268
269 //*************************************************************************************
270 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
271                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
272 {
273   DataPtr aData = theLine->data();
274   if (!aData.get() || !aData->isValid())
275     return std::shared_ptr<GeomAPI_Pnt2d>();
276
277   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
278       aData->attribute(SketchPlugin_Line::START_ID()));
279   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
280       aData->attribute(SketchPlugin_Line::END_ID()));
281
282   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
283   return aLin2d.project(thePoint);
284 }
285
286 static int MyPixelRatio = 1;
287
288 void setPixelRatio(int theRatio)
289 {
290   MyPixelRatio = theRatio;
291 }
292
293 int pixelRatio()
294 {
295   return MyPixelRatio;
296 }
297
298 static double MyArrowSize = 20;
299
300 double getArrowSize()
301 {
302   return MyArrowSize;
303 }
304
305 void setArrowSize(double theSize)
306 {
307   MyArrowSize = theSize;
308 }
309
310 int getDefaultArrowSize()
311 {
312   return 20;
313 }
314
315 int getConfigArrowSize()
316 {
317   return Config_PropManager::integer("Visualization", "dimension_arrow_size");
318 }
319
320 static double MyTextHeight = 16;
321 double getTextHeight()
322 {
323   return MyTextHeight * MyPixelRatio;
324 }
325
326 void setTextHeight(double theHeight)
327 {
328   MyTextHeight = theHeight;
329 }
330
331 double getDefaultTextHeight()
332 {
333   return 16 * MyPixelRatio;
334 }
335
336 double getConfigTextHeight()
337 {
338   return Config_PropManager::integer("Visualization", "dimension_value_size") * MyPixelRatio;
339 }
340
341 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
342 {
343   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
344       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
345       const_cast<ModelAPI_Feature*>(
346       theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
347   // for not initialized values return zero distance to avoid Presentation crash
348   if (!aFlyoutPoint->isInitialized())
349     return 0;
350   return aFlyoutPoint->y();
351 }
352
353 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
354                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
355 {
356   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
357   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
358       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
359   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
360     return std::shared_ptr<GeomAPI_Pnt>();
361
362   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
363   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
364   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
365     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
366         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
367   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
368     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
369         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
370   } else
371     return std::shared_ptr<GeomAPI_Pnt>();
372
373   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
374   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
375       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
376   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
377       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
378   double aLen = aFlyoutPnt->distance(anOrigin);
379   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
380   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
381   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
382   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
383 }
384
385 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
386 {
387   Events_InfoMessage("SketcherPrs_Tools",
388     "An empty AIS presentation: SketcherPrs_LengthDimension").send();
389   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
390   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
391   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
392 }
393 };