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 #include <PartSet_OperationSketch.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <ModuleBase_OperationDescription.h>
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
21
22 #include <SketchPlugin_Constraint.h>
23
24 #include <Geom_Line.hxx>
25 #include <gp_Lin.hxx>
26
27 #include <XGUI_ViewerPrs.h>
28 #include <XGUI_Constants.h>
29
30 #include <SketchPlugin_Line.h>
31
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS.hxx>
35 #include <BRep_Tool.hxx>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 #include <QMouseEvent>
42
43 using namespace std;
44
45 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
46                                                   QObject* theParent,
47                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
49   myPointSelectionMode(SM_FirstPoint)
50 {
51 }
52
53 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
54 {
55 }
56
57 bool PartSet_OperationSketchLine::canBeCommitted() const
58 {
59   return myPointSelectionMode == SM_DonePoint;
60 }
61
62 bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
63 {
64   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
65 }
66
67 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
68 {
69   std::list<int> aModes;
70   if (theFeature != feature())
71     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
72   return aModes;
73 }
74
75 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
76                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
77                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
78 {
79   if (!theFeature || theFeature->getKind() != "SketchLine")
80     return;
81   // use the last point of the previous feature as the first of the new one
82   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
83   myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
84 }
85
86 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
87 {
88   return mySketch;
89 }
90
91 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
92                                                 const std::list<XGUI_ViewerPrs>& theSelected,
93                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
94 {
95   if (myPointSelectionMode == SM_DonePoint)
96   {
97     // if the point creation is finished, the next mouse release should commit the modification
98     // the next release can happens by double click in the viewer
99     commit();
100     restartOperation(PartSet_OperationSketchLine::Type(), feature());
101     return;
102   }
103
104   double aX, anY;
105
106   bool isFoundPoint = false;
107   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
108   if (theSelected.empty()) {
109     PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
110     isFoundPoint = true;
111   }
112   else {
113     XGUI_ViewerPrs aPrs = theSelected.front();
114     const TopoDS_Shape& aShape = aPrs.shape();
115     if (!aShape.IsNull()) // the point is selected
116     {
117       if (aShape.ShapeType() == TopAbs_VERTEX) {
118         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
119         if (!aVertex.IsNull()) {
120           aPoint = BRep_Tool::Pnt(aVertex);
121           PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
122           isFoundPoint = true;
123
124           setConstraints(aX, anY);
125         }
126       }
127       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
128       {
129         boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
130         if (aFeature) {
131           double X0, X1, X2, X3;
132           double Y0, Y1, Y2, Y3;
133           getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
134           getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
135           PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
136
137           switch (myPointSelectionMode) {
138             case SM_FirstPoint:
139               PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
140             break;
141             case SM_SecondPoint: {
142               getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
143               PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
144             }
145             break;
146             default:
147             break;
148           }
149           isFoundPoint = true;
150         }
151       }
152     }
153   }
154
155   switch (myPointSelectionMode)
156   {
157     case SM_FirstPoint: {
158       setLinePoint(feature(), aX, anY, LINE_ATTR_START);
159       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
160       flushUpdated();
161
162       setPointSelectionMode(SM_SecondPoint);
163     }
164     break;
165     case SM_SecondPoint: {
166       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
167       flushUpdated();
168
169       setPointSelectionMode(SM_DonePoint);
170    }
171     break;
172     default:
173       break;
174   }
175 }
176
177 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
178 {
179   switch (myPointSelectionMode)
180   {
181     case SM_FirstPoint: {
182       double aX, anY;
183       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
184       PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
185       setLinePoint(feature(), aX, anY, LINE_ATTR_START);
186       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
187       flushUpdated();
188       emit focusActivated(LINE_ATTR_START);
189     }
190     break;
191     case SM_SecondPoint:
192     {
193       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
194       setLinePoint(aPoint, theView, LINE_ATTR_END);
195       flushUpdated();
196       emit focusActivated(LINE_ATTR_END);
197     }
198     break;
199     case SM_DonePoint:
200     {
201       commit();
202       restartOperation(PartSet_OperationSketchLine::Type(), feature());
203     }
204     default:
205       break;
206   }
207 }
208
209 void PartSet_OperationSketchLine::keyReleased(std::string theName, QKeyEvent* theEvent)
210 {
211   int aKeyType = theEvent->key();
212   // the second point should be activated by any modification in the property panel
213   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/) {
214     if (theName == LINE_ATTR_START) {
215       setPointSelectionMode(SM_SecondPoint, false);
216     }
217     else if (theName == LINE_ATTR_END) {
218       setPointSelectionMode(SM_DonePoint, false);
219     }
220   }
221   keyReleased(theEvent->key());
222 }
223
224 void PartSet_OperationSketchLine::keyReleased(const int theKey)
225 {
226   switch (theKey) {
227     case Qt::Key_Return: {
228       if (myPointSelectionMode == SM_DonePoint)
229       {
230         commit();
231         restartOperation(PartSet_OperationSketchLine::Type(), feature());
232       }
233       //else
234       //  abort();
235       //emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
236     }
237     break;
238     case Qt::Key_Escape: {
239       if (myPointSelectionMode == SM_DonePoint)
240       {
241         commit();
242       }
243       else
244         abort();
245     }
246     default:
247     break;
248   }
249 }
250
251 void PartSet_OperationSketchLine::startOperation()
252 {
253   PartSet_OperationSketchBase::startOperation();
254   setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
255
256   emit multiSelectionEnabled(false);
257 }
258
259 void PartSet_OperationSketchLine::abortOperation()
260 {
261   emit featureConstructed(feature(), FM_Hide);
262   PartSet_OperationSketchBase::abortOperation();
263 }
264
265 void PartSet_OperationSketchLine::stopOperation()
266 {
267   PartSet_OperationSketchBase::stopOperation();
268   emit multiSelectionEnabled(true);
269 }
270
271 void PartSet_OperationSketchLine::afterCommitOperation()
272 {
273   PartSet_OperationSketchBase::afterCommitOperation();  
274   emit featureConstructed(feature(), FM_Deactivation);
275 }
276
277 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature(const bool theFlushMessage)
278 {
279   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
280   if (sketch()) {
281     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
282                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
283
284     aFeature->addSub(aNewFeature);
285   }
286   if (myInitPoint) {
287     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
288     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
289
290     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
291     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
292                                                                 (aData->attribute(LINE_ATTR_START));
293     createConstraint(myInitPoint, aPoint);
294   }
295
296   emit featureConstructed(aNewFeature, FM_Activation);
297   if (theFlushMessage)
298     flushCreated();
299   return aNewFeature;
300 }
301
302 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
303                                                    boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
304 {
305   boost::shared_ptr<ModelAPI_Document> aDoc = document();
306   boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
307
308   if (sketch()) {
309     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
310                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
311     aSketch->addSub(aFeature);
312   }
313
314   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
315
316   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
317         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
318   aRef1->setAttr(thePoint1);
319
320   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
321         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
322   aRef2->setAttr(thePoint2);
323
324   if (aFeature) // TODO: generate an error if feature was not created
325     aFeature->execute();
326 }
327
328 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
329 {
330   std::string aPointArg;
331   switch (myPointSelectionMode)
332   {
333     case SM_FirstPoint:
334       aPointArg = LINE_ATTR_START;
335       break;
336     case SM_SecondPoint:
337       aPointArg = LINE_ATTR_END;
338       break;
339     default:
340       break;
341   }
342
343   boost::shared_ptr<ModelAPI_Feature> aSkFeature = feature();
344
345   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
346   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
347                                                               (aData->attribute(aPointArg));
348   aData = sketch()->data();
349   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
350         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
351
352   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
353   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
354                                                                   aLast = aFeatures.end();
355   for (; anIt != aLast; anIt++) {
356     boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
357     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
358     if (aFPoint)
359       createConstraint(aFPoint, aPoint);
360   }
361 }
362
363 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
364                                                const std::string& theAttribute,
365                                                double& theX, double& theY)
366 {
367   if (!theFeature || theFeature->getKind() != "SketchLine")
368     return;
369   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
370   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
371         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
372   theX = aPoint->x();
373   theY = aPoint->y();
374 }
375
376 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
377                                                boost::shared_ptr<ModelAPI_Feature> theFeature,
378                                                double theX, double theY)
379 {
380   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
381   if (!theFeature || theFeature->getKind() != "SketchLine")
382     return aPoint2D;
383   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
384   
385   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
386         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
387   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
388     aPoint2D = aPoint;
389   else {
390     aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
391     if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
392       aPoint2D = aPoint;
393   }
394   return aPoint2D;
395 }
396
397 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
398                                                double theX, double theY,
399                                                const std::string& theAttribute)
400 {
401   if (!theFeature)
402     return;
403   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
404   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
405         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
406   aPoint->setValue(theX, theY);
407 }
408
409 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
410                                                Handle(V3d_View) theView,
411                                                const std::string& theAttribute)
412 {
413   double aX, anY;
414   PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
415   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
416   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
417         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
418   aPoint->setValue(aX, anY);
419 }
420
421 void PartSet_OperationSketchLine::setPointSelectionMode(const PointSelectionMode& theMode,
422                                                         const bool isToEmitSignal)
423 {
424   myPointSelectionMode = theMode;
425   if (isToEmitSignal) {
426     std::string aName;
427     switch (theMode) {
428       case SM_FirstPoint:
429         aName = LINE_ATTR_START;
430         break;
431       case SM_SecondPoint:
432         aName = LINE_ATTR_END;
433         break;
434       case SM_DonePoint:
435         aName = XGUI::PROP_PANEL_OK;
436         break;
437       default:
438         break;
439     }
440     emit focusActivated(aName);
441   }
442 }