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