Salome HOME
72020fb9ac11a479596aa97f8a33e97127f988db
[modules/shaper.git] / src / PartSet / PartSet_OperationEditLine.cpp
1 // File:        PartSet_OperationEditLine.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationEditLine.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_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
24                                                   QObject* theParent,
25                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
26 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
27 {
28 }
29
30 PartSet_OperationEditLine::~PartSet_OperationEditLine()
31 {
32 }
33
34 bool PartSet_OperationEditLine::isGranted() const
35 {
36   return true;
37 }
38
39 std::list<int> PartSet_OperationEditLine::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   return aModes;
45 }
46
47 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
48 {
49   setFeature(theFeature);
50 }
51
52 void PartSet_OperationEditLine::mouseReleased(const gp_Pnt& thePoint)
53 {
54   /*switch (myPointSelectionMode)
55   {
56     case SM_FirstPoint: {
57       setLinePoint(thePoint, LINE_ATTR_START);
58       myPointSelectionMode = SM_SecondPoint;
59     }
60     break;
61     case SM_SecondPoint: {
62       setLinePoint(thePoint, LINE_ATTR_END);
63       myPointSelectionMode = SM_None;
64     }
65     break;
66     case SM_None: {
67
68     }
69     break;
70     default:
71       break;
72   }
73 */
74 }
75
76 void PartSet_OperationEditLine::mouseMoved(const gp_Pnt& thePoint)
77 {
78 /*  switch (myPointSelectionMode)
79   {
80     case SM_SecondPoint:
81       setLinePoint(thePoint, LINE_ATTR_END);
82       break;
83     case SM_None: {
84       boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
85       // stop the last operation
86       commitOperation();
87       document()->finishOperation();
88       //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
89       // start a new operation
90       document()->startOperation();
91       startOperation();
92       // use the last point of the previous feature as the first of the new one
93       setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
94       myPointSelectionMode = SM_SecondPoint;
95
96       emit featureConstructed(aPrevFeature, FM_Deactivation);
97     }
98     break;
99     default:
100       break;
101   }
102 */
103 }
104
105 void PartSet_OperationEditLine::keyReleased(const int theKey)
106 {
107 /*  switch (theKey) {
108     case Qt::Key_Escape: {
109       if (myPointSelectionMode != SM_None)
110         emit featureConstructed(feature(), FM_Abort);
111       abort();
112     }
113     break;
114     case Qt::Key_Return: {
115       if (myPointSelectionMode != SM_None) {
116         emit featureConstructed(feature(), FM_Abort);
117         myPointSelectionMode = SM_FirstPoint;
118         document()->abortOperation();
119       }
120       else
121         myPointSelectionMode = SM_FirstPoint;
122     }
123     break;
124     default:
125     break;
126   }
127   */
128 }
129
130 void PartSet_OperationEditLine::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
131                                             const TopoDS_Shape& theShape)
132 {
133   if (theFeature == feature())
134     return;
135
136   commit();
137
138   if (theFeature)
139     emit launchOperation("EditLine", theFeature);
140 }
141
142 void PartSet_OperationEditLine::startOperation()
143 {
144   //PartSet_OperationSketchBase::startOperation();
145   //myPointSelectionMode = SM_FirstPoint;
146 }
147
148 void PartSet_OperationEditLine::stopOperation()
149 {
150   PartSet_OperationSketchBase::stopOperation();
151   //myPointSelectionMode = SM_None;
152 }
153
154 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
155 {
156   return boost::shared_ptr<ModelAPI_Feature>();
157 }
158
159 void PartSet_OperationEditLine::setLinePoint(const gp_Pnt& thePoint,
160                                                const std::string& theAttribute)
161 {
162   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
163   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
164         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
165
166   double aX = 0;
167   double anY = 0;
168   convertTo2D(thePoint, aX, anY);
169   aPoint->setValue(aX, anY);
170 }
171
172 void PartSet_OperationEditLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
173                                                const std::string& theSourceAttribute,
174                                                const std::string& theAttribute)
175 {
176   boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
177   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
178         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
179   double aX = aPoint->x();
180   double anY = aPoint->y();
181
182   aData = feature()->data();
183   aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
184   aPoint->setValue(aX, anY);
185 }
186
187 void PartSet_OperationEditLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
188 {
189   if (!mySketch)
190     return;
191
192   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
193   boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
194
195   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
196     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
197
198   boost::shared_ptr<GeomDataAPI_Dir> aX = 
199     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
200   boost::shared_ptr<GeomDataAPI_Dir> anY = 
201     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
202
203   gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
204   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
205   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
206 }