]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
d94e396a6e0cc5f07f361025a851af08daef5463
[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     if (aFeature.get() != NULL) {
200       if (aFeature == aSketchFeature) {
201         aCanDisplay = false;
202       }
203       else {
204         for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
205           FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
206           std::list<ResultPtr> aResults = aSubFeature->results();
207           std::list<ResultPtr>::const_iterator aIt;
208           for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
209             if (theObject == (*aIt))
210               aCanDisplay = true;
211           }
212           if (aSubFeature == theObject)
213             aCanDisplay = true;
214         }
215       }
216     }
217   }
218   else {
219     aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
220   }
221   return aCanDisplay;
222 }
223
224 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
225 {
226   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
227   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
228     (theOperation->isEditOperation())) {
229     // we have to manually activate the sketch label in edit mode
230       aPanel->activateWidget(aPanel->modelWidgets().first());
231       return;
232   }
233
234   // Restart last operation type 
235   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
236     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
237     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
238       // Initialise new line with first point equal to end of previous
239       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
240       if (aPnt2dWgt) {
241         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
242         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
243           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
244         if (aPoint) {
245           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
246           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
247             aWgt->attributeID(), aPoint->x(), aPoint->y());
248           aPanel->activateNextWidget(aPnt2dWgt);
249         }
250       }
251     }
252   } else {
253     // Start editing constraint
254     if (theOperation->isEditOperation()) {
255       // TODO: #391 - to be removed
256       std::string aId = theOperation->id().toStdString();
257       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
258         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
259             (aId == SketchPlugin_ConstraintLength::ID()) || 
260             (aId == SketchPlugin_ConstraintDistance::ID())) {
261           // Find and activate widget for management of point for dimension line position
262           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
263           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
264             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
265             if (aPntWgt) {
266               aPanel->activateWidget(aPntWgt);
267               return;
268             }
269           }
270         } 
271       }
272     }
273   }
274 }
275
276
277 void PartSet_Module::onSelectionChanged()
278 {
279   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
280   if (!aOperation)
281     return;
282
283   bool isSketcherOp = false;
284   // An edit operation is enable only if the current opeation is the sketch operation
285   if (mySketchMgr->activeSketch()) {
286     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
287       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
288   }
289   if (isSketcherOp) {
290     // Editing of constraints can be done on selection
291     ModuleBase_ISelection* aSelect = myWorkshop->selection();
292     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
293     if (aSelected.size() == 1) {
294       ModuleBase_ViewerPrs aPrs = aSelected.first();
295       ObjectPtr aObject = aPrs.object();
296       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
297       if (aFeature) {
298         std::string aId = aFeature->getKind();
299         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
300             (aId == SketchPlugin_ConstraintLength::ID()) || 
301             (aId == SketchPlugin_ConstraintDistance::ID())) {
302           editFeature(aFeature);
303         }
304       }
305     }
306   } 
307 }
308
309 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
310 {
311   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
312   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
313   anOpMgr->onKeyReleased(theEvent);
314 }
315
316 void PartSet_Module::onEnterReleased()
317 {
318   myRestartingMode = RM_EmptyFeatureUsed;
319 }
320
321 void PartSet_Module::onOperationActivatedByPreselection()
322 {
323   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
324   if (!aOperation)
325     return;
326
327   // Set final definitions if they are necessary
328   //propertyPanelDefined(aOperation);
329
330   /// Commit 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     aOperation->commit();
336   }
337 }
338
339 void PartSet_Module::onNoMoreWidgets()
340 {
341   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
342   if (aOperation) {
343     /// Restart sketcher operations automatically
344     FeaturePtr aFeature = aOperation->feature();
345     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
346               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
347     if (aSPFeature) {
348       if (myRestartingMode != RM_Forbided)
349         myRestartingMode = RM_LastFeatureUsed;
350       aOperation->commit();
351     }
352   }
353 }
354
355 void PartSet_Module::onVertexSelected()
356 {
357   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
358   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
359     /// If last line finished on vertex the lines creation sequence has to be break
360     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
361     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
362     if (aWidgets.last() == aPanel->activeWidget()) {
363       myRestartingMode = RM_Forbided;
364     }
365   }
366 }
367
368 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
369                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
370                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
371 {
372   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
373   XGUI_Workshop* aWorkshop = aConnector->workshop();
374   if (theType == "sketch-start-label") {
375     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
376     aWgt->setWorkshop(aWorkshop);
377     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
378       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
379     theModelWidgets.append(aWgt);
380     return aWgt->getControl();
381
382   } else if (theType == "sketch-2dpoint_selector") {
383     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
384     aWgt->setWorkshop(aWorkshop);
385     aWgt->setSketch(mySketchMgr->activeSketch());
386
387     connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
388
389     theModelWidgets.append(aWgt);
390     return aWgt->getControl();
391
392   } if (theType == "point2ddistance") {
393     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
394     aWgt->setWorkshop(aWorkshop);
395     aWgt->setSketch(mySketchMgr->activeSketch());
396
397     theModelWidgets.append(aWgt);
398     return aWgt->getControl();
399
400   } if (theType == "sketch_shape_selector") {
401     PartSet_WidgetShapeSelector* aWgt = 
402       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
403     aWgt->setSketcher(mySketchMgr->activeSketch());
404
405     theModelWidgets.append(aWgt);
406     return aWgt->getControl();
407
408   } if (theType == "sketch_constraint_shape_selector") {
409     PartSet_WidgetConstraintShapeSelector* aWgt = 
410       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
411     aWgt->setSketcher(mySketchMgr->activeSketch());
412
413     theModelWidgets.append(aWgt);
414     return aWgt->getControl();
415
416   } else
417     return 0;
418 }
419