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