Salome HOME
Copyright update 2020
[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         const std::set<AttributePtr>& aRefs = aReferenced->data()->refsToMe();
231         std::set<AttributePtr>::iterator aRIt = aRefs.begin();
232         for (; aRIt != aRefs.end(); ++aRIt) {
233           FeaturePtr aRefFeat = ModelAPI_Feature::feature((*aRIt)->owner());
234           std::shared_ptr<SketchPlugin_Constraint> aRefConstr =
235               std::dynamic_pointer_cast<SketchPlugin_Constraint>(aRefFeat);
236           if (aRefConstr) {
237             aIsFree = false;
238             break;
239           }
240         }
241       }
242       if (aIsFree)
243         aFreePoints.push_back(aCurrent->lastResult());
244     }
245   }
246   return aFreePoints;
247 }
248
249 //*************************************************************************************
250 FeaturePtr getFeatureLine(DataPtr theData,
251                           const std::string& theAttribute)
252 {
253   FeaturePtr aLine;
254   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
255     return aLine;
256
257   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
258     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
259   if (anAttr) {
260     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
261     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
262       return aFeature;
263     }
264   }
265   return aLine;
266 }
267
268 //*************************************************************************************
269 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
270                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
271 {
272   DataPtr aData = theLine->data();
273   if (!aData.get() || !aData->isValid())
274     return std::shared_ptr<GeomAPI_Pnt2d>();
275
276   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
277       aData->attribute(SketchPlugin_Line::START_ID()));
278   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
279       aData->attribute(SketchPlugin_Line::END_ID()));
280
281   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
282   return aLin2d.project(thePoint);
283 }
284
285 static int MyPixelRatio = 1;
286
287 void setPixelRatio(int theRatio)
288 {
289   MyPixelRatio = theRatio;
290 }
291
292 int pixelRatio()
293 {
294   return MyPixelRatio;
295 }
296
297 static double MyArrowSize = 20;
298
299 double getArrowSize()
300 {
301   return MyArrowSize;
302 }
303
304 void setArrowSize(double theSize)
305 {
306   MyArrowSize = theSize;
307 }
308
309 int getDefaultArrowSize()
310 {
311   return 20;
312 }
313
314 int getConfigArrowSize()
315 {
316   return Config_PropManager::integer("Visualization", "dimension_arrow_size");
317 }
318
319 static double MyTextHeight = 16;
320 double getTextHeight()
321 {
322   return MyTextHeight * MyPixelRatio;
323 }
324
325 void setTextHeight(double theHeight)
326 {
327   MyTextHeight = theHeight;
328 }
329
330 double getDefaultTextHeight()
331 {
332   return 16 * MyPixelRatio;
333 }
334
335 double getConfigTextHeight()
336 {
337   return Config_PropManager::integer("Visualization", "dimension_value_size") * MyPixelRatio;
338 }
339
340 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
341 {
342   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
343       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
344       const_cast<ModelAPI_Feature*>(
345       theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
346   // for not initialized values return zero distance to avoid Presentation crash
347   if (!aFlyoutPoint->isInitialized())
348     return 0;
349   return aFlyoutPoint->y();
350 }
351
352 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
353                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
354 {
355   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
356   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
357       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
358   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
359     return std::shared_ptr<GeomAPI_Pnt>();
360
361   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
362   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
363   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
364     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
365         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
366   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
367     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
368         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
369   } else
370     return std::shared_ptr<GeomAPI_Pnt>();
371
372   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
373   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
374       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
375   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
376       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
377   double aLen = aFlyoutPnt->distance(anOrigin);
378   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
379   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
380   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
381   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
382 }
383
384 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
385 {
386   Events_InfoMessage("SketcherPrs_Tools",
387     "An empty AIS presentation: SketcherPrs_LengthDimension").send();
388   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
389   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
390   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
391 }
392 };