]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketchLine.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <V3d_View.hxx>
17
18 #ifdef _DEBUG
19 #include <QDebug>
20 #endif
21
22 #include <QMouseEvent>
23
24 using namespace std;
25
26 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
27                                                   QObject* theParent,
28                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
29 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
30   myPointSelectionMode(SM_FirstPoint)
31 {
32 }
33
34 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
35 {
36 }
37
38 bool PartSet_OperationSketchLine::isGranted() const
39 {
40   return true;
41 }
42
43 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
44 {
45   return std::list<int>();
46 }
47
48 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
49 {
50   if (!theFeature)
51     return;
52   // use the last point of the previous feature as the first of the new one
53   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
54   myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
55 }
56
57 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView)
58 {
59   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
60   switch (myPointSelectionMode)
61   {
62     case SM_FirstPoint: {
63       setLinePoint(aPoint, theView, LINE_ATTR_START);
64       myPointSelectionMode = SM_SecondPoint;
65     }
66     break;
67     case SM_SecondPoint: {
68       setLinePoint(aPoint, theView, LINE_ATTR_END);
69       commit();
70       emit featureConstructed(feature(), FM_Deactivation);
71       emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
72     }
73     break;
74     default:
75       break;
76   }
77 }
78
79 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
80 {
81   switch (myPointSelectionMode)
82   {
83     case SM_SecondPoint:
84     {
85       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
86       setLinePoint(aPoint, theView, LINE_ATTR_END);
87     }
88     break;
89     default:
90       break;
91   }
92 }
93
94 void PartSet_OperationSketchLine::keyReleased(const int theKey)
95 {
96   switch (theKey) {
97     case Qt::Key_Escape: {
98       abort();
99     }
100     break;
101     case Qt::Key_Return: {
102       abort();
103       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
104     }
105     break;
106     default:
107     break;
108   }
109 }
110
111 void PartSet_OperationSketchLine::startOperation()
112 {
113   PartSet_OperationSketchBase::startOperation();
114   myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
115 }
116
117 void PartSet_OperationSketchLine::abortOperation()
118 {
119   emit featureConstructed(feature(), FM_Abort);
120   PartSet_OperationSketchBase::abortOperation();
121 }
122
123 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
124 {
125   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
126   if (mySketch) {
127     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
128                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
129
130     aFeature->addSub(aNewFeature);
131   }
132   if (myInitPoint) {
133     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
134     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
135                                                                 (aData->attribute(LINE_ATTR_START));
136     aPoint->setValue(myInitPoint->x(), myInitPoint->y());
137   }
138
139   emit featureConstructed(aNewFeature, FM_Activation);
140   return aNewFeature;
141 }
142
143 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
144                                                Handle(V3d_View) theView,
145                                                const std::string& theAttribute)
146 {
147   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
148   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
149         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
150
151   double aX, anY;
152   PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
153   aPoint->setValue(aX, anY);
154 }