1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_Module.h"
4 #include "PartSet_WidgetSketchLabel.h"
5 #include "PartSet_Validators.h"
6 #include "PartSet_Tools.h"
7 #include "PartSet_WidgetPoint2d.h"
8 #include "PartSet_WidgetPoint2dDistance.h"
9 #include "PartSet_WidgetPoint2DFlyout.h"
10 #include "PartSet_WidgetShapeSelector.h"
11 #include "PartSet_WidgetPoint2dAngle.h"
12 #include "PartSet_WidgetMultiSelector.h"
13 #include "PartSet_WidgetEditor.h"
14 #include "PartSet_WidgetFileSelector.h"
15 #include "PartSet_WidgetSketchCreator.h"
16 #include "PartSet_SketcherMgr.h"
17 #include "PartSet_MenuMgr.h"
18 #include "PartSet_CustomPrs.h"
19 #include "PartSet_IconFactory.h"
21 #include "PartSet_Filters.h"
22 #include "PartSet_FilterInfinite.h"
24 #include <PartSetPlugin_Remove.h>
25 #include <PartSetPlugin_Part.h>
26 #include <PartSetPlugin_Duplicate.h>
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_OperationAction.h>
30 #include <ModuleBase_IViewer.h>
31 #include <ModuleBase_IViewWindow.h>
32 #include <ModuleBase_IPropertyPanel.h>
33 #include <ModuleBase_WidgetEditor.h>
34 #include <ModuleBase_WidgetValidated.h>
35 #include <ModuleBase_FilterFactory.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_OperationFeature.h>
39 #include <GeomValidators_ShapeType.h>
40 #include <GeomValidators_Finite.h>
41 #include <GeomValidators_Face.h>
42 #include <GeomValidators_ConstructionComposite.h>
43 #include <GeomValidators_ZeroOffset.h>
44 #include <GeomValidators_BooleanArguments.h>
45 #include <GeomValidators_Different.h>
46 #include <GeomValidators_PartitionArguments.h>
49 #include <ModelAPI_Object.h>
50 #include <ModelAPI_Events.h>
51 #include <ModelAPI_Validator.h>
52 #include <ModelAPI_Data.h>
53 #include <ModelAPI_Session.h>
54 #include <GeomValidators_DifferentShapes.h>
55 #include <ModelAPI_ResultBody.h>
56 #include <ModelAPI_AttributeString.h>
58 #include <GeomDataAPI_Point2D.h>
59 #include <GeomDataAPI_Point.h>
60 #include <GeomDataAPI_Dir.h>
62 #include <XGUI_Displayer.h>
63 #include <XGUI_Workshop.h>
64 #include <XGUI_OperationMgr.h>
65 #include <XGUI_PropertyPanel.h>
66 #include <XGUI_ModuleConnector.h>
67 #include <XGUI_ContextMenuMgr.h>
68 #include <XGUI_Tools.h>
69 #include <XGUI_ObjectsBrowser.h>
70 #include <XGUI_SelectionMgr.h>
71 #include <XGUI_DataModel.h>
72 #include <XGUI_ErrorMgr.h>
74 #include <SketchPlugin_Feature.h>
75 #include <SketchPlugin_Sketch.h>
76 #include <SketchPlugin_Line.h>
77 #include <SketchPlugin_Arc.h>
78 #include <SketchPlugin_Circle.h>
79 #include <SketchPlugin_Point.h>
80 #include <SketchPlugin_ConstraintAngle.h>
81 #include <SketchPlugin_ConstraintLength.h>
82 #include <SketchPlugin_ConstraintDistance.h>
83 #include <SketchPlugin_ConstraintParallel.h>
84 #include <SketchPlugin_ConstraintPerpendicular.h>
85 #include <SketchPlugin_ConstraintRadius.h>
87 #include <SketcherPrs_SymbolPrs.h>
88 #include <SketcherPrs_Tools.h>
90 #include <Events_Loop.h>
91 #include <Config_PropManager.h>
92 #include <Config_Keywords.h>
94 #include <StdSelect_TypeOfFace.hxx>
95 #include <TopoDS_Vertex.hxx>
97 #include <TopoDS_Shape.hxx>
98 #include <BRep_Tool.hxx>
99 #include <AIS_Dimension.hxx>
102 #include <QMouseEvent>
105 #include <QApplication>
106 #include <QMessageBox>
107 #include <QMainWindow>
110 #include <GeomAlgoAPI_FaceBuilder.h>
111 #include <GeomDataAPI_Dir.h>
113 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
119 /*!Create and return new instance of XGUI_Module*/
120 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
122 return new PartSet_Module(theWshop);
125 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
126 : ModuleBase_IModule(theWshop),
127 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
129 new PartSet_IconFactory();
131 mySketchMgr = new PartSet_SketcherMgr(this);
133 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
134 XGUI_Workshop* aWorkshop = aConnector->workshop();
136 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
137 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
138 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
139 this, SLOT(onOperationActivatedByPreselection()));
141 ModuleBase_IViewer* aViewer = theWshop->viewer();
142 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
143 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
144 connect(aViewer, SIGNAL(viewTransformed(int)),
145 SLOT(onViewTransformed(int)));
146 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
147 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
149 myMenuMgr = new PartSet_MenuMgr(this);
150 myCustomPrs = new PartSet_CustomPrs(theWshop);
152 Events_Loop* aLoop = Events_Loop::loop();
153 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
155 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
156 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
159 PartSet_Module::~PartSet_Module()
161 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
162 for (; aIt.More(); aIt.Next()) {
163 Handle(SelectMgr_Filter) aFilter = aIt.Value();
164 if (!aFilter.IsNull())
170 void PartSet_Module::activateSelectionFilters()
172 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
173 for (; aIt.More(); aIt.Next()) {
174 Handle(SelectMgr_Filter) aFilter = aIt.Value();
175 if (!aFilter.IsNull())
176 myWorkshop->viewer()->addSelectionFilter(aFilter);
180 void PartSet_Module::deactivateSelectionFilters()
182 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
183 for (; aIt.More(); aIt.Next()) {
184 Handle(SelectMgr_Filter) aFilter = aIt.Value();
185 if (!aFilter.IsNull())
186 myWorkshop->viewer()->removeSelectionFilter(aFilter);
190 void PartSet_Module::registerValidators()
192 //Registering of validators
193 SessionPtr aMgr = ModelAPI_Session::get();
194 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
195 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
196 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
197 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
198 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
199 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
200 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
201 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
202 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
203 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
204 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
205 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
206 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
207 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
209 aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
210 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
211 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
212 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
214 aFactory->registerValidator("GeomValidators_ConstructionComposite",
215 new GeomValidators_ConstructionComposite);
217 aFactory->registerValidator("GeomValidators_ZeroOffset",
218 new GeomValidators_ZeroOffset);
220 aFactory->registerValidator("GeomValidators_BooleanArguments",
221 new GeomValidators_BooleanArguments);
223 aFactory->registerValidator("PartSet_SketchEntityValidator",
224 new PartSet_SketchEntityValidator);
226 aFactory->registerValidator("GeomValidators_Different",
227 new GeomValidators_Different);
229 aFactory->registerValidator("GeomValidators_PartitionArguments",
230 new GeomValidators_PartitionArguments);
233 void PartSet_Module::registerFilters()
235 //Registering of selection filters
236 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
237 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
240 void PartSet_Module::registerProperties()
242 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
244 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
245 Config_Prop::Integer, SKETCH_WIDTH);
248 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
250 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
251 mySketchMgr->commitNestedSketch(theOperation);
254 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
255 if (!aFOperation || aFOperation->isEditOperation())
257 // the selection is cleared after commit the create operation
258 // in order to do not use the same selected objects in the restarted operation
259 // for common behaviour, the selection is cleared even if the operation is not restarted
260 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
261 XGUI_Workshop* aWorkshop = aConnector->workshop();
262 aWorkshop->selector()->clearSelection();
264 /// Restart sketcher operations automatically
265 FeaturePtr aFeature = aFOperation->feature();
266 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
267 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
268 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
269 myRestartingMode == RM_EmptyFeatureUsed)) {
270 myLastOperationId = aFOperation->id();
271 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
272 if (!sketchMgr()->sketchSolverError())
273 launchOperation(myLastOperationId);
275 breakOperationSequence();
278 void PartSet_Module::breakOperationSequence()
280 myLastOperationId = "";
281 myLastFeature = FeaturePtr();
282 myRestartingMode = RM_None;
285 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
287 breakOperationSequence();
290 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
292 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
293 mySketchMgr->startSketch(theOperation);
295 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
296 mySketchMgr->startNestedSketch(theOperation);
299 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
301 myCustomPrs->activate(aFOperation->feature(), true);
304 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
306 ModuleBase_IModule::onOperationResumed(theOperation);
308 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
310 myCustomPrs->activate(aFOperation->feature(), true);
313 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
315 bool isModified = myCustomPrs->deactivate(false);
317 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
318 mySketchMgr->stopSketch(theOperation);
320 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
321 mySketchMgr->stopNestedSketch(theOperation);
324 //VSV: Viewer is updated on feature update and redisplay
326 // XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
327 // XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
328 // aDisplayer->updateViewer();
330 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
333 ModuleBase_Operation* PartSet_Module::currentOperation() const
335 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
336 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
337 return anOpMgr->currentOperation();
340 bool PartSet_Module::canUndo() const
342 bool aCanUndo = false;
343 SessionPtr aMgr = ModelAPI_Session::get();
344 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
345 aCanUndo = !aMgr->isOperation();
346 if (!aCanUndo) // check the enable state additionally by sketch manager
347 aCanUndo = aMgr->canUndo();
352 bool PartSet_Module::canRedo() const
354 bool aCanRedo = false;
355 SessionPtr aMgr = ModelAPI_Session::get();
356 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
357 aCanRedo = !aMgr->isOperation();
358 if (!aCanRedo) // check the enable state additionally by sketch manager
359 aCanRedo = aMgr->canRedo();
364 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
367 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
368 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
370 // part features are removed in the PartSet module only.
371 if (aFeature->getKind() == PartSetPlugin_Part::ID())
378 bool PartSet_Module::canCommitOperation() const
380 return mySketchMgr->canCommitOperation();
383 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
385 // the sketch manager put the restriction to the objects erase
386 return mySketchMgr->canEraseObject(theObject);
389 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
391 // the sketch manager put the restriction to the objects display
392 return mySketchMgr->canDisplayObject(theObject);
395 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
397 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
399 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
400 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
401 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
402 if (isSketchOp || isNestedOp) {
403 // in active sketch operation it is possible to activate operation object in selection
404 // in the edit operation, e.g. points of the line can be moved when the line is edited
405 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
406 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
411 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
413 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
416 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
418 myMenuMgr->updateViewerMenu(theStdActions);
421 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
423 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
425 if (anError.isEmpty())
426 anError = sketchMgr()->getFeatureError(theFeature);
428 if (anError.isEmpty()) {
429 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
430 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
432 if (anOpMgr->isValidationLocked()) {
433 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
434 (anOpMgr->currentOperation());
435 if (!aFOperation || theFeature == aFOperation->feature())
436 anError = "Validation is locked by the current operation";
442 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
443 QStringList& theIds) const
445 myMenuMgr->grantedOperationIds(theOperation, theIds);
447 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
448 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
449 XGUI_Workshop* aWorkshop = aConnector->workshop();
451 theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
455 void PartSet_Module::activeSelectionModes(QIntList& theModes)
458 if (mySketchMgr->activeSketch().get())
459 PartSet_SketcherMgr::sketchSelectionModes(theModes);
462 bool PartSet_Module::isMouseOverWindow()
464 return mySketchMgr->isMouseOverWindow();
467 void PartSet_Module::closeDocument()
472 void PartSet_Module::clearViewer()
474 myCustomPrs->clearPrs();
476 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
477 XGUI_Workshop* aWorkshop = aConnector->workshop();
478 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
479 aDisplayer->deactivateSelectionFilters();
482 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
484 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
488 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
489 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
490 // we have to manually activate the sketch label in edit mode
491 aPanel->activateWidget(aPanel->modelWidgets().first());
495 // Restart last operation type
496 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
497 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
498 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
499 // Initialise new line with first point equal to end of previous
500 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
502 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
503 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
504 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
506 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
507 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
508 aWgt->attributeID(), aPoint->x(), aPoint->y());
509 aPanel->activateNextWidget(aPnt2dWgt);
517 void PartSet_Module::onSelectionChanged()
519 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
523 bool isSketcherOp = false;
524 // An edit operation is enable only if the current opeation is the sketch operation
525 if (mySketchMgr->activeSketch()) {
526 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
527 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
530 // Editing of constraints can be done on selection
531 ModuleBase_ISelection* aSelect = myWorkshop->selection();
532 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
533 if (aSelected.size() == 1) {
534 ModuleBase_ViewerPrs aPrs = aSelected.first();
535 ObjectPtr aObject = aPrs.object();
536 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
538 std::string aId = aFeature->getKind();
539 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
540 (aId == SketchPlugin_ConstraintLength::ID()) ||
541 (aId == SketchPlugin_ConstraintDistance::ID()) ||
542 (aId == SketchPlugin_ConstraintAngle::ID())) {
543 editFeature(aFeature);
550 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
552 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
553 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
554 anOpMgr->onKeyReleased(theEvent);
557 void PartSet_Module::onEnterReleased()
559 myRestartingMode = RM_EmptyFeatureUsed;
562 void PartSet_Module::onOperationActivatedByPreselection()
564 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
565 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
566 // Set final definitions if they are necessary
567 //propertyPanelDefined(aOperation);
569 /// Commit sketcher operations automatically
570 anOperation->commit();
574 void PartSet_Module::onNoMoreWidgets()
576 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
578 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
579 if (myRestartingMode != RM_Forbided)
580 myRestartingMode = RM_LastFeatureUsed;
581 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
582 XGUI_Workshop* aWorkshop = aConnector->workshop();
583 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
584 // do nothing if the feature can not be applyed
585 if (anOpMgr->isApplyEnabled())
586 anOperation->commit();
591 void PartSet_Module::onVertexSelected()
593 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
594 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
595 /// If last line finished on vertex the lines creation sequence has to be break
596 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
597 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
598 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
599 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
600 bool aFoundWidget = false;
601 bool aFoundObligatory = false;
602 for (; anIt != aLast && !aFoundObligatory; anIt++) {
604 aFoundWidget = *anIt == anActiveWidget;
606 aFoundObligatory = (*anIt)->isObligatory();
608 if (!aFoundObligatory)
609 myRestartingMode = RM_Forbided;
613 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
614 Config_WidgetAPI* theWidgetApi, std::string theParentId)
616 ModuleBase_IWorkshop* aWorkshop = workshop();
617 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
618 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
619 ModuleBase_ModelWidget* aWgt = NULL;
620 if (theType == "sketch-start-label") {
621 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
622 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
623 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
624 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
625 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
626 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
628 } else if (theType == "sketch-2dpoint_selector") {
629 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
630 theWidgetApi, theParentId);
631 aPointWgt->setSketch(mySketchMgr->activeSketch());
632 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
634 } else if (theType == "sketch-2dpoint_flyout_selector") {
635 PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
636 theWidgetApi, theParentId);
637 aPointWgt->setSketch(mySketchMgr->activeSketch());
638 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
640 } else if (theType == "point2ddistance") {
641 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
642 aWorkshop, theWidgetApi, theParentId);
643 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
645 } else if(theType == "point2dangle") {
646 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
647 aWorkshop, theWidgetApi, theParentId);
648 anAngleWgt->setSketch(mySketchMgr->activeSketch());
650 } else if (theType == "sketch_shape_selector") {
651 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
652 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
653 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
654 aWgt = aShapeSelectorWgt;
655 } else if (theType == "sketch_multi_selector") {
656 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
657 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
658 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
659 aWgt = aShapeSelectorWgt;
660 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
661 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
662 } else if (theType == "export_file_selector") {
663 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
664 } else if (theType == "sketch_launcher") {
665 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
671 bool PartSet_Module::deleteObjects()
673 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
674 XGUI_Workshop* aWorkshop = aConnector->workshop();
675 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
677 //SessionPtr aMgr = ModelAPI_Session::get();
678 // 1. check whether the delete should be processed in the module
679 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
680 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
681 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
682 if (isSketchOp || isNestedOp) {
683 // 2. find selected presentations
684 // selected objects should be collected before the current operation abort because
685 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
686 ModuleBase_ISelection* aSel = workshop()->selection();
687 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
688 // if there are no selected objects in the viewer, that means that the selection in another
689 // place cased this method. It is necessary to return the false value to understande in above
690 // method that delete is not processed
691 if (aSelectedObj.count() == 0)
694 // avoid delete of the objects, which are not belong to the current sketch
695 // in order to do not delete results of other sketches
696 QObjectPtrList aSketchObjects;
697 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
698 for ( ; anIt != aLast; anIt++) {
699 ObjectPtr anObject = *anIt;
700 if (mySketchMgr->isObjectOfSketch(anObject)) {
701 // sketch feature should be used in this list because workshop deletes features only
702 // results are skipped
703 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
704 aSketchObjects.append(aSketchFeature);
707 // if the selection contains only local selected presentations from other sketches,
708 // the Delete operation should not be done at all
709 if (aSketchObjects.size() == 0)
712 // 3. start operation
713 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
714 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
716 // the active nested sketch operation should be aborted unconditionally
717 // the Delete action should be additionally granted for the Sketch operation
718 // in order to do not abort/commit it
719 if (!anOpMgr->canStartOperation(anOpAction->id()))
720 return true; // the objects are processed but can not be deleted
722 anOpMgr->startOperation(anOpAction);
724 // 4. delete features
725 // sketch feature should be skipped, only sub-features can be removed
726 // when sketch operation is active
727 aWorkshop->deleteFeatures(aSketchObjects);
729 anOpMgr->commitOperation();
732 bool isPartRemoved = false;
733 // Delete part with help of PartSet plugin
734 // TODO: the deleted objects has to be processed by multiselection
735 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
736 if (aObjects.size() == 1) {
737 ObjectPtr aObj = aObjects.first();
738 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
739 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
740 // Remove feature should be created in the document of the part results
741 ResultPtr aPartResult = aFeature->firstResult();
742 if (aPartResult.get()) {
743 std::shared_ptr<ModelAPI_ResultPart> aPart =
744 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
745 DocumentPtr aPartDoc = aPart->partDoc();
746 if (aPartDoc.get()) {
747 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
748 (PartSetPlugin_Remove::ID().c_str(), this);
749 if (!anOpMgr->canStartOperation(anOpAction->id()))
750 return true; // the objects are processed but can not be deleted
752 anOpMgr->startOperation(anOpAction);
754 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
757 anOpMgr->commitOperation();
758 isPartRemoved = true;
763 return isPartRemoved;
768 void PartSet_Module::onFeatureTriggered()
770 QAction* aCmd = dynamic_cast<QAction*>(sender());
771 if (aCmd->isCheckable() && aCmd->isChecked()) {
772 // 1. check whether the delete should be processed in the module
773 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
774 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
776 // in case if in the viewer nothing is displayed, the create operation should not be
777 // comitted even if all values of the feature are initialized
778 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
779 // the action information should be saved before the operation is aborted
780 // because this abort leads to update command status, which unchecks this action
781 anOperation->abort();
782 launchOperation(aCmd->data().toString());
786 ModuleBase_IModule::onFeatureTriggered();
789 void PartSet_Module::launchOperation(const QString& theCmdId)
791 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
792 // Show constraints if a constraint was anOperation
793 myHasConstraintShown = mySketchMgr->isConstraintsShown();
794 mySketchMgr->onShowConstraintsToggle(true);
796 ModuleBase_IModule::launchOperation(theCmdId);
800 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
802 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
803 if (!anAIS.IsNull()) {
804 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
805 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
806 if (!aDim.IsNull()) {
807 aCtx->SetZLayer(aDim, myVisualLayerId);
809 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
811 aCtx->SetZLayer(aCons, myVisualLayerId);
816 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
819 // it should be recomputed in order to disappear in the viewer if the corresponded object
821 //if (myCustomPrs->isActive())
822 // myCustomPrs->redisplay(theObject, false);
825 void PartSet_Module::onViewTransformed(int theTrsfType)
827 // Set length of arrows constant in pixel size
828 // if the operation is panning or rotate or panglobal then do nothing
829 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
831 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
832 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
833 if (aContext.IsNull())
836 //Handle(V3d_View) aView = aViewer->activeView();
838 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
839 XGUI_Workshop* aWorkshop = aConnector->workshop();
840 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
841 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
842 Handle(V3d_View) aView;
844 for (aV3dViewer->InitDefinedViews();
845 aV3dViewer->MoreDefinedViews();
846 aV3dViewer->NextDefinedViews()) {
847 Handle(V3d_View) aV = aV3dViewer->DefinedView();
848 double aS = aV->Scale();
856 double aLen = aView->Convert(20);
858 double aPrevLen = SketcherPrs_Tools::getArrowSize();
859 SketcherPrs_Tools::setArrowSize(aLen);
860 const double aPrevScale = aViewer->Scale(aViewer->activeView());
861 const double aCurScale = aViewer->activeView()->Camera()->Scale();
862 aViewer->SetScale(aViewer->activeView(), aCurScale);
863 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
864 bool isModified = false;
865 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
866 foreach (AISObjectPtr aAIS, aPrsList) {
867 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
869 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
870 if (!aDim.IsNull()) {
871 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
872 aContext->Redisplay(aDim, false);
877 aDisplayer->updateViewer();
880 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
882 bool isRedisplayed = false;
883 if (myCustomPrs->isActive())
884 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
886 return isRedisplayed;
889 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
891 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
893 //QLineEdit* aLabel = aOB->activeDocLabel();
894 //QPalette aPalet = aLabel->palette();
895 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
896 //aLabel->setPalette(aPalet);
897 aOB->treeView()->setExpandsOnDoubleClick(false);
898 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
899 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
903 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
906 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
908 /// If last line finished on vertex the lines creation sequence has to be break
909 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
910 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
911 // if there is an active widget, find the presented object in it
913 anActiveWidget = aPanel->preselectionWidget();
915 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
917 if (aWidgetValidated)
918 anObject = aWidgetValidated->findPresentedObject(theAIS);
923 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
925 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
926 int aSelected = aObjects.size();
927 SessionPtr aMgr = ModelAPI_Session::get();
928 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
929 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
931 ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
932 if (aSelected == 1) {
933 bool hasResult = false;
934 bool hasFeature = false;
935 bool hasParameter = false;
936 bool hasSubFeature = false;
937 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
939 ObjectPtr aObject = aObjects.first();
941 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
942 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
943 bool isPart = aPart.get() ||
944 (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
946 DocumentPtr aPartDoc;
948 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
950 if (aPart.get()) // this may be null is Part feature is disabled
951 aPartDoc = aPart->partDoc();
953 theMenu->addAction(aActivatePartAction);
954 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
956 } else if (aObject->document() == aMgr->activeDocument()) {
957 if (hasParameter || hasFeature) {
958 myMenuMgr->action("EDIT_CMD")->setEnabled(true);
959 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
960 if (aCurrentOp && aFeature.get()) {
961 if (aCurrentOp->id().toStdString() == aFeature->getKind())
962 myMenuMgr->action("EDIT_CMD")->setEnabled(false);
967 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
969 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
970 } else { // If feature is 0 the it means that selected root object (document)
971 theMenu->addAction(aActivatePartSetAction);
972 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
974 } else if (aSelected == 0) {
975 // if there is no selection then it means that upper label is selected
976 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
977 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
978 theMenu->addAction(aActivatePartSetAction);
979 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
981 bool aNotDeactivate = (aCurrentOp == 0);
982 if (!aNotDeactivate) {
983 aActivatePartAction->setEnabled(false);
984 aActivatePartSetAction->setEnabled(false);
988 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
990 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
991 // Do not change activation of parts if an operation active
992 static QStringList aAllowActivationList;
993 if (aAllowActivationList.isEmpty())
994 aAllowActivationList <<
995 QString(PartSetPlugin_Part::ID().c_str()) <<
996 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
997 QString(PartSetPlugin_Remove::ID().c_str());
998 if (myWorkshop->currentOperation() &&
999 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
1001 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1002 XGUI_Workshop* aWorkshop = aConnector->workshop();
1003 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1004 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
1005 QPalette aPalet = aLabel->palette();
1007 SessionPtr aMgr = ModelAPI_Session::get();
1008 DocumentPtr aActiveDoc = aMgr->activeDocument();
1009 if (aActivePartIndex.isValid())
1010 aTreeView->setExpanded(aActivePartIndex, false);
1011 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1012 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1013 if (aActivePartIndex.isValid())
1014 aTreeView->setExpanded(aActivePartIndex, true);
1016 aLabel->setPalette(aPalet);
1017 aWorkshop->updateCommandStatus();
1019 // Update displayed objects in order to update active color
1020 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1021 QObjectPtrList aObjects = aDisplayer->displayedObjects();
1023 foreach(ObjectPtr aObj, aObjects) {
1024 //TODO: replace by redisplay event.
1025 aHidden = !aObj->data() || !aObj->data()->isValid() ||
1026 aObj->isDisabled() || (!aObj->isDisplayed());
1028 aDisplayer->redisplay(aObj, false);
1030 aDisplayer->updateViewer();
1034 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1036 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1038 SessionPtr aMgr = ModelAPI_Session::get();
1039 if (!theIndex.isValid()) {
1040 aMgr->setActiveDocument(aMgr->moduleDocument());
1043 if (theIndex.column() != 0) // Use only first column
1046 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1047 XGUI_Workshop* aWorkshop = aConnector->workshop();
1048 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1049 // De not use non editable Indexes
1050 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1052 ObjectPtr aObj = aDataModel->object(theIndex);
1054 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1055 if (!aPart.get()) { // Probably this is Feature
1056 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1057 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1058 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1061 if (aPart.get()) { // if this is a part
1062 if (aPart->partDoc() == aMgr->activeDocument()) {
1063 myMenuMgr->activatePartSet();
1071 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1073 // z layer is created for all started operations in order to visualize operation AIS presentation
1075 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1076 if (aContext.IsNull())
1079 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1080 if (myVisualLayerId == 0) {
1081 if (myVisualLayerId == 0)
1082 aViewer->AddZLayer(myVisualLayerId);
1084 TColStd_SequenceOfInteger aZList;
1085 aViewer->GetAllZLayers(aZList);
1086 bool aFound = false;
1087 for (int i = 1; i <= aZList.Length(); i++) {
1088 if (aZList(i) == myVisualLayerId) {
1094 aViewer->AddZLayer(myVisualLayerId);
1096 // if there is an active operation with validated widget,
1097 // the filters of this widget should be activated in the created view
1098 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1100 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1101 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1102 if (anActiveWidget) {
1103 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1105 if (aWidgetValidated)
1106 aWidgetValidated->activateFilters(true);