Salome HOME
5e67aaeeef6ade656ab6f1eb7a19e28fe453d626
[modules/shaper.git] / src / PartSet / PartSet_FeatureLinePrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     04 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureLinePrs.h>
6 #include <PartSet_Tools.h>
7
8 #include <SketchPlugin_Feature.h>
9 #include <SketchPlugin_Sketch.h>
10 #include <SketchPlugin_ConstraintCoincidence.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Constraint.h>
13
14 #include <GeomDataAPI_Point2D.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 <Precision.hxx>
22
23 using namespace std;
24
25 PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch)
26 : PartSet_FeaturePrs(theSketch)
27 {
28 }
29
30 void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature)
31 {
32   if (feature() && theFeature)
33   {
34     // use the last point of the previous feature as the first of the new one
35     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
36     boost::shared_ptr<GeomDataAPI_Point2D> anInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
37                                                                   (aData->attribute(LINE_ATTR_END));
38     PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START);
39     PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END);
40
41     aData = feature()->data();
42     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
43                                                                  (aData->attribute(LINE_ATTR_START));
44     PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint);
45   }
46 }
47
48 PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY,
49                                                        const PartSet_SelectionMode& theMode)
50 {
51   PartSet_SelectionMode aMode = theMode;
52   switch (theMode)
53   {
54     case SM_FirstPoint: {
55       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START);
56       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
57       aMode = SM_SecondPoint;
58     }
59     break;
60     case SM_SecondPoint: {
61       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
62       aMode = SM_DonePoint;
63    }
64     break;
65     default:
66       break;
67   }
68   return aMode;
69 }
70
71 std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const
72 {
73   std::string aAttribute;
74   switch (theMode)
75   {
76     case SM_FirstPoint:
77       aAttribute = LINE_ATTR_START;
78     break;
79     case SM_SecondPoint:
80       aAttribute = LINE_ATTR_END;
81     break;
82     default:
83     break;
84   }
85   return aAttribute;
86 }
87
88 PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const
89 {
90   PartSet_SelectionMode aMode;
91
92   if (theAttribute == LINE_ATTR_START)
93     aMode = SM_SecondPoint;
94   else if (theAttribute == LINE_ATTR_END)
95     aMode = SM_DonePoint;
96   return aMode;
97 }
98
99 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
100                                                      (const PartSet_SelectionMode& theMode)
101 {
102   std::string aPointArg;
103   switch (theMode)
104   {
105     case SM_FirstPoint:
106       aPointArg = LINE_ATTR_START;
107       break;
108     case SM_SecondPoint:
109       aPointArg = LINE_ATTR_END;
110       break;
111     default:
112       break;
113   }
114   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
115   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
116                                                               (aData->attribute(aPointArg));
117   return aPoint;
118 }