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_WidgetPoint2DFlyout.h"
10 #include "PartSet_WidgetShapeSelector.h"
11 #include "PartSet_WidgetPoint2dAngle.h"
12 #include "PartSet_WidgetMultiSelector.h"
13 #include "PartSet_WidgetEditor.h"
14 #include "PartSet_WidgetFileSelector.h"
15 #include "PartSet_WidgetSketchCreator.h"
16 #include "PartSet_SketcherMgr.h"
17 #include "PartSet_MenuMgr.h"
18 #include "PartSet_CustomPrs.h"
19 #include "PartSet_IconFactory.h"
21 #include "PartSet_Filters.h"
22 #include "PartSet_FilterInfinite.h"
24 #include <PartSetPlugin_Remove.h>
25 #include <PartSetPlugin_Part.h>
26 #include <PartSetPlugin_Duplicate.h>
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_OperationAction.h>
30 #include <ModuleBase_IViewer.h>
31 #include <ModuleBase_IViewWindow.h>
32 #include <ModuleBase_IPropertyPanel.h>
33 #include <ModuleBase_WidgetEditor.h>
34 #include <ModuleBase_WidgetValidated.h>
35 #include <ModuleBase_FilterFactory.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_OperationFeature.h>
39 #include <ModelAPI_Object.h>
40 #include <ModelAPI_Events.h>
41 #include <ModelAPI_Validator.h>
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_ResultBody.h>
45 #include <ModelAPI_AttributeString.h>
47 #include <GeomDataAPI_Point2D.h>
48 #include <GeomDataAPI_Point.h>
49 #include <GeomDataAPI_Dir.h>
51 #include <XGUI_Displayer.h>
52 #include <XGUI_Workshop.h>
53 #include <XGUI_OperationMgr.h>
54 #include <XGUI_PropertyPanel.h>
55 #include <XGUI_ModuleConnector.h>
56 #include <XGUI_ContextMenuMgr.h>
57 #include <XGUI_Tools.h>
58 #include <XGUI_ObjectsBrowser.h>
59 #include <XGUI_SelectionMgr.h>
60 #include <XGUI_DataModel.h>
61 #include <XGUI_ErrorMgr.h>
63 #include <SketchPlugin_Feature.h>
64 #include <SketchPlugin_Sketch.h>
65 #include <SketchPlugin_Line.h>
66 #include <SketchPlugin_Arc.h>
67 #include <SketchPlugin_Circle.h>
68 #include <SketchPlugin_Point.h>
69 #include <SketchPlugin_ConstraintAngle.h>
70 #include <SketchPlugin_ConstraintLength.h>
71 #include <SketchPlugin_ConstraintDistance.h>
72 #include <SketchPlugin_ConstraintParallel.h>
73 #include <SketchPlugin_ConstraintPerpendicular.h>
74 #include <SketchPlugin_ConstraintRadius.h>
76 #include <SketcherPrs_SymbolPrs.h>
77 #include <SketcherPrs_Tools.h>
79 #include <Events_Loop.h>
80 #include <Config_PropManager.h>
81 #include <Config_Keywords.h>
83 #include <StdSelect_TypeOfFace.hxx>
84 #include <TopoDS_Vertex.hxx>
86 #include <TopoDS_Shape.hxx>
87 #include <BRep_Tool.hxx>
88 #include <AIS_Dimension.hxx>
91 #include <QMouseEvent>
94 #include <QApplication>
95 #include <QMessageBox>
96 #include <QMainWindow>
99 #include <GeomAlgoAPI_FaceBuilder.h>
100 #include <GeomDataAPI_Dir.h>
102 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
108 /*!Create and return new instance of XGUI_Module*/
109 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
111 return new PartSet_Module(theWshop);
114 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
115 : ModuleBase_IModule(theWshop),
116 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
118 new PartSet_IconFactory();
120 mySketchMgr = new PartSet_SketcherMgr(this);
122 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
123 XGUI_Workshop* aWorkshop = aConnector->workshop();
125 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
126 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
127 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
128 this, SLOT(onOperationActivatedByPreselection()));
130 ModuleBase_IViewer* aViewer = theWshop->viewer();
131 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
132 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
133 connect(aViewer, SIGNAL(viewTransformed(int)),
134 SLOT(onViewTransformed(int)));
135 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
136 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
138 myMenuMgr = new PartSet_MenuMgr(this);
139 myCustomPrs = new PartSet_CustomPrs(theWshop);
141 Events_Loop* aLoop = Events_Loop::loop();
142 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
144 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
145 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
148 PartSet_Module::~PartSet_Module()
150 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
151 for (; aIt.More(); aIt.Next()) {
152 Handle(SelectMgr_Filter) aFilter = aIt.Value();
153 if (!aFilter.IsNull())
159 void PartSet_Module::activateSelectionFilters()
161 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
162 for (; aIt.More(); aIt.Next()) {
163 Handle(SelectMgr_Filter) aFilter = aIt.Value();
164 if (!aFilter.IsNull())
165 myWorkshop->viewer()->addSelectionFilter(aFilter);
169 void PartSet_Module::deactivateSelectionFilters()
171 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
172 for (; aIt.More(); aIt.Next()) {
173 Handle(SelectMgr_Filter) aFilter = aIt.Value();
174 if (!aFilter.IsNull())
175 myWorkshop->viewer()->removeSelectionFilter(aFilter);
179 void PartSet_Module::registerValidators()
181 //Registering of validators
182 SessionPtr aMgr = ModelAPI_Session::get();
183 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
184 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
185 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
186 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
187 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
188 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
189 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
190 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
191 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
192 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
193 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
194 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
195 aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
196 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
197 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
198 aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator);
201 void PartSet_Module::registerFilters()
203 //Registering of selection filters
204 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
205 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
208 void PartSet_Module::registerProperties()
210 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
212 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
213 Config_Prop::Integer, SKETCH_WIDTH);
216 void PartSet_Module::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
218 mySketchMgr->connectToPropertyPanel(theWidget, isToConnect);
221 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
223 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
224 mySketchMgr->commitNestedSketch(theOperation);
227 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
228 if (!aFOperation || aFOperation->isEditOperation())
230 // the selection is cleared after commit the create operation
231 // in order to do not use the same selected objects in the restarted operation
232 // for common behaviour, the selection is cleared even if the operation is not restarted
233 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
234 XGUI_Workshop* aWorkshop = aConnector->workshop();
235 aWorkshop->selector()->clearSelection();
237 /// Restart sketcher operations automatically
238 FeaturePtr aFeature = aFOperation->feature();
239 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
240 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
241 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
242 myRestartingMode == RM_EmptyFeatureUsed)) {
243 myLastOperationId = aFOperation->id();
244 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
245 if (!sketchMgr()->sketchSolverError())
246 launchOperation(myLastOperationId);
248 breakOperationSequence();
251 void PartSet_Module::breakOperationSequence()
253 myLastOperationId = "";
254 myLastFeature = FeaturePtr();
255 myRestartingMode = RM_None;
258 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
260 breakOperationSequence();
263 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
265 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
266 mySketchMgr->startSketch(theOperation);
268 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
269 mySketchMgr->startNestedSketch(theOperation);
272 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
274 myCustomPrs->activate(aFOperation->feature(), true);
277 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
279 ModuleBase_IModule::operationResumed(theOperation);
281 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
283 myCustomPrs->activate(aFOperation->feature(), true);
286 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
288 bool isModified = myCustomPrs->deactivate(false);
290 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
291 mySketchMgr->stopSketch(theOperation);
293 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
294 mySketchMgr->stopNestedSketch(theOperation);
297 //VSV: Viewer is updated on feature update and redisplay
299 // XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
300 // XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
301 // aDisplayer->updateViewer();
303 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
306 ModuleBase_Operation* PartSet_Module::currentOperation() const
308 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
309 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
310 return anOpMgr->currentOperation();
313 bool PartSet_Module::canUndo() const
315 bool aCanUndo = false;
316 SessionPtr aMgr = ModelAPI_Session::get();
317 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
318 aCanUndo = !aMgr->isOperation();
319 if (!aCanUndo) // check the enable state additionally by sketch manager
320 aCanUndo = aMgr->canUndo();
325 bool PartSet_Module::canRedo() const
327 bool aCanRedo = false;
328 SessionPtr aMgr = ModelAPI_Session::get();
329 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
330 aCanRedo = !aMgr->isOperation();
331 if (!aCanRedo) // check the enable state additionally by sketch manager
332 aCanRedo = aMgr->canRedo();
337 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
340 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
341 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
343 // part features are removed in the PartSet module only.
344 if (aFeature->getKind() == PartSetPlugin_Part::ID())
351 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
353 // the sketch manager put the restriction to the objects erase
354 return mySketchMgr->canEraseObject(theObject);
357 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
359 // the sketch manager put the restriction to the objects display
360 return mySketchMgr->canDisplayObject(theObject);
363 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
365 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
367 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
368 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
369 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
370 if (isSketchOp || isNestedOp) {
371 // in active sketch operation it is possible to activate operation object in selection
372 // in the edit operation, e.g. points of the line can be moved when the line is edited
373 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
374 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
379 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
381 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
384 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
386 myMenuMgr->updateViewerMenu(theStdActions);
389 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
391 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
392 if (anError.isEmpty())
393 anError = sketchMgr()->getFeatureError(theFeature);
398 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
399 QStringList& theIds) const
401 myMenuMgr->grantedOperationIds(theOperation, theIds);
403 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
404 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
405 XGUI_Workshop* aWorkshop = aConnector->workshop();
407 theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
411 void PartSet_Module::activeSelectionModes(QIntList& theModes)
414 if (mySketchMgr->activeSketch().get())
415 PartSet_SketcherMgr::sketchSelectionModes(theModes);
418 bool PartSet_Module::isMouseOverWindow()
420 return mySketchMgr->isMouseOverWindow();
423 void PartSet_Module::closeDocument()
428 void PartSet_Module::clearViewer()
430 myCustomPrs->clearPrs();
432 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
433 XGUI_Workshop* aWorkshop = aConnector->workshop();
434 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
435 aDisplayer->deactivateSelectionFilters();
438 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
440 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
444 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
445 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
446 // we have to manually activate the sketch label in edit mode
447 aPanel->activateWidget(aPanel->modelWidgets().first());
451 // Restart last operation type
452 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
453 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
454 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
455 // Initialise new line with first point equal to end of previous
456 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
458 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
459 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
460 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
462 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
463 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
464 aWgt->attributeID(), aPoint->x(), aPoint->y());
465 aPanel->activateNextWidget(aPnt2dWgt);
473 void PartSet_Module::onSelectionChanged()
475 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
479 bool isSketcherOp = false;
480 // An edit operation is enable only if the current opeation is the sketch operation
481 if (mySketchMgr->activeSketch()) {
482 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
483 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
486 // Editing of constraints can be done on selection
487 ModuleBase_ISelection* aSelect = myWorkshop->selection();
488 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
489 if (aSelected.size() == 1) {
490 ModuleBase_ViewerPrs aPrs = aSelected.first();
491 ObjectPtr aObject = aPrs.object();
492 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
494 std::string aId = aFeature->getKind();
495 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
496 (aId == SketchPlugin_ConstraintLength::ID()) ||
497 (aId == SketchPlugin_ConstraintDistance::ID()) ||
498 (aId == SketchPlugin_ConstraintAngle::ID())) {
499 editFeature(aFeature);
506 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
508 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
509 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
510 anOpMgr->onKeyReleased(theEvent);
513 void PartSet_Module::onEnterReleased()
515 myRestartingMode = RM_EmptyFeatureUsed;
518 void PartSet_Module::onOperationActivatedByPreselection()
520 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
521 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
522 // Set final definitions if they are necessary
523 //propertyPanelDefined(aOperation);
525 /// Commit sketcher operations automatically
526 anOperation->commit();
530 void PartSet_Module::onNoMoreWidgets()
532 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
534 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
535 if (myRestartingMode != RM_Forbided)
536 myRestartingMode = RM_LastFeatureUsed;
537 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
538 XGUI_Workshop* aWorkshop = aConnector->workshop();
539 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
540 // do nothing if the feature can not be applyed
541 if (anOpMgr->isApplyEnabled())
542 anOperation->commit();
547 void PartSet_Module::onVertexSelected()
549 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
550 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
551 /// If last line finished on vertex the lines creation sequence has to be break
552 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
553 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
554 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
555 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
556 bool aFoundWidget = false;
557 bool aFoundObligatory = false;
558 for (; anIt != aLast && !aFoundObligatory; anIt++) {
560 aFoundWidget = *anIt == anActiveWidget;
562 aFoundObligatory = (*anIt)->isObligatory();
564 if (!aFoundObligatory)
565 myRestartingMode = RM_Forbided;
569 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
570 Config_WidgetAPI* theWidgetApi, std::string theParentId)
572 ModuleBase_IWorkshop* aWorkshop = workshop();
573 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
574 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
575 ModuleBase_ModelWidget* aWgt = NULL;
576 if (theType == "sketch-start-label") {
577 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
578 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
579 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
580 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
581 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
582 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
584 } else if (theType == "sketch-2dpoint_selector") {
585 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
586 theWidgetApi, theParentId);
587 aPointWgt->setSketch(mySketchMgr->activeSketch());
588 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
590 } else if (theType == "sketch-2dpoint_flyout_selector") {
591 PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
592 theWidgetApi, theParentId);
593 aPointWgt->setSketch(mySketchMgr->activeSketch());
594 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
596 } else if (theType == "point2ddistance") {
597 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
598 aWorkshop, theWidgetApi, theParentId);
599 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
601 } else if(theType == "point2dangle") {
602 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
603 aWorkshop, theWidgetApi, theParentId);
604 anAngleWgt->setSketch(mySketchMgr->activeSketch());
606 } else if (theType == "sketch_shape_selector") {
607 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
608 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
609 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
610 aWgt = aShapeSelectorWgt;
611 } else if (theType == "sketch_multi_selector") {
612 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
613 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
614 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
615 aWgt = aShapeSelectorWgt;
616 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
617 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
618 } else if (theType == "export_file_selector") {
619 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
620 } else if (theType == "sketch_launcher") {
621 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
627 bool PartSet_Module::deleteObjects()
629 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
630 XGUI_Workshop* aWorkshop = aConnector->workshop();
631 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
633 //SessionPtr aMgr = ModelAPI_Session::get();
634 // 1. check whether the delete should be processed in the module
635 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
636 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
637 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
638 if (isSketchOp || isNestedOp) {
639 // 2. find selected presentations
640 // selected objects should be collected before the current operation abort because
641 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
642 ModuleBase_ISelection* aSel = workshop()->selection();
643 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
644 // if there are no selected objects in the viewer, that means that the selection in another
645 // place cased this method. It is necessary to return the false value to understande in above
646 // method that delete is not processed
647 if (aSelectedObj.count() == 0)
650 // avoid delete of the objects, which are not belong to the current sketch
651 // in order to do not delete results of other sketches
652 QObjectPtrList aSketchObjects;
653 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
654 for ( ; anIt != aLast; anIt++) {
655 ObjectPtr anObject = *anIt;
656 if (mySketchMgr->isObjectOfSketch(anObject)) {
657 // sketch feature should be used in this list because workshop deletes features only
658 // results are skipped
659 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
660 aSketchObjects.append(aSketchFeature);
663 // if the selection contains only local selected presentations from other sketches,
664 // the Delete operation should not be done at all
665 if (aSketchObjects.size() == 0)
668 // 3. start operation
669 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
670 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
672 // the active nested sketch operation should be aborted unconditionally
673 // the Delete action should be additionally granted for the Sketch operation
674 // in order to do not abort/commit it
675 if (!anOpMgr->canStartOperation(anOpAction->id()))
676 return true; // the objects are processed but can not be deleted
678 anOpMgr->startOperation(anOpAction);
680 // 4. delete features
681 // sketch feature should be skipped, only sub-features can be removed
682 // when sketch operation is active
683 aWorkshop->deleteFeatures(aSketchObjects);
685 anOpMgr->commitOperation();
688 bool isPartRemoved = false;
689 // Delete part with help of PartSet plugin
690 // TODO: the deleted objects has to be processed by multiselection
691 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
692 if (aObjects.size() == 1) {
693 ObjectPtr aObj = aObjects.first();
694 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
695 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
696 // Remove feature should be created in the document of the part results
697 ResultPtr aPartResult = aFeature->firstResult();
698 if (aPartResult.get()) {
699 std::shared_ptr<ModelAPI_ResultPart> aPart =
700 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
701 DocumentPtr aPartDoc = aPart->partDoc();
702 if (aPartDoc.get()) {
703 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
704 (PartSetPlugin_Remove::ID().c_str(), this);
705 if (!anOpMgr->canStartOperation(anOpAction->id()))
706 return true; // the objects are processed but can not be deleted
708 anOpMgr->startOperation(anOpAction);
710 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
713 anOpMgr->commitOperation();
714 isPartRemoved = true;
719 return isPartRemoved;
724 void PartSet_Module::onFeatureTriggered()
726 QAction* aCmd = dynamic_cast<QAction*>(sender());
727 if (aCmd->isCheckable() && aCmd->isChecked()) {
728 // 1. check whether the delete should be processed in the module
729 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
730 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
732 // in case if in the viewer nothing is displayed, the create operation should not be
733 // comitted even if all values of the feature are initialized
734 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
735 // the action information should be saved before the operation is aborted
736 // because this abort leads to update command status, which unchecks this action
737 anOperation->abort();
738 launchOperation(aCmd->data().toString());
742 ModuleBase_IModule::onFeatureTriggered();
745 void PartSet_Module::launchOperation(const QString& theCmdId)
747 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
748 // Show constraints if a constraint was anOperation
749 myHasConstraintShown = mySketchMgr->isConstraintsShown();
750 mySketchMgr->onShowConstraintsToggle(true);
752 ModuleBase_IModule::launchOperation(theCmdId);
756 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
758 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
759 if (!anAIS.IsNull()) {
760 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
761 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
762 if (!aDim.IsNull()) {
763 aCtx->SetZLayer(aDim, myVisualLayerId);
765 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
767 aCtx->SetZLayer(aCons, myVisualLayerId);
772 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
775 // it should be recomputed in order to disappear in the viewer if the corresponded object
777 //if (myCustomPrs->isActive())
778 // myCustomPrs->redisplay(theObject, false);
781 void PartSet_Module::onViewTransformed(int theTrsfType)
783 // Set length of arrows constant in pixel size
784 // if the operation is panning or rotate or panglobal then do nothing
785 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
787 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
788 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
789 if (aContext.IsNull())
792 //Handle(V3d_View) aView = aViewer->activeView();
794 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
795 XGUI_Workshop* aWorkshop = aConnector->workshop();
796 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
797 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
798 Handle(V3d_View) aView;
800 for (aV3dViewer->InitDefinedViews();
801 aV3dViewer->MoreDefinedViews();
802 aV3dViewer->NextDefinedViews()) {
803 Handle(V3d_View) aV = aV3dViewer->DefinedView();
804 double aS = aV->Scale();
812 double aLen = aView->Convert(20);
814 double aPrevLen = SketcherPrs_Tools::getArrowSize();
815 SketcherPrs_Tools::setArrowSize(aLen);
816 const double aPrevScale = aViewer->Scale(aViewer->activeView());
817 const double aCurScale = aViewer->activeView()->Camera()->Scale();
818 aViewer->SetScale(aViewer->activeView(), aCurScale);
819 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
820 bool isModified = false;
821 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
822 foreach (AISObjectPtr aAIS, aPrsList) {
823 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
825 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
826 if (!aDim.IsNull()) {
827 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
828 aContext->Redisplay(aDim, false);
833 aDisplayer->updateViewer();
836 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
838 bool isRedisplayed = false;
839 if (myCustomPrs->isActive())
840 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
842 return isRedisplayed;
845 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
847 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
849 QLineEdit* aLabel = aOB->activeDocLabel();
850 connect(aLabel, SIGNAL(customContextMenuRequested(const QPoint&)),
851 SLOT(onActiveDocPopup(const QPoint&)));
852 //QPalette aPalet = aLabel->palette();
853 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
854 //aLabel->setPalette(aPalet);
855 aOB->treeView()->setExpandsOnDoubleClick(false);
856 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
857 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
861 void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
863 SessionPtr aMgr = ModelAPI_Session::get();
864 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
866 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
867 XGUI_Workshop* aWorkshop = aConnector->workshop();
868 QLineEdit* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
870 aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
873 aMenu.addAction(aActivatePartAction);
874 aMenu.exec(aHeader->mapToGlobal(thePnt));
878 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
881 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
883 /// If last line finished on vertex the lines creation sequence has to be break
884 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
885 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
886 // if there is an active widget, find the presented object in it
888 anActiveWidget = aPanel->preselectionWidget();
890 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
892 if (aWidgetValidated)
893 anObject = aWidgetValidated->findPresentedObject(theAIS);
898 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
900 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
901 int aSelected = aObjects.size();
902 SessionPtr aMgr = ModelAPI_Session::get();
903 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
905 ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
906 if (aSelected == 1) {
907 bool hasResult = false;
908 bool hasFeature = false;
909 bool hasParameter = false;
910 bool hasSubFeature = false;
911 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
913 ObjectPtr aObject = aObjects.first();
915 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
916 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
917 bool isPart = aPart.get() ||
918 (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
920 DocumentPtr aPartDoc;
922 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
924 if (aPart.get()) // this may be null is Part feature is disabled
925 aPartDoc = aPart->partDoc();
927 theMenu->addAction(aActivatePartAction);
928 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
930 } else if (aObject->document() == aMgr->activeDocument()) {
931 if (hasParameter || hasFeature) {
932 myMenuMgr->action("EDIT_CMD")->setEnabled(true);
933 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
934 if (aCurrentOp && aFeature.get()) {
935 if (aCurrentOp->id().toStdString() == aFeature->getKind())
936 myMenuMgr->action("EDIT_CMD")->setEnabled(false);
941 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
943 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
946 bool aNotDeactivate = (aCurrentOp == 0);
947 if (!aNotDeactivate) {
948 aActivatePartAction->setEnabled(false);
952 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
954 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
955 // Do not change activation of parts if an operation active
956 static QStringList aAllowActivationList;
957 if (aAllowActivationList.isEmpty())
958 aAllowActivationList <<
959 QString(PartSetPlugin_Part::ID().c_str()) <<
960 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
961 QString(PartSetPlugin_Remove::ID().c_str());
962 if (myWorkshop->currentOperation() &&
963 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
965 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
966 XGUI_Workshop* aWorkshop = aConnector->workshop();
967 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
968 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
969 QPalette aPalet = aLabel->palette();
971 SessionPtr aMgr = ModelAPI_Session::get();
972 DocumentPtr aActiveDoc = aMgr->activeDocument();
973 if (aActivePartIndex.isValid())
974 aTreeView->setExpanded(aActivePartIndex, false);
975 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
976 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
977 if (aActivePartIndex.isValid())
978 aTreeView->setExpanded(aActivePartIndex, true);
980 aLabel->setPalette(aPalet);
981 aWorkshop->updateCommandStatus();
983 // Update displayed objects in order to update active color
984 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
985 QObjectPtrList aObjects = aDisplayer->displayedObjects();
987 foreach(ObjectPtr aObj, aObjects) {
988 //TODO: replace by redisplay event.
989 aHidden = !aObj->data() || !aObj->data()->isValid() ||
990 aObj->isDisabled() || (!aObj->isDisplayed());
992 aDisplayer->redisplay(aObj, false);
994 aDisplayer->updateViewer();
998 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1000 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1002 SessionPtr aMgr = ModelAPI_Session::get();
1003 if (!theIndex.isValid()) {
1004 aMgr->setActiveDocument(aMgr->moduleDocument());
1007 if (theIndex.column() != 0) // Use only first column
1010 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1011 XGUI_Workshop* aWorkshop = aConnector->workshop();
1012 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1013 // De not use non editable Indexes
1014 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1016 ObjectPtr aObj = aDataModel->object(theIndex);
1018 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1019 if (!aPart.get()) { // Probably this is Feature
1020 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1021 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1022 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1025 if (aPart.get()) { // if this is a part
1026 if (aPart->partDoc() == aMgr->activeDocument()) {
1027 myMenuMgr->activatePartSet();
1035 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1037 // z layer is created for all started operations in order to visualize operation AIS presentation
1039 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1040 if (aContext.IsNull())
1043 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1044 if (myVisualLayerId == 0) {
1045 if (myVisualLayerId == 0)
1046 aViewer->AddZLayer(myVisualLayerId);
1048 TColStd_SequenceOfInteger aZList;
1049 aViewer->GetAllZLayers(aZList);
1050 bool aFound = false;
1051 for (int i = 1; i <= aZList.Length(); i++) {
1052 if (aZList(i) == myVisualLayerId) {
1058 aViewer->AddZLayer(myVisualLayerId);
1060 // if there is an active operation with validated widget,
1061 // the filters of this widget should be activated in the created view
1062 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1064 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1065 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1066 if (anActiveWidget) {
1067 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1069 if (aWidgetValidated)
1070 aWidgetValidated->activateFilters(true);
1075 //******************************************************
1076 void PartSet_Module::widgetStateChanged(int thePreviousState)
1078 mySketchMgr->widgetStateChanged(thePreviousState);