Salome HOME
309c6a60793ee566a9d8c5a49a15f3c30f30f007
[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   bool isSketcherOp = false;
232   // An edit operation is enable only if the current opeation is the sketch operation
233   if (aOperation && mySketchMgr->activeSketch()) {
234     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
235       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
236   }
237   if (!isSketcherOp)
238     return;
239
240   // Editing of constraints can be done on selection
241   ModuleBase_ISelection* aSelect = myWorkshop->selection();
242   QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
243   if (aSelected.size() == 1) {
244     ModuleBase_ViewerPrs aPrs = aSelected.first();
245     ObjectPtr aObject = aPrs.object();
246     FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
247     if (aFeature) {
248       std::string aId = aFeature->getKind();
249       if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
250           (aId == SketchPlugin_ConstraintLength::ID()) || 
251           (aId == SketchPlugin_ConstraintDistance::ID())) {
252         editFeature(aFeature);
253       }
254     }
255   }
256 }
257
258
259 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
260 {
261   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
262   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
263   anOpMgr->onKeyReleased(theEvent);
264 }
265
266 void PartSet_Module::onEnterReleased()
267 {
268   myRestartingMode = RM_EmptyFeatureUsed;
269 }
270
271 void PartSet_Module::onOperationActivatedByPreselection()
272 {
273   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
274   if (!aOperation)
275     return;
276
277   // Set final definitions if they are necessary
278   //propertyPanelDefined(aOperation);
279
280   /// Commit sketcher operations automatically
281   FeaturePtr aFeature = aOperation->feature();
282   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
283             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
284   if (aSPFeature) {
285     aOperation->commit();
286   }
287 }
288
289 void PartSet_Module::onNoMoreWidgets()
290 {
291   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
292   if (aOperation) {
293     /// Restart sketcher operations automatically
294     FeaturePtr aFeature = aOperation->feature();
295     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
296               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
297     if (aSPFeature) {
298       if (myRestartingMode != RM_Forbided)
299         myRestartingMode = RM_LastFeatureUsed;
300       aOperation->commit();
301     }
302   }
303 }
304
305 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
306 {
307   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
308   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
309     /// If last line finished on vertex the lines creation sequence has to be break
310     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
311     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
312     if (aWidgets.last() == aPanel->activeWidget()) {
313       myRestartingMode = RM_Forbided;
314     }
315   }
316 }
317
318 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
319                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
320                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
321 {
322   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
323   XGUI_Workshop* aWorkshop = aConnector->workshop();
324   if (theType == "sketch-start-label") {
325     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
326     aWgt->setWorkshop(aWorkshop);
327     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
328       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
329     theModelWidgets.append(aWgt);
330     return aWgt->getControl();
331
332   } else if (theType == "sketch-2dpoint_selector") {
333     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
334     aWgt->setWorkshop(aWorkshop);
335     aWgt->setSketch(mySketchMgr->activeSketch());
336
337     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
338       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
339
340     theModelWidgets.append(aWgt);
341     return aWgt->getControl();
342
343   } if (theType == "point2ddistance") {
344     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
345     aWgt->setWorkshop(aWorkshop);
346     aWgt->setSketch(mySketchMgr->activeSketch());
347
348     theModelWidgets.append(aWgt);
349     return aWgt->getControl();
350
351   } if (theType == "sketch_shape_selector") {
352     PartSet_WidgetShapeSelector* aWgt = 
353       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
354     aWgt->setSketcher(mySketchMgr->activeSketch());
355
356     theModelWidgets.append(aWgt);
357     return aWgt->getControl();
358
359   } if (theType == "sketch_constraint_shape_selector") {
360     PartSet_WidgetConstraintShapeSelector* aWgt = 
361       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
362     aWgt->setSketcher(mySketchMgr->activeSketch());
363
364     theModelWidgets.append(aWgt);
365     return aWgt->getControl();
366
367   } else
368     return 0;
369 }
370