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