]> 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 <SketchPlugin_Feature.h>
8 #include <GeomDataAPI_Point2D.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
13
14 #include <SketchPlugin_Sketch.h>
15 #include <SketchPlugin_Line.h>
16
17 #ifdef _DEBUG
18 #include <QDebug>
19 #endif
20
21 using namespace std;
22
23 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
24                                                   QObject* theParent,
25                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
26 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
27   myPointSelectionMode(SM_FirstPoint)
28 {
29 }
30
31 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
32 {
33 }
34
35 bool PartSet_OperationSketchLine::isGranted() const
36 {
37   return true;
38 }
39
40 int PartSet_OperationSketchLine::getSelectionMode(boost::shared_ptr<ModelAPI_Feature> theFeature) const
41 {
42   int aMode = 0;
43   if (theFeature != feature())
44     aMode = TopAbs_VERTEX;
45   return aMode;
46 }
47
48 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
49 {
50   switch (myPointSelectionMode)
51   {
52     case SM_FirstPoint: {
53       setLinePoint(thePoint, LINE_ATTR_START);
54       myPointSelectionMode = SM_SecondPoint;
55     }
56     break;
57     case SM_SecondPoint: {
58       setLinePoint(thePoint, LINE_ATTR_END);
59       myPointSelectionMode = SM_None;
60     }
61     break;
62     case SM_None: {
63
64     }
65     break;
66     default:
67       break;
68   }
69 }
70
71 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint)
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     }
115     break;
116     default:
117     break;
118   }
119 }
120
121 void PartSet_OperationSketchLine::startOperation()
122 {
123   PartSet_OperationSketchBase::startOperation();
124   myPointSelectionMode = SM_FirstPoint;
125 }
126
127 void PartSet_OperationSketchLine::stopOperation()
128 {
129   PartSet_OperationSketchBase::stopOperation();
130   myPointSelectionMode = SM_None;
131 }
132
133 void PartSet_OperationSketchLine::createFeature()
134 {
135   PartSet_OperationSketchBase::createFeature();
136   if (mySketch) {
137     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
138                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
139
140     aFeature->addSub(feature());
141   }
142   //emit featureConstructed(aPrevFeature, FM_Activation);
143 }
144
145 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
146                                                const std::string& theAttribute)
147 {
148   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
149   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
150         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
151
152   double aX = 0;
153   double anY = 0;
154   convertTo2D(thePoint, aX, anY);
155   aPoint->setValue(aX, anY);
156 }
157
158 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
159                                                const std::string& theSourceAttribute,
160                                                const std::string& theAttribute)
161 {
162   boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
163   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
164         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
165   double aX = aPoint->x();
166   double anY = aPoint->y();
167
168   aData = feature()->data();
169   aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
170   aPoint->setValue(aX, anY);
171 }
172
173 void PartSet_OperationSketchLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
174 {
175   if (!mySketch)
176     return;
177
178   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
179   boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
180
181   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
182     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
183
184   boost::shared_ptr<GeomDataAPI_Dir> aX = 
185     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
186   boost::shared_ptr<GeomDataAPI_Dir> anY = 
187     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
188
189   gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
190   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
191   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
192 }