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_IViewer.h>
29 #include <ModuleBase_IViewWindow.h>
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_WidgetEditor.h>
32 #include <ModuleBase_WidgetValidated.h>
33 #include <ModuleBase_FilterFactory.h>
34 #include <ModuleBase_Tools.h>
35 #include <GeomValidators_ShapeType.h>
37 #include <GeomValidators_Face.h>
38 #include <GeomValidators_ConstructionComposite.h>
39 #include <GeomValidators_ZeroOffset.h>
40 #include <GeomValidators_BooleanArguments.h>
41 #include <GeomValidators_Different.h>
44 #include <ModelAPI_Object.h>
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_Validator.h>
47 #include <ModelAPI_Data.h>
48 #include <ModelAPI_Session.h>
49 #include <ModelAPI_ShapeValidator.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_ResultCompSolid.h>
53 #include <GeomDataAPI_Point2D.h>
54 #include <GeomDataAPI_Point.h>
55 #include <GeomDataAPI_Dir.h>
57 #include <XGUI_Displayer.h>
58 #include <XGUI_Workshop.h>
59 #include <XGUI_OperationMgr.h>
60 #include <XGUI_PropertyPanel.h>
61 #include <XGUI_ModuleConnector.h>
62 #include <XGUI_ContextMenuMgr.h>
63 #include <XGUI_Tools.h>
64 #include <XGUI_ObjectsBrowser.h>
65 #include <XGUI_SelectionMgr.h>
66 #include <XGUI_DataModel.h>
68 #include <SketchPlugin_Feature.h>
69 #include <SketchPlugin_Sketch.h>
70 #include <SketchPlugin_Line.h>
71 #include <SketchPlugin_Arc.h>
72 #include <SketchPlugin_Circle.h>
73 #include <SketchPlugin_Point.h>
74 #include <SketchPlugin_ConstraintLength.h>
75 #include <SketchPlugin_ConstraintDistance.h>
76 #include <SketchPlugin_ConstraintParallel.h>
77 #include <SketchPlugin_ConstraintPerpendicular.h>
78 #include <SketchPlugin_ConstraintRadius.h>
80 #include <SketcherPrs_SymbolPrs.h>
81 #include <SketcherPrs_Tools.h>
83 #include <Events_Loop.h>
84 #include <Config_PropManager.h>
85 #include <Config_Keywords.h>
87 #include <StdSelect_TypeOfFace.hxx>
88 #include <TopoDS_Vertex.hxx>
90 #include <TopoDS_Shape.hxx>
91 #include <BRep_Tool.hxx>
92 #include <AIS_Dimension.hxx>
95 #include <QMouseEvent>
98 #include <QApplication>
99 #include <QMessageBox>
100 #include <QMainWindow>
103 #include <GeomAlgoAPI_FaceBuilder.h>
104 #include <GeomDataAPI_Dir.h>
106 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
112 #define DEBUG_COMPOSOLID
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);
203 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
204 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
206 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
208 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
209 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
211 aFactory->registerValidator("GeomValidators_ConstructionComposite",
212 new GeomValidators_ConstructionComposite);
214 aFactory->registerValidator("GeomValidators_ZeroOffset",
215 new GeomValidators_ZeroOffset);
217 aFactory->registerValidator("GeomValidators_BooleanArguments",
218 new GeomValidators_BooleanArguments);
220 aFactory->registerValidator("PartSet_SketchEntityValidator",
221 new PartSet_SketchEntityValidator);
223 aFactory->registerValidator("PartSet_SameTypeAttr",
224 new PartSet_SameTypeAttrValidator);
226 aFactory->registerValidator("GeomValidators_Different",
227 new GeomValidators_Different);
230 void PartSet_Module::registerFilters()
232 //Registering of selection filters
233 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
234 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
237 void PartSet_Module::registerProperties()
239 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
241 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
242 Config_Prop::Integer, SKETCH_WIDTH);
245 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
247 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
248 mySketchMgr->commitNestedSketch(theOperation);
251 if (theOperation->isEditOperation())
253 // the selection is cleared after commit the create operation
254 // in order to do not use the same selected objects in the restarted operation
255 // for common behaviour, the selection is cleared even if the operation is not restarted
256 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
257 XGUI_Workshop* aWorkshop = aConnector->workshop();
258 aWorkshop->selector()->clearSelection();
260 /// Restart sketcher operations automatically
261 FeaturePtr aFeature = theOperation->feature();
262 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
263 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
264 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
265 myRestartingMode == RM_EmptyFeatureUsed)) {
266 myLastOperationId = theOperation->id();
267 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
269 launchOperation(myLastOperationId);
271 breakOperationSequence();
274 void PartSet_Module::breakOperationSequence()
276 myLastOperationId = "";
277 myLastFeature = FeaturePtr();
278 myRestartingMode = RM_None;
281 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
283 breakOperationSequence();
286 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
288 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
289 mySketchMgr->startSketch(theOperation);
291 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
292 mySketchMgr->startNestedSketch(theOperation);
295 myCustomPrs->activate(theOperation->feature());
298 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
300 myCustomPrs->deactivate();
302 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
303 mySketchMgr->stopSketch(theOperation);
305 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
306 mySketchMgr->stopNestedSketch(theOperation);
310 ModuleBase_Operation* PartSet_Module::currentOperation() const
312 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
313 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
314 return anOpMgr->currentOperation();
317 bool PartSet_Module::canUndo() const
319 bool aCanUndo = false;
320 SessionPtr aMgr = ModelAPI_Session::get();
321 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
322 aCanUndo = !aMgr->isOperation();
323 if (!aCanUndo) // check the enable state additionally by sketch manager
324 aCanUndo = aMgr->canUndo();
329 bool PartSet_Module::canRedo() const
331 bool aCanRedo = false;
332 SessionPtr aMgr = ModelAPI_Session::get();
333 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
334 aCanRedo = !aMgr->isOperation();
335 if (!aCanRedo) // check the enable state additionally by sketch manager
336 aCanRedo = aMgr->canRedo();
341 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
344 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
345 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
347 // part features are removed in the PartSet module only.
348 if (aFeature->getKind() == PartSetPlugin_Part::ID())
355 bool PartSet_Module::canCommitOperation() const
357 return mySketchMgr->canCommitOperation();
360 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
362 #ifdef DEBUG_COMPOSOLID
363 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>
365 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0)
369 // the sketch manager put the restriction to the objects display
370 return mySketchMgr->canDisplayObject(theObject);
373 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
375 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
377 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
378 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
379 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
380 if (isSketchOp || isNestedOp) {
381 // in active sketch operation it is possible to activate operation object in selection
382 // in the edit operation, e.g. points of the line can be moved when the line is edited
383 aCanActivate = aCanActivate || anOperation->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::propertyPanelDefined(ModuleBase_Operation* theOperation)
413 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
414 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
415 // we have to manually activate the sketch label in edit mode
416 aPanel->activateWidget(aPanel->modelWidgets().first());
420 // Restart last operation type
421 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
422 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
423 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
424 // Initialise new line with first point equal to end of previous
425 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
427 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
428 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
429 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
431 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
432 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
433 aWgt->attributeID(), aPoint->x(), aPoint->y());
434 aPanel->activateNextWidget(aPnt2dWgt);
442 void PartSet_Module::onSelectionChanged()
444 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
448 bool isSketcherOp = false;
449 // An edit operation is enable only if the current opeation is the sketch operation
450 if (mySketchMgr->activeSketch()) {
451 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
452 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
455 // Editing of constraints can be done on selection
456 ModuleBase_ISelection* aSelect = myWorkshop->selection();
457 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
458 if (aSelected.size() == 1) {
459 ModuleBase_ViewerPrs aPrs = aSelected.first();
460 ObjectPtr aObject = aPrs.object();
461 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
463 std::string aId = aFeature->getKind();
464 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
465 (aId == SketchPlugin_ConstraintLength::ID()) ||
466 (aId == SketchPlugin_ConstraintDistance::ID())) {
467 editFeature(aFeature);
474 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
476 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
477 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
478 anOpMgr->onKeyReleased(theEvent);
481 void PartSet_Module::onEnterReleased()
483 myRestartingMode = RM_EmptyFeatureUsed;
486 void PartSet_Module::onOperationActivatedByPreselection()
488 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
489 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
490 // Set final definitions if they are necessary
491 //propertyPanelDefined(aOperation);
493 /// Commit sketcher operations automatically
494 anOperation->commit();
498 void PartSet_Module::onNoMoreWidgets()
500 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
502 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
503 if (myRestartingMode != RM_Forbided)
504 myRestartingMode = RM_LastFeatureUsed;
505 anOperation->commit();
510 void PartSet_Module::onVertexSelected()
512 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
513 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
514 /// If last line finished on vertex the lines creation sequence has to be break
515 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
516 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
517 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
518 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
519 bool aFoundWidget = false;
520 bool aFoundObligatory = false;
521 for (; anIt != aLast && !aFoundObligatory; anIt++) {
523 aFoundWidget = *anIt == anActiveWidget;
525 aFoundObligatory = (*anIt)->isObligatory();
527 if (!aFoundObligatory)
528 myRestartingMode = RM_Forbided;
532 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
533 Config_WidgetAPI* theWidgetApi, std::string theParentId)
535 ModuleBase_IWorkshop* aWorkshop = workshop();
536 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
537 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
538 ModuleBase_ModelWidget* aWgt = NULL;
539 if (theType == "sketch-start-label") {
540 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
541 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
542 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
543 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
544 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
545 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
547 } else if (theType == "sketch-2dpoint_selector") {
548 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
549 theWidgetApi, theParentId);
550 aPointWgt->setSketch(mySketchMgr->activeSketch());
551 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
553 } else if (theType == "point2ddistance") {
554 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
555 aWorkshop, theWidgetApi, theParentId);
556 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
558 } else if(theType == "point2dangle") {
559 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
560 aWorkshop, theWidgetApi, theParentId);
561 anAngleWgt->setSketch(mySketchMgr->activeSketch());
563 } else if (theType == "sketch_shape_selector") {
564 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
565 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
566 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
567 aWgt = aShapeSelectorWgt;
568 } else if (theType == "sketch_multi_selector") {
569 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
570 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
571 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
572 aWgt = aShapeSelectorWgt;
573 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
574 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
575 } else if (theType == "export_file_selector") {
576 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
577 } else if (theType == "sketch_launcher") {
578 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
584 bool PartSet_Module::deleteObjects()
586 SessionPtr aMgr = ModelAPI_Session::get();
587 // 1. check whether the delete should be processed in the module
588 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
589 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
590 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
591 if (isSketchOp || isNestedOp) {
592 // 2. find selected presentations
593 // selected objects should be collected before the current operation abort because
594 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
595 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
596 XGUI_Workshop* aWorkshop = aConnector->workshop();
597 ModuleBase_ISelection* aSel = workshop()->selection();
598 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
599 // if there are no selected objects in the viewer, that means that the selection in another
600 // place cased this method. It is necessary to return the false value to understande in above
601 // method that delete is not processed
602 if (aSelectedObj.count() == 0)
605 // avoid delete of the objects, which are not belong to the current sketch
606 // in order to do not delete results of other sketches
607 QObjectPtrList aSketchObjects;
608 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
609 for ( ; anIt != aLast; anIt++) {
610 ObjectPtr anObject = *anIt;
611 if (mySketchMgr->isObjectOfSketch(anObject)) {
612 // sketch feature should be used in this list because workshop deletes features only
613 // results are skipped
614 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
615 aSketchObjects.append(aSketchFeature);
618 // if the selection contains only local selected presentations from other sketches,
619 // the Delete operation should not be done at all
620 if (aSketchObjects.size() == 0)
623 // the active nested sketch operation should be aborted unconditionally
625 anOperation->abort();
627 // 3. start operation
628 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
629 aMgr->startOperation(aDescription.toStdString());
631 // 4. delete features
632 // sketch feature should be skipped, only sub-features can be removed
633 // when sketch operation is active
634 aWorkshop->deleteFeatures(aSketchObjects);
636 aWorkshop->displayer()->updateViewer();
637 aMgr->finishOperation();
639 bool isPartRemoved = false;
640 // Delete part with help of PartSet plugin
641 // TODO: the deleted objects has to be processed by multiselection
642 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
643 if (aObjects.size() == 1) {
644 ObjectPtr aObj = aObjects.first();
645 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
646 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
647 // Remove feature should be created in the document of the part results
648 ResultPtr aPartResult = aFeature->firstResult();
649 if (aPartResult.get()) {
650 std::shared_ptr<ModelAPI_ResultPart> aPart =
651 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
652 DocumentPtr aPartDoc = aPart->partDoc();
653 if (aPartDoc.get()) {
654 aMgr->startOperation(PartSetPlugin_Remove::ID());
655 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
657 aMgr->finishOperation();
658 isPartRemoved = true;
663 return isPartRemoved;
668 void PartSet_Module::onFeatureTriggered()
670 SessionPtr aMgr = ModelAPI_Session::get();
671 // 1. check whether the delete should be processed in the module
672 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
673 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
675 // in case if in the viewer nothing is displayed, the create operation should not be
676 // comitted even if all values of the feature are initialized
677 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
678 QAction* aCmd = dynamic_cast<QAction*>(sender());
679 //Do nothing on uncheck
680 if (aCmd->isCheckable() && !aCmd->isChecked())
683 // the action information should be saved before the operation is aborted
684 // because this abort leads to update command status, which unchecks this action
685 anOperation->abort();
687 launchOperation(aCmd->data().toString());
690 ModuleBase_IModule::onFeatureTriggered();
693 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
695 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
696 if (!anAIS.IsNull()) {
697 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
698 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
699 if (!aDim.IsNull()) {
700 aCtx->SetZLayer(aDim, myVisualLayerId);
702 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
704 aCtx->SetZLayer(aCons, myVisualLayerId);
709 void PartSet_Module::onViewTransformed(int theTrsfType)
711 // Set length of arrows constant in pixel size
712 // if the operation is panning or rotate or panglobal then do nothing
713 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
716 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
717 //Handle(V3d_View) aView = aViewer->activeView();
719 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
720 XGUI_Workshop* aWorkshop = aConnector->workshop();
721 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
722 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
724 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
725 Handle(V3d_View) aView;
727 for (aV3dViewer->InitDefinedViews();
728 aV3dViewer->MoreDefinedViews();
729 aV3dViewer->NextDefinedViews()) {
730 Handle(V3d_View) aV = aV3dViewer->DefinedView();
731 double aS = aV->Scale();
739 double aLen = aView->Convert(20);
741 double aPrevLen = SketcherPrs_Tools::getArrowSize();
742 SketcherPrs_Tools::setArrowSize(aLen);
743 const double aPrevScale = aViewer->Scale(aViewer->activeView());
744 const double aCurScale = aViewer->activeView()->Camera()->Scale();
745 aViewer->SetScale(aViewer->activeView(), aCurScale);
746 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
747 bool isModified = false;
748 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
749 foreach (AISObjectPtr aAIS, aPrsList) {
750 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
752 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
753 if (!aDim.IsNull()) {
754 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
755 aContext->Redisplay(aDim, false);
760 aDisplayer->updateViewer();
763 void PartSet_Module::customizeObject(ObjectPtr theObject)
765 if (myCustomPrs->isActive())
766 myCustomPrs->customize(theObject);
769 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
771 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
773 QLineEdit* aLabel = aOB->activeDocLabel();
774 QPalette aPalet = aLabel->palette();
775 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
776 aLabel->setPalette(aPalet);
777 aOB->treeView()->setExpandsOnDoubleClick(false);
778 #ifdef ModuleDataModel
779 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
780 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
781 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
782 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
783 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
784 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
789 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
792 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
794 /// If last line finished on vertex the lines creation sequence has to be break
795 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
796 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
797 // if there is an active widget, find the presented object in it
799 anActiveWidget = aPanel->preselectionWidget();
801 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
803 if (aWidgetValidated)
804 anObject = aWidgetValidated->findPresentedObject(theAIS);
809 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
811 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
812 int aSelected = aObjects.size();
813 SessionPtr aMgr = ModelAPI_Session::get();
814 if (aSelected == 1) {
815 bool hasResult = false;
816 bool hasFeature = false;
817 bool hasParameter = false;
818 bool hasSubFeature = false;
819 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
821 ObjectPtr aObject = aObjects.first();
823 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
824 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
825 bool isPart = aPart.get() ||
826 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
828 DocumentPtr aPartDoc;
830 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
832 if (aPart.get()) // this may be null is Part feature is disabled
833 aPartDoc = aPart->partDoc();
834 if (aMgr->activeDocument() == aPartDoc)
835 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
837 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
838 } else if (aObject->document() == aMgr->activeDocument()) {
839 if (hasParameter || hasFeature)
840 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
843 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
846 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
848 } else { // If feature is 0 the it means that selected root object (document)
849 if (aMgr->activeDocument() != aMgr->moduleDocument())
850 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
852 } else if (aSelected == 0) {
853 // if there is no selection then it means that upper label is selected
854 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
855 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
856 if (aMgr->activeDocument() != aMgr->moduleDocument())
857 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
859 bool aCanDeactivate = (myWorkshop->currentOperation() == 0);
860 myMenuMgr->action("ACTIVATE_PARTSET_CMD")->setEnabled(aCanDeactivate);
861 myMenuMgr->action("DEACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
862 myMenuMgr->action("ACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
865 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
867 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
868 // Do not change activation of parts if an operation active
869 static QStringList aAllowActivationList;
870 if (aAllowActivationList.isEmpty())
871 aAllowActivationList <<
872 QString(PartSetPlugin_Part::ID().c_str()) <<
873 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
874 QString(PartSetPlugin_Remove::ID().c_str());
875 if (myWorkshop->currentOperation() &&
876 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
878 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
879 XGUI_Workshop* aWorkshop = aConnector->workshop();
880 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
881 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
882 QPalette aPalet = aLabel->palette();
884 SessionPtr aMgr = ModelAPI_Session::get();
885 DocumentPtr aActiveDoc = aMgr->activeDocument();
886 #ifdef ModuleDataModel
887 QModelIndex aOldIndex = myDataModel->activePartTree();
888 DocumentPtr aDoc = aMgr->moduleDocument();
889 if (aActiveDoc == aDoc) {
890 if (aOldIndex.isValid())
891 aTreeView->setExpanded(aOldIndex, false);
892 myDataModel->deactivatePart();
893 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
895 std::string aGrpName = ModelAPI_ResultPart::group();
896 for (int i = 0; i < aDoc->size(aGrpName); i++) {
897 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
898 if (aPart->partDoc() == aActiveDoc) {
899 QModelIndex aIndex = myDataModel->partIndex(aPart);
900 if (myDataModel->activatePart(aIndex)) {
901 if (aOldIndex.isValid())
902 aTreeView->setExpanded(aOldIndex, false);
903 aTreeView->setExpanded(myDataModel->activePartTree(), true);
904 aPalet.setColor(QPalette::Text, Qt::black);
911 // Problem with MPV: At first time on creation it doesn't work because Part feature
912 // creation event will be sent after
913 if (aActivePartIndex.isValid())
914 aTreeView->setExpanded(aActivePartIndex, false);
915 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
916 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
917 if (aActivePartIndex.isValid())
918 aTreeView->setExpanded(aActivePartIndex, true);
920 aLabel->setPalette(aPalet);
921 aWorkshop->updateCommandStatus();
923 // Update displayed objects in order to update active color
924 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
925 QObjectPtrList aObjects = aDisplayer->displayedObjects();
926 foreach(ObjectPtr aObj, aObjects)
927 aDisplayer->redisplay(aObj, false);
928 aDisplayer->updateViewer();
932 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
934 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
936 SessionPtr aMgr = ModelAPI_Session::get();
937 if (!theIndex.isValid()) {
938 aMgr->setActiveDocument(aMgr->moduleDocument());
941 if (theIndex.column() != 0) // Use only first column
943 ObjectPtr aObj = myDataModel->object(theIndex);
944 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
945 if (!aPart.get()) { // Probably this is Feature
946 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
947 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
948 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
951 if (aPart.get()) { // if this is a part
952 if (aPart->partDoc() == aMgr->activeDocument()) {
953 aMgr->setActiveDocument(aMgr->moduleDocument());
961 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
963 // z layer is created for all started operations in order to visualize operation AIS presentation
965 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
966 if (myVisualLayerId == 0) {
967 if (myVisualLayerId == 0)
968 aViewer->AddZLayer(myVisualLayerId);
970 TColStd_SequenceOfInteger aZList;
971 aViewer->GetAllZLayers(aZList);
973 for (int i = 1; i <= aZList.Length(); i++) {
974 if (aZList(i) == myVisualLayerId) {
980 aViewer->AddZLayer(myVisualLayerId);