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