]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketchLine.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/SolveSpace'
[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                                                 const std::list<XGUI_ViewerPrs>& theSelected)
59 {
60   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
61   switch (myPointSelectionMode)
62   {
63     case SM_FirstPoint: {
64       setLinePoint(aPoint, theView, LINE_ATTR_START);
65       myPointSelectionMode = SM_SecondPoint;
66     }
67     break;
68     case SM_SecondPoint: {
69       setLinePoint(aPoint, theView, LINE_ATTR_END);
70       commit();
71       emit featureConstructed(feature(), FM_Deactivation);
72       emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
73     }
74     break;
75     default:
76       break;
77   }
78 }
79
80 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
81 {
82   switch (myPointSelectionMode)
83   {
84     case SM_SecondPoint:
85     {
86       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
87       setLinePoint(aPoint, theView, LINE_ATTR_END);
88     }
89     break;
90     default:
91       break;
92   }
93 }
94
95 void PartSet_OperationSketchLine::keyReleased(const int theKey)
96 {
97   switch (theKey) {
98     case Qt::Key_Escape: {
99       abort();
100     }
101     break;
102     case Qt::Key_Return: {
103       abort();
104       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
105     }
106     break;
107     default:
108     break;
109   }
110 }
111
112 void PartSet_OperationSketchLine::startOperation()
113 {
114   PartSet_OperationSketchBase::startOperation();
115   myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
116 }
117
118 void PartSet_OperationSketchLine::abortOperation()
119 {
120   emit featureConstructed(feature(), FM_Abort);
121   PartSet_OperationSketchBase::abortOperation();
122 }
123
124 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
125 {
126   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
127   if (mySketch) {
128     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
129                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
130
131     aFeature->addSub(aNewFeature);
132   }
133   if (myInitPoint) {
134     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
135     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
136                                                                 (aData->attribute(LINE_ATTR_START));
137     aPoint->setValue(myInitPoint->x(), myInitPoint->y());
138   }
139
140   emit featureConstructed(aNewFeature, FM_Activation);
141   return aNewFeature;
142 }
143
144 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
145                                                Handle(V3d_View) theView,
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, anY;
153   PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
154   aPoint->setValue(aX, anY);
155 }