Salome HOME
According to "operation-widget_factory-property"
[modules/shaper.git] / src / PartSet / PartSet_FeatureArcPrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     04 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureArcPrs.h>
6 #include <PartSet_Tools.h>
7
8 #include <SketchPlugin_Feature.h>
9 #include <SketchPlugin_Sketch.h>
10 #include <SketchPlugin_Arc.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomAPI_Circ2d.h>
15
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeRefList.h>
20
21 #include <V3d_View.hxx>
22
23 #include <Precision.hxx>
24
25 using namespace std;
26
27 PartSet_FeatureArcPrs::PartSet_FeatureArcPrs(FeaturePtr theSketch)
28 : PartSet_FeaturePrs(theSketch)
29 {
30 }
31
32 std::string PartSet_FeatureArcPrs::getKind()
33 {
34   return SKETCH_ARC_KIND;
35 }
36
37 PartSet_SelectionMode PartSet_FeatureArcPrs::setPoint(double theX, double theY,
38                                                        const PartSet_SelectionMode& theMode)
39 {
40   PartSet_SelectionMode aMode = theMode;
41   switch (theMode)
42   {
43     case SM_FirstPoint: {
44       PartSet_Tools::setFeaturePoint(feature(), theX, theY, ARC_ATTR_CENTER);
45       aMode = SM_SecondPoint;
46     }
47     break;
48     case SM_SecondPoint: {
49       PartSet_Tools::setFeaturePoint(feature(), theX, theY, ARC_ATTR_START);
50       aMode = SM_ThirdPoint;
51    }
52    break;
53    case SM_ThirdPoint: {
54      PartSet_Tools::setFeaturePoint(feature(), theX, theY, ARC_ATTR_END);
55      aMode = SM_DonePoint;
56    }
57     break;
58     default:
59       break;
60   }
61   return aMode;
62 }
63
64 std::string PartSet_FeatureArcPrs::getAttribute(const PartSet_SelectionMode& theMode) const
65 {
66   std::string aAttribute;
67   switch (theMode)
68   {
69     case SM_FirstPoint:
70       aAttribute = ARC_ATTR_CENTER;
71     break;
72     case SM_SecondPoint:
73       aAttribute = ARC_ATTR_START;
74     break;
75     case SM_ThirdPoint:
76       aAttribute = ARC_ATTR_END;
77     break;
78     default:
79     break;
80   }
81   return aAttribute;
82 }
83
84 PartSet_SelectionMode PartSet_FeatureArcPrs::getNextMode(const std::string& theAttribute) const
85 {
86   PartSet_SelectionMode aMode;
87
88   if (theAttribute == ARC_ATTR_CENTER)
89     aMode = SM_SecondPoint;
90   else if (theAttribute == ARC_ATTR_START)
91     aMode = SM_ThirdPoint;
92   else if (theAttribute == ARC_ATTR_END)
93     aMode = SM_DonePoint;
94   return aMode;
95 }
96
97 void PartSet_FeatureArcPrs::move(double theDeltaX, double theDeltaY)
98 {
99   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
100   if (!aData->isValid())
101     return;
102
103   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
104         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
105   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
106
107   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
108         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
109   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
110
111   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
112         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
113   aPoint1->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
114 }
115
116 double PartSet_FeatureArcPrs::distanceToPoint(FeaturePtr theFeature,
117                                                double theX, double theY)
118 {
119   double aDelta = 0;
120   if (!theFeature || theFeature->getKind() != getKind())
121     return aDelta;
122   boost::shared_ptr<GeomAPI_Pnt2d> aPoint2d(new GeomAPI_Pnt2d(theX, theY));
123
124
125   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
126
127   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
128         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
129   aDelta = aPoint1->pnt()->distance(aPoint2d);
130
131   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
132         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
133   aDelta = qMin(aDelta, aPoint2->pnt()->distance(aPoint2d));
134
135   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
136         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
137   aDelta = qMin(aDelta, aPoint3->pnt()->distance(aPoint2d));
138
139   return aDelta;
140 }
141
142 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureArcPrs::findPoint(FeaturePtr theFeature,
143                                                                         double theX, double theY)
144 {
145   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
146   if (!theFeature || theFeature->getKind() != getKind())
147     return aPoint2D;
148
149   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
150   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
151         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
152   if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
153       fabs(aPoint->y() - theY) < Precision::Confusion()) {
154     aPoint2D = aPoint;
155   }
156   if (!aPoint2D) {
157     aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
158     if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
159         fabs(aPoint->y() - theY) < Precision::Confusion())
160       aPoint2D = aPoint;
161   }
162   if (!aPoint2D) {
163     aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
164     if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
165         fabs(aPoint->y() - theY) < Precision::Confusion())
166       aPoint2D = aPoint;
167   }
168   return aPoint2D;
169 }
170
171 void PartSet_FeatureArcPrs::projectPointOnArc(gp_Pnt& thePoint, Handle(V3d_View) theView,
172                                               double& theX, double& theY)
173 {
174   FeaturePtr aSketch = sketch();
175   if (aSketch) {
176     double aX, anY;
177     PartSet_Tools::convertTo2D(thePoint, aSketch, theView, aX, anY);
178
179     // circle origin point and radius
180     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
181     boost::shared_ptr<GeomDataAPI_Point2D> aCenter = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
182                                                               (aData->attribute(ARC_ATTR_CENTER));
183     boost::shared_ptr<GeomDataAPI_Point2D> aStart = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
184                                                               (aData->attribute(ARC_ATTR_START));
185
186     boost::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenter->pnt(), aStart->pnt()));
187     boost::shared_ptr<GeomAPI_Pnt2d> aGeomPoint2d(new GeomAPI_Pnt2d(aX, anY));
188     boost::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCirc->project(aGeomPoint2d);
189     if (aPnt2d) {
190       theX = aPnt2d->x();
191       theY = aPnt2d->y();
192     }
193   }
194 }
195
196 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureArcPrs::featurePoint
197                                                      (const PartSet_SelectionMode& theMode)
198 {
199   std::string aPointArg;
200   switch (theMode)
201   {
202     case SM_FirstPoint:
203       aPointArg = ARC_ATTR_CENTER;
204       break;
205     case SM_SecondPoint:
206       aPointArg = ARC_ATTR_START;
207       break;
208     case SM_ThirdPoint:
209       aPointArg = ARC_ATTR_END;
210       break;
211     default:
212       break;
213   }
214   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
215   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
216                                                               (aData->attribute(aPointArg));
217   return aPoint;
218 }