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