]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FeatureCirclePrs.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <GeomDataAPI_Dir.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_Circ2d.h>
16 #include <GeomAPI_Dir2d.h>
17 #include <GeomAPI_Dir.h>
18
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23
24 #include <V3d_View.hxx>
25 #include <Precision.hxx>
26
27 using namespace std;
28
29 PartSet_FeatureCirclePrs::PartSet_FeatureCirclePrs(FeaturePtr theSketch)
30 : PartSet_FeaturePrs(theSketch)
31 {
32 }
33
34 std::string PartSet_FeatureCirclePrs::getKind()
35 {
36   return SKETCH_CIRCLE_KIND;
37 }
38
39 PartSet_SelectionMode PartSet_FeatureCirclePrs::setPoint(double theX, double theY,
40                                                          const PartSet_SelectionMode& theMode)
41 {
42   PartSet_SelectionMode aMode = theMode;
43   switch (theMode)
44   {
45     case SM_FirstPoint: {
46       PartSet_Tools::setFeaturePoint(feature(), theX, theY, CIRCLE_ATTR_CENTER);
47       aMode = SM_SecondPoint;
48     }
49     break;
50     case SM_SecondPoint: {
51       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
52       boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
53                                                                   (aData->attribute(CIRCLE_ATTR_CENTER));
54       boost::shared_ptr<GeomAPI_Pnt2d> aCoordPoint(new GeomAPI_Pnt2d(theX, theY));
55       double aRadius = aCoordPoint->distance(aPoint->pnt());
56       PartSet_Tools::setFeatureValue(feature(), aRadius, CIRCLE_ATTR_RADIUS);
57
58       aMode = SM_DonePoint;
59    }
60     break;
61     default:
62       break;
63   }
64   return aMode;
65 }
66
67 std::string PartSet_FeatureCirclePrs::getAttribute(const PartSet_SelectionMode& theMode) const
68 {
69   std::string aAttribute;
70   switch (theMode)
71   {
72     case SM_FirstPoint:
73       aAttribute = CIRCLE_ATTR_CENTER;
74     break;
75     case SM_SecondPoint:
76       aAttribute = CIRCLE_ATTR_RADIUS;
77     break;
78     default:
79     break;
80   }
81   return aAttribute;
82 }
83
84 PartSet_SelectionMode PartSet_FeatureCirclePrs::getNextMode(const std::string& theAttribute) const
85 {
86   PartSet_SelectionMode aMode;
87
88   if (theAttribute == CIRCLE_ATTR_CENTER)
89     aMode = SM_SecondPoint;
90   else if (theAttribute == CIRCLE_ATTR_RADIUS)
91     aMode = SM_DonePoint;
92   return aMode;
93 }
94
95 void PartSet_FeatureCirclePrs::move(double theDeltaX, double theDeltaY)
96 {
97   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
98   if (!aData->isValid())
99     return;
100
101   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
102         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
103   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
104 }
105
106 double PartSet_FeatureCirclePrs::distanceToPoint(FeaturePtr theFeature,
107                                                  double theX, double theY)
108 {
109   double aDelta = 0;
110   if (!theFeature || theFeature->getKind() != getKind())
111     return aDelta;
112
113   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
114   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
115         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
116
117   boost::shared_ptr<GeomAPI_Pnt2d> aPoint2d(new GeomAPI_Pnt2d(theX, theY));
118   return aPoint->pnt()->distance(aPoint2d);
119 }
120
121 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::findPoint(FeaturePtr theFeature,
122                                                                            double theX, double theY)
123 {
124   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
125   if (!theFeature || theFeature->getKind() != getKind())
126     return aPoint2D;
127
128   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
129   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
130         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
131   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
132     aPoint2D = aPoint;
133
134   return aPoint2D;
135 }
136
137 void PartSet_FeatureCirclePrs::projectPointOnFeature(FeaturePtr theFeature, FeaturePtr theSketch,
138                                                      gp_Pnt& thePoint, Handle(V3d_View) theView,
139                                                      double& theX, double& theY)
140 {
141   FeaturePtr aSketch = theSketch;
142   if (aSketch) {
143     double aX, anY;
144     PartSet_Tools::convertTo2D(thePoint, aSketch, theView, aX, anY);
145
146     // circle origin point and radius
147     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
148     boost::shared_ptr<GeomDataAPI_Point2D> aCenter = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
149                                                               (aData->attribute(CIRCLE_ATTR_CENTER));
150     bool isValid;
151     double aRadius = PartSet_Tools::featureValue(theFeature, CIRCLE_ATTR_RADIUS, isValid);
152
153     boost::shared_ptr<GeomAPI_Dir2d> aNormal(new GeomAPI_Dir2d(aX, anY));
154     boost::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenter->pnt(), aNormal, aRadius));
155     boost::shared_ptr<GeomAPI_Pnt2d> aGeomPoint2d(new GeomAPI_Pnt2d(aX, anY));
156     boost::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCirc->project(aGeomPoint2d);
157     if (aPnt2d) {
158       theX = aPnt2d->x();
159       theY = aPnt2d->y();
160     }
161   }
162 }
163
164 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::featurePoint
165                                                      (const PartSet_SelectionMode& theMode)
166 {
167   std::string aPointArg;
168   switch (theMode)
169   {
170     case SM_FirstPoint:
171       aPointArg = CIRCLE_ATTR_CENTER;
172       break;
173     default:
174       break;
175   }
176   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
177   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
178                                                               (aData->attribute(aPointArg));
179   return aPoint;
180 }