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