Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_FeatureLengthPrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     16 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureLengthPrs.h>
6 #include <PartSet_Tools.h>
7
8 #include <PartSet_FeatureLinePrs.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_ConstraintLength.h>
14
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Lin2d.h>
18
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeDouble.h>
24
25 #include <Precision.hxx>
26
27 using namespace std;
28
29 PartSet_FeatureLengthPrs::PartSet_FeatureLengthPrs(FeaturePtr theSketch)
30 : PartSet_FeaturePrs(theSketch)
31 {
32 }
33
34 std::string PartSet_FeatureLengthPrs::getKind()
35 {
36   return SKETCH_CONSTRAINT_LENGTH_KIND;
37 }
38
39 PartSet_SelectionMode PartSet_FeatureLengthPrs::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<ModelAPI_AttributeRefAttr> anAttr = 
53               boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
54       FeaturePtr aFeature;
55       if (anAttr) {
56         aFeature = anAttr->feature();
57         if (aFeature->getKind() != SKETCH_LINE_KIND) {
58           aFeature = FeaturePtr();
59         }
60       }
61       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
62                                                              (new GeomAPI_Pnt2d(theX, theY));
63       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
64       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
65       double aDistance = aPoint->distance(aResult);
66
67       double aStartX, aStartY;
68       PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
69
70       if (!aFeatureLin->isRight(aPoint))
71         aDistance = -aDistance;
72
73       AttributeDoublePtr aFlyoutAttr = 
74           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
75       aFlyoutAttr->setValue(aDistance);
76
77       aMode = SM_DonePoint;
78     }
79     break;
80     default:
81       break;
82   }
83   return aMode;
84 }
85
86 std::string PartSet_FeatureLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
87 {
88   std::string aAttribute;
89   switch (theMode)
90   {
91     case SM_FirstPoint:
92       //aAttribute = CIRCLE_ATTR_CENTER;
93     break;
94     case SM_SecondPoint:
95       //aAttribute = CIRCLE_ATTR_RADIUS;
96     break;
97     default:
98     break;
99   }
100   return aAttribute;
101 }
102
103 PartSet_SelectionMode PartSet_FeatureLengthPrs::getNextMode(const std::string& theAttribute) const
104 {
105   PartSet_SelectionMode aMode = SM_SecondPoint;
106
107   /*if (theAttribute == CIRCLE_ATTR_CENTER)
108     aMode = SM_SecondPoint;
109   else if (theAttribute == CIRCLE_ATTR_RADIUS)
110     aMode = SM_DonePoint;*/
111   return aMode;
112 }
113
114 void PartSet_FeatureLengthPrs::move(double theDeltaX, double theDeltaY)
115 {
116   /*boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
117   if (!aData->isValid())
118     return;
119
120   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
121         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
122   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);*/
123 }
124
125 double PartSet_FeatureLengthPrs::distanceToPoint(FeaturePtr theFeature,
126                                                  double theX, double theY)
127 {
128   /*double aDelta = 0;
129   if (!theFeature || theFeature->getKind() != getKind())
130     return aDelta;
131
132   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
133   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
134         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
135
136   boost::shared_ptr<GeomAPI_Pnt2d> aPoint2d(new GeomAPI_Pnt2d(theX, theY));
137   return aPoint->pnt()->distance(aPoint2d);*/
138   return 0;
139 }
140
141 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::findPoint(FeaturePtr theFeature,
142                                                                            double theX, double theY)
143 {
144   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
145   /*if (!theFeature || theFeature->getKind() != getKind())
146     return aPoint2D;
147
148   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
149   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
150         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
151   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
152     aPoint2D = aPoint;
153 */
154   return aPoint2D;
155 }
156
157 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::featurePoint
158                                                      (const PartSet_SelectionMode& theMode)
159 {
160   /*std::string aPointArg;
161   switch (theMode)
162   {
163     case SM_FirstPoint:
164       aPointArg = CIRCLE_ATTR_CENTER;
165       break;
166     default:
167       break;
168   }
169   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
170   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
171                                                               (aData->attribute(aPointArg));
172   return aPoint;
173   */
174   return boost::shared_ptr<GeomDataAPI_Point2D>();
175 }
176
177 bool PartSet_FeatureLengthPrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
178 {
179   bool aResult = false;
180   if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
181   {
182     // set length feature
183     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
184     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
185           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
186     aRef->setFeature(theFeature);
187
188     // set length value
189     aData = theFeature->data();
190     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
191           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
192     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
193           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
194
195     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
196     PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
197     aResult = true;
198   }
199   return aResult;
200 }