Salome HOME
BUG: Set empty special symbol in lenght constraint
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Tools.cpp
4 // Created:     10 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Tools.h"
8
9 #include <SketchPlugin_Constraint.h>
10 #include <SketchPlugin_Point.h>
11 #include <SketchPlugin_Circle.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Arc.h>
14
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeDouble.h>
18
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Lin2d.h>
21
22 #include <TopoDS.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <TopoDS_Vertex.hxx>
25
26 #include <BRep_Tool.hxx>
27 #include <Precision.hxx>
28
29 #include <AIS_Dimension.hxx>
30
31 // it is not possible to use 0x2211 as summ symbol because it is not supported by
32 // debian Linux platform
33 static const Standard_ExtCharacter MyEmptySymbol(' ');
34 static const Standard_ExtCharacter MySigmaSymbol(0x03A3);
35
36 namespace SketcherPrs_Tools {
37
38 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
39 {
40   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
41   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
42   return anAttr->object();
43 }
44
45
46 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
47 {
48   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
49   if (aRes.get() != NULL) {
50     return aRes->shape();
51   }
52   return std::shared_ptr<GeomAPI_Shape>();
53 }
54
55
56 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
57                                         const std::string& theAttribute)
58 {
59   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
60
61   if (!theFeature->data())
62     return std::shared_ptr<GeomAPI_Pnt2d>();
63
64   FeaturePtr aFeature;
65   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
66       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
67   if(!anAttr.get()) {
68     return std::shared_ptr<GeomAPI_Pnt2d>();
69   }
70   aFeature = ModelAPI_Feature::feature(anAttr->object());
71
72   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
73     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
74         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
75
76   else if (anAttr->attr()) {
77     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
78   }
79   if (aPointAttr.get() != NULL)
80     return aPointAttr->pnt();
81   return std::shared_ptr<GeomAPI_Pnt2d>();
82 }
83
84 //*************************************************************************************
85 /// Find an attribute of the given object which corresponds to a vetrex 
86 /// defined by a shape
87 /// \param theObject an object
88 /// \param theShape a vertex
89 /// \param thePlane a projection plane (sketcher plane)
90 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject, 
91                                     const TopoDS_Shape& theShape, 
92                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
93 {
94   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
95
96   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
97   if (anObjectFeature) {
98     if (theShape.ShapeType() == TopAbs_VERTEX) {
99       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
100       if (!aShapeVertex.IsNull())  {
101         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
102         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
103             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
104
105         // find the given point in the feature attributes
106         std::list<AttributePtr> anObjectAttiributes = 
107           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
108         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(), 
109                                                 aLast = anObjectAttiributes.end();
110         for (; anIt != aLast && !aGeomPoint; anIt++) {
111           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
112             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
113
114           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
115                                                                        anAttributePoint->y());
116           if (anAttributePnt.get() &&
117               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
118             aGeomPoint = anAttributePoint;
119             break;
120           }
121         }
122       }
123     }
124   }
125   return aGeomPoint;
126 }
127
128 //*************************************************************************************
129 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
130                                                      const std::string& theAttribute,
131                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
132 {
133   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
134
135   if (!theData)
136     return aPointAttr;
137
138   FeaturePtr aFeature;
139   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
140       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
141   if (anAttr) {
142     if (anAttr->isObject()) {
143       ObjectPtr anObject = anAttr->object();
144       aFeature = ModelAPI_Feature::feature(anObject);
145       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
146         // Attribute refers to a point
147         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
149       }
150       else {
151         // if the attribute refers on another object
152         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
153         if (aRes.get()) {
154           GeomShapePtr aShape = aRes->shape();
155           if (aShape.get()) {
156             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
157             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
158           }
159         }
160       }
161     }
162     else if (anAttr->attr()) {
163       // If attribute is a point
164       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
165     }
166   }
167   return aPointAttr;
168 }
169
170 //*************************************************************************************
171 FeaturePtr getFeatureLine(DataPtr theData,
172                           const std::string& theAttribute)
173 {
174   FeaturePtr aLine;
175   if (!theData)
176     return aLine;
177
178   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
179     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
180   if (anAttr) {
181     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
182     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
183       return aFeature;
184     }
185   }
186   return aLine;
187 }
188
189 //*************************************************************************************
190 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
191                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
192 {
193   DataPtr aData = theLine->data();
194   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195       aData->attribute(SketchPlugin_Line::START_ID()));
196   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197       aData->attribute(SketchPlugin_Line::END_ID()));
198
199   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
200   return aLin2d.project(thePoint);
201 }
202
203
204 static double MyArrowSize = 20;
205 double getArrowSize()
206 {
207   return MyArrowSize;
208 }
209
210 void setArrowSize(double theSize)
211 {
212   MyArrowSize = theSize;
213 }
214
215 int getDefaultArrowSize()
216 {
217   return 20;
218 }
219
220 static double MyTextHeight = 16;
221 double getTextHeight()
222 {
223   return MyTextHeight;
224 }
225
226 void setTextHeight(double theHeight)
227 {
228   MyTextHeight = theHeight;
229 }
230
231 double getDefaultTextHeight()
232 {
233   return 16;
234 }
235
236 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
237 {
238   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
239       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
240       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
241
242   return aFlyoutPoint->y();
243 }
244
245 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
246                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
247 {
248   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
249   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
250       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
251   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
252     return std::shared_ptr<GeomAPI_Pnt>();
253
254   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
255   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
256   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
257     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
258         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
259   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
260     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
261         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
262   } else 
263     return std::shared_ptr<GeomAPI_Pnt>();
264
265   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
266   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
267       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
268   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
269       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
270   double aLen = aFlyoutPnt->distance(anOrigin);
271   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
272   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
273   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
274   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
275 }
276
277 void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
278 {
279   if (theToDisplay) {
280     theDimension->SetSpecialSymbol(MySigmaSymbol);
281     theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
282   }
283   else {
284     theDimension->SetSpecialSymbol(MyEmptySymbol);
285     theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
286   }
287 }
288
289 };