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