]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
ba997dcd37879ac691669d57c1304512aecd2242
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include "PartSet_Module.h"
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_WidgetSketchLabel.h>
4 #include <PartSet_Validators.h>
5 #include <PartSet_Tools.h>
6 #include <PartSet_WidgetPoint2D.h>
7 #include <PartSet_WidgetPoint2dDistance.h>
8 #include <PartSet_WidgetShapeSelector.h>
9
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IPropertyPanel.h>
14
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Session.h>
20
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Point.h>
23 #include <GeomDataAPI_Dir.h>
24
25 #include <XGUI_MainWindow.h>
26 #include <XGUI_Displayer.h>
27 #include <XGUI_Viewer.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_OperationMgr.h>
30 #include <XGUI_ViewPort.h>
31 #include <XGUI_ActionsMgr.h>
32 #include <XGUI_ViewerProxy.h>
33 #include <XGUI_ContextMenuMgr.h>
34 #include <XGUI_PropertyPanel.h>
35 #include <XGUI_ModuleConnector.h>
36 #include <XGUI_Tools.h>
37
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Sketch.h>
40 #include <SketchPlugin_Point.h>
41 #include <SketchPlugin_Arc.h>
42 #include <SketchPlugin_Circle.h>
43 #include <SketchPlugin_ConstraintLength.h>
44 #include <SketchPlugin_ConstraintDistance.h>
45 #include <SketchPlugin_ConstraintParallel.h>
46 #include <SketchPlugin_ConstraintPerpendicular.h>
47 #include <SketchPlugin_ConstraintRadius.h>
48 #include <SketchPlugin_ConstraintRigid.h>
49
50 #include <Events_Loop.h>
51 #include <StdSelect_TypeOfFace.hxx>
52
53 #include <QObject>
54 #include <QMouseEvent>
55 #include <QString>
56 #include <QTimer>
57 #include <QApplication>
58
59 #include <GeomAlgoAPI_FaceBuilder.h>
60 #include <GeomDataAPI_Dir.h>
61
62 #ifdef _DEBUG
63 #include <QDebug>
64 #endif
65
66
67 /*!Create and return new instance of XGUI_Module*/
68 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
69 {
70   return new PartSet_Module(theWshop);
71 }
72
73 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
74   : ModuleBase_IModule(theWshop), 
75   myIsDragging(false), myRestartingMode(true), myDragDone(false)
76 {
77   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
78   ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
79   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
80           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
81
82   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
83           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
84
85   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
86           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
87
88 }
89
90 PartSet_Module::~PartSet_Module()
91 {
92 }
93
94 void PartSet_Module::registerValidators()
95 {
96   //Registering of validators
97   SessionPtr aMgr = ModelAPI_Session::get();
98   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
99   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
100   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
101   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
102   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
103   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
104 }
105
106
107 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation) 
108 {
109   if (theOperation->isEditOperation())
110     return;
111   /// Restart sketcher operations automatically
112   FeaturePtr aFeature = theOperation->feature();
113   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
114             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
115   if (aSPFeature && myRestartingMode) {
116     myLastOperationId = theOperation->id();
117     myLastFeature = theOperation->feature();
118     launchOperation(myLastOperationId);
119   } else {
120     breakOperationSequence();
121   }
122 }
123
124 void PartSet_Module::breakOperationSequence()
125 {
126   myLastOperationId = "";
127   myLastFeature = FeaturePtr();
128   myRestartingMode = false;
129
130 }
131
132 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
133 {
134   breakOperationSequence();
135 }
136
137 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
138 {
139   myRestartingMode = true;
140   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
141     // Display all sketcher sub-Objects
142     myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
143     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
144     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
145
146     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
147       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
148       std::list<ResultPtr> aResults = aFeature->results();
149       std::list<ResultPtr>::const_iterator aIt;
150       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
151         aDisplayer->display((*aIt), false);
152       }
153       aDisplayer->display(aFeature);
154     }
155     // Hide sketcher result
156     std::list<ResultPtr> aResults = myCurrentSketch->results();
157     std::list<ResultPtr>::const_iterator aIt;
158     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
159       aDisplayer->erase((*aIt), false);
160     }
161     aDisplayer->erase(myCurrentSketch);
162
163
164     if (myPlaneFilter.IsNull()) 
165       myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
166     myWorkshop->viewer()->addSelectionFilter(myPlaneFilter);
167     if (theOperation->isEditOperation()) {
168       // If it is editing of sketch then it means that plane is already defined
169       std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
170       myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
171     }
172   }
173 }
174
175 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
176 {
177   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
178     DataPtr aData = myCurrentSketch->data();
179     if ((!aData) || (!aData->isValid())) {
180       // The sketch was aborted
181       myCurrentSketch = CompositeFeaturePtr();
182       return; 
183     }
184     // Hide all sketcher sub-Objects
185     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
186     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
187     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
188       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
189       std::list<ResultPtr> aResults = aFeature->results();
190       std::list<ResultPtr>::const_iterator aIt;
191       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
192         aDisplayer->erase((*aIt), false);
193       }
194       aDisplayer->erase(aFeature, false);
195     }
196     // Display sketcher result
197     std::list<ResultPtr> aResults = myCurrentSketch->results();
198     std::list<ResultPtr>::const_iterator aIt;
199     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
200       aDisplayer->display((*aIt), false);
201     }
202     aDisplayer->display(myCurrentSketch);
203     
204     myCurrentSketch = CompositeFeaturePtr();
205     myWorkshop->viewer()->removeSelectionFilter(myPlaneFilter);
206   }
207 }
208
209 void PartSet_Module::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
210 {
211   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
212 }
213
214
215 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
216 {
217   // Restart last operation type 
218   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
219     ModuleBase_ModelWidget* aWgt = theOperation->propertyPanel()->activeWidget();
220     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
221       // Initialise new line with first point equal to end of previous
222       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
223       if (aPnt2dWgt) {
224         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
225         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
226           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
227         if (aPoint) {
228           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
229           PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(), 
230             aWgt->attributeID(), aPoint->x(), aPoint->y());
231           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
232         }
233       }
234     }
235   } else {
236     // Start editing constraint
237     if (theOperation->isEditOperation()) {
238       std::string aId = theOperation->id().toStdString();
239       if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
240           (aId == SketchPlugin_ConstraintLength::ID()) || 
241           (aId == SketchPlugin_ConstraintDistance::ID())) {
242         ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
243         // Find and activate widget for management of point for dimension line position
244         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
245         foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
246           PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
247           if (aPntWgt) {
248             aPanel->activateWidget(aPntWgt);
249             return;
250           }
251         }
252       }
253     }
254   }
255 }
256
257
258 void PartSet_Module::onSelectionChanged()
259 {
260   // Editing of constraints can be done on selection
261   ModuleBase_ISelection* aSelect = myWorkshop->selection();
262   QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
263   if (aSelected.size() == 1) {
264     ModuleBase_ViewerPrs aPrs = aSelected.first();
265     ObjectPtr aObject = aPrs.object();
266     FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
267     if (aFeature) {
268       std::string aId = aFeature->getKind();
269       if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
270           (aId == SketchPlugin_ConstraintLength::ID()) || 
271           (aId == SketchPlugin_ConstraintDistance::ID())) {
272         editFeature(aFeature);
273       }
274     }
275   }
276 }
277
278 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
279 {
280   if (!(theEvent->buttons() & Qt::LeftButton))
281     return;
282   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
283   // Use only for sketch operations
284   if (aOperation && myCurrentSketch) {
285     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
286       return;
287
288     bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
289     bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
290
291     // Avoid non-sketch operations
292     if ((!isSketchOpe) && (!isSketcher))
293       return;
294
295     bool isEditing = aOperation->isEditOperation();
296
297     // Ignore creation sketch operation
298     if ((!isSketcher) && (!isEditing))
299       return;
300
301     // Remember highlighted objects for editing
302     ModuleBase_ISelection* aSelect = myWorkshop->selection();
303     QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
304      myEditingFeatures.clear();
305     if (aObjects.size() > 0) {
306       foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
307         FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
308         if (aFeature)
309           myEditingFeatures.append(aFeature);
310       }
311     } 
312     // If nothing highlighted - return
313     if (myEditingFeatures.size() == 0)
314       return;
315
316     if (isSketcher) {
317       CompositeFeaturePtr aSketch = 
318         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aOperation->feature());
319       if (!PartSet_Tools::sketchPlane(aSketch))
320         return;
321       
322       //myCurrentSketch = aOperation->feature();
323       myIsDragging = true;
324       get2dPoint(theWnd, theEvent, myCurX, myCurY);
325       myDragDone = false;
326       myWorkshop->viewer()->enableSelection(false);
327
328       launchEditing();
329
330     } else if (isSketchOpe && isEditing) {
331       aOperation->abort();
332
333       myIsDragging = true;
334       get2dPoint(theWnd, theEvent, myCurX, myCurY);
335       myDragDone = false;
336       myWorkshop->viewer()->enableSelection(false);
337
338       // This is necessary in order to finalize previous operation
339       QApplication::processEvents();
340       launchEditing();
341     }
342   }
343 }
344
345
346 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
347                                 double& theX, double& theY)
348 {
349   Handle(V3d_View) aView = theWnd->v3dView();
350   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
351   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
352 }
353
354
355 void PartSet_Module::launchEditing()
356 {
357   if (myEditingFeatures.size() == 1) {
358     FeaturePtr aFeature = myEditingFeatures.first();
359     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
360               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
361     if (aSPFeature) {
362       editFeature(aSPFeature);
363     }
364   }
365 }
366
367 /// Returns new instance of operation object (used in createOperation for customization)
368 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
369 {
370   if (theFeatureId == PartSet_OperationSketch::Type()) {
371     return new PartSet_OperationSketch(theFeatureId.c_str(), this);
372   }
373   return ModuleBase_IModule::getNewOperation(theFeatureId);
374 }
375
376
377 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
378 {
379   if (myIsDragging) {
380     myIsDragging = false;
381     myWorkshop->viewer()->enableSelection(true);
382     if (myDragDone)
383       myWorkshop->currentOperation()->commit();
384   }
385 }
386
387
388 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
389 {
390   if (myIsDragging) {
391     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
392     if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
393       return; // No edit operation activated
394
395     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
396     Handle(V3d_View) aView = theWnd->v3dView();
397     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
398     double aX, aY;
399     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
400     double dX =  aX - myCurX;
401     double dY =  aY - myCurY;
402
403     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
404       std::dynamic_pointer_cast<SketchPlugin_Feature>(aOperation->feature());
405     if (aSketchFeature) { 
406       aSketchFeature->move(dX, dY);
407       ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
408       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
409     }/* else { // Alternative case for moving
410       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
411       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
412       foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
413         PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
414         if (aWgt2d) {
415           aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
416         }
417       }
418     }*/
419     myDragDone = true;
420     myCurX = aX;
421     myCurY = aY;
422   }
423 }
424
425
426 QStringList PartSet_Module::sketchOperationIdList() const
427 {
428   QStringList aIds;
429   aIds << SketchPlugin_Line::ID().c_str();
430   aIds << SketchPlugin_Point::ID().c_str();
431   aIds << SketchPlugin_Arc::ID().c_str();
432   aIds << SketchPlugin_Circle::ID().c_str();
433   aIds << SketchPlugin_ConstraintLength::ID().c_str();
434   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
435   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
436   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
437   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
438   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
439   return aIds;
440 }
441
442 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
443 {
444   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
445   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
446     /// If last line finished on vertex the lines creation sequence has to be break
447     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
448     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
449     if (aWidgets.last() == aPanel->activeWidget()) {
450       breakOperationSequence();
451     }
452   }
453 }
454
455 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
456                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
457                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
458 {
459   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
460   XGUI_Workshop* aWorkshop = aConnector->workshop();
461   if (theType == "sketch-start-label") {
462     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
463     aWgt->setWorkshop(aWorkshop);
464     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
465       this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
466     theModelWidgets.append(aWgt);
467     return aWgt->getControl();
468
469   } else if (theType == "sketch-2dpoint_selector") {
470     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
471     aWgt->setWorkshop(aWorkshop);
472     aWgt->setSketch(myCurrentSketch);
473
474     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
475       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
476
477     theModelWidgets.append(aWgt);
478     return aWgt->getControl();
479
480   } if (theType == "point2ddistance") {
481     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
482     aWgt->setWorkshop(aWorkshop);
483     aWgt->setSketch(myCurrentSketch);
484
485     theModelWidgets.append(aWgt);
486     return aWgt->getControl();
487
488   } if (theType == "sketch_shape_selector") {
489     PartSet_WidgetShapeSelector* aWgt = 
490       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
491     aWgt->setSketcher(myCurrentSketch);
492
493     theModelWidgets.append(aWgt);
494     return aWgt->getControl();
495
496   } if (theType == "sketch_constraint_shape_selector") {
497     PartSet_WidgetConstraintShapeSelector* aWgt = 
498       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
499     aWgt->setSketcher(myCurrentSketch);
500
501     theModelWidgets.append(aWgt);
502     return aWgt->getControl();
503
504   }else
505     return 0;
506 }