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>
45 #include <GeomValidators_PartitionArguments.h>
48 #include <ModelAPI_Object.h>
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_Validator.h>
51 #include <ModelAPI_Data.h>
52 #include <ModelAPI_Session.h>
53 #include <GeomValidators_DifferentShapes.h>
54 #include <ModelAPI_ResultBody.h>
55 #include <ModelAPI_AttributeString.h>
57 #include <GeomDataAPI_Point2D.h>
58 #include <GeomDataAPI_Point.h>
59 #include <GeomDataAPI_Dir.h>
61 #include <XGUI_Displayer.h>
62 #include <XGUI_Workshop.h>
63 #include <XGUI_OperationMgr.h>
64 #include <XGUI_PropertyPanel.h>
65 #include <XGUI_ModuleConnector.h>
66 #include <XGUI_ContextMenuMgr.h>
67 #include <XGUI_Tools.h>
68 #include <XGUI_ObjectsBrowser.h>
69 #include <XGUI_SelectionMgr.h>
70 #include <XGUI_DataModel.h>
71 #include <XGUI_ErrorMgr.h>
73 #include <SketchPlugin_Feature.h>
74 #include <SketchPlugin_Sketch.h>
75 #include <SketchPlugin_Line.h>
76 #include <SketchPlugin_Arc.h>
77 #include <SketchPlugin_Circle.h>
78 #include <SketchPlugin_Point.h>
79 #include <SketchPlugin_ConstraintAngle.h>
80 #include <SketchPlugin_ConstraintLength.h>
81 #include <SketchPlugin_ConstraintDistance.h>
82 #include <SketchPlugin_ConstraintParallel.h>
83 #include <SketchPlugin_ConstraintPerpendicular.h>
84 #include <SketchPlugin_ConstraintRadius.h>
86 #include <SketcherPrs_SymbolPrs.h>
87 #include <SketcherPrs_Tools.h>
89 #include <Events_Loop.h>
90 #include <Config_PropManager.h>
91 #include <Config_Keywords.h>
93 #include <StdSelect_TypeOfFace.hxx>
94 #include <TopoDS_Vertex.hxx>
96 #include <TopoDS_Shape.hxx>
97 #include <BRep_Tool.hxx>
98 #include <AIS_Dimension.hxx>
101 #include <QMouseEvent>
104 #include <QApplication>
105 #include <QMessageBox>
106 #include <QMainWindow>
109 #include <GeomAlgoAPI_FaceBuilder.h>
110 #include <GeomDataAPI_Dir.h>
112 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
118 /*!Create and return new instance of XGUI_Module*/
119 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
121 return new PartSet_Module(theWshop);
124 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
125 : ModuleBase_IModule(theWshop),
126 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
128 new PartSet_IconFactory();
130 mySketchMgr = new PartSet_SketcherMgr(this);
132 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
133 XGUI_Workshop* aWorkshop = aConnector->workshop();
135 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
136 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
137 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
138 this, SLOT(onOperationActivatedByPreselection()));
140 ModuleBase_IViewer* aViewer = theWshop->viewer();
141 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
142 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
143 connect(aViewer, SIGNAL(viewTransformed(int)),
144 SLOT(onViewTransformed(int)));
145 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
146 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
148 myMenuMgr = new PartSet_MenuMgr(this);
149 myCustomPrs = new PartSet_CustomPrs(theWshop);
151 Events_Loop* aLoop = Events_Loop::loop();
152 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
154 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
155 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
158 PartSet_Module::~PartSet_Module()
160 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
161 for (; aIt.More(); aIt.Next()) {
162 Handle(SelectMgr_Filter) aFilter = aIt.Value();
163 if (!aFilter.IsNull())
169 void PartSet_Module::activateSelectionFilters()
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()->addSelectionFilter(aFilter);
179 void PartSet_Module::deactivateSelectionFilters()
181 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
182 for (; aIt.More(); aIt.Next()) {
183 Handle(SelectMgr_Filter) aFilter = aIt.Value();
184 if (!aFilter.IsNull())
185 myWorkshop->viewer()->removeSelectionFilter(aFilter);
189 void PartSet_Module::registerValidators()
191 //Registering of validators
192 SessionPtr aMgr = ModelAPI_Session::get();
193 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
194 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
195 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
196 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
197 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
198 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
199 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
200 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
201 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
202 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
203 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
204 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
205 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
206 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
208 aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
209 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
210 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
211 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
213 aFactory->registerValidator("GeomValidators_ConstructionComposite",
214 new GeomValidators_ConstructionComposite);
216 aFactory->registerValidator("GeomValidators_ZeroOffset",
217 new GeomValidators_ZeroOffset);
219 aFactory->registerValidator("GeomValidators_BooleanArguments",
220 new GeomValidators_BooleanArguments);
222 aFactory->registerValidator("PartSet_SketchEntityValidator",
223 new PartSet_SketchEntityValidator);
225 aFactory->registerValidator("GeomValidators_Different",
226 new GeomValidators_Different);
228 aFactory->registerValidator("GeomValidators_PartitionArguments",
229 new GeomValidators_PartitionArguments);
232 void PartSet_Module::registerFilters()
234 //Registering of selection filters
235 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
236 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
239 void PartSet_Module::registerProperties()
241 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
243 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
244 Config_Prop::Integer, SKETCH_WIDTH);
247 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
249 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
250 mySketchMgr->commitNestedSketch(theOperation);
253 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
254 if (!aFOperation || aFOperation->isEditOperation())
256 // the selection is cleared after commit the create operation
257 // in order to do not use the same selected objects in the restarted operation
258 // for common behaviour, the selection is cleared even if the operation is not restarted
259 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
260 XGUI_Workshop* aWorkshop = aConnector->workshop();
261 aWorkshop->selector()->clearSelection();
263 /// Restart sketcher operations automatically
264 FeaturePtr aFeature = aFOperation->feature();
265 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
266 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
267 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
268 myRestartingMode == RM_EmptyFeatureUsed)) {
269 myLastOperationId = aFOperation->id();
270 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
271 if (!sketchMgr()->sketchSolverError())
272 launchOperation(myLastOperationId);
274 breakOperationSequence();
277 void PartSet_Module::breakOperationSequence()
279 myLastOperationId = "";
280 myLastFeature = FeaturePtr();
281 myRestartingMode = RM_None;
284 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
286 breakOperationSequence();
289 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
291 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
292 mySketchMgr->startSketch(theOperation);
294 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
295 mySketchMgr->startNestedSketch(theOperation);
298 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
300 myCustomPrs->activate(aFOperation->feature(), true);
303 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
305 ModuleBase_IModule::onOperationResumed(theOperation);
307 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
309 myCustomPrs->activate(aFOperation->feature(), true);
312 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
314 bool isModified = myCustomPrs->deactivate(false);
316 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
317 mySketchMgr->stopSketch(theOperation);
319 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
320 mySketchMgr->stopNestedSketch(theOperation);
323 //VSV: Viewer is updated on feature update and redisplay
325 // XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
326 // XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
327 // aDisplayer->updateViewer();
329 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
332 ModuleBase_Operation* PartSet_Module::currentOperation() const
334 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
335 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
336 return anOpMgr->currentOperation();
339 bool PartSet_Module::canUndo() const
341 bool aCanUndo = false;
342 SessionPtr aMgr = ModelAPI_Session::get();
343 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
344 aCanUndo = !aMgr->isOperation();
345 if (!aCanUndo) // check the enable state additionally by sketch manager
346 aCanUndo = aMgr->canUndo();
351 bool PartSet_Module::canRedo() const
353 bool aCanRedo = false;
354 SessionPtr aMgr = ModelAPI_Session::get();
355 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
356 aCanRedo = !aMgr->isOperation();
357 if (!aCanRedo) // check the enable state additionally by sketch manager
358 aCanRedo = aMgr->canRedo();
363 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
366 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
367 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
369 // part features are removed in the PartSet module only.
370 if (aFeature->getKind() == PartSetPlugin_Part::ID())
377 bool PartSet_Module::canCommitOperation() const
379 return mySketchMgr->canCommitOperation();
382 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
384 // the sketch manager put the restriction to the objects erase
385 return mySketchMgr->canEraseObject(theObject);
388 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
390 // the sketch manager put the restriction to the objects display
391 return mySketchMgr->canDisplayObject(theObject);
394 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
396 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
398 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
399 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
400 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
401 if (isSketchOp || isNestedOp) {
402 // in active sketch operation it is possible to activate operation object in selection
403 // in the edit operation, e.g. points of the line can be moved when the line is edited
404 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
405 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
410 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
412 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
415 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
417 myMenuMgr->updateViewerMenu(theStdActions);
420 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
422 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
424 if (anError.isEmpty())
425 anError = sketchMgr()->getFeatureError(theFeature);
427 if (anError.isEmpty()) {
428 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
429 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
431 if (anOpMgr->isValidationLocked()) {
432 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
433 (anOpMgr->currentOperation());
434 if (!aFOperation || theFeature == aFOperation->feature())
435 anError = "Validation is locked by the current operation";
441 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
442 QStringList& theIds) const
444 myMenuMgr->grantedOperationIds(theOperation, theIds);
446 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
447 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
448 XGUI_Workshop* aWorkshop = aConnector->workshop();
450 theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
454 void PartSet_Module::activeSelectionModes(QIntList& theModes)
457 if (mySketchMgr->activeSketch().get())
458 PartSet_SketcherMgr::sketchSelectionModes(theModes);
461 bool PartSet_Module::isMouseOverWindow()
463 return mySketchMgr->isMouseOverWindow();
466 void PartSet_Module::closeDocument()
471 void PartSet_Module::clearViewer()
473 myCustomPrs->clearPrs();
475 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
476 XGUI_Workshop* aWorkshop = aConnector->workshop();
477 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
478 aDisplayer->deactivateSelectionFilters();
481 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
483 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
487 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
488 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
489 // we have to manually activate the sketch label in edit mode
490 aPanel->activateWidget(aPanel->modelWidgets().first());
494 // Restart last operation type
495 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
496 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
497 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
498 // Initialise new line with first point equal to end of previous
499 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
501 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
502 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
503 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
505 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
506 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
507 aWgt->attributeID(), aPoint->x(), aPoint->y());
508 aPanel->activateNextWidget(aPnt2dWgt);
516 void PartSet_Module::onSelectionChanged()
518 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
522 bool isSketcherOp = false;
523 // An edit operation is enable only if the current opeation is the sketch operation
524 if (mySketchMgr->activeSketch()) {
525 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
526 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
529 // Editing of constraints can be done on selection
530 ModuleBase_ISelection* aSelect = myWorkshop->selection();
531 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
532 if (aSelected.size() == 1) {
533 ModuleBase_ViewerPrs aPrs = aSelected.first();
534 ObjectPtr aObject = aPrs.object();
535 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
537 std::string aId = aFeature->getKind();
538 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
539 (aId == SketchPlugin_ConstraintLength::ID()) ||
540 (aId == SketchPlugin_ConstraintDistance::ID()) ||
541 (aId == SketchPlugin_ConstraintAngle::ID())) {
542 editFeature(aFeature);
549 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
551 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
552 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
553 anOpMgr->onKeyReleased(theEvent);
556 void PartSet_Module::onEnterReleased()
558 myRestartingMode = RM_EmptyFeatureUsed;
561 void PartSet_Module::onOperationActivatedByPreselection()
563 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
564 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
565 // Set final definitions if they are necessary
566 //propertyPanelDefined(aOperation);
568 /// Commit sketcher operations automatically
569 anOperation->commit();
573 void PartSet_Module::onNoMoreWidgets()
575 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
577 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
578 if (myRestartingMode != RM_Forbided)
579 myRestartingMode = RM_LastFeatureUsed;
580 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
581 XGUI_Workshop* aWorkshop = aConnector->workshop();
582 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
583 // do nothing if the feature can not be applyed
584 if (anOpMgr->isApplyEnabled())
585 anOperation->commit();
590 void PartSet_Module::onVertexSelected()
592 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
593 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
594 /// If last line finished on vertex the lines creation sequence has to be break
595 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
596 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
597 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
598 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
599 bool aFoundWidget = false;
600 bool aFoundObligatory = false;
601 for (; anIt != aLast && !aFoundObligatory; anIt++) {
603 aFoundWidget = *anIt == anActiveWidget;
605 aFoundObligatory = (*anIt)->isObligatory();
607 if (!aFoundObligatory)
608 myRestartingMode = RM_Forbided;
612 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
613 Config_WidgetAPI* theWidgetApi, std::string theParentId)
615 ModuleBase_IWorkshop* aWorkshop = workshop();
616 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
617 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
618 ModuleBase_ModelWidget* aWgt = NULL;
619 if (theType == "sketch-start-label") {
620 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
621 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
622 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
623 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
624 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
625 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
627 } else if (theType == "sketch-2dpoint_selector") {
628 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
629 theWidgetApi, theParentId);
630 aPointWgt->setSketch(mySketchMgr->activeSketch());
631 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
633 } else if (theType == "point2ddistance") {
634 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
635 aWorkshop, theWidgetApi, theParentId);
636 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
638 } else if(theType == "point2dangle") {
639 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
640 aWorkshop, theWidgetApi, theParentId);
641 anAngleWgt->setSketch(mySketchMgr->activeSketch());
643 } else if (theType == "sketch_shape_selector") {
644 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
645 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
646 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
647 aWgt = aShapeSelectorWgt;
648 } else if (theType == "sketch_multi_selector") {
649 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
650 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
651 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
652 aWgt = aShapeSelectorWgt;
653 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
654 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
655 } else if (theType == "export_file_selector") {
656 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
657 } else if (theType == "sketch_launcher") {
658 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
664 bool PartSet_Module::deleteObjects()
666 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
667 XGUI_Workshop* aWorkshop = aConnector->workshop();
668 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
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 isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
674 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
675 if (isSketchOp || isNestedOp) {
676 // 2. find selected presentations
677 // selected objects should be collected before the current operation abort because
678 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
679 ModuleBase_ISelection* aSel = workshop()->selection();
680 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
681 // if there are no selected objects in the viewer, that means that the selection in another
682 // place cased this method. It is necessary to return the false value to understande in above
683 // method that delete is not processed
684 if (aSelectedObj.count() == 0)
687 // avoid delete of the objects, which are not belong to the current sketch
688 // in order to do not delete results of other sketches
689 QObjectPtrList aSketchObjects;
690 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
691 for ( ; anIt != aLast; anIt++) {
692 ObjectPtr anObject = *anIt;
693 if (mySketchMgr->isObjectOfSketch(anObject)) {
694 // sketch feature should be used in this list because workshop deletes features only
695 // results are skipped
696 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
697 aSketchObjects.append(aSketchFeature);
700 // if the selection contains only local selected presentations from other sketches,
701 // the Delete operation should not be done at all
702 if (aSketchObjects.size() == 0)
705 // 3. start operation
706 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
707 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
709 // the active nested sketch operation should be aborted unconditionally
710 // the Delete action should be additionally granted for the Sketch operation
711 // in order to do not abort/commit it
712 if (!anOpMgr->canStartOperation(anOpAction->id()))
713 return true; // the objects are processed but can not be deleted
715 anOpMgr->startOperation(anOpAction);
717 // 4. delete features
718 // sketch feature should be skipped, only sub-features can be removed
719 // when sketch operation is active
720 aWorkshop->deleteFeatures(aSketchObjects);
722 anOpMgr->commitOperation();
725 bool isPartRemoved = false;
726 // Delete part with help of PartSet plugin
727 // TODO: the deleted objects has to be processed by multiselection
728 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
729 if (aObjects.size() == 1) {
730 ObjectPtr aObj = aObjects.first();
731 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
732 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
733 // Remove feature should be created in the document of the part results
734 ResultPtr aPartResult = aFeature->firstResult();
735 if (aPartResult.get()) {
736 std::shared_ptr<ModelAPI_ResultPart> aPart =
737 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
738 DocumentPtr aPartDoc = aPart->partDoc();
739 if (aPartDoc.get()) {
740 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
741 (PartSetPlugin_Remove::ID().c_str(), this);
742 if (!anOpMgr->canStartOperation(anOpAction->id()))
743 return true; // the objects are processed but can not be deleted
745 anOpMgr->startOperation(anOpAction);
747 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
750 anOpMgr->commitOperation();
751 isPartRemoved = true;
756 return isPartRemoved;
761 void PartSet_Module::onFeatureTriggered()
763 QAction* aCmd = dynamic_cast<QAction*>(sender());
764 if (aCmd->isCheckable() && aCmd->isChecked()) {
765 // 1. check whether the delete should be processed in the module
766 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
767 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
769 // in case if in the viewer nothing is displayed, the create operation should not be
770 // comitted even if all values of the feature are initialized
771 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
772 // the action information should be saved before the operation is aborted
773 // because this abort leads to update command status, which unchecks this action
774 anOperation->abort();
775 launchOperation(aCmd->data().toString());
779 ModuleBase_IModule::onFeatureTriggered();
782 void PartSet_Module::launchOperation(const QString& theCmdId)
784 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
785 // Show constraints if a constraint was anOperation
786 myHasConstraintShown = mySketchMgr->isConstraintsShown();
787 mySketchMgr->onShowConstraintsToggle(true);
789 ModuleBase_IModule::launchOperation(theCmdId);
793 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
795 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
796 if (!anAIS.IsNull()) {
797 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
798 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
799 if (!aDim.IsNull()) {
800 aCtx->SetZLayer(aDim, myVisualLayerId);
802 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
804 aCtx->SetZLayer(aCons, myVisualLayerId);
809 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
812 // it should be recomputed in order to disappear in the viewer if the corresponded object
814 //if (myCustomPrs->isActive())
815 // myCustomPrs->redisplay(theObject, false);
818 void PartSet_Module::onViewTransformed(int theTrsfType)
820 // Set length of arrows constant in pixel size
821 // if the operation is panning or rotate or panglobal then do nothing
822 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
824 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
825 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
826 if (aContext.IsNull())
829 //Handle(V3d_View) aView = aViewer->activeView();
831 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
832 XGUI_Workshop* aWorkshop = aConnector->workshop();
833 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
834 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
835 Handle(V3d_View) aView;
837 for (aV3dViewer->InitDefinedViews();
838 aV3dViewer->MoreDefinedViews();
839 aV3dViewer->NextDefinedViews()) {
840 Handle(V3d_View) aV = aV3dViewer->DefinedView();
841 double aS = aV->Scale();
849 double aLen = aView->Convert(20);
851 double aPrevLen = SketcherPrs_Tools::getArrowSize();
852 SketcherPrs_Tools::setArrowSize(aLen);
853 const double aPrevScale = aViewer->Scale(aViewer->activeView());
854 const double aCurScale = aViewer->activeView()->Camera()->Scale();
855 aViewer->SetScale(aViewer->activeView(), aCurScale);
856 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
857 bool isModified = false;
858 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
859 foreach (AISObjectPtr aAIS, aPrsList) {
860 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
862 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
863 if (!aDim.IsNull()) {
864 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
865 aContext->Redisplay(aDim, false);
870 aDisplayer->updateViewer();
873 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
875 bool isRedisplayed = false;
876 if (myCustomPrs->isActive())
877 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
879 return isRedisplayed;
882 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
884 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
886 //QLineEdit* aLabel = aOB->activeDocLabel();
887 //QPalette aPalet = aLabel->palette();
888 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
889 //aLabel->setPalette(aPalet);
890 aOB->treeView()->setExpandsOnDoubleClick(false);
891 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
892 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
896 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
899 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
901 /// If last line finished on vertex the lines creation sequence has to be break
902 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
903 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
904 // if there is an active widget, find the presented object in it
906 anActiveWidget = aPanel->preselectionWidget();
908 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
910 if (aWidgetValidated)
911 anObject = aWidgetValidated->findPresentedObject(theAIS);
916 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
918 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
919 int aSelected = aObjects.size();
920 SessionPtr aMgr = ModelAPI_Session::get();
921 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
922 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
924 ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
925 if (aSelected == 1) {
926 bool hasResult = false;
927 bool hasFeature = false;
928 bool hasParameter = false;
929 bool hasSubFeature = false;
930 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
932 ObjectPtr aObject = aObjects.first();
934 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
935 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
936 bool isPart = aPart.get() ||
937 (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
939 DocumentPtr aPartDoc;
941 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
943 if (aPart.get()) // this may be null is Part feature is disabled
944 aPartDoc = aPart->partDoc();
946 theMenu->addAction(aActivatePartAction);
947 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
949 } else if (aObject->document() == aMgr->activeDocument()) {
950 if (hasParameter || hasFeature) {
951 myMenuMgr->action("EDIT_CMD")->setEnabled(true);
952 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
953 if (aCurrentOp && aFeature.get()) {
954 if (aCurrentOp->id().toStdString() == aFeature->getKind())
955 myMenuMgr->action("EDIT_CMD")->setEnabled(false);
960 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
962 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
963 } else { // If feature is 0 the it means that selected root object (document)
964 theMenu->addAction(aActivatePartSetAction);
965 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
967 } else if (aSelected == 0) {
968 // if there is no selection then it means that upper label is selected
969 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
970 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
971 theMenu->addAction(aActivatePartSetAction);
972 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
974 bool aNotDeactivate = (aCurrentOp == 0);
975 if (!aNotDeactivate) {
976 aActivatePartAction->setEnabled(false);
977 aActivatePartSetAction->setEnabled(false);
981 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
983 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
984 // Do not change activation of parts if an operation active
985 static QStringList aAllowActivationList;
986 if (aAllowActivationList.isEmpty())
987 aAllowActivationList <<
988 QString(PartSetPlugin_Part::ID().c_str()) <<
989 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
990 QString(PartSetPlugin_Remove::ID().c_str());
991 if (myWorkshop->currentOperation() &&
992 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
994 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
995 XGUI_Workshop* aWorkshop = aConnector->workshop();
996 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
997 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
998 QPalette aPalet = aLabel->palette();
1000 SessionPtr aMgr = ModelAPI_Session::get();
1001 DocumentPtr aActiveDoc = aMgr->activeDocument();
1002 if (aActivePartIndex.isValid())
1003 aTreeView->setExpanded(aActivePartIndex, false);
1004 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1005 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1006 if (aActivePartIndex.isValid())
1007 aTreeView->setExpanded(aActivePartIndex, true);
1009 aLabel->setPalette(aPalet);
1010 aWorkshop->updateCommandStatus();
1012 // Update displayed objects in order to update active color
1013 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1014 QObjectPtrList aObjects = aDisplayer->displayedObjects();
1016 foreach(ObjectPtr aObj, aObjects) {
1017 //TODO: replace by redisplay event.
1018 aHidden = !aObj->data() || !aObj->data()->isValid() ||
1019 aObj->isDisabled() || (!aObj->isDisplayed());
1021 aDisplayer->redisplay(aObj, false);
1023 aDisplayer->updateViewer();
1027 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1029 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1031 SessionPtr aMgr = ModelAPI_Session::get();
1032 if (!theIndex.isValid()) {
1033 aMgr->setActiveDocument(aMgr->moduleDocument());
1036 if (theIndex.column() != 0) // Use only first column
1039 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1040 XGUI_Workshop* aWorkshop = aConnector->workshop();
1041 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1042 // De not use non editable Indexes
1043 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1045 ObjectPtr aObj = aDataModel->object(theIndex);
1047 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1048 if (!aPart.get()) { // Probably this is Feature
1049 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1050 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1051 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1054 if (aPart.get()) { // if this is a part
1055 if (aPart->partDoc() == aMgr->activeDocument()) {
1056 myMenuMgr->activatePartSet();
1064 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1066 // z layer is created for all started operations in order to visualize operation AIS presentation
1068 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1069 if (aContext.IsNull())
1072 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1073 if (myVisualLayerId == 0) {
1074 if (myVisualLayerId == 0)
1075 aViewer->AddZLayer(myVisualLayerId);
1077 TColStd_SequenceOfInteger aZList;
1078 aViewer->GetAllZLayers(aZList);
1079 bool aFound = false;
1080 for (int i = 1; i <= aZList.Length(); i++) {
1081 if (aZList(i) == myVisualLayerId) {
1087 aViewer->AddZLayer(myVisualLayerId);
1089 // if there is an active operation with validated widget,
1090 // the filters of this widget should be activated in the created view
1091 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1093 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1094 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1095 if (anActiveWidget) {
1096 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1098 if (aWidgetValidated)
1099 aWidgetValidated->activateFilters(true);