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