]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FeatureCirclePrs.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::findPoint(FeaturePtr theFeature,
96                                                                            double theX, double theY)
97 {
98   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
99   if (!theFeature || theFeature->getKind() != getKind())
100     return aPoint2D;
101
102   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
103   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
104         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
105   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
106     aPoint2D = aPoint;
107
108   return aPoint2D;
109 }
110
111 void PartSet_FeatureCirclePrs::projectPointOnFeature(FeaturePtr theFeature, FeaturePtr theSketch,
112                                                      gp_Pnt& thePoint, Handle(V3d_View) theView,
113                                                      double& theX, double& theY)
114 {
115   FeaturePtr aSketch = theSketch;
116   if (aSketch) {
117     double aX, anY;
118     PartSet_Tools::convertTo2D(thePoint, aSketch, theView, aX, anY);
119
120     // circle origin point and radius
121     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
122     boost::shared_ptr<GeomDataAPI_Point2D> aCenter = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
123                                                               (aData->attribute(CIRCLE_ATTR_CENTER));
124     bool isValid;
125     double aRadius = PartSet_Tools::featureValue(theFeature, CIRCLE_ATTR_RADIUS, isValid);
126
127     boost::shared_ptr<GeomAPI_Dir2d> aNormal(new GeomAPI_Dir2d(aX, anY));
128     boost::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenter->pnt(), aNormal, aRadius));
129     boost::shared_ptr<GeomAPI_Pnt2d> aGeomPoint2d(new GeomAPI_Pnt2d(aX, anY));
130     boost::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCirc->project(aGeomPoint2d);
131     if (aPnt2d) {
132       theX = aPnt2d->x();
133       theY = aPnt2d->y();
134     }
135   }
136 }
137
138 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureCirclePrs::featurePoint
139                                                      (const PartSet_SelectionMode& theMode)
140 {
141   std::string aPointArg;
142   switch (theMode)
143   {
144     case SM_FirstPoint:
145       aPointArg = CIRCLE_ATTR_CENTER;
146       break;
147     default:
148       break;
149   }
150   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
151   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
152                                                               (aData->attribute(aPointArg));
153   return aPoint;
154 }