]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
e5a54cb382e6f332335040c2d9afe5bac7e48911
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureCreate.cpp
1 // File:        PartSet_OperationFeatureCreate.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureCreate.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9 #include <PartSet_FeaturePointPrs.h>
10 #include <PartSet_FeatureLinePrs.h>
11 #include <PartSet_FeatureCirclePrs.h>
12 #include <PartSet_FeatureArcPrs.h>
13
14 #include <SketchPlugin_Feature.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Circle.h>
18 #include <SketchPlugin_Arc.h>
19 #include <SketchPlugin_ConstraintDistance.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintRadius.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24
25 #include <GeomAPI_Pnt2d.h>
26
27 #include <ModuleBase_OperationDescription.h>
28 #include <ModuleBase_WidgetPoint2D.h>
29 #include <ModuleBase_WidgetFeature.h>
30 #include <ModuleBase_WidgetPoint2dDistance.h>
31
32 #include <XGUI_ViewerPrs.h>
33 #include <XGUI_Constants.h>
34
35 #include <V3d_View.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS.hxx>
38 #include <BRep_Tool.hxx>
39
40 #ifdef _DEBUG
41 #include <QDebug>
42 #endif
43
44 #include <QMouseEvent>
45
46 using namespace std;
47
48 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
49                                                                QObject* theParent,
50                                                                  FeaturePtr theFeature)
51 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
52 {
53 }
54
55 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
56 {
57 }
58
59 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
60 {
61   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
62          theId == SKETCH_CIRCLE_KIND ||
63          theId == SKETCH_ARC_KIND ||
64          theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
65          theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
66          theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
67          theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
68          theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND;
69 }
70
71 bool PartSet_OperationFeatureCreate::canBeCommitted() const
72 {
73   return !myActiveWidget;
74 }
75
76 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
77 {
78   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
79 }
80
81 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
82 {
83   std::list<int> aModes;
84   if (theFeature != feature())
85     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
86   return aModes;
87 }
88
89 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
90                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
91                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
92 {
93   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
94     return;
95   myInitFeature = theFeature;
96 }
97
98 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
99 {
100   return mySketch;
101 }
102
103 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104                                                 const std::list<XGUI_ViewerPrs>& theSelected,
105                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
106 {
107   if (canBeCommitted())
108   {
109     // if the point creation is finished, the next mouse release should commit the modification
110     // the next release can happens by double click in the viewer
111     commit();
112     restartOperation(feature()->getKind(), feature());
113     return;
114   }
115
116   double aX = 0, anY = 0;
117
118   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
119   if (theSelected.empty()) {
120     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
121   }
122   else {
123     XGUI_ViewerPrs aPrs = theSelected.front();
124     const TopoDS_Shape& aShape = aPrs.shape();
125     if (!aShape.IsNull()) // the point is selected
126     {
127       if (aShape.ShapeType() == TopAbs_VERTEX) {
128         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
129         if (!aVertex.IsNull()) {
130           aPoint = BRep_Tool::Pnt(aVertex);
131           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
132
133           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
134                                         aX, anY);
135         }
136       }
137       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
138       {
139         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140         // move to selected line
141         if (feature()->getKind() == SKETCH_LINE_KIND) {
142           //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
143           //                       boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
144           //if (aLinePrs) {
145           //  FeaturePtr aFeature = aPrs.feature();
146             //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
147           //}
148         }
149       }
150     }
151   }
152   bool isApplyed = false;
153   if (isPointWidget())
154     isApplyed = setWidgetPoint(aX, anY);
155   else {
156     if (!theSelected.empty()) {
157       XGUI_ViewerPrs aPrs = theSelected.front();
158       FeaturePtr aFeature = aPrs.feature();
159       if (aFeature)
160         isApplyed = setWidgetFeature(aFeature);
161     }
162   }
163   if (isApplyed) {
164     flushUpdated();
165     emit activateNextWidget(myActiveWidget);
166   }
167 }
168
169 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
170 {
171   if (canBeCommitted()) {
172     commit();
173     restartOperation(feature()->getKind(), feature());
174   }
175   else {
176     double aX, anY;
177     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
178     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
179     setWidgetPoint(aX, anY);
180     flushUpdated();
181   }
182 }
183
184 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
185 {
186   int aKeyType = theEvent->key();
187   // the second point should be activated by any modification in the property panel
188   if (!theName.empty())
189   {
190     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
191   }
192   keyReleased(theEvent->key());
193 }
194
195 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
196 {
197   myActiveWidget = theWidget;
198
199   if (myInitFeature && myActiveWidget) {
200     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
201     if (aWgt)
202       aWgt->initFromPrevious(myInitFeature);
203     myInitFeature = FeaturePtr();
204     emit activateNextWidget(myActiveWidget);
205   }
206 }
207
208 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
209 {
210   switch (theKey) {
211     case Qt::Key_Return: {
212       if (canBeCommitted())
213       {
214         commit();
215         // it start a new line creation at a free point
216         restartOperation(feature()->getKind(), FeaturePtr());
217       }
218     }
219     break;
220     case Qt::Key_Escape: {
221       if (canBeCommitted())
222       {
223         commit();
224       }
225       else
226       {
227         abort();
228       }
229     }
230     default:
231     break;
232   }
233 }
234
235 void PartSet_OperationFeatureCreate::startOperation()
236 {
237   PartSet_OperationSketchBase::startOperation();
238   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
239
240   emit multiSelectionEnabled(false);
241 }
242
243 void PartSet_OperationFeatureCreate::abortOperation()
244 {
245   emit featureConstructed(feature(), FM_Hide);
246   PartSet_OperationSketchBase::abortOperation();
247 }
248
249 void PartSet_OperationFeatureCreate::stopOperation()
250 {
251   PartSet_OperationSketchBase::stopOperation();
252   emit multiSelectionEnabled(true);
253 }
254
255 void PartSet_OperationFeatureCreate::afterCommitOperation()
256 {
257   PartSet_OperationSketchBase::afterCommitOperation();  
258   emit featureConstructed(feature(), FM_Deactivation);
259 }
260
261 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
262 {
263   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
264   if (sketch()) {
265     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
266                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
267
268     aFeature->addSub(aNewFeature);
269   }
270   //myFeaturePrs->init(aNewFeature);
271   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
272
273   emit featureConstructed(aNewFeature, FM_Activation);
274   if (theFlushMessage)
275     flushCreated();
276   return aNewFeature;
277 }
278
279 bool PartSet_OperationFeatureCreate::isPointWidget() const
280 {
281   return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget) ||
282          dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
283 }
284
285 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
286 {
287   boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
288   ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
289   if (aWidget) {
290     aWidget->setPoint(aPoint);
291     return true;
292   } else {
293     ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
294     if (aWgt) {
295       aWgt->setPoint(feature(), aPoint);
296       return true;
297     }
298   }
299   return false;
300 }
301
302 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
303 {
304   ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
305   if (!aWidget)
306     return false;
307
308   return aWidget->setFeature(theFeature);
309 }