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