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