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