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