Salome HOME
Merge branch 'master' of newgeom:newgeom
[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::projectPointOnFeature(FeaturePtr theFeature, FeaturePtr theSketch,
96                                                      gp_Pnt& thePoint, Handle(V3d_View) theView,
97                                                      double& theX, double& theY)
98 {
99   FeaturePtr aSketch = theSketch;
100   if (aSketch) {
101     double aX, anY;
102     PartSet_Tools::convertTo2D(thePoint, aSketch, theView, aX, anY);
103
104     // circle origin point and radius
105     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
106     boost::shared_ptr<GeomDataAPI_Point2D> aCenter = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
107                                                               (aData->attribute(CIRCLE_ATTR_CENTER));
108     bool isValid;
109     double aRadius = PartSet_Tools::featureValue(theFeature, CIRCLE_ATTR_RADIUS, isValid);
110
111     boost::shared_ptr<GeomAPI_Dir2d> aNormal(new GeomAPI_Dir2d(aX, anY));
112     boost::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenter->pnt(), aNormal, aRadius));
113     boost::shared_ptr<GeomAPI_Pnt2d> aGeomPoint2d(new GeomAPI_Pnt2d(aX, anY));
114     boost::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCirc->project(aGeomPoint2d);
115     if (aPnt2d) {
116       theX = aPnt2d->x();
117       theY = aPnt2d->y();
118     }
119   }
120 }
121
122 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::featurePoint
123                                                      (const PartSet_SelectionMode& theMode)
124 {
125   std::string aPointArg;
126   switch (theMode)
127   {
128     case SM_FirstPoint:
129       aPointArg = CIRCLE_ATTR_CENTER;
130       break;
131     default:
132       break;
133   }
134   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
135   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
136                                                               (aData->attribute(aPointArg));
137   return aPoint;
138 }