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);
131 #ifdef ModuleDataModel
132 myDataModel = new PartSet_DocumentDataModel(this);
135 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
136 XGUI_Workshop* aWorkshop = aConnector->workshop();
138 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
139 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
140 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
141 this, SLOT(onOperationActivatedByPreselection()));
143 ModuleBase_IViewer* aViewer = theWshop->viewer();
144 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
145 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
146 connect(aViewer, SIGNAL(viewTransformed(int)),
147 SLOT(onViewTransformed(int)));
148 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
149 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
151 myMenuMgr = new PartSet_MenuMgr(this);
152 myCustomPrs = new PartSet_CustomPrs(theWshop);
154 Events_Loop* aLoop = Events_Loop::loop();
155 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
157 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
158 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
161 PartSet_Module::~PartSet_Module()
163 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
164 for (; aIt.More(); aIt.Next()) {
165 Handle(SelectMgr_Filter) aFilter = aIt.Value();
166 if (!aFilter.IsNull())
172 void PartSet_Module::activateSelectionFilters()
174 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
175 for (; aIt.More(); aIt.Next()) {
176 Handle(SelectMgr_Filter) aFilter = aIt.Value();
177 if (!aFilter.IsNull())
178 myWorkshop->viewer()->addSelectionFilter(aFilter);
182 void PartSet_Module::deactivateSelectionFilters()
184 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
185 for (; aIt.More(); aIt.Next()) {
186 Handle(SelectMgr_Filter) aFilter = aIt.Value();
187 if (!aFilter.IsNull())
188 myWorkshop->viewer()->removeSelectionFilter(aFilter);
192 void PartSet_Module::registerValidators()
194 //Registering of validators
195 SessionPtr aMgr = ModelAPI_Session::get();
196 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
197 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
198 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
199 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
200 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
201 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
202 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
203 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
204 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
205 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
206 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
207 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
208 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
209 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
211 aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
212 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
213 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
214 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
216 aFactory->registerValidator("GeomValidators_ConstructionComposite",
217 new GeomValidators_ConstructionComposite);
219 aFactory->registerValidator("GeomValidators_ZeroOffset",
220 new GeomValidators_ZeroOffset);
222 aFactory->registerValidator("GeomValidators_BooleanArguments",
223 new GeomValidators_BooleanArguments);
225 aFactory->registerValidator("PartSet_SketchEntityValidator",
226 new PartSet_SketchEntityValidator);
228 aFactory->registerValidator("GeomValidators_Different",
229 new GeomValidators_Different);
231 aFactory->registerValidator("GeomValidators_PartitionArguments",
232 new GeomValidators_PartitionArguments);
235 void PartSet_Module::registerFilters()
237 //Registering of selection filters
238 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
239 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
242 void PartSet_Module::registerProperties()
244 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
246 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
247 Config_Prop::Integer, SKETCH_WIDTH);
250 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
252 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
253 mySketchMgr->commitNestedSketch(theOperation);
256 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
257 if (!aFOperation || aFOperation->isEditOperation())
259 // the selection is cleared after commit the create operation
260 // in order to do not use the same selected objects in the restarted operation
261 // for common behaviour, the selection is cleared even if the operation is not restarted
262 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
263 XGUI_Workshop* aWorkshop = aConnector->workshop();
264 aWorkshop->selector()->clearSelection();
266 /// Restart sketcher operations automatically
267 FeaturePtr aFeature = aFOperation->feature();
268 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
269 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
270 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
271 myRestartingMode == RM_EmptyFeatureUsed)) {
272 myLastOperationId = aFOperation->id();
273 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
274 if (!sketchMgr()->sketchSolverError())
275 launchOperation(myLastOperationId);
277 breakOperationSequence();
280 void PartSet_Module::breakOperationSequence()
282 myLastOperationId = "";
283 myLastFeature = FeaturePtr();
284 myRestartingMode = RM_None;
287 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
289 breakOperationSequence();
292 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
294 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
295 mySketchMgr->startSketch(theOperation);
297 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
298 mySketchMgr->startNestedSketch(theOperation);
301 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
303 myCustomPrs->activate(aFOperation->feature(), true);
306 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
308 ModuleBase_IModule::onOperationResumed(theOperation);
310 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
312 myCustomPrs->activate(aFOperation->feature(), true);
315 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
317 bool isModified = myCustomPrs->deactivate(false);
319 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
320 mySketchMgr->stopSketch(theOperation);
322 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
323 mySketchMgr->stopNestedSketch(theOperation);
327 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
328 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
329 aDisplayer->updateViewer();
331 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
334 ModuleBase_Operation* PartSet_Module::currentOperation() const
336 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
337 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
338 return anOpMgr->currentOperation();
341 bool PartSet_Module::canUndo() const
343 bool aCanUndo = false;
344 SessionPtr aMgr = ModelAPI_Session::get();
345 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
346 aCanUndo = !aMgr->isOperation();
347 if (!aCanUndo) // check the enable state additionally by sketch manager
348 aCanUndo = aMgr->canUndo();
353 bool PartSet_Module::canRedo() const
355 bool aCanRedo = false;
356 SessionPtr aMgr = ModelAPI_Session::get();
357 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
358 aCanRedo = !aMgr->isOperation();
359 if (!aCanRedo) // check the enable state additionally by sketch manager
360 aCanRedo = aMgr->canRedo();
365 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
368 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
369 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
371 // part features are removed in the PartSet module only.
372 if (aFeature->getKind() == PartSetPlugin_Part::ID())
379 bool PartSet_Module::canCommitOperation() const
381 return mySketchMgr->canCommitOperation();
384 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
386 // the sketch manager put the restriction to the objects erase
387 return mySketchMgr->canEraseObject(theObject);
390 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
392 // the sketch manager put the restriction to the objects display
393 return mySketchMgr->canDisplayObject(theObject);
396 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
398 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
400 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
401 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
402 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
403 if (isSketchOp || isNestedOp) {
404 // in active sketch operation it is possible to activate operation object in selection
405 // in the edit operation, e.g. points of the line can be moved when the line is edited
406 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
407 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
412 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
414 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
417 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
419 myMenuMgr->updateViewerMenu(theStdActions);
422 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
424 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
426 if (anError.isEmpty())
427 anError = sketchMgr()->getFeatureError(theFeature);
429 if (anError.isEmpty()) {
430 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
431 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
433 if (anOpMgr->isValidationLocked()) {
434 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
435 (anOpMgr->currentOperation());
436 if (!aFOperation || theFeature == aFOperation->feature())
437 anError = "Validation is locked by the current operation";
443 void PartSet_Module::activeSelectionModes(QIntList& theModes)
446 if (mySketchMgr->activeSketch().get())
447 PartSet_SketcherMgr::sketchSelectionModes(theModes);
450 bool PartSet_Module::isMouseOverWindow()
452 return mySketchMgr->isMouseOverWindow();
455 void PartSet_Module::closeDocument()
460 void PartSet_Module::clearViewer()
462 myCustomPrs->clearPrs();
464 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
465 XGUI_Workshop* aWorkshop = aConnector->workshop();
466 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
467 aDisplayer->deactivateSelectionFilters();
470 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
472 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
476 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
477 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
478 // we have to manually activate the sketch label in edit mode
479 aPanel->activateWidget(aPanel->modelWidgets().first());
483 // Restart last operation type
484 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
485 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
486 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
487 // Initialise new line with first point equal to end of previous
488 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
490 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
491 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
492 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
494 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
495 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
496 aWgt->attributeID(), aPoint->x(), aPoint->y());
497 aPanel->activateNextWidget(aPnt2dWgt);
505 void PartSet_Module::onSelectionChanged()
507 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
511 bool isSketcherOp = false;
512 // An edit operation is enable only if the current opeation is the sketch operation
513 if (mySketchMgr->activeSketch()) {
514 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
515 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
518 // Editing of constraints can be done on selection
519 ModuleBase_ISelection* aSelect = myWorkshop->selection();
520 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
521 if (aSelected.size() == 1) {
522 ModuleBase_ViewerPrs aPrs = aSelected.first();
523 ObjectPtr aObject = aPrs.object();
524 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
526 std::string aId = aFeature->getKind();
527 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
528 (aId == SketchPlugin_ConstraintLength::ID()) ||
529 (aId == SketchPlugin_ConstraintDistance::ID()) ||
530 (aId == SketchPlugin_ConstraintAngle::ID())) {
531 editFeature(aFeature);
538 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
540 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
541 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
542 anOpMgr->onKeyReleased(theEvent);
545 void PartSet_Module::onEnterReleased()
547 myRestartingMode = RM_EmptyFeatureUsed;
550 void PartSet_Module::onOperationActivatedByPreselection()
552 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
553 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
554 // Set final definitions if they are necessary
555 //propertyPanelDefined(aOperation);
557 /// Commit sketcher operations automatically
558 anOperation->commit();
562 void PartSet_Module::onNoMoreWidgets()
564 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
566 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
567 if (myRestartingMode != RM_Forbided)
568 myRestartingMode = RM_LastFeatureUsed;
569 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
570 XGUI_Workshop* aWorkshop = aConnector->workshop();
571 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
572 // do nothing if the feature can not be applyed
573 if (anOpMgr->isApplyEnabled())
574 anOperation->commit();
579 void PartSet_Module::onVertexSelected()
581 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
582 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
583 /// If last line finished on vertex the lines creation sequence has to be break
584 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
585 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
586 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
587 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
588 bool aFoundWidget = false;
589 bool aFoundObligatory = false;
590 for (; anIt != aLast && !aFoundObligatory; anIt++) {
592 aFoundWidget = *anIt == anActiveWidget;
594 aFoundObligatory = (*anIt)->isObligatory();
596 if (!aFoundObligatory)
597 myRestartingMode = RM_Forbided;
601 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
602 Config_WidgetAPI* theWidgetApi, std::string theParentId)
604 ModuleBase_IWorkshop* aWorkshop = workshop();
605 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
606 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
607 ModuleBase_ModelWidget* aWgt = NULL;
608 if (theType == "sketch-start-label") {
609 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
610 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
611 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
612 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
613 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
614 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
616 } else if (theType == "sketch-2dpoint_selector") {
617 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
618 theWidgetApi, theParentId);
619 aPointWgt->setSketch(mySketchMgr->activeSketch());
620 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
622 } else if (theType == "point2ddistance") {
623 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
624 aWorkshop, theWidgetApi, theParentId);
625 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
627 } else if(theType == "point2dangle") {
628 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
629 aWorkshop, theWidgetApi, theParentId);
630 anAngleWgt->setSketch(mySketchMgr->activeSketch());
632 } else if (theType == "sketch_shape_selector") {
633 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
634 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
635 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
636 aWgt = aShapeSelectorWgt;
637 } else if (theType == "sketch_multi_selector") {
638 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
639 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
640 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
641 aWgt = aShapeSelectorWgt;
642 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
643 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
644 } else if (theType == "export_file_selector") {
645 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
646 } else if (theType == "sketch_launcher") {
647 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
653 bool PartSet_Module::deleteObjects()
655 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
656 XGUI_Workshop* aWorkshop = aConnector->workshop();
657 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
659 //SessionPtr aMgr = ModelAPI_Session::get();
660 // 1. check whether the delete should be processed in the module
661 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
662 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
663 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
664 if (isSketchOp || isNestedOp) {
665 // 2. find selected presentations
666 // selected objects should be collected before the current operation abort because
667 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
668 ModuleBase_ISelection* aSel = workshop()->selection();
669 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
670 // if there are no selected objects in the viewer, that means that the selection in another
671 // place cased this method. It is necessary to return the false value to understande in above
672 // method that delete is not processed
673 if (aSelectedObj.count() == 0)
676 // avoid delete of the objects, which are not belong to the current sketch
677 // in order to do not delete results of other sketches
678 QObjectPtrList aSketchObjects;
679 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
680 for ( ; anIt != aLast; anIt++) {
681 ObjectPtr anObject = *anIt;
682 if (mySketchMgr->isObjectOfSketch(anObject)) {
683 // sketch feature should be used in this list because workshop deletes features only
684 // results are skipped
685 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
686 aSketchObjects.append(aSketchFeature);
689 // if the selection contains only local selected presentations from other sketches,
690 // the Delete operation should not be done at all
691 if (aSketchObjects.size() == 0)
694 // 3. start operation
695 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
696 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
698 // the active nested sketch operation should be aborted unconditionally
699 // the Delete action should be additionally granted for the Sketch operation
700 // in order to do not abort/commit it
701 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
702 return true; // the objects are processed but can not be deleted
704 anOpMgr->startOperation(anOpAction);
706 // 4. delete features
707 // sketch feature should be skipped, only sub-features can be removed
708 // when sketch operation is active
709 aWorkshop->deleteFeatures(aSketchObjects);
711 anOpMgr->commitOperation();
714 bool isPartRemoved = false;
715 // Delete part with help of PartSet plugin
716 // TODO: the deleted objects has to be processed by multiselection
717 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
718 if (aObjects.size() == 1) {
719 ObjectPtr aObj = aObjects.first();
720 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
721 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
722 // Remove feature should be created in the document of the part results
723 ResultPtr aPartResult = aFeature->firstResult();
724 if (aPartResult.get()) {
725 std::shared_ptr<ModelAPI_ResultPart> aPart =
726 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
727 DocumentPtr aPartDoc = aPart->partDoc();
728 if (aPartDoc.get()) {
729 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
730 (PartSetPlugin_Remove::ID().c_str(), this);
731 if (!anOpMgr->canStartOperation(anOpAction->id()))
732 return true; // the objects are processed but can not be deleted
734 anOpMgr->startOperation(anOpAction);
736 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
739 anOpMgr->commitOperation();
740 isPartRemoved = true;
745 return isPartRemoved;
750 void PartSet_Module::onFeatureTriggered()
752 QAction* aCmd = dynamic_cast<QAction*>(sender());
753 if (aCmd->isCheckable() && aCmd->isChecked()) {
754 // 1. check whether the delete should be processed in the module
755 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
756 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
758 // in case if in the viewer nothing is displayed, the create operation should not be
759 // comitted even if all values of the feature are initialized
760 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
761 // the action information should be saved before the operation is aborted
762 // because this abort leads to update command status, which unchecks this action
763 anOperation->abort();
764 launchOperation(aCmd->data().toString());
768 ModuleBase_IModule::onFeatureTriggered();
771 void PartSet_Module::launchOperation(const QString& theCmdId)
773 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
774 // Show constraints if a constraint was anOperation
775 myHasConstraintShown = mySketchMgr->isConstraintsShown();
776 mySketchMgr->onShowConstraintsToggle(true);
778 ModuleBase_IModule::launchOperation(theCmdId);
782 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
784 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
785 if (!anAIS.IsNull()) {
786 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
787 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
788 if (!aDim.IsNull()) {
789 aCtx->SetZLayer(aDim, myVisualLayerId);
791 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
793 aCtx->SetZLayer(aCons, myVisualLayerId);
798 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
801 // it should be recomputed in order to disappear in the viewer if the corresponded object
803 //if (myCustomPrs->isActive())
804 // myCustomPrs->redisplay(theObject, false);
807 void PartSet_Module::onViewTransformed(int theTrsfType)
809 // Set length of arrows constant in pixel size
810 // if the operation is panning or rotate or panglobal then do nothing
811 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
813 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
814 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
815 if (aContext.IsNull())
818 //Handle(V3d_View) aView = aViewer->activeView();
820 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
821 XGUI_Workshop* aWorkshop = aConnector->workshop();
822 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
823 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
824 Handle(V3d_View) aView;
826 for (aV3dViewer->InitDefinedViews();
827 aV3dViewer->MoreDefinedViews();
828 aV3dViewer->NextDefinedViews()) {
829 Handle(V3d_View) aV = aV3dViewer->DefinedView();
830 double aS = aV->Scale();
838 double aLen = aView->Convert(20);
840 double aPrevLen = SketcherPrs_Tools::getArrowSize();
841 SketcherPrs_Tools::setArrowSize(aLen);
842 const double aPrevScale = aViewer->Scale(aViewer->activeView());
843 const double aCurScale = aViewer->activeView()->Camera()->Scale();
844 aViewer->SetScale(aViewer->activeView(), aCurScale);
845 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
846 bool isModified = false;
847 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
848 foreach (AISObjectPtr aAIS, aPrsList) {
849 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
851 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
852 if (!aDim.IsNull()) {
853 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
854 aContext->Redisplay(aDim, false);
859 aDisplayer->updateViewer();
862 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
864 bool isRedisplayed = false;
865 if (myCustomPrs->isActive())
866 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
868 return isRedisplayed;
871 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
873 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
875 //QLineEdit* aLabel = aOB->activeDocLabel();
876 //QPalette aPalet = aLabel->palette();
877 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
878 //aLabel->setPalette(aPalet);
879 aOB->treeView()->setExpandsOnDoubleClick(false);
880 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
881 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
882 #ifdef ModuleDataModel
883 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
884 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
885 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
886 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
891 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
894 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
896 /// If last line finished on vertex the lines creation sequence has to be break
897 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
898 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
899 // if there is an active widget, find the presented object in it
901 anActiveWidget = aPanel->preselectionWidget();
903 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
905 if (aWidgetValidated)
906 anObject = aWidgetValidated->findPresentedObject(theAIS);
911 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
913 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
914 int aSelected = aObjects.size();
915 SessionPtr aMgr = ModelAPI_Session::get();
916 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
917 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
918 if (aSelected == 1) {
919 bool hasResult = false;
920 bool hasFeature = false;
921 bool hasParameter = false;
922 bool hasSubFeature = false;
923 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
925 ObjectPtr aObject = aObjects.first();
927 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
928 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
929 bool isPart = aPart.get() ||
930 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
932 DocumentPtr aPartDoc;
934 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
936 if (aPart.get()) // this may be null is Part feature is disabled
937 aPartDoc = aPart->partDoc();
939 theMenu->addAction(aActivatePartAction);
940 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
942 } else if (aObject->document() == aMgr->activeDocument()) {
943 if (hasParameter || hasFeature)
944 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
947 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
949 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
950 } else { // If feature is 0 the it means that selected root object (document)
951 theMenu->addAction(aActivatePartSetAction);
952 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
954 } else if (aSelected == 0) {
955 // if there is no selection then it means that upper label is selected
956 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
957 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
958 theMenu->addAction(aActivatePartSetAction);
959 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
961 bool aNotDeactivate = (myWorkshop->currentOperation() == 0);
962 if (!aNotDeactivate) {
963 aActivatePartAction->setEnabled(false);
964 aActivatePartSetAction->setEnabled(false);
968 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
970 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
971 // Do not change activation of parts if an operation active
972 static QStringList aAllowActivationList;
973 if (aAllowActivationList.isEmpty())
974 aAllowActivationList <<
975 QString(PartSetPlugin_Part::ID().c_str()) <<
976 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
977 QString(PartSetPlugin_Remove::ID().c_str());
978 if (myWorkshop->currentOperation() &&
979 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
981 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
982 XGUI_Workshop* aWorkshop = aConnector->workshop();
983 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
984 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
985 QPalette aPalet = aLabel->palette();
987 SessionPtr aMgr = ModelAPI_Session::get();
988 DocumentPtr aActiveDoc = aMgr->activeDocument();
989 #ifdef ModuleDataModel
990 QModelIndex aOldIndex = myDataModel->activePartTree();
991 DocumentPtr aDoc = aMgr->moduleDocument();
992 if (aActiveDoc == aDoc) {
993 if (aOldIndex.isValid())
994 aTreeView->setExpanded(aOldIndex, false);
995 myDataModel->deactivatePart();
996 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
998 std::string aGrpName = ModelAPI_ResultPart::group();
999 for (int i = 0; i < aDoc->size(aGrpName); i++) {
1000 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
1001 if (aPart->partDoc() == aActiveDoc) {
1002 QModelIndex aIndex = myDataModel->partIndex(aPart);
1003 if (myDataModel->activatePart(aIndex)) {
1004 if (aOldIndex.isValid())
1005 aTreeView->setExpanded(aOldIndex, false);
1006 aTreeView->setExpanded(myDataModel->activePartTree(), true);
1007 aPalet.setColor(QPalette::Text, Qt::black);
1014 // Problem with MPV: At first time on creation it doesn't work because Part feature
1015 // creation event will be sent after
1016 if (aActivePartIndex.isValid())
1017 aTreeView->setExpanded(aActivePartIndex, false);
1018 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1019 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1020 if (aActivePartIndex.isValid())
1021 aTreeView->setExpanded(aActivePartIndex, true);
1023 aLabel->setPalette(aPalet);
1024 aWorkshop->updateCommandStatus();
1026 // Update displayed objects in order to update active color
1027 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1028 QObjectPtrList aObjects = aDisplayer->displayedObjects();
1029 foreach(ObjectPtr aObj, aObjects)
1030 aDisplayer->redisplay(aObj, false);
1031 aDisplayer->updateViewer();
1035 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1037 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1039 SessionPtr aMgr = ModelAPI_Session::get();
1040 if (!theIndex.isValid()) {
1041 aMgr->setActiveDocument(aMgr->moduleDocument());
1044 if (theIndex.column() != 0) // Use only first column
1046 #ifdef ModuleDataModel
1047 ObjectPtr aObj = myDataModel->object(theIndex);
1049 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1050 XGUI_Workshop* aWorkshop = aConnector->workshop();
1051 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1052 // De not use non editable Indexes
1053 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1055 ObjectPtr aObj = aDataModel->object(theIndex);
1058 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1059 if (!aPart.get()) { // Probably this is Feature
1060 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1061 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1062 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1065 if (aPart.get()) { // if this is a part
1066 if (aPart->partDoc() == aMgr->activeDocument()) {
1067 myMenuMgr->activatePartSet();
1075 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1077 // z layer is created for all started operations in order to visualize operation AIS presentation
1079 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1080 if (aContext.IsNull())
1083 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1084 if (myVisualLayerId == 0) {
1085 if (myVisualLayerId == 0)
1086 aViewer->AddZLayer(myVisualLayerId);
1088 TColStd_SequenceOfInteger aZList;
1089 aViewer->GetAllZLayers(aZList);
1090 bool aFound = false;
1091 for (int i = 1; i <= aZList.Length(); i++) {
1092 if (aZList(i) == myVisualLayerId) {
1098 aViewer->AddZLayer(myVisualLayerId);
1100 // if there is an active operation with validated widget,
1101 // the filters of this widget should be activated in the created view
1102 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1104 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1105 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1106 if (anActiveWidget) {
1107 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1109 if (aWidgetValidated)
1110 aWidgetValidated->activateFilters(true);