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