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