Salome HOME
b05715c3847463a83df72c4fdda44ae894ae3c8d
[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   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
143   if (!aContext.IsNull())
144     aContext->ClearSelected();
145
146   /// Restart sketcher operations automatically
147   FeaturePtr aFeature = theOperation->feature();
148   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
149             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
150   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
151                      myRestartingMode == RM_EmptyFeatureUsed)) {
152     myLastOperationId = theOperation->id();
153     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
154     
155     launchOperation(myLastOperationId);
156   }
157   breakOperationSequence();
158 }
159
160 void PartSet_Module::breakOperationSequence()
161 {
162   myLastOperationId = "";
163   myLastFeature = FeaturePtr();
164   myRestartingMode = RM_None;
165 }
166
167 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
168 {
169   breakOperationSequence();
170 }
171
172 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
173 {
174   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
175     mySketchMgr->startSketch(theOperation);
176   }
177   if (myDocumentShapeFilter.IsNull())
178     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
179   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
180 }
181
182 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
183 {
184   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
185     mySketchMgr->stopSketch(theOperation);
186   }
187   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
188 }
189
190 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
191 {
192   bool aCanDisplay = false;
193   if (mySketchMgr->activeSketch()) {
194     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
195
196     if (aFeature.get() != NULL) {
197       if (aFeature == mySketchMgr->activeSketch()) {
198         aCanDisplay = false;
199       }
200       else {
201         aCanDisplay = mySketchMgr->sketchOperationIdList().contains(aFeature->getKind().c_str());
202       }
203     }
204   }
205   else {
206     aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
207   }
208   return aCanDisplay;
209 }
210
211 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
212 {
213   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
214   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
215     (theOperation->isEditOperation())) {
216     // we have to manually activate the sketch label in edit mode
217       aPanel->activateWidget(aPanel->modelWidgets().first());
218       return;
219   }
220
221   // Restart last operation type 
222   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
223     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
224     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
225       // Initialise new line with first point equal to end of previous
226       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
227       if (aPnt2dWgt) {
228         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
229         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
230           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
231         if (aPoint) {
232           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
233           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
234             aWgt->attributeID(), aPoint->x(), aPoint->y());
235           aPanel->activateNextWidget(aPnt2dWgt);
236         }
237       }
238     }
239   } else {
240     // Start editing constraint
241     if (theOperation->isEditOperation()) {
242       // TODO: #391 - to be removed
243       std::string aId = theOperation->id().toStdString();
244       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
245         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
246             (aId == SketchPlugin_ConstraintLength::ID()) || 
247             (aId == SketchPlugin_ConstraintDistance::ID())) {
248           // Find and activate widget for management of point for dimension line position
249           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
250           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
251             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
252             if (aPntWgt) {
253               aPanel->activateWidget(aPntWgt);
254               return;
255             }
256           }
257         } 
258       }
259     }
260   }
261 }
262
263
264 void PartSet_Module::onSelectionChanged()
265 {
266   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
267   if (!aOperation)
268     return;
269
270   bool isSketcherOp = false;
271   // An edit operation is enable only if the current opeation is the sketch operation
272   if (mySketchMgr->activeSketch()) {
273     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
274       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
275   }
276   if (isSketcherOp) {
277     // Editing of constraints can be done on selection
278     ModuleBase_ISelection* aSelect = myWorkshop->selection();
279     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
280     if (aSelected.size() == 1) {
281       ModuleBase_ViewerPrs aPrs = aSelected.first();
282       ObjectPtr aObject = aPrs.object();
283       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
284       if (aFeature) {
285         std::string aId = aFeature->getKind();
286         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
287             (aId == SketchPlugin_ConstraintLength::ID()) || 
288             (aId == SketchPlugin_ConstraintDistance::ID())) {
289           editFeature(aFeature);
290         }
291       }
292     }
293   } 
294 }
295
296 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
297 {
298   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
299   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
300   anOpMgr->onKeyReleased(theEvent);
301 }
302
303 void PartSet_Module::onEnterReleased()
304 {
305   myRestartingMode = RM_EmptyFeatureUsed;
306 }
307
308 void PartSet_Module::onOperationActivatedByPreselection()
309 {
310   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
311   if (!aOperation)
312     return;
313
314   // Set final definitions if they are necessary
315   //propertyPanelDefined(aOperation);
316
317   /// Commit sketcher operations automatically
318   FeaturePtr aFeature = aOperation->feature();
319   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
320             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
321   if (aSPFeature) {
322     aOperation->commit();
323   }
324 }
325
326 void PartSet_Module::onNoMoreWidgets()
327 {
328   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
329   if (aOperation) {
330     /// Restart sketcher operations automatically
331     FeaturePtr aFeature = aOperation->feature();
332     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
333               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
334     if (aSPFeature) {
335       if (myRestartingMode != RM_Forbided)
336         myRestartingMode = RM_LastFeatureUsed;
337       aOperation->commit();
338     }
339   }
340 }
341
342 void PartSet_Module::onVertexSelected()
343 {
344   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
345   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
346     /// If last line finished on vertex the lines creation sequence has to be break
347     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
348     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
349     if (aWidgets.last() == aPanel->activeWidget()) {
350       myRestartingMode = RM_Forbided;
351     }
352   }
353 }
354
355 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
356                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
357                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
358 {
359   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
360   XGUI_Workshop* aWorkshop = aConnector->workshop();
361   if (theType == "sketch-start-label") {
362     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
363     aWgt->setWorkshop(aWorkshop);
364     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
365       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
366     theModelWidgets.append(aWgt);
367     return aWgt->getControl();
368
369   } else if (theType == "sketch-2dpoint_selector") {
370     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
371     aWgt->setWorkshop(aWorkshop);
372     aWgt->setSketch(mySketchMgr->activeSketch());
373
374     connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
375
376     theModelWidgets.append(aWgt);
377     return aWgt->getControl();
378
379   } if (theType == "point2ddistance") {
380     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
381     aWgt->setWorkshop(aWorkshop);
382     aWgt->setSketch(mySketchMgr->activeSketch());
383
384     theModelWidgets.append(aWgt);
385     return aWgt->getControl();
386
387   } if (theType == "sketch_shape_selector") {
388     PartSet_WidgetShapeSelector* aWgt = 
389       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
390     aWgt->setSketcher(mySketchMgr->activeSketch());
391
392     theModelWidgets.append(aWgt);
393     return aWgt->getControl();
394
395   } if (theType == "sketch_constraint_shape_selector") {
396     PartSet_WidgetConstraintShapeSelector* aWgt = 
397       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
398     aWgt->setSketcher(mySketchMgr->activeSketch());
399
400     theModelWidgets.append(aWgt);
401     return aWgt->getControl();
402
403   } else
404     return 0;
405 }
406