Salome HOME
According to "operation-widget_factory-property"
[modules/shaper.git] / src / PartSet / PartSet_FeatureCirclePrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     04 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureCirclePrs.h>
6 #include <PartSet_Tools.h>
7
8 #include <SketchPlugin_Feature.h>
9 #include <SketchPlugin_Sketch.h>
10 #include <SketchPlugin_Circle.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomAPI_Pnt2d.h>
14
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeRefList.h>
19
20 #include <Precision.hxx>
21
22 using namespace std;
23
24 PartSet_FeatureCirclePrs::PartSet_FeatureCirclePrs(FeaturePtr theSketch)
25 : PartSet_FeaturePrs(theSketch)
26 {
27 }
28
29 std::string PartSet_FeatureCirclePrs::getKind()
30 {
31   return SKETCH_CIRCLE_KIND;
32 }
33
34 PartSet_SelectionMode PartSet_FeatureCirclePrs::setPoint(double theX, double theY,
35                                                          const PartSet_SelectionMode& theMode)
36 {
37   PartSet_SelectionMode aMode = theMode;
38   switch (theMode)
39   {
40     case SM_FirstPoint: {
41       PartSet_Tools::setFeaturePoint(feature(), theX, theY, CIRCLE_ATTR_CENTER);
42       aMode = SM_SecondPoint;
43     }
44     break;
45     case SM_SecondPoint: {
46       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
47       boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
48                                                                   (aData->attribute(CIRCLE_ATTR_CENTER));
49       boost::shared_ptr<GeomAPI_Pnt2d> aCoordPoint(new GeomAPI_Pnt2d(theX, theY));
50       double aRadius = aCoordPoint->distance(aPoint->pnt());
51       PartSet_Tools::setFeatureValue(feature(), aRadius, CIRCLE_ATTR_RADIUS);
52
53       aMode = SM_DonePoint;
54    }
55     break;
56     default:
57       break;
58   }
59   return aMode;
60 }
61
62 std::string PartSet_FeatureCirclePrs::getAttribute(const PartSet_SelectionMode& theMode) const
63 {
64   std::string aAttribute;
65   switch (theMode)
66   {
67     case SM_FirstPoint:
68       aAttribute = CIRCLE_ATTR_CENTER;
69     break;
70     case SM_SecondPoint:
71       aAttribute = CIRCLE_ATTR_RADIUS;
72     break;
73     default:
74     break;
75   }
76   return aAttribute;
77 }
78
79 PartSet_SelectionMode PartSet_FeatureCirclePrs::getNextMode(const std::string& theAttribute) const
80 {
81   PartSet_SelectionMode aMode;
82
83   if (theAttribute == CIRCLE_ATTR_CENTER)
84     aMode = SM_SecondPoint;
85   else if (theAttribute == CIRCLE_ATTR_RADIUS)
86     aMode = SM_DonePoint;
87   return aMode;
88 }
89
90 void PartSet_FeatureCirclePrs::move(double theDeltaX, double theDeltaY)
91 {
92   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
93   if (!aData->isValid())
94     return;
95
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
97         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
98   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
99 }
100
101 double PartSet_FeatureCirclePrs::distanceToPoint(FeaturePtr theFeature,
102                                                  double theX, double theY)
103 {
104   double aDelta = 0;
105   if (!theFeature || theFeature->getKind() != getKind())
106     return aDelta;
107
108   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
109   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
110         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
111
112   boost::shared_ptr<GeomAPI_Pnt2d> aPoint2d(new GeomAPI_Pnt2d(theX, theY));
113   return aPoint->pnt()->distance(aPoint2d);
114 }
115
116 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::findPoint(FeaturePtr theFeature,
117                                                                            double theX, double theY)
118 {
119   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
120   if (!theFeature || theFeature->getKind() != getKind())
121     return aPoint2D;
122
123   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
124   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
125         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
126   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
127     aPoint2D = aPoint;
128
129   return aPoint2D;
130 }
131
132 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::featurePoint
133                                                      (const PartSet_SelectionMode& theMode)
134 {
135   std::string aPointArg;
136   switch (theMode)
137   {
138     case SM_FirstPoint:
139       aPointArg = CIRCLE_ATTR_CENTER;
140       break;
141     default:
142       break;
143   }
144   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
145   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
146                                                               (aData->attribute(aPointArg));
147   return aPoint;
148 }