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