1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
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_WidgetEditor.h>
11 #include "PartSet_SketcherMgr.h"
12 #include "PartSet_MenuMgr.h"
14 #include <ModuleBase_Operation.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_IViewWindow.h>
17 #include <ModuleBase_IPropertyPanel.h>
18 #include <ModuleBase_WidgetEditor.h>
19 #include <ModuleBase_FilterFactory.h>
20 #include <GeomValidators_Edge.h>
21 #include <GeomValidators_EdgeOrVertex.h>
22 #include <GeomValidators_Face.h>
23 #include <GeomValidators_ConstructionComposite.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ShapeValidator.h>
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomDataAPI_Point.h>
35 #include <GeomDataAPI_Dir.h>
37 #include <XGUI_Displayer.h>
38 #include <XGUI_Workshop.h>
39 #include <XGUI_OperationMgr.h>
40 #include <XGUI_PropertyPanel.h>
41 #include <XGUI_ModuleConnector.h>
42 #include <XGUI_ContextMenuMgr.h>
43 #include <XGUI_Tools.h>
45 #include <SketchPlugin_Feature.h>
46 #include <SketchPlugin_Sketch.h>
47 #include <SketchPlugin_Line.h>
48 #include <SketchPlugin_Arc.h>
49 #include <SketchPlugin_Circle.h>
50 #include <SketchPlugin_Point.h>
51 #include <SketchPlugin_ConstraintLength.h>
52 #include <SketchPlugin_ConstraintDistance.h>
53 #include <SketchPlugin_ConstraintParallel.h>
54 #include <SketchPlugin_ConstraintPerpendicular.h>
55 #include <SketchPlugin_ConstraintRadius.h>
57 #include <SketcherPrs_SymbolPrs.h>
58 #include <SketcherPrs_Tools.h>
60 #include <Events_Loop.h>
61 #include <Config_PropManager.h>
62 #include <Config_Keywords.h>
64 #include <StdSelect_TypeOfFace.hxx>
65 #include <TopoDS_Vertex.hxx>
67 #include <TopoDS_Shape.hxx>
68 #include <BRep_Tool.hxx>
69 #include <AIS_Dimension.hxx>
72 #include <QMouseEvent>
75 #include <QApplication>
76 #include <QMessageBox>
77 #include <QMainWindow>
79 #include <GeomAlgoAPI_FaceBuilder.h>
80 #include <GeomDataAPI_Dir.h>
86 /*!Create and return new instance of XGUI_Module*/
87 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
89 return new PartSet_Module(theWshop);
92 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
93 : ModuleBase_IModule(theWshop),
94 myRestartingMode(RM_None), myVisualLayerId(0)
96 mySketchMgr = new PartSet_SketcherMgr(this);
98 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
99 XGUI_Workshop* aWorkshop = aConnector->workshop();
101 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
102 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
103 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
104 this, SLOT(onOperationActivatedByPreselection()));
106 ModuleBase_IViewer* aViewer = theWshop->viewer();
107 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
108 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
109 connect(aViewer, SIGNAL(viewTransformed(int)),
110 SLOT(onViewTransformed(int)));
112 myMenuMgr = new PartSet_MenuMgr(this);
115 PartSet_Module::~PartSet_Module()
117 if (!myDocumentShapeFilter.IsNull())
118 myDocumentShapeFilter.Nullify();
121 void PartSet_Module::registerValidators()
123 //Registering of validators
124 SessionPtr aMgr = ModelAPI_Session::get();
125 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
126 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
127 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
128 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
129 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
130 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
131 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
132 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
133 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
134 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
135 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
137 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
138 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
140 aFactory->registerValidator("GeomValidators_Edge", new GeomValidators_Edge);
141 aFactory->registerValidator("GeomValidators_EdgeOrVertex",
142 new GeomValidators_EdgeOrVertex);
143 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
145 aFactory->registerValidator("GeomValidators_ConstructionComposite",
146 new GeomValidators_ConstructionComposite);
148 aFactory->registerValidator("PartSet_SketchEntityValidator",
149 new PartSet_SketchEntityValidator);
151 aFactory->registerValidator("PartSet_SameTypeAttr",
152 new PartSet_SameTypeAttrValidator);
155 void PartSet_Module::registerFilters()
157 //Registering of selection filters
158 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
159 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
162 void PartSet_Module::registerProperties()
164 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
166 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
167 Config_Prop::Integer, SKETCH_WIDTH);
170 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
172 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
173 mySketchMgr->commitNestedSketch(theOperation);
176 if (theOperation->isEditOperation())
178 // the selection is cleared after commit the create operation
179 // in order to do not use the same selected objects in the restarted operation
180 // for common behaviour, the selection is cleared even if the operation is not restarted
181 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
182 if (!aContext.IsNull())
183 aContext->ClearSelected();
185 /// Restart sketcher operations automatically
186 FeaturePtr aFeature = theOperation->feature();
187 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
188 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
189 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
190 myRestartingMode == RM_EmptyFeatureUsed)) {
191 myLastOperationId = theOperation->id();
192 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
194 launchOperation(myLastOperationId);
196 breakOperationSequence();
199 void PartSet_Module::breakOperationSequence()
201 myLastOperationId = "";
202 myLastFeature = FeaturePtr();
203 myRestartingMode = RM_None;
206 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
208 breakOperationSequence();
211 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
213 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
214 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
215 aViewer->AddZLayer(myVisualLayerId);
216 mySketchMgr->startSketch(theOperation);
218 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
219 mySketchMgr->startNestedSketch(theOperation);
222 if (myDocumentShapeFilter.IsNull())
223 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
224 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
227 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
229 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
230 mySketchMgr->stopSketch(theOperation);
231 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
232 aViewer->RemoveZLayer(myVisualLayerId);
235 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
236 mySketchMgr->stopNestedSketch(theOperation);
238 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
241 ModuleBase_Operation* PartSet_Module::currentOperation() const
243 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
244 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
245 return anOpMgr->currentOperation();
248 bool PartSet_Module::canUndo() const
250 bool aCanUndo = false;
251 SessionPtr aMgr = ModelAPI_Session::get();
252 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
253 aCanUndo = !aMgr->isOperation();
254 if (!aCanUndo) // check the enable state additionally by sketch manager
255 aCanUndo = aMgr->canUndo();
260 bool PartSet_Module::canRedo() const
262 bool aCanRedo = false;
263 SessionPtr aMgr = ModelAPI_Session::get();
264 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
265 aCanRedo = !aMgr->isOperation();
266 if (!aCanRedo) // check the enable state additionally by sketch manager
267 aCanRedo = aMgr->canRedo();
272 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
274 // the sketch manager put the restriction to the objects display
275 return mySketchMgr->canDisplayObject(theObject);
279 bool PartSet_Module::addViewerItems(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
281 return myMenuMgr->addViewerItems(theMenu, theStdActions);
284 bool PartSet_Module::isMouseOverWindow()
286 return mySketchMgr->isMouseOverWindow();
289 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
291 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
292 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
293 // we have to manually activate the sketch label in edit mode
294 aPanel->activateWidget(aPanel->modelWidgets().first());
298 // Restart last operation type
299 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
300 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
301 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
302 // Initialise new line with first point equal to end of previous
303 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
305 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
306 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
307 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
309 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
310 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
311 aWgt->attributeID(), aPoint->x(), aPoint->y());
312 aPanel->activateNextWidget(aPnt2dWgt);
317 // Start editing constraint
318 if (theOperation->isEditOperation()) {
319 // TODO: #391 - to be removed
320 std::string aId = theOperation->id().toStdString();
321 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
322 PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
323 // Find and activate widget for management of point for dimension line position
324 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
325 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
326 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
328 aPanel->activateWidget(aPntWgt);
338 void PartSet_Module::onSelectionChanged()
340 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
344 bool isSketcherOp = false;
345 // An edit operation is enable only if the current opeation is the sketch operation
346 if (mySketchMgr->activeSketch()) {
347 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
348 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
351 // Editing of constraints can be done on selection
352 ModuleBase_ISelection* aSelect = myWorkshop->selection();
353 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
354 if (aSelected.size() == 1) {
355 ModuleBase_ViewerPrs aPrs = aSelected.first();
356 ObjectPtr aObject = aPrs.object();
357 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
359 std::string aId = aFeature->getKind();
360 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
361 (aId == SketchPlugin_ConstraintLength::ID()) ||
362 (aId == SketchPlugin_ConstraintDistance::ID())) {
363 editFeature(aFeature);
370 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
372 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
373 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
374 anOpMgr->onKeyReleased(theEvent);
377 void PartSet_Module::onEnterReleased()
379 myRestartingMode = RM_EmptyFeatureUsed;
382 void PartSet_Module::onOperationActivatedByPreselection()
384 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
385 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
386 // Set final definitions if they are necessary
387 //propertyPanelDefined(aOperation);
389 /// Commit sketcher operations automatically
390 anOperation->commit();
394 void PartSet_Module::onNoMoreWidgets()
396 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
398 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
399 if (myRestartingMode != RM_Forbided)
400 myRestartingMode = RM_LastFeatureUsed;
401 anOperation->commit();
406 void PartSet_Module::onVertexSelected()
408 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
409 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
410 /// If last line finished on vertex the lines creation sequence has to be break
411 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
412 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
413 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
414 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
415 bool aFoundWidget = false;
416 bool aFoundObligatory = false;
417 for (; anIt != aLast && !aFoundObligatory; anIt++) {
419 aFoundWidget = *anIt == anActiveWidget;
421 aFoundObligatory = (*anIt)->isObligatory();
423 if (!aFoundObligatory)
424 myRestartingMode = RM_Forbided;
428 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
429 Config_WidgetAPI* theWidgetApi, std::string theParentId)
431 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
432 XGUI_Workshop* aWorkshop = aConnector->workshop();
433 ModuleBase_ModelWidget* aWgt = NULL;
434 if (theType == "sketch-start-label") {
435 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent,
436 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
437 aLabelWgt->setWorkshop(aWorkshop);
438 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
439 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
440 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
441 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
443 } else if (theType == "sketch-2dpoint_selector") {
444 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
445 aPointWgt->setWorkshop(aWorkshop);
446 aPointWgt->setSketch(mySketchMgr->activeSketch());
447 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
449 } if (theType == "point2ddistance") {
450 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
451 aDistanceWgt->setWorkshop(aWorkshop);
452 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
454 } if (theType == "sketch_shape_selector") {
455 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
456 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
457 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
458 aWgt = aShapeSelectorWgt;
459 } if (theType == WDG_DOUBLEVALUE_EDITOR) {
460 aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
466 bool PartSet_Module::deleteObjects()
468 // 1. check whether the delete should be processed in the module
469 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
470 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
471 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
472 if (!isSketchOp && !isNestedOp)
475 // 2. find selected presentations
476 // selected objects should be collected before the current operation abort because
477 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
478 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
479 XGUI_Workshop* aWorkshop = aConnector->workshop();
480 ModuleBase_ISelection* aSel = workshop()->selection();
481 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
482 // if there are no selected objects in the viewer, that means that the selection in another
483 // place cased this method. It is necessary to return the false value to understande in above
484 // method that delete is not processed
485 if (aSelectedObj.count() == 0)
488 // the active nested sketch operation should be aborted unconditionally
490 anOperation->abort();
492 // 3. start operation
493 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
494 SessionPtr aMgr = ModelAPI_Session::get();
495 aMgr->startOperation(aDescription.toStdString());
497 // 4. delete features
498 // sketch feature should be skipped, only sub-features can be removed
499 // when sketch operation is active
500 std::set<FeaturePtr> anIgnoredFeatures;
501 anIgnoredFeatures.insert(mySketchMgr->activeSketch());
502 aWorkshop->deleteFeatures(aSelectedObj, anIgnoredFeatures);
505 aWorkshop->displayer()->updateViewer();
506 aMgr->finishOperation();
512 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
514 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
515 if (!anAIS.IsNull()) {
516 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
517 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
518 if (!aDim.IsNull()) {
519 aCtx->SetZLayer(aDim, myVisualLayerId);
521 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
523 aCtx->SetZLayer(aCons, myVisualLayerId);
528 void PartSet_Module::onViewTransformed(int theTrsfType)
530 // Set length of arrows constant in pixel size
531 // if the operation is panning or rotate or panglobal then do nothing
532 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
535 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
536 Handle(V3d_View) aView = aViewer->activeView();
538 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
539 XGUI_Workshop* aWorkshop = aConnector->workshop();
540 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
541 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
543 double aLen = aView->Convert(15);
545 SketcherPrs_Tools::setArrowSize(aLen);
546 bool isModified = false;
547 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
548 foreach (AISObjectPtr aAIS, aPrsList) {
549 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
551 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
552 if (!aDim.IsNull()) {
553 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
554 aContext->Redisplay(aDim, false);
559 aDisplayer->updateViewer();