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