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 <ModelAPI_Object.h>
40 #include <ModelAPI_Events.h>
41 #include <ModelAPI_Validator.h>
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_ResultBody.h>
45 #include <ModelAPI_AttributeString.h>
47 #include <GeomDataAPI_Point2D.h>
48 #include <GeomDataAPI_Point.h>
49 #include <GeomDataAPI_Dir.h>
51 #include <XGUI_Displayer.h>
52 #include <XGUI_Workshop.h>
53 #include <XGUI_OperationMgr.h>
54 #include <XGUI_PropertyPanel.h>
55 #include <XGUI_ModuleConnector.h>
56 #include <XGUI_ContextMenuMgr.h>
57 #include <XGUI_Tools.h>
58 #include <XGUI_ObjectsBrowser.h>
59 #include <XGUI_SelectionMgr.h>
60 #include <XGUI_DataModel.h>
61 #include <XGUI_ErrorMgr.h>
63 #include <SketchPlugin_Feature.h>
64 #include <SketchPlugin_Sketch.h>
65 #include <SketchPlugin_Line.h>
66 #include <SketchPlugin_Arc.h>
67 #include <SketchPlugin_Circle.h>
68 #include <SketchPlugin_Point.h>
69 #include <SketchPlugin_ConstraintAngle.h>
70 #include <SketchPlugin_ConstraintLength.h>
71 #include <SketchPlugin_ConstraintDistance.h>
72 #include <SketchPlugin_ConstraintParallel.h>
73 #include <SketchPlugin_ConstraintPerpendicular.h>
74 #include <SketchPlugin_ConstraintRadius.h>
76 #include <SketcherPrs_SymbolPrs.h>
77 #include <SketcherPrs_Tools.h>
79 #include <Events_Loop.h>
80 #include <Config_PropManager.h>
81 #include <Config_Keywords.h>
83 #include <StdSelect_TypeOfFace.hxx>
84 #include <TopoDS_Vertex.hxx>
86 #include <TopoDS_Shape.hxx>
87 #include <BRep_Tool.hxx>
88 #include <AIS_Dimension.hxx>
91 #include <QMouseEvent>
94 #include <QApplication>
95 #include <QMessageBox>
96 #include <QMainWindow>
99 #include <GeomAlgoAPI_FaceBuilder.h>
100 #include <GeomDataAPI_Dir.h>
102 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
108 /*!Create and return new instance of XGUI_Module*/
109 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
111 return new PartSet_Module(theWshop);
114 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
115 : ModuleBase_IModule(theWshop),
116 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
118 new PartSet_IconFactory();
120 mySketchMgr = new PartSet_SketcherMgr(this);
122 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
123 XGUI_Workshop* aWorkshop = aConnector->workshop();
125 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
126 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
127 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
128 this, SLOT(onOperationActivatedByPreselection()));
130 ModuleBase_IViewer* aViewer = theWshop->viewer();
131 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
132 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
133 connect(aViewer, SIGNAL(viewTransformed(int)),
134 SLOT(onViewTransformed(int)));
135 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
136 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
138 myMenuMgr = new PartSet_MenuMgr(this);
139 myCustomPrs = new PartSet_CustomPrs(theWshop);
141 Events_Loop* aLoop = Events_Loop::loop();
142 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
144 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
145 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
148 PartSet_Module::~PartSet_Module()
150 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
151 for (; aIt.More(); aIt.Next()) {
152 Handle(SelectMgr_Filter) aFilter = aIt.Value();
153 if (!aFilter.IsNull())
159 void PartSet_Module::activateSelectionFilters()
161 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
162 for (; aIt.More(); aIt.Next()) {
163 Handle(SelectMgr_Filter) aFilter = aIt.Value();
164 if (!aFilter.IsNull())
165 myWorkshop->viewer()->addSelectionFilter(aFilter);
169 void PartSet_Module::deactivateSelectionFilters()
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()->removeSelectionFilter(aFilter);
179 void PartSet_Module::registerValidators()
181 //Registering of validators
182 SessionPtr aMgr = ModelAPI_Session::get();
183 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
184 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
185 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
186 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
187 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
188 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
189 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
190 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
191 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
192 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
193 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
194 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
195 aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
196 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
197 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
198 aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator);
201 void PartSet_Module::registerFilters()
203 //Registering of selection filters
204 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
205 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
208 void PartSet_Module::registerProperties()
210 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
212 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
213 Config_Prop::Integer, SKETCH_WIDTH);
214 Config_PropManager::registerProp("Sketch planes", "rotate_to_plane", "Rotate to plane when selected",
215 Config_Prop::Boolean, "false");
218 void PartSet_Module::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
220 mySketchMgr->connectToPropertyPanel(theWidget, isToConnect);
223 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
225 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
226 mySketchMgr->commitNestedSketch(theOperation);
229 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
230 if (!aFOperation || aFOperation->isEditOperation())
232 // the selection is cleared after commit the create operation
233 // in order to do not use the same selected objects in the restarted operation
234 // for common behaviour, the selection is cleared even if the operation is not restarted
235 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
236 XGUI_Workshop* aWorkshop = aConnector->workshop();
237 aWorkshop->selector()->clearSelection();
239 /// Restart sketcher operations automatically
240 FeaturePtr aFeature = aFOperation->feature();
241 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
242 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
243 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
244 myRestartingMode == RM_EmptyFeatureUsed)) {
245 myLastOperationId = aFOperation->id();
246 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
247 if (!sketchMgr()->sketchSolverError())
248 launchOperation(myLastOperationId);
250 breakOperationSequence();
253 void PartSet_Module::breakOperationSequence()
255 myLastOperationId = "";
256 myLastFeature = FeaturePtr();
257 myRestartingMode = RM_None;
260 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
262 breakOperationSequence();
265 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
267 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
268 mySketchMgr->startSketch(theOperation);
270 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
271 mySketchMgr->startNestedSketch(theOperation);
274 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
276 myCustomPrs->activate(aFOperation->feature(), true);
279 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
281 ModuleBase_IModule::operationResumed(theOperation);
283 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
285 myCustomPrs->activate(aFOperation->feature(), true);
288 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
290 bool isModified = myCustomPrs->deactivate(false);
292 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
293 mySketchMgr->stopSketch(theOperation);
295 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
296 mySketchMgr->stopNestedSketch(theOperation);
299 //VSV: Viewer is updated on feature update and redisplay
301 // XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
302 // XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
303 // aDisplayer->updateViewer();
305 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
308 ModuleBase_Operation* PartSet_Module::currentOperation() const
310 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
311 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
312 return anOpMgr->currentOperation();
315 bool PartSet_Module::canUndo() const
317 bool aCanUndo = false;
318 SessionPtr aMgr = ModelAPI_Session::get();
319 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
320 aCanUndo = !aMgr->isOperation();
321 if (!aCanUndo) // check the enable state additionally by sketch manager
322 aCanUndo = aMgr->canUndo();
327 bool PartSet_Module::canRedo() const
329 bool aCanRedo = false;
330 SessionPtr aMgr = ModelAPI_Session::get();
331 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
332 aCanRedo = !aMgr->isOperation();
333 if (!aCanRedo) // check the enable state additionally by sketch manager
334 aCanRedo = aMgr->canRedo();
339 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
342 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
343 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
345 // part features are removed in the PartSet module only.
346 if (aFeature->getKind() == PartSetPlugin_Part::ID())
353 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
355 // the sketch manager put the restriction to the objects erase
356 return mySketchMgr->canEraseObject(theObject);
359 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
361 // the sketch manager put the restriction to the objects display
362 return mySketchMgr->canDisplayObject(theObject);
365 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
367 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
369 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
370 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
371 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
372 if (isSketchOp || isNestedOp) {
373 // in active sketch operation it is possible to activate operation object in selection
374 // in the edit operation, e.g. points of the line can be moved when the line is edited
375 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
376 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
381 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
383 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
386 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
388 myMenuMgr->updateViewerMenu(theStdActions);
391 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
393 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
394 if (anError.isEmpty())
395 anError = sketchMgr()->getFeatureError(theFeature);
400 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
401 QStringList& theIds) const
403 myMenuMgr->grantedOperationIds(theOperation, theIds);
405 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
406 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
407 XGUI_Workshop* aWorkshop = aConnector->workshop();
409 theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
413 void PartSet_Module::activeSelectionModes(QIntList& theModes)
416 if (mySketchMgr->activeSketch().get())
417 PartSet_SketcherMgr::sketchSelectionModes(theModes);
420 bool PartSet_Module::isMouseOverWindow()
422 return mySketchMgr->isMouseOverWindow();
425 void PartSet_Module::closeDocument()
430 void PartSet_Module::clearViewer()
432 myCustomPrs->clearPrs();
434 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
435 XGUI_Workshop* aWorkshop = aConnector->workshop();
436 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
437 aDisplayer->deactivateSelectionFilters();
440 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
442 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
446 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
447 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
448 // we have to manually activate the sketch label in edit mode
449 aPanel->activateWidget(aPanel->modelWidgets().first());
453 // Restart last operation type
454 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
455 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
456 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
457 // Initialise new line with first point equal to end of previous
458 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
460 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
461 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
462 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
464 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
465 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
466 aWgt->attributeID(), aPoint->x(), aPoint->y());
467 aPanel->activateNextWidget(aPnt2dWgt);
475 void PartSet_Module::onSelectionChanged()
477 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
481 bool isSketcherOp = false;
482 // An edit operation is enable only if the current opeation is the sketch operation
483 if (mySketchMgr->activeSketch()) {
484 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
485 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
488 // Editing of constraints can be done on selection
489 ModuleBase_ISelection* aSelect = myWorkshop->selection();
490 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
491 if (aSelected.size() == 1) {
492 ModuleBase_ViewerPrs aPrs = aSelected.first();
493 ObjectPtr aObject = aPrs.object();
494 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
496 std::string aId = aFeature->getKind();
497 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
498 (aId == SketchPlugin_ConstraintLength::ID()) ||
499 (aId == SketchPlugin_ConstraintDistance::ID()) ||
500 (aId == SketchPlugin_ConstraintAngle::ID())) {
501 editFeature(aFeature);
508 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
510 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
511 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
512 anOpMgr->onKeyReleased(theEvent);
515 void PartSet_Module::onEnterReleased()
517 myRestartingMode = RM_EmptyFeatureUsed;
520 void PartSet_Module::onOperationActivatedByPreselection()
522 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
523 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
524 // Set final definitions if they are necessary
525 //propertyPanelDefined(aOperation);
527 /// Commit sketcher operations automatically
528 anOperation->commit();
532 void PartSet_Module::onNoMoreWidgets()
534 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
536 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
537 if (myRestartingMode != RM_Forbided)
538 myRestartingMode = RM_LastFeatureUsed;
539 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
540 XGUI_Workshop* aWorkshop = aConnector->workshop();
541 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
542 // do nothing if the feature can not be applyed
543 if (anOpMgr->isApplyEnabled())
544 anOperation->commit();
549 void PartSet_Module::onVertexSelected()
551 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
552 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
553 /// If last line finished on vertex the lines creation sequence has to be break
554 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
555 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
556 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
557 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
558 bool aFoundWidget = false;
559 bool aFoundObligatory = false;
560 for (; anIt != aLast && !aFoundObligatory; anIt++) {
562 aFoundWidget = *anIt == anActiveWidget;
564 aFoundObligatory = (*anIt)->isObligatory();
566 if (!aFoundObligatory)
567 myRestartingMode = RM_Forbided;
571 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
572 Config_WidgetAPI* theWidgetApi, std::string theParentId)
574 ModuleBase_IWorkshop* aWorkshop = workshop();
575 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
576 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
577 ModuleBase_ModelWidget* aWgt = NULL;
578 if (theType == "sketch-start-label") {
579 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
580 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
581 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
582 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
583 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
584 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
586 } else if (theType == "sketch-2dpoint_selector") {
587 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
588 theWidgetApi, theParentId);
589 aPointWgt->setSketch(mySketchMgr->activeSketch());
590 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
592 } else if (theType == "sketch-2dpoint_flyout_selector") {
593 PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
594 theWidgetApi, theParentId);
595 aPointWgt->setSketch(mySketchMgr->activeSketch());
596 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
598 } else if (theType == "point2ddistance") {
599 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
600 aWorkshop, theWidgetApi, theParentId);
601 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
603 } else if(theType == "point2dangle") {
604 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
605 aWorkshop, theWidgetApi, theParentId);
606 anAngleWgt->setSketch(mySketchMgr->activeSketch());
608 } else if (theType == "sketch_shape_selector") {
609 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
610 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
611 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
612 aWgt = aShapeSelectorWgt;
613 } else if (theType == "sketch_multi_selector") {
614 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
615 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
616 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
617 aWgt = aShapeSelectorWgt;
618 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
619 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
620 } else if (theType == "export_file_selector") {
621 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
622 } else if (theType == "sketch_launcher") {
623 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
629 bool PartSet_Module::deleteObjects()
631 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
632 XGUI_Workshop* aWorkshop = aConnector->workshop();
633 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
635 //SessionPtr aMgr = ModelAPI_Session::get();
636 // 1. check whether the delete should be processed in the module
637 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
638 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
639 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
640 if (isSketchOp || isNestedOp) {
641 // 2. find selected presentations
642 // selected objects should be collected before the current operation abort because
643 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
644 ModuleBase_ISelection* aSel = workshop()->selection();
645 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
646 // if there are no selected objects in the viewer, that means that the selection in another
647 // place cased this method. It is necessary to return the false value to understande in above
648 // method that delete is not processed
649 if (aSelectedObj.count() == 0)
652 // avoid delete of the objects, which are not belong to the current sketch
653 // in order to do not delete results of other sketches
654 QObjectPtrList aSketchObjects;
655 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
656 for ( ; anIt != aLast; anIt++) {
657 ObjectPtr anObject = *anIt;
658 if (mySketchMgr->isObjectOfSketch(anObject)) {
659 // sketch feature should be used in this list because workshop deletes features only
660 // results are skipped
661 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
662 aSketchObjects.append(aSketchFeature);
665 // if the selection contains only local selected presentations from other sketches,
666 // the Delete operation should not be done at all
667 if (aSketchObjects.size() == 0)
670 // 3. start operation
671 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
672 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
674 // the active nested sketch operation should be aborted unconditionally
675 // the Delete action should be additionally granted for the Sketch operation
676 // in order to do not abort/commit it
677 if (!anOpMgr->canStartOperation(anOpAction->id()))
678 return true; // the objects are processed but can not be deleted
680 anOpMgr->startOperation(anOpAction);
682 // 4. delete features
683 // sketch feature should be skipped, only sub-features can be removed
684 // when sketch operation is active
685 aWorkshop->deleteFeatures(aSketchObjects);
687 anOpMgr->commitOperation();
690 bool isPartRemoved = false;
691 // Delete part with help of PartSet plugin
692 // TODO: the deleted objects has to be processed by multiselection
693 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
694 if (aObjects.size() == 1) {
695 ObjectPtr aObj = aObjects.first();
696 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
697 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
698 // Remove feature should be created in the document of the part results
699 ResultPtr aPartResult = aFeature->firstResult();
700 if (aPartResult.get()) {
701 std::shared_ptr<ModelAPI_ResultPart> aPart =
702 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
703 DocumentPtr aPartDoc = aPart->partDoc();
704 if (aPartDoc.get()) {
705 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
706 (PartSetPlugin_Remove::ID().c_str(), this);
707 if (!anOpMgr->canStartOperation(anOpAction->id()))
708 return true; // the objects are processed but can not be deleted
710 anOpMgr->startOperation(anOpAction);
712 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
715 anOpMgr->commitOperation();
716 isPartRemoved = true;
721 return isPartRemoved;
726 void PartSet_Module::onFeatureTriggered()
728 QAction* aCmd = dynamic_cast<QAction*>(sender());
729 if (aCmd->isCheckable() && aCmd->isChecked()) {
730 // 1. check whether the delete should be processed in the module
731 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
732 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
734 // in case if in the viewer nothing is displayed, the create operation should not be
735 // comitted even if all values of the feature are initialized
736 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
737 // the action information should be saved before the operation is aborted
738 // because this abort leads to update command status, which unchecks this action
739 anOperation->abort();
740 launchOperation(aCmd->data().toString());
744 ModuleBase_IModule::onFeatureTriggered();
747 void PartSet_Module::launchOperation(const QString& theCmdId)
749 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
750 // Show constraints if a constraint was anOperation
751 myHasConstraintShown = mySketchMgr->isConstraintsShown();
752 mySketchMgr->onShowConstraintsToggle(true);
754 ModuleBase_IModule::launchOperation(theCmdId);
758 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
760 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
761 if (!anAIS.IsNull()) {
762 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
763 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
764 if (!aDim.IsNull()) {
765 aCtx->SetZLayer(aDim, myVisualLayerId);
767 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
769 aCtx->SetZLayer(aCons, myVisualLayerId);
774 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
777 // it should be recomputed in order to disappear in the viewer if the corresponded object
779 //if (myCustomPrs->isActive())
780 // myCustomPrs->redisplay(theObject, false);
783 void PartSet_Module::onViewTransformed(int theTrsfType)
785 // Set length of arrows constant in pixel size
786 // if the operation is panning or rotate or panglobal then do nothing
787 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
789 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
790 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
791 if (aContext.IsNull())
794 //Handle(V3d_View) aView = aViewer->activeView();
796 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
797 XGUI_Workshop* aWorkshop = aConnector->workshop();
798 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
799 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
800 Handle(V3d_View) aView;
802 for (aV3dViewer->InitDefinedViews();
803 aV3dViewer->MoreDefinedViews();
804 aV3dViewer->NextDefinedViews()) {
805 Handle(V3d_View) aV = aV3dViewer->DefinedView();
806 double aS = aV->Scale();
814 double aLen = aView->Convert(20);
816 double aPrevLen = SketcherPrs_Tools::getArrowSize();
817 SketcherPrs_Tools::setArrowSize(aLen);
818 const double aPrevScale = aViewer->Scale(aViewer->activeView());
819 const double aCurScale = aViewer->activeView()->Camera()->Scale();
820 aViewer->SetScale(aViewer->activeView(), aCurScale);
821 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
822 bool isModified = false;
823 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
824 foreach (AISObjectPtr aAIS, aPrsList) {
825 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
827 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
828 if (!aDim.IsNull()) {
829 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
830 aContext->Redisplay(aDim, false);
835 aDisplayer->updateViewer();
838 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
840 bool isRedisplayed = false;
841 if (myCustomPrs->isActive())
842 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
844 return isRedisplayed;
847 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
849 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
851 QLineEdit* aLabel = aOB->activeDocLabel();
852 connect(aLabel, SIGNAL(customContextMenuRequested(const QPoint&)),
853 SLOT(onActiveDocPopup(const QPoint&)));
854 //QPalette aPalet = aLabel->palette();
855 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
856 //aLabel->setPalette(aPalet);
857 aOB->treeView()->setExpandsOnDoubleClick(false);
858 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
859 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
863 void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
865 SessionPtr aMgr = ModelAPI_Session::get();
866 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
868 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
869 XGUI_Workshop* aWorkshop = aConnector->workshop();
870 QLineEdit* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
872 aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
875 aMenu.addAction(aActivatePartAction);
876 aMenu.exec(aHeader->mapToGlobal(thePnt));
880 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
883 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
885 /// If last line finished on vertex the lines creation sequence has to be break
886 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
887 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
888 // if there is an active widget, find the presented object in it
890 anActiveWidget = aPanel->preselectionWidget();
892 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
894 if (aWidgetValidated)
895 anObject = aWidgetValidated->findPresentedObject(theAIS);
900 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
902 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
903 int aSelected = aObjects.size();
904 SessionPtr aMgr = ModelAPI_Session::get();
905 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
907 ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
908 if (aSelected == 1) {
909 bool hasResult = false;
910 bool hasFeature = false;
911 bool hasParameter = false;
912 bool hasSubFeature = false;
913 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
915 ObjectPtr aObject = aObjects.first();
917 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
918 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
919 bool isPart = aPart.get() ||
920 (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
922 DocumentPtr aPartDoc;
924 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
926 if (aPart.get()) // this may be null is Part feature is disabled
927 aPartDoc = aPart->partDoc();
929 theMenu->addAction(aActivatePartAction);
930 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
932 } else if (aObject->document() == aMgr->activeDocument()) {
933 if (hasParameter || hasFeature) {
934 myMenuMgr->action("EDIT_CMD")->setEnabled(true);
935 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
936 if (aCurrentOp && aFeature.get()) {
937 if (aCurrentOp->id().toStdString() == aFeature->getKind())
938 myMenuMgr->action("EDIT_CMD")->setEnabled(false);
943 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
945 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
948 bool aNotDeactivate = (aCurrentOp == 0);
949 if (!aNotDeactivate) {
950 aActivatePartAction->setEnabled(false);
954 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
956 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
957 // Do not change activation of parts if an operation active
958 static QStringList aAllowActivationList;
959 if (aAllowActivationList.isEmpty())
960 aAllowActivationList <<
961 QString(PartSetPlugin_Part::ID().c_str()) <<
962 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
963 QString(PartSetPlugin_Remove::ID().c_str());
964 if (myWorkshop->currentOperation() &&
965 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
967 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
968 XGUI_Workshop* aWorkshop = aConnector->workshop();
969 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
970 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
971 QPalette aPalet = aLabel->palette();
973 SessionPtr aMgr = ModelAPI_Session::get();
974 DocumentPtr aActiveDoc = aMgr->activeDocument();
975 if (aActivePartIndex.isValid())
976 aTreeView->setExpanded(aActivePartIndex, false);
977 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
978 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
979 if (aActivePartIndex.isValid())
980 aTreeView->setExpanded(aActivePartIndex, true);
982 aLabel->setPalette(aPalet);
983 aWorkshop->updateCommandStatus();
985 // Update displayed objects in order to update active color
986 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
987 QObjectPtrList aObjects = aDisplayer->displayedObjects();
989 foreach(ObjectPtr aObj, aObjects) {
990 //TODO: replace by redisplay event.
991 aHidden = !aObj->data() || !aObj->data()->isValid() ||
992 aObj->isDisabled() || (!aObj->isDisplayed());
994 aDisplayer->redisplay(aObj, false);
996 aDisplayer->updateViewer();
1000 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1002 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1004 SessionPtr aMgr = ModelAPI_Session::get();
1005 if (!theIndex.isValid()) {
1006 aMgr->setActiveDocument(aMgr->moduleDocument());
1009 if (theIndex.column() != 0) // Use only first column
1012 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1013 XGUI_Workshop* aWorkshop = aConnector->workshop();
1014 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1015 // De not use non editable Indexes
1016 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1018 ObjectPtr aObj = aDataModel->object(theIndex);
1020 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1021 if (!aPart.get()) { // Probably this is Feature
1022 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1023 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1024 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1027 if (aPart.get()) { // if this is a part
1028 if (aPart->partDoc() == aMgr->activeDocument()) {
1029 myMenuMgr->activatePartSet();
1037 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1039 // z layer is created for all started operations in order to visualize operation AIS presentation
1041 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1042 if (aContext.IsNull())
1045 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1046 if (myVisualLayerId == 0) {
1047 if (myVisualLayerId == 0)
1048 aViewer->AddZLayer(myVisualLayerId);
1050 TColStd_SequenceOfInteger aZList;
1051 aViewer->GetAllZLayers(aZList);
1052 bool aFound = false;
1053 for (int i = 1; i <= aZList.Length(); i++) {
1054 if (aZList(i) == myVisualLayerId) {
1060 aViewer->AddZLayer(myVisualLayerId);
1062 // if there is an active operation with validated widget,
1063 // the filters of this widget should be activated in the created view
1064 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1066 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1067 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1068 if (anActiveWidget) {
1069 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1071 if (aWidgetValidated)
1072 aWidgetValidated->activateFilters(true);
1077 //******************************************************
1078 void PartSet_Module::widgetStateChanged(int thePreviousState)
1080 mySketchMgr->widgetStateChanged(thePreviousState);