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