]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FeatureLinePrs.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 #include <V3d_View.hxx>
23
24 using namespace std;
25
26 PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch)
27 : PartSet_FeaturePrs(theSketch)
28 {
29 }
30
31 void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature)
32 {
33   if (feature() && theFeature)
34   {
35     // use the last point of the previous feature as the first of the new one
36     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
37     boost::shared_ptr<GeomDataAPI_Point2D> anInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
38                                                                   (aData->attribute(LINE_ATTR_END));
39     PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START);
40     PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END);
41
42     aData = feature()->data();
43     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
44                                                                  (aData->attribute(LINE_ATTR_START));
45     PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint);
46   }
47 }
48
49 PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY,
50                                                        const PartSet_SelectionMode& theMode)
51 {
52   PartSet_SelectionMode aMode = theMode;
53   switch (theMode)
54   {
55     case SM_FirstPoint: {
56       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START);
57       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
58       aMode = SM_SecondPoint;
59     }
60     break;
61     case SM_SecondPoint: {
62       PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
63       aMode = SM_DonePoint;
64    }
65     break;
66     default:
67       break;
68   }
69   return aMode;
70 }
71
72 std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const
73 {
74   std::string aAttribute;
75   switch (theMode)
76   {
77     case SM_FirstPoint:
78       aAttribute = LINE_ATTR_START;
79     break;
80     case SM_SecondPoint:
81       aAttribute = LINE_ATTR_END;
82     break;
83     default:
84     break;
85   }
86   return aAttribute;
87 }
88
89 PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const
90 {
91   PartSet_SelectionMode aMode;
92
93   if (theAttribute == LINE_ATTR_START)
94     aMode = SM_SecondPoint;
95   else if (theAttribute == LINE_ATTR_END)
96     aMode = SM_DonePoint;
97   return aMode;
98 }
99
100 void PartSet_FeatureLinePrs::projectPointOnLine(FeaturePtr theFeature,
101                                                 const PartSet_SelectionMode& theMode,
102                                                 const gp_Pnt& thePoint, Handle(V3d_View) theView,
103                                                 double& theX, double& theY)
104 {
105   if (theFeature) {
106     double X0, X1, X2, X3;
107     double Y0, Y1, Y2, Y3;
108     PartSet_Tools::getLinePoint(theFeature, LINE_ATTR_START, X2, Y2);
109     PartSet_Tools::getLinePoint(theFeature, LINE_ATTR_END, X3, Y3);
110     PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1);
111
112     switch (theMode) {
113       case SM_FirstPoint:
114         PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, theX, theY);
115       break;
116       case SM_SecondPoint: {
117         PartSet_Tools::getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
118         PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, theX, theY);
119       }
120       break;
121       default:
122       break;
123     }
124   }
125 }
126
127 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
128                                                      (const PartSet_SelectionMode& theMode)
129 {
130   std::string aPointArg;
131   switch (theMode)
132   {
133     case SM_FirstPoint:
134       aPointArg = LINE_ATTR_START;
135       break;
136     case SM_SecondPoint:
137       aPointArg = LINE_ATTR_END;
138       break;
139     default:
140       break;
141   }
142   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
143   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
144                                                               (aData->attribute(aPointArg));
145   return aPoint;
146 }