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