]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketchLine.cpp
Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
1 // File:        PartSet_OperationSketchLine.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationSketchLine.h>
6
7 #include <PartSet_Tools.h>
8
9 #include <SketchPlugin_Feature.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
13
14 #include <SketchPlugin_Line.h>
15
16 #ifdef _DEBUG
17 #include <QDebug>
18 #endif
19
20 using namespace std;
21
22 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
23                                                   QObject* theParent,
24                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
25 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
26   myPointSelectionMode(SM_FirstPoint)
27 {
28 }
29
30 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
31 {
32 }
33
34 bool PartSet_OperationSketchLine::isGranted() const
35 {
36   return true;
37 }
38
39 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
40 {
41   std::list<int> aModes;
42   if (theFeature != feature()) {
43     aModes.push_back(TopAbs_VERTEX);
44     aModes.push_back(TopAbs_EDGE);
45   }
46   return aModes;
47 }
48
49 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint, QMouseEvent* /*theEvent*/)
50 {
51   switch (myPointSelectionMode)
52   {
53     case SM_FirstPoint: {
54       setLinePoint(thePoint, LINE_ATTR_START);
55       myPointSelectionMode = SM_SecondPoint;
56     }
57     break;
58     case SM_SecondPoint: {
59       setLinePoint(thePoint, LINE_ATTR_END);
60       myPointSelectionMode = SM_None;
61     }
62     break;
63     case SM_None: {
64     }
65     break;
66     default:
67       break;
68   }
69 }
70
71 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint, QMouseEvent* /*theEvent*/)
72 {
73   switch (myPointSelectionMode)
74   {
75     case SM_SecondPoint:
76       setLinePoint(thePoint, LINE_ATTR_END);
77       break;
78     case SM_None: {
79       boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
80       // stop the last operation
81       commitOperation();
82       document()->finishOperation();
83       //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
84       // start a new operation
85       document()->startOperation();
86       startOperation();
87       // use the last point of the previous feature as the first of the new one
88       setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
89       myPointSelectionMode = SM_SecondPoint;
90
91       emit featureConstructed(aPrevFeature, FM_Deactivation);
92     }
93     break;
94     default:
95       break;
96   }
97 }
98
99 void PartSet_OperationSketchLine::keyReleased(const int theKey)
100 {
101   switch (theKey) {
102     case Qt::Key_Escape: {
103       if (myPointSelectionMode != SM_None)
104         emit featureConstructed(feature(), FM_Abort);
105       abort();
106     }
107     break;
108     case Qt::Key_Return: {
109       if (myPointSelectionMode != SM_None) {
110         emit featureConstructed(feature(), FM_Abort);
111         myPointSelectionMode = SM_FirstPoint;
112         document()->abortOperation();
113       }
114       else
115         myPointSelectionMode = SM_FirstPoint;
116     }
117     break;
118     default:
119     break;
120   }
121 }
122
123 void PartSet_OperationSketchLine::startOperation()
124 {
125   PartSet_OperationSketchBase::startOperation();
126   myPointSelectionMode = SM_FirstPoint;
127 }
128
129 void PartSet_OperationSketchLine::stopOperation()
130 {
131   PartSet_OperationSketchBase::stopOperation();
132   myPointSelectionMode = SM_None;
133 }
134
135 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
136 {
137   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
138   if (mySketch) {
139     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
140                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
141
142     aFeature->addSub(aNewFeature);
143   }
144   emit featureConstructed(aNewFeature, FM_Activation);
145   return aNewFeature;
146 }
147
148 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
149                                                const std::string& theAttribute)
150 {
151   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
152   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
153         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
154
155   double aX, anY;
156   PartSet_Tools::ConvertTo2D(thePoint, mySketch, aX, anY);
157   aPoint->setValue(aX, anY);
158 }
159
160 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
161                                                const std::string& theSourceAttribute,
162                                                const std::string& theAttribute)
163 {
164   boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
165   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
166         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
167   double aX = aPoint->x();
168   double anY = aPoint->y();
169
170   aData = feature()->data();
171   aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
172   aPoint->setValue(aX, anY);
173 }