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_WidgetPoint2dAngle.h"
11 #include "PartSet_WidgetMultiSelector.h"
12 #include "PartSet_WidgetEditor.h"
13 #include "PartSet_WidgetFileSelector.h"
14 #include "PartSet_WidgetSketchCreator.h"
15 #include "PartSet_SketcherMgr.h"
16 #include "PartSet_MenuMgr.h"
17 #include "PartSet_CustomPrs.h"
18 #include "PartSet_IconFactory.h"
20 #include "PartSet_Filters.h"
21 #include "PartSet_FilterInfinite.h"
23 #include <PartSetPlugin_Remove.h>
24 #include <PartSetPlugin_Part.h>
25 #include <PartSetPlugin_Duplicate.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_OperationAction.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_WidgetEditor.h>
33 #include <ModuleBase_WidgetValidated.h>
34 #include <ModuleBase_FilterFactory.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_OperationFeature.h>
38 #include <GeomValidators_ShapeType.h>
39 #include <GeomValidators_Finite.h>
40 #include <GeomValidators_Face.h>
41 #include <GeomValidators_ConstructionComposite.h>
42 #include <GeomValidators_ZeroOffset.h>
43 #include <GeomValidators_BooleanArguments.h>
44 #include <GeomValidators_Different.h>
47 #include <ModelAPI_Object.h>
48 #include <ModelAPI_Events.h>
49 #include <ModelAPI_Validator.h>
50 #include <ModelAPI_Data.h>
51 #include <ModelAPI_Session.h>
52 #include <ModelAPI_ShapeValidator.h>
53 #include <ModelAPI_ResultBody.h>
55 #include <GeomDataAPI_Point2D.h>
56 #include <GeomDataAPI_Point.h>
57 #include <GeomDataAPI_Dir.h>
59 #include <XGUI_Displayer.h>
60 #include <XGUI_Workshop.h>
61 #include <XGUI_OperationMgr.h>
62 #include <XGUI_PropertyPanel.h>
63 #include <XGUI_ModuleConnector.h>
64 #include <XGUI_ContextMenuMgr.h>
65 #include <XGUI_Tools.h>
66 #include <XGUI_ObjectsBrowser.h>
67 #include <XGUI_SelectionMgr.h>
68 #include <XGUI_DataModel.h>
70 #include <SketchPlugin_Feature.h>
71 #include <SketchPlugin_Sketch.h>
72 #include <SketchPlugin_Line.h>
73 #include <SketchPlugin_Arc.h>
74 #include <SketchPlugin_Circle.h>
75 #include <SketchPlugin_Point.h>
76 #include <SketchPlugin_ConstraintAngle.h>
77 #include <SketchPlugin_ConstraintLength.h>
78 #include <SketchPlugin_ConstraintDistance.h>
79 #include <SketchPlugin_ConstraintParallel.h>
80 #include <SketchPlugin_ConstraintPerpendicular.h>
81 #include <SketchPlugin_ConstraintRadius.h>
83 #include <SketcherPrs_SymbolPrs.h>
84 #include <SketcherPrs_Tools.h>
86 #include <Events_Loop.h>
87 #include <Config_PropManager.h>
88 #include <Config_Keywords.h>
90 #include <StdSelect_TypeOfFace.hxx>
91 #include <TopoDS_Vertex.hxx>
93 #include <TopoDS_Shape.hxx>
94 #include <BRep_Tool.hxx>
95 #include <AIS_Dimension.hxx>
98 #include <QMouseEvent>
101 #include <QApplication>
102 #include <QMessageBox>
103 #include <QMainWindow>
106 #include <GeomAlgoAPI_FaceBuilder.h>
107 #include <GeomDataAPI_Dir.h>
109 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
115 /*!Create and return new instance of XGUI_Module*/
116 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
118 return new PartSet_Module(theWshop);
121 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
122 : ModuleBase_IModule(theWshop),
123 myRestartingMode(RM_None), myVisualLayerId(0)
125 new PartSet_IconFactory();
127 mySketchMgr = new PartSet_SketcherMgr(this);
128 myDataModel = new PartSet_DocumentDataModel(this);
130 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
131 XGUI_Workshop* aWorkshop = aConnector->workshop();
133 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
134 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
135 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
136 this, SLOT(onOperationActivatedByPreselection()));
138 ModuleBase_IViewer* aViewer = theWshop->viewer();
139 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
140 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
141 connect(aViewer, SIGNAL(viewTransformed(int)),
142 SLOT(onViewTransformed(int)));
143 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
144 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
146 myMenuMgr = new PartSet_MenuMgr(this);
147 myCustomPrs = new PartSet_CustomPrs(theWshop);
149 Events_Loop* aLoop = Events_Loop::loop();
150 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
152 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
153 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
156 PartSet_Module::~PartSet_Module()
158 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
159 for (; aIt.More(); aIt.Next()) {
160 Handle(SelectMgr_Filter) aFilter = aIt.Value();
161 if (!aFilter.IsNull())
167 void PartSet_Module::activateSelectionFilters()
169 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
170 for (; aIt.More(); aIt.Next()) {
171 Handle(SelectMgr_Filter) aFilter = aIt.Value();
172 if (!aFilter.IsNull())
173 myWorkshop->viewer()->addSelectionFilter(aFilter);
177 void PartSet_Module::deactivateSelectionFilters()
179 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
180 for (; aIt.More(); aIt.Next()) {
181 Handle(SelectMgr_Filter) aFilter = aIt.Value();
182 if (!aFilter.IsNull())
183 myWorkshop->viewer()->removeSelectionFilter(aFilter);
187 void PartSet_Module::registerValidators()
189 //Registering of validators
190 SessionPtr aMgr = ModelAPI_Session::get();
191 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
192 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
193 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
194 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
195 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
196 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
197 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
198 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
199 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
200 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
201 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
202 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
204 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
205 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
207 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
209 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
210 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
212 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
214 aFactory->registerValidator("GeomValidators_ConstructionComposite",
215 new GeomValidators_ConstructionComposite);
217 aFactory->registerValidator("GeomValidators_ZeroOffset",
218 new GeomValidators_ZeroOffset);
220 aFactory->registerValidator("GeomValidators_BooleanArguments",
221 new GeomValidators_BooleanArguments);
223 aFactory->registerValidator("PartSet_SketchEntityValidator",
224 new PartSet_SketchEntityValidator);
226 aFactory->registerValidator("PartSet_SameTypeAttr",
227 new PartSet_SameTypeAttrValidator);
229 aFactory->registerValidator("GeomValidators_Different",
230 new GeomValidators_Different);
233 void PartSet_Module::registerFilters()
235 //Registering of selection filters
236 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
237 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
240 void PartSet_Module::registerProperties()
242 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
244 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
245 Config_Prop::Integer, SKETCH_WIDTH);
248 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
250 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
251 mySketchMgr->commitNestedSketch(theOperation);
254 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
255 if (!aFOperation || aFOperation->isEditOperation())
257 // the selection is cleared after commit the create operation
258 // in order to do not use the same selected objects in the restarted operation
259 // for common behaviour, the selection is cleared even if the operation is not restarted
260 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
261 XGUI_Workshop* aWorkshop = aConnector->workshop();
262 aWorkshop->selector()->clearSelection();
264 /// Restart sketcher operations automatically
265 FeaturePtr aFeature = aFOperation->feature();
266 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
267 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
268 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
269 myRestartingMode == RM_EmptyFeatureUsed)) {
270 myLastOperationId = aFOperation->id();
271 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
273 launchOperation(myLastOperationId);
275 breakOperationSequence();
278 void PartSet_Module::breakOperationSequence()
280 myLastOperationId = "";
281 myLastFeature = FeaturePtr();
282 myRestartingMode = RM_None;
285 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
287 breakOperationSequence();
290 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
292 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
293 mySketchMgr->startSketch(theOperation);
295 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
296 mySketchMgr->startNestedSketch(theOperation);
299 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
301 myCustomPrs->activate(aFOperation->feature());
304 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
306 myCustomPrs->deactivate();
308 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
309 mySketchMgr->stopSketch(theOperation);
311 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
312 mySketchMgr->stopNestedSketch(theOperation);
316 ModuleBase_Operation* PartSet_Module::currentOperation() const
318 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
319 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
320 return anOpMgr->currentOperation();
323 bool PartSet_Module::canUndo() const
325 bool aCanUndo = false;
326 SessionPtr aMgr = ModelAPI_Session::get();
327 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
328 aCanUndo = !aMgr->isOperation();
329 if (!aCanUndo) // check the enable state additionally by sketch manager
330 aCanUndo = aMgr->canUndo();
335 bool PartSet_Module::canRedo() const
337 bool aCanRedo = false;
338 SessionPtr aMgr = ModelAPI_Session::get();
339 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
340 aCanRedo = !aMgr->isOperation();
341 if (!aCanRedo) // check the enable state additionally by sketch manager
342 aCanRedo = aMgr->canRedo();
347 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
350 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
351 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
353 // part features are removed in the PartSet module only.
354 if (aFeature->getKind() == PartSetPlugin_Part::ID())
361 bool PartSet_Module::canCommitOperation() const
363 return mySketchMgr->canCommitOperation();
366 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
368 // the sketch manager put the restriction to the objects display
369 return mySketchMgr->canDisplayObject(theObject);
372 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
374 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
376 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
377 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
378 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
379 if (isSketchOp || isNestedOp) {
380 // in active sketch operation it is possible to activate operation object in selection
381 // in the edit operation, e.g. points of the line can be moved when the line is edited
382 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
383 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
388 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
390 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
393 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
395 myMenuMgr->updateViewerMenu(theStdActions);
399 void PartSet_Module::activeSelectionModes(QIntList& theModes)
402 if (mySketchMgr->activeSketch().get())
403 PartSet_SketcherMgr::sketchSelectionModes(theModes);
406 bool PartSet_Module::isMouseOverWindow()
408 return mySketchMgr->isMouseOverWindow();
411 void PartSet_Module::closeDocument()
416 void PartSet_Module::clearViewer()
418 myCustomPrs->clearPrs();
421 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
423 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
427 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
428 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
429 // we have to manually activate the sketch label in edit mode
430 aPanel->activateWidget(aPanel->modelWidgets().first());
434 // Restart last operation type
435 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
436 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
437 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
438 // Initialise new line with first point equal to end of previous
439 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
441 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
442 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
443 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
445 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
446 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
447 aWgt->attributeID(), aPoint->x(), aPoint->y());
448 aPanel->activateNextWidget(aPnt2dWgt);
456 void PartSet_Module::onSelectionChanged()
458 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
462 bool isSketcherOp = false;
463 // An edit operation is enable only if the current opeation is the sketch operation
464 if (mySketchMgr->activeSketch()) {
465 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
466 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
469 // Editing of constraints can be done on selection
470 ModuleBase_ISelection* aSelect = myWorkshop->selection();
471 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
472 if (aSelected.size() == 1) {
473 ModuleBase_ViewerPrs aPrs = aSelected.first();
474 ObjectPtr aObject = aPrs.object();
475 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
477 std::string aId = aFeature->getKind();
478 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
479 (aId == SketchPlugin_ConstraintLength::ID()) ||
480 (aId == SketchPlugin_ConstraintDistance::ID()) ||
481 (aId == SketchPlugin_ConstraintAngle::ID())) {
482 editFeature(aFeature);
489 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
491 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
492 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
493 anOpMgr->onKeyReleased(theEvent);
496 void PartSet_Module::onEnterReleased()
498 myRestartingMode = RM_EmptyFeatureUsed;
501 void PartSet_Module::onOperationActivatedByPreselection()
503 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
504 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
505 // Set final definitions if they are necessary
506 //propertyPanelDefined(aOperation);
508 /// Commit sketcher operations automatically
509 anOperation->commit();
513 void PartSet_Module::onNoMoreWidgets()
515 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
517 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
518 if (myRestartingMode != RM_Forbided)
519 myRestartingMode = RM_LastFeatureUsed;
520 anOperation->commit();
525 void PartSet_Module::onVertexSelected()
527 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
528 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
529 /// If last line finished on vertex the lines creation sequence has to be break
530 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
531 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
532 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
533 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
534 bool aFoundWidget = false;
535 bool aFoundObligatory = false;
536 for (; anIt != aLast && !aFoundObligatory; anIt++) {
538 aFoundWidget = *anIt == anActiveWidget;
540 aFoundObligatory = (*anIt)->isObligatory();
542 if (!aFoundObligatory)
543 myRestartingMode = RM_Forbided;
547 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
548 Config_WidgetAPI* theWidgetApi, std::string theParentId)
550 ModuleBase_IWorkshop* aWorkshop = workshop();
551 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
552 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
553 ModuleBase_ModelWidget* aWgt = NULL;
554 if (theType == "sketch-start-label") {
555 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
556 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
557 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
558 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
559 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
560 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
562 } else if (theType == "sketch-2dpoint_selector") {
563 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
564 theWidgetApi, theParentId);
565 aPointWgt->setSketch(mySketchMgr->activeSketch());
566 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
568 } else if (theType == "point2ddistance") {
569 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
570 aWorkshop, theWidgetApi, theParentId);
571 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
573 } else if(theType == "point2dangle") {
574 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
575 aWorkshop, theWidgetApi, theParentId);
576 anAngleWgt->setSketch(mySketchMgr->activeSketch());
578 } else if (theType == "sketch_shape_selector") {
579 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
580 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
581 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
582 aWgt = aShapeSelectorWgt;
583 } else if (theType == "sketch_multi_selector") {
584 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
585 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
586 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
587 aWgt = aShapeSelectorWgt;
588 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
589 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
590 } else if (theType == "export_file_selector") {
591 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
592 } else if (theType == "sketch_launcher") {
593 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
599 bool PartSet_Module::deleteObjects()
601 SessionPtr aMgr = ModelAPI_Session::get();
602 // 1. check whether the delete should be processed in the module
603 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
604 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
605 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
606 if (isSketchOp || isNestedOp) {
607 // 2. find selected presentations
608 // selected objects should be collected before the current operation abort because
609 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
610 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
611 XGUI_Workshop* aWorkshop = aConnector->workshop();
612 ModuleBase_ISelection* aSel = workshop()->selection();
613 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
614 // if there are no selected objects in the viewer, that means that the selection in another
615 // place cased this method. It is necessary to return the false value to understande in above
616 // method that delete is not processed
617 if (aSelectedObj.count() == 0)
620 // avoid delete of the objects, which are not belong to the current sketch
621 // in order to do not delete results of other sketches
622 QObjectPtrList aSketchObjects;
623 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
624 for ( ; anIt != aLast; anIt++) {
625 ObjectPtr anObject = *anIt;
626 if (mySketchMgr->isObjectOfSketch(anObject)) {
627 // sketch feature should be used in this list because workshop deletes features only
628 // results are skipped
629 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
630 aSketchObjects.append(aSketchFeature);
633 // if the selection contains only local selected presentations from other sketches,
634 // the Delete operation should not be done at all
635 if (aSketchObjects.size() == 0)
638 // 3. start operation
639 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
640 ModuleBase_OperationAction* anAction = new ModuleBase_OperationAction(aDescription, this);
642 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
643 // the active nested sketch operation should be aborted unconditionally
645 anOperation->addGrantedOperationId(anAction->id());
646 if (!anOpMgr->canStartOperation(anAction->id()))
647 return true; // the objects are processed but can not be deleted
649 anOperation->removeGrantedOperationId(anAction->id());
651 anOpMgr->startOperation(anAction);
652 // 4. delete features
653 // sketch feature should be skipped, only sub-features can be removed
654 // when sketch operation is active
655 aWorkshop->deleteFeatures(aSketchObjects);
657 anOpMgr->commitOperation();
659 bool isPartRemoved = false;
660 // Delete part with help of PartSet plugin
661 // TODO: the deleted objects has to be processed by multiselection
662 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
663 if (aObjects.size() == 1) {
664 ObjectPtr aObj = aObjects.first();
665 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
666 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
667 // Remove feature should be created in the document of the part results
668 ResultPtr aPartResult = aFeature->firstResult();
669 if (aPartResult.get()) {
670 std::shared_ptr<ModelAPI_ResultPart> aPart =
671 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
672 DocumentPtr aPartDoc = aPart->partDoc();
673 if (aPartDoc.get()) {
674 aMgr->startOperation(PartSetPlugin_Remove::ID());
675 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
677 aMgr->finishOperation();
678 isPartRemoved = true;
683 return isPartRemoved;
688 void PartSet_Module::onFeatureTriggered()
690 QAction* aCmd = dynamic_cast<QAction*>(sender());
691 if (aCmd->isCheckable() && aCmd->isChecked()) {
692 // 1. check whether the delete should be processed in the module
693 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
694 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
696 // in case if in the viewer nothing is displayed, the create operation should not be
697 // comitted even if all values of the feature are initialized
698 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
699 // the action information should be saved before the operation is aborted
700 // because this abort leads to update command status, which unchecks this action
701 anOperation->abort();
702 launchOperation(aCmd->data().toString());
706 ModuleBase_IModule::onFeatureTriggered();
709 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
711 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
712 if (!anAIS.IsNull()) {
713 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
714 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
715 if (!aDim.IsNull()) {
716 aCtx->SetZLayer(aDim, myVisualLayerId);
718 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
720 aCtx->SetZLayer(aCons, myVisualLayerId);
725 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
727 // it should be recomputed in order to disappear in the viewer if the corresponded object
729 if (myCustomPrs->isActive())
730 myCustomPrs->customize(theObject);
733 void PartSet_Module::onViewTransformed(int theTrsfType)
735 // Set length of arrows constant in pixel size
736 // if the operation is panning or rotate or panglobal then do nothing
737 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
740 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
741 //Handle(V3d_View) aView = aViewer->activeView();
743 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
744 XGUI_Workshop* aWorkshop = aConnector->workshop();
745 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
746 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
748 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
749 Handle(V3d_View) aView;
751 for (aV3dViewer->InitDefinedViews();
752 aV3dViewer->MoreDefinedViews();
753 aV3dViewer->NextDefinedViews()) {
754 Handle(V3d_View) aV = aV3dViewer->DefinedView();
755 double aS = aV->Scale();
763 double aLen = aView->Convert(20);
765 double aPrevLen = SketcherPrs_Tools::getArrowSize();
766 SketcherPrs_Tools::setArrowSize(aLen);
767 const double aPrevScale = aViewer->Scale(aViewer->activeView());
768 const double aCurScale = aViewer->activeView()->Camera()->Scale();
769 aViewer->SetScale(aViewer->activeView(), aCurScale);
770 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
771 bool isModified = false;
772 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
773 foreach (AISObjectPtr aAIS, aPrsList) {
774 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
776 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
777 if (!aDim.IsNull()) {
778 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
779 aContext->Redisplay(aDim, false);
784 aDisplayer->updateViewer();
787 void PartSet_Module::customizeObject(ObjectPtr theObject)
789 if (myCustomPrs->isActive())
790 myCustomPrs->customize(theObject);
793 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
795 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
797 QLineEdit* aLabel = aOB->activeDocLabel();
798 QPalette aPalet = aLabel->palette();
799 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
800 aLabel->setPalette(aPalet);
801 aOB->treeView()->setExpandsOnDoubleClick(false);
802 #ifdef ModuleDataModel
803 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
804 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
805 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
806 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
807 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
808 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
813 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
816 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
818 /// If last line finished on vertex the lines creation sequence has to be break
819 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
820 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
821 // if there is an active widget, find the presented object in it
823 anActiveWidget = aPanel->preselectionWidget();
825 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
827 if (aWidgetValidated)
828 anObject = aWidgetValidated->findPresentedObject(theAIS);
833 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
835 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
836 int aSelected = aObjects.size();
837 SessionPtr aMgr = ModelAPI_Session::get();
838 if (aSelected == 1) {
839 bool hasResult = false;
840 bool hasFeature = false;
841 bool hasParameter = false;
842 bool hasSubFeature = false;
843 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
845 ObjectPtr aObject = aObjects.first();
847 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
848 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
849 bool isPart = aPart.get() ||
850 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
852 DocumentPtr aPartDoc;
854 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
856 if (aPart.get()) // this may be null is Part feature is disabled
857 aPartDoc = aPart->partDoc();
858 if (aMgr->activeDocument() == aPartDoc)
859 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
861 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
862 } else if (aObject->document() == aMgr->activeDocument()) {
863 if (hasParameter || hasFeature)
864 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
867 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
870 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
872 } else { // If feature is 0 the it means that selected root object (document)
873 if (aMgr->activeDocument() != aMgr->moduleDocument())
874 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
876 } else if (aSelected == 0) {
877 // if there is no selection then it means that upper label is selected
878 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
879 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
880 if (aMgr->activeDocument() != aMgr->moduleDocument())
881 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
883 bool aCanDeactivate = (myWorkshop->currentOperation() == 0);
884 myMenuMgr->action("ACTIVATE_PARTSET_CMD")->setEnabled(aCanDeactivate);
885 myMenuMgr->action("DEACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
886 myMenuMgr->action("ACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
889 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
891 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
892 // Do not change activation of parts if an operation active
893 static QStringList aAllowActivationList;
894 if (aAllowActivationList.isEmpty())
895 aAllowActivationList <<
896 QString(PartSetPlugin_Part::ID().c_str()) <<
897 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
898 QString(PartSetPlugin_Remove::ID().c_str());
899 if (myWorkshop->currentOperation() &&
900 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
902 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
903 XGUI_Workshop* aWorkshop = aConnector->workshop();
904 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
905 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
906 QPalette aPalet = aLabel->palette();
908 SessionPtr aMgr = ModelAPI_Session::get();
909 DocumentPtr aActiveDoc = aMgr->activeDocument();
910 #ifdef ModuleDataModel
911 QModelIndex aOldIndex = myDataModel->activePartTree();
912 DocumentPtr aDoc = aMgr->moduleDocument();
913 if (aActiveDoc == aDoc) {
914 if (aOldIndex.isValid())
915 aTreeView->setExpanded(aOldIndex, false);
916 myDataModel->deactivatePart();
917 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
919 std::string aGrpName = ModelAPI_ResultPart::group();
920 for (int i = 0; i < aDoc->size(aGrpName); i++) {
921 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
922 if (aPart->partDoc() == aActiveDoc) {
923 QModelIndex aIndex = myDataModel->partIndex(aPart);
924 if (myDataModel->activatePart(aIndex)) {
925 if (aOldIndex.isValid())
926 aTreeView->setExpanded(aOldIndex, false);
927 aTreeView->setExpanded(myDataModel->activePartTree(), true);
928 aPalet.setColor(QPalette::Text, Qt::black);
935 // Problem with MPV: At first time on creation it doesn't work because Part feature
936 // creation event will be sent after
937 if (aActivePartIndex.isValid())
938 aTreeView->setExpanded(aActivePartIndex, false);
939 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
940 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
941 if (aActivePartIndex.isValid())
942 aTreeView->setExpanded(aActivePartIndex, true);
944 aLabel->setPalette(aPalet);
945 aWorkshop->updateCommandStatus();
947 // Update displayed objects in order to update active color
948 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
949 QObjectPtrList aObjects = aDisplayer->displayedObjects();
950 foreach(ObjectPtr aObj, aObjects)
951 aDisplayer->redisplay(aObj, false);
952 aDisplayer->updateViewer();
956 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
958 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
960 SessionPtr aMgr = ModelAPI_Session::get();
961 if (!theIndex.isValid()) {
962 aMgr->setActiveDocument(aMgr->moduleDocument());
965 if (theIndex.column() != 0) // Use only first column
967 ObjectPtr aObj = myDataModel->object(theIndex);
968 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
969 if (!aPart.get()) { // Probably this is Feature
970 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
971 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
972 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
975 if (aPart.get()) { // if this is a part
976 if (aPart->partDoc() == aMgr->activeDocument()) {
977 aMgr->setActiveDocument(aMgr->moduleDocument());
985 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
987 // z layer is created for all started operations in order to visualize operation AIS presentation
989 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
990 if (myVisualLayerId == 0) {
991 if (myVisualLayerId == 0)
992 aViewer->AddZLayer(myVisualLayerId);
994 TColStd_SequenceOfInteger aZList;
995 aViewer->GetAllZLayers(aZList);
997 for (int i = 1; i <= aZList.Length(); i++) {
998 if (aZList(i) == myVisualLayerId) {
1004 aViewer->AddZLayer(myVisualLayerId);