Salome HOME
797fcd11602a123fecb98dfdaa01720fccb55adb
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_Module.h"
4 #include <PartSet_WidgetSketchLabel.h>
5 #include <PartSet_Validators.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_WidgetPoint2d.h>
8 #include <PartSet_WidgetPoint2dDistance.h>
9 #include <PartSet_WidgetShapeSelector.h>
10 #include <PartSet_SketcherMgr.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_FilterFactory.h>
18 #include <ModuleBase_FilterLinearEdge.h>
19 #include <ModuleBase_FilterFace.h>
20 #include <ModuleBase_FilterMulti.h>
21 #include <ModuleBase_FilterCustom.h>
22 #include <ModuleBase_FilterNoConsructionSubShapes.h>
23
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
33
34 #include <XGUI_Displayer.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_OperationMgr.h>
37 #include <XGUI_PropertyPanel.h>
38 #include <XGUI_ModuleConnector.h>
39 #include <XGUI_Tools.h>
40
41 #include <SketchPlugin_Feature.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_Line.h>
44 //#include <SketchPlugin_Arc.h>
45 //#include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintLength.h>
47 #include <SketchPlugin_ConstraintDistance.h>
48 #include <SketchPlugin_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintPerpendicular.h>
50 #include <SketchPlugin_ConstraintRadius.h>
51 //#include <SketchPlugin_ConstraintRigid.h>
52
53 #include <Events_Loop.h>
54
55 #include <StdSelect_TypeOfFace.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS.hxx>
58 #include <TopoDS_Shape.hxx>
59 #include <BRep_Tool.hxx>
60
61 #include <QObject>
62 #include <QMouseEvent>
63 #include <QString>
64 #include <QTimer>
65 #include <QApplication>
66
67 #include <GeomAlgoAPI_FaceBuilder.h>
68 #include <GeomDataAPI_Dir.h>
69
70 #ifdef _DEBUG
71 #include <QDebug>
72 #endif
73
74 /*!Create and return new instance of XGUI_Module*/
75 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
76 {
77   return new PartSet_Module(theWshop);
78 }
79
80 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
81   : ModuleBase_IModule(theWshop), 
82   myRestartingMode(RM_None)
83 {
84   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
85   mySketchMgr = new PartSet_SketcherMgr(this);
86
87   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
88   XGUI_Workshop* aWorkshop = aConnector->workshop();
89
90   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
91   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
92   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
93           this, SLOT(onOperationActivatedByPreselection()));
94
95   ModuleBase_IViewer* aViewer = theWshop->viewer();
96   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
97           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
98 }
99
100 PartSet_Module::~PartSet_Module()
101 {
102   if (!myDocumentShapeFilter.IsNull())
103     myDocumentShapeFilter.Nullify();
104 }
105
106 void PartSet_Module::registerValidators()
107 {
108   //Registering of validators
109   SessionPtr aMgr = ModelAPI_Session::get();
110   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
111   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
112   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
113   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
114   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
115   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
116   aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
117   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
118   aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
119 }
120
121 void PartSet_Module::registerFilters()
122 {
123   //Registering of selection filters
124   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
125   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
126
127   aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
128   aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
129   aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
130   Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
131   aFactory->registerFilter("NoConstructionSubShapesFilter",
132             new ModuleBase_FilterCustom(aSelectFilter));
133 }
134
135 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
136 {
137   if (theOperation->isEditOperation())
138     return;
139   // the selection is cleared after commit the create operation
140   // in order to do not use the same selected objects in the restarted operation
141   // for common behaviour, the selection is cleared even if the operation is not restarted
142   myWorkshop->viewer()->AISContext()->ClearSelected();
143
144   /// Restart sketcher operations automatically
145   FeaturePtr aFeature = theOperation->feature();
146   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
147             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
148   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
149                      myRestartingMode == RM_EmptyFeatureUsed)) {
150     myLastOperationId = theOperation->id();
151     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
152     
153     launchOperation(myLastOperationId);
154   }
155   breakOperationSequence();
156 }
157
158 void PartSet_Module::breakOperationSequence()
159 {
160   myLastOperationId = "";
161   myLastFeature = FeaturePtr();
162   myRestartingMode = RM_None;
163 }
164
165 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
166 {
167   breakOperationSequence();
168 }
169
170 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
171 {
172   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
173     mySketchMgr->startSketch(theOperation);
174   }
175   if (myDocumentShapeFilter.IsNull())
176     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
177   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
178 }
179
180 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
181 {
182   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
183     mySketchMgr->stopSketch(theOperation);
184   }
185   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
186 }
187
188
189 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
190 {
191   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
192   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
193     (theOperation->isEditOperation())) {
194     // we have to manually activate the sketch label in edit mode
195       aPanel->activateWidget(aPanel->modelWidgets().first());
196       return;
197   }
198
199   // Restart last operation type 
200   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
201     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
202     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
203       // Initialise new line with first point equal to end of previous
204       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
205       if (aPnt2dWgt) {
206         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
207         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
208           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
209         if (aPoint) {
210           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
211           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
212             aWgt->attributeID(), aPoint->x(), aPoint->y());
213           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
214         }
215       }
216     }
217   } else {
218     // Start editing constraint
219     if (theOperation->isEditOperation()) {
220       std::string aId = theOperation->id().toStdString();
221       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
222         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
223             (aId == SketchPlugin_ConstraintLength::ID()) || 
224             (aId == SketchPlugin_ConstraintDistance::ID())) {
225           // Find and activate widget for management of point for dimension line position
226           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
227           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
228             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
229             if (aPntWgt) {
230               aPanel->activateWidget(aPntWgt);
231               return;
232             }
233           }
234         } 
235       }
236     }
237   }
238 }
239
240
241 void PartSet_Module::onSelectionChanged()
242 {
243   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
244   if (!aOperation)
245     return;
246
247   bool isSketcherOp = false;
248   // An edit operation is enable only if the current opeation is the sketch operation
249   if (mySketchMgr->activeSketch()) {
250     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
251       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
252   }
253   if (isSketcherOp) {
254     // Editing of constraints can be done on selection
255     ModuleBase_ISelection* aSelect = myWorkshop->selection();
256     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
257     if (aSelected.size() == 1) {
258       ModuleBase_ViewerPrs aPrs = aSelected.first();
259       ObjectPtr aObject = aPrs.object();
260       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
261       if (aFeature) {
262         std::string aId = aFeature->getKind();
263         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
264             (aId == SketchPlugin_ConstraintLength::ID()) || 
265             (aId == SketchPlugin_ConstraintDistance::ID())) {
266           editFeature(aFeature);
267         }
268       }
269     }
270   } 
271 }
272
273 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
274 {
275   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
276   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
277   anOpMgr->onKeyReleased(theEvent);
278 }
279
280 void PartSet_Module::onEnterReleased()
281 {
282   myRestartingMode = RM_EmptyFeatureUsed;
283 }
284
285 void PartSet_Module::onOperationActivatedByPreselection()
286 {
287   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
288   if (!aOperation)
289     return;
290
291   // Set final definitions if they are necessary
292   //propertyPanelDefined(aOperation);
293
294   /// Commit sketcher operations automatically
295   FeaturePtr aFeature = aOperation->feature();
296   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
297             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
298   if (aSPFeature) {
299     aOperation->commit();
300   }
301 }
302
303 void PartSet_Module::onNoMoreWidgets()
304 {
305   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
306   if (aOperation) {
307     /// Restart sketcher operations automatically
308     FeaturePtr aFeature = aOperation->feature();
309     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
310               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
311     if (aSPFeature) {
312       if (myRestartingMode != RM_Forbided)
313         myRestartingMode = RM_LastFeatureUsed;
314       aOperation->commit();
315     }
316   }
317 }
318
319 void PartSet_Module::onVertexSelected()
320 {
321   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
322   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
323     /// If last line finished on vertex the lines creation sequence has to be break
324     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
325     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
326     if (aWidgets.last() == aPanel->activeWidget()) {
327       myRestartingMode = RM_Forbided;
328     }
329   }
330 }
331
332 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
333                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
334                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
335 {
336   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
337   XGUI_Workshop* aWorkshop = aConnector->workshop();
338   if (theType == "sketch-start-label") {
339     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
340     aWgt->setWorkshop(aWorkshop);
341     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
342       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
343     theModelWidgets.append(aWgt);
344     return aWgt->getControl();
345
346   } else if (theType == "sketch-2dpoint_selector") {
347     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
348     aWgt->setWorkshop(aWorkshop);
349     aWgt->setSketch(mySketchMgr->activeSketch());
350
351     connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
352
353     theModelWidgets.append(aWgt);
354     return aWgt->getControl();
355
356   } if (theType == "point2ddistance") {
357     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
358     aWgt->setWorkshop(aWorkshop);
359     aWgt->setSketch(mySketchMgr->activeSketch());
360
361     theModelWidgets.append(aWgt);
362     return aWgt->getControl();
363
364   } if (theType == "sketch_shape_selector") {
365     PartSet_WidgetShapeSelector* aWgt = 
366       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
367     aWgt->setSketcher(mySketchMgr->activeSketch());
368
369     theModelWidgets.append(aWgt);
370     return aWgt->getControl();
371
372   } if (theType == "sketch_constraint_shape_selector") {
373     PartSet_WidgetConstraintShapeSelector* aWgt = 
374       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
375     aWgt->setSketcher(mySketchMgr->activeSketch());
376
377     theModelWidgets.append(aWgt);
378     return aWgt->getControl();
379
380   } else
381     return 0;
382 }
383