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