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