]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketchLine.cpp
Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[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       myPointSelectionMode = SM_SecondPoint;
148     }
149     break;
150     case SM_SecondPoint: {
151       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
152       myPointSelectionMode = SM_DonePoint;
153     }
154     break;
155     default:
156       break;
157   }
158 }
159
160 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
161 {
162   switch (myPointSelectionMode)
163   {
164     case SM_FirstPoint: {
165       double aX, anY;
166       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
167       PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
168       setLinePoint(feature(), aX, anY, LINE_ATTR_START);
169       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
170     }
171     break;
172     case SM_SecondPoint:
173     {
174       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
175       setLinePoint(aPoint, theView, LINE_ATTR_END);
176     }
177     break;
178     case SM_DonePoint:
179     {
180       commit();
181       emit featureConstructed(feature(), FM_Deactivation);
182       emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
183     }
184     default:
185       break;
186   }
187 }
188
189 void PartSet_OperationSketchLine::keyReleased(const int theKey)
190 {
191   switch (theKey) {
192     case Qt::Key_Return: {
193       if (myPointSelectionMode == SM_DonePoint)
194       {
195         commit();
196         emit featureConstructed(feature(), FM_Deactivation);
197       }
198       else
199         abort();
200       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
201     }
202     break;
203     case Qt::Key_Escape: {
204       if (myPointSelectionMode == SM_DonePoint)
205       {
206         commit();
207         emit featureConstructed(feature(), FM_Deactivation);
208       }
209       else
210         abort();
211     }
212     default:
213     break;
214   }
215 }
216
217 void PartSet_OperationSketchLine::startOperation()
218 {
219   PartSet_OperationSketchBase::startOperation();
220   myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
221   emit multiSelectionEnabled(false);
222 }
223
224 void PartSet_OperationSketchLine::abortOperation()
225 {
226   emit featureConstructed(feature(), FM_Hide);
227   PartSet_OperationSketchBase::abortOperation();
228 }
229
230 void PartSet_OperationSketchLine::stopOperation()
231 {
232   PartSet_OperationSketchBase::stopOperation();
233   emit multiSelectionEnabled(true);
234 }
235
236 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
237 {
238   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
239   if (sketch()) {
240     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
241                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
242
243     aFeature->addSub(aNewFeature);
244   }
245   if (myInitPoint) {
246     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
247     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
248
249     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
250     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
251                                                                 (aData->attribute(LINE_ATTR_START));
252     createConstraint(myInitPoint, aPoint);
253   }
254
255   emit featureConstructed(aNewFeature, FM_Activation);
256   return aNewFeature;
257 }
258
259 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
260                                                    boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
261 {
262   boost::shared_ptr<ModelAPI_Document> aDoc = document();
263   boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
264
265   if (sketch()) {
266     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
267                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
268     aSketch->addSub(aFeature);
269   }
270
271   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
272
273   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
274         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
275   aRef1->setAttr(thePoint1);
276
277   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
278         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
279   aRef2->setAttr(thePoint2);
280
281   if (aFeature) // TODO: generate an error if feature was not created
282     aFeature->execute();
283 }
284
285 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
286 {
287   std::string aPointArg;
288   switch (myPointSelectionMode)
289   {
290     case SM_FirstPoint:
291       aPointArg = LINE_ATTR_START;
292       break;
293     case SM_SecondPoint:
294       aPointArg = LINE_ATTR_END;
295       break;
296   }
297
298   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
299   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
300                                                               (aData->attribute(aPointArg));
301   aData = sketch()->data();
302   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
303         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
304
305   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
306   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
307                                                                   aLast = aFeatures.end();
308   for (; anIt != aLast; anIt++) {
309     boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
310     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
311     if (aFPoint)
312       createConstraint(aFPoint, aPoint);
313   }
314 }
315
316 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
317                                                const std::string& theAttribute,
318                                                double& theX, double& theY)
319 {
320   if (!theFeature || theFeature->getKind() != "SketchLine")
321     return;
322   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
323   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
324         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
325   theX = aPoint->x();
326   theY = aPoint->y();
327 }
328
329 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
330                                                boost::shared_ptr<ModelAPI_Feature> theFeature,
331                                                double theX, double theY)
332 {
333   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
334   if (!theFeature || theFeature->getKind() != "SketchLine")
335     return aPoint2D;
336   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
337   
338   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
339         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
340   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
341     aPoint2D = aPoint;
342   else {
343     aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
344     if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
345       aPoint2D = aPoint;
346   }
347   return aPoint2D;
348 }
349
350 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
351                                                double theX, double theY,
352                                                const std::string& theAttribute)
353 {
354   if (!theFeature)
355     return;
356   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
357   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
358         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
359   aPoint->setValue(theX, theY);
360 }
361
362 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
363                                                Handle(V3d_View) theView,
364                                                const std::string& theAttribute)
365 {
366   double aX, anY;
367   PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
368   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
369   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
370         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
371   aPoint->setValue(aX, anY);
372 }