Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into 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 <SketchPlugin_Sketch.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_AttributeRefAttr.h>
16 #include <ModelAPI_AttributeRefList.h>
17
18 #include <SketchPlugin_Constraint.h>
19
20 #include <Geom_Line.hxx>
21 #include <gp_Lin.hxx>
22
23 #include <XGUI_ViewerPrs.h>
24
25 #include <SketchPlugin_Line.h>
26
27 #include <V3d_View.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS.hxx>
30 #include <BRep_Tool.hxx>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <QMouseEvent>
37
38 using namespace std;
39
40 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
41                                                   QObject* theParent,
42                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
43 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
44   myPointSelectionMode(SM_FirstPoint)
45 {
46 }
47
48 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
49 {
50 }
51
52 bool PartSet_OperationSketchLine::isGranted() const
53 {
54   return true;
55 }
56
57 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
58 {
59   std::list<int> aModes;
60   if (theFeature != feature())
61   {
62     aModes.push_back(TopAbs_VERTEX);
63     aModes.push_back(TopAbs_EDGE);
64   }
65   return aModes;
66 }
67
68 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
69 {
70   if (!theFeature)
71     return;
72   // use the last point of the previous feature as the first of the new one
73   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
74   myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
75 }
76
77 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
78                                                 const std::list<XGUI_ViewerPrs>& theSelected)
79 {
80   double aX, anY;
81
82   bool isFoundPoint = false;
83   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
84   if (theSelected.empty()) {
85     PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
86     isFoundPoint = true;
87   }
88   else {
89     XGUI_ViewerPrs aPrs = theSelected.front();
90     const TopoDS_Shape& aShape = aPrs.shape();
91     if (!aShape.IsNull()) // the point is selected
92     {
93       if (aShape.ShapeType() == TopAbs_VERTEX) {
94         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
95         if (!aVertex.IsNull()) {
96           aPoint = BRep_Tool::Pnt(aVertex);
97           PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
98           isFoundPoint = true;
99
100           setConstraints(aX, anY);
101         }
102       }
103       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
104       {
105         boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
106         if (aFeature) {
107           double X0, X1, X2, X3;
108           double Y0, Y1, Y2, Y3;
109           getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
110           getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
111           PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
112
113           switch (myPointSelectionMode) {
114             case SM_FirstPoint:
115               PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
116             break;
117             case SM_SecondPoint: {
118               getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
119               PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
120             }
121             break;
122             default:
123             break;
124           }
125           isFoundPoint = true;
126         }
127       }
128     }
129   }
130   //if (!isFoundPoint)
131   //  return;
132
133   switch (myPointSelectionMode)
134   {
135     case SM_FirstPoint: {
136       setLinePoint(aX, anY, LINE_ATTR_START);
137       myPointSelectionMode = SM_SecondPoint;
138     }
139     break;
140     case SM_SecondPoint: {
141       setLinePoint(aX, anY, LINE_ATTR_END);
142       commit();
143       emit featureConstructed(feature(), FM_Deactivation);
144       emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
145     }
146     break;
147     default:
148       break;
149   }
150 }
151
152 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView,
153                                              const std::list<XGUI_ViewerPrs>& /*theSelected*/)
154 {
155   switch (myPointSelectionMode)
156   {
157     case SM_SecondPoint:
158     {
159       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
160       setLinePoint(aPoint, theView, LINE_ATTR_END);
161     }
162     break;
163     default:
164       break;
165   }
166 }
167
168 void PartSet_OperationSketchLine::keyReleased(const int theKey)
169 {
170   switch (theKey) {
171     case Qt::Key_Escape: {
172       abort();
173     }
174     break;
175     case Qt::Key_Return: {
176       abort();
177       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
178     }
179     break;
180     default:
181     break;
182   }
183 }
184
185 void PartSet_OperationSketchLine::startOperation()
186 {
187   PartSet_OperationSketchBase::startOperation();
188   myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
189   emit multiSelectionEnabled(false);
190 }
191
192 void PartSet_OperationSketchLine::abortOperation()
193 {
194   emit featureConstructed(feature(), FM_Abort);
195   PartSet_OperationSketchBase::abortOperation();
196 }
197
198 void PartSet_OperationSketchLine::stopOperation()
199 {
200   PartSet_OperationSketchBase::stopOperation();
201   emit multiSelectionEnabled(true);
202 }
203
204 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
205 {
206   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
207   if (mySketch) {
208     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
209                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
210
211     aFeature->addSub(aNewFeature);
212   }
213   if (myInitPoint) {
214     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
215     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
216                                                                 (aData->attribute(LINE_ATTR_START));
217     aPoint->setValue(myInitPoint->x(), myInitPoint->y());
218
219     createConstraint(myInitPoint, aPoint);
220   }
221
222   emit featureConstructed(aNewFeature, FM_Activation);
223   return aNewFeature;
224 }
225
226 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
227                                                    boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
228 {
229   boost::shared_ptr<ModelAPI_Document> aDoc = document();
230   boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
231
232   if (mySketch) {
233     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
234                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
235     aSketch->addSub(aFeature);
236   }
237
238   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
239
240   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
241         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
242   aRef1->setAttr(thePoint1);
243
244   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
245         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
246   aRef2->setAttr(thePoint2);
247
248   if (aFeature) // TODO: generate an error if feature was not created
249     aFeature->execute();
250 }
251
252 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
253 {
254   std::string aPointArg;
255   switch (myPointSelectionMode)
256   {
257     case SM_FirstPoint:
258       aPointArg = LINE_ATTR_START;
259       break;
260     case SM_SecondPoint:
261       aPointArg = LINE_ATTR_END;
262       break;
263   }
264
265   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
266   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
267                                                               (aData->attribute(aPointArg));
268   aData = mySketch->data();
269   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
270         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
271
272   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
273   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
274                                                                   aLast = aFeatures.end();
275   for (; anIt != aLast; anIt++) {
276     boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
277     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
278     if (aFPoint)
279       createConstraint(aFPoint, aPoint);
280   }
281 }
282
283 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
284                                                const std::string& theAttribute,
285                                                double& theX, double& theY)
286 {
287   if (!theFeature)
288     return;
289   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
290   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
291         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
292   theX = aPoint->x();
293   theY = aPoint->y();
294 }
295
296 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
297                                                boost::shared_ptr<ModelAPI_Feature> theFeature,
298                                                double theX, double theY)
299 {
300   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
301   if (!theFeature)
302     return aPoint2D;
303   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
304
305   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
306         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
307   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
308     aPoint2D = aPoint;
309   else {
310     aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
311     if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
312       aPoint2D = aPoint;
313   }
314   return aPoint2D;
315 }
316
317 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
318                                                const std::string& theAttribute)
319 {
320   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
321   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
322         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
323   aPoint->setValue(theX, theY);
324 }
325
326 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
327                                                Handle(V3d_View) theView,
328                                                const std::string& theAttribute)
329 {
330   double aX, anY;
331   PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
332   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
333   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
334         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
335   aPoint->setValue(aX, anY);
336 }