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