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 <ModelAPI_Object.h>
39 #include <ModelAPI_Events.h>
40 #include <ModelAPI_Validator.h>
41 #include <ModelAPI_Data.h>
42 #include <ModelAPI_Session.h>
43 #include <ModelAPI_ResultBody.h>
44 #include <ModelAPI_AttributeString.h>
46 #include <GeomDataAPI_Point2D.h>
47 #include <GeomDataAPI_Point.h>
48 #include <GeomDataAPI_Dir.h>
50 #include <XGUI_Displayer.h>
51 #include <XGUI_Workshop.h>
52 #include <XGUI_OperationMgr.h>
53 #include <XGUI_PropertyPanel.h>
54 #include <XGUI_ModuleConnector.h>
55 #include <XGUI_ContextMenuMgr.h>
56 #include <XGUI_Tools.h>
57 #include <XGUI_ObjectsBrowser.h>
58 #include <XGUI_SelectionMgr.h>
59 #include <XGUI_DataModel.h>
60 #include <XGUI_ErrorMgr.h>
62 #include <SketchPlugin_Feature.h>
63 #include <SketchPlugin_Sketch.h>
64 #include <SketchPlugin_Line.h>
65 #include <SketchPlugin_Arc.h>
66 #include <SketchPlugin_Circle.h>
67 #include <SketchPlugin_Point.h>
68 #include <SketchPlugin_ConstraintAngle.h>
69 #include <SketchPlugin_ConstraintLength.h>
70 #include <SketchPlugin_ConstraintDistance.h>
71 #include <SketchPlugin_ConstraintParallel.h>
72 #include <SketchPlugin_ConstraintPerpendicular.h>
73 #include <SketchPlugin_ConstraintRadius.h>
75 #include <SketcherPrs_SymbolPrs.h>
76 #include <SketcherPrs_Tools.h>
78 #include <Events_Loop.h>
79 #include <Config_PropManager.h>
80 #include <Config_Keywords.h>
82 #include <StdSelect_TypeOfFace.hxx>
83 #include <TopoDS_Vertex.hxx>
85 #include <TopoDS_Shape.hxx>
86 #include <BRep_Tool.hxx>
87 #include <AIS_Dimension.hxx>
90 #include <QMouseEvent>
93 #include <QApplication>
94 #include <QMessageBox>
95 #include <QMainWindow>
98 #include <GeomAlgoAPI_FaceBuilder.h>
99 #include <GeomDataAPI_Dir.h>
101 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
107 /*!Create and return new instance of XGUI_Module*/
108 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
110 return new PartSet_Module(theWshop);
113 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
114 : ModuleBase_IModule(theWshop),
115 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
117 new PartSet_IconFactory();
119 mySketchMgr = new PartSet_SketcherMgr(this);
120 #ifdef ModuleDataModel
121 myDataModel = new PartSet_DocumentDataModel(this);
124 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
125 XGUI_Workshop* aWorkshop = aConnector->workshop();
127 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
128 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
129 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
130 this, SLOT(onOperationActivatedByPreselection()));
132 ModuleBase_IViewer* aViewer = theWshop->viewer();
133 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
134 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
135 connect(aViewer, SIGNAL(viewTransformed(int)),
136 SLOT(onViewTransformed(int)));
137 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
138 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
140 myMenuMgr = new PartSet_MenuMgr(this);
141 myCustomPrs = new PartSet_CustomPrs(theWshop);
143 Events_Loop* aLoop = Events_Loop::loop();
144 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
146 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
147 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
150 PartSet_Module::~PartSet_Module()
152 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
153 for (; aIt.More(); aIt.Next()) {
154 Handle(SelectMgr_Filter) aFilter = aIt.Value();
155 if (!aFilter.IsNull())
161 void PartSet_Module::activateSelectionFilters()
163 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
164 for (; aIt.More(); aIt.Next()) {
165 Handle(SelectMgr_Filter) aFilter = aIt.Value();
166 if (!aFilter.IsNull())
167 myWorkshop->viewer()->addSelectionFilter(aFilter);
171 void PartSet_Module::deactivateSelectionFilters()
173 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
174 for (; aIt.More(); aIt.Next()) {
175 Handle(SelectMgr_Filter) aFilter = aIt.Value();
176 if (!aFilter.IsNull())
177 myWorkshop->viewer()->removeSelectionFilter(aFilter);
181 void PartSet_Module::registerValidators()
183 //Registering of validators
184 SessionPtr aMgr = ModelAPI_Session::get();
185 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
186 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
187 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
188 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
189 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
190 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
191 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
192 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
193 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
194 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
195 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
196 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
197 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
198 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
199 aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator);
202 void PartSet_Module::registerFilters()
204 //Registering of selection filters
205 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
206 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
209 void PartSet_Module::registerProperties()
211 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
213 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
214 Config_Prop::Integer, SKETCH_WIDTH);
217 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
219 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
220 mySketchMgr->commitNestedSketch(theOperation);
223 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
224 if (!aFOperation || aFOperation->isEditOperation())
226 // the selection is cleared after commit the create operation
227 // in order to do not use the same selected objects in the restarted operation
228 // for common behaviour, the selection is cleared even if the operation is not restarted
229 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
230 XGUI_Workshop* aWorkshop = aConnector->workshop();
231 aWorkshop->selector()->clearSelection();
233 /// Restart sketcher operations automatically
234 FeaturePtr aFeature = aFOperation->feature();
235 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
236 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
237 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
238 myRestartingMode == RM_EmptyFeatureUsed)) {
239 myLastOperationId = aFOperation->id();
240 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
241 if (!sketchMgr()->sketchSolverError())
242 launchOperation(myLastOperationId);
244 breakOperationSequence();
247 void PartSet_Module::breakOperationSequence()
249 myLastOperationId = "";
250 myLastFeature = FeaturePtr();
251 myRestartingMode = RM_None;
254 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
256 breakOperationSequence();
259 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
261 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
262 mySketchMgr->startSketch(theOperation);
264 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
265 mySketchMgr->startNestedSketch(theOperation);
268 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
270 myCustomPrs->activate(aFOperation->feature(), true);
273 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
275 ModuleBase_IModule::onOperationResumed(theOperation);
277 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
279 myCustomPrs->activate(aFOperation->feature(), true);
282 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
284 bool isModified = myCustomPrs->deactivate(false);
286 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
287 mySketchMgr->stopSketch(theOperation);
289 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
290 mySketchMgr->stopNestedSketch(theOperation);
294 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
295 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
296 aDisplayer->updateViewer();
298 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
301 ModuleBase_Operation* PartSet_Module::currentOperation() const
303 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
304 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
305 return anOpMgr->currentOperation();
308 bool PartSet_Module::canUndo() const
310 bool aCanUndo = false;
311 SessionPtr aMgr = ModelAPI_Session::get();
312 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
313 aCanUndo = !aMgr->isOperation();
314 if (!aCanUndo) // check the enable state additionally by sketch manager
315 aCanUndo = aMgr->canUndo();
320 bool PartSet_Module::canRedo() const
322 bool aCanRedo = false;
323 SessionPtr aMgr = ModelAPI_Session::get();
324 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
325 aCanRedo = !aMgr->isOperation();
326 if (!aCanRedo) // check the enable state additionally by sketch manager
327 aCanRedo = aMgr->canRedo();
332 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
335 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
336 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
338 // part features are removed in the PartSet module only.
339 if (aFeature->getKind() == PartSetPlugin_Part::ID())
346 bool PartSet_Module::canCommitOperation() const
348 return mySketchMgr->canCommitOperation();
351 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
353 // the sketch manager put the restriction to the objects erase
354 return mySketchMgr->canEraseObject(theObject);
357 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
359 // the sketch manager put the restriction to the objects display
360 return mySketchMgr->canDisplayObject(theObject);
363 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
365 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
367 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
368 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
369 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
370 if (isSketchOp || isNestedOp) {
371 // in active sketch operation it is possible to activate operation object in selection
372 // in the edit operation, e.g. points of the line can be moved when the line is edited
373 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
374 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
379 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
381 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
384 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
386 myMenuMgr->updateViewerMenu(theStdActions);
389 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
391 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
393 if (anError.isEmpty())
394 anError = sketchMgr()->getFeatureError(theFeature);
396 if (anError.isEmpty()) {
397 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
398 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
400 if (anOpMgr->isValidationLocked()) {
401 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
402 (anOpMgr->currentOperation());
403 if (!aFOperation || theFeature == aFOperation->feature())
404 anError = "Validation is locked by the current operation";
410 void PartSet_Module::activeSelectionModes(QIntList& theModes)
413 if (mySketchMgr->activeSketch().get())
414 PartSet_SketcherMgr::sketchSelectionModes(theModes);
417 bool PartSet_Module::isMouseOverWindow()
419 return mySketchMgr->isMouseOverWindow();
422 void PartSet_Module::closeDocument()
427 void PartSet_Module::clearViewer()
429 myCustomPrs->clearPrs();
431 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
432 XGUI_Workshop* aWorkshop = aConnector->workshop();
433 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
434 aDisplayer->deactivateSelectionFilters();
437 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
439 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
443 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
444 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
445 // we have to manually activate the sketch label in edit mode
446 aPanel->activateWidget(aPanel->modelWidgets().first());
450 // Restart last operation type
451 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
452 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
453 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
454 // Initialise new line with first point equal to end of previous
455 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
457 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
458 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
459 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
461 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
462 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
463 aWgt->attributeID(), aPoint->x(), aPoint->y());
464 aPanel->activateNextWidget(aPnt2dWgt);
472 void PartSet_Module::onSelectionChanged()
474 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
478 bool isSketcherOp = false;
479 // An edit operation is enable only if the current opeation is the sketch operation
480 if (mySketchMgr->activeSketch()) {
481 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
482 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
485 // Editing of constraints can be done on selection
486 ModuleBase_ISelection* aSelect = myWorkshop->selection();
487 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
488 if (aSelected.size() == 1) {
489 ModuleBase_ViewerPrs aPrs = aSelected.first();
490 ObjectPtr aObject = aPrs.object();
491 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
493 std::string aId = aFeature->getKind();
494 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
495 (aId == SketchPlugin_ConstraintLength::ID()) ||
496 (aId == SketchPlugin_ConstraintDistance::ID()) ||
497 (aId == SketchPlugin_ConstraintAngle::ID())) {
498 editFeature(aFeature);
505 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
507 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
508 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
509 anOpMgr->onKeyReleased(theEvent);
512 void PartSet_Module::onEnterReleased()
514 myRestartingMode = RM_EmptyFeatureUsed;
517 void PartSet_Module::onOperationActivatedByPreselection()
519 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
520 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
521 // Set final definitions if they are necessary
522 //propertyPanelDefined(aOperation);
524 /// Commit sketcher operations automatically
525 anOperation->commit();
529 void PartSet_Module::onNoMoreWidgets()
531 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
533 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
534 if (myRestartingMode != RM_Forbided)
535 myRestartingMode = RM_LastFeatureUsed;
536 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
537 XGUI_Workshop* aWorkshop = aConnector->workshop();
538 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
539 // do nothing if the feature can not be applyed
540 if (anOpMgr->isApplyEnabled())
541 anOperation->commit();
546 void PartSet_Module::onVertexSelected()
548 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
549 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
550 /// If last line finished on vertex the lines creation sequence has to be break
551 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
552 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
553 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
554 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
555 bool aFoundWidget = false;
556 bool aFoundObligatory = false;
557 for (; anIt != aLast && !aFoundObligatory; anIt++) {
559 aFoundWidget = *anIt == anActiveWidget;
561 aFoundObligatory = (*anIt)->isObligatory();
563 if (!aFoundObligatory)
564 myRestartingMode = RM_Forbided;
568 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
569 Config_WidgetAPI* theWidgetApi, std::string theParentId)
571 ModuleBase_IWorkshop* aWorkshop = workshop();
572 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
573 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
574 ModuleBase_ModelWidget* aWgt = NULL;
575 if (theType == "sketch-start-label") {
576 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
577 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
578 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
579 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
580 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
581 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
583 } else if (theType == "sketch-2dpoint_selector") {
584 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
585 theWidgetApi, theParentId);
586 aPointWgt->setSketch(mySketchMgr->activeSketch());
587 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
589 } else if (theType == "point2ddistance") {
590 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
591 aWorkshop, theWidgetApi, theParentId);
592 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
594 } else if(theType == "point2dangle") {
595 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
596 aWorkshop, theWidgetApi, theParentId);
597 anAngleWgt->setSketch(mySketchMgr->activeSketch());
599 } else if (theType == "sketch_shape_selector") {
600 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
601 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
602 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
603 aWgt = aShapeSelectorWgt;
604 } else if (theType == "sketch_multi_selector") {
605 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
606 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
607 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
608 aWgt = aShapeSelectorWgt;
609 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
610 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
611 } else if (theType == "export_file_selector") {
612 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
613 } else if (theType == "sketch_launcher") {
614 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
620 bool PartSet_Module::deleteObjects()
622 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
623 XGUI_Workshop* aWorkshop = aConnector->workshop();
624 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
626 //SessionPtr aMgr = ModelAPI_Session::get();
627 // 1. check whether the delete should be processed in the module
628 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
629 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
630 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
631 if (isSketchOp || isNestedOp) {
632 // 2. find selected presentations
633 // selected objects should be collected before the current operation abort because
634 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
635 ModuleBase_ISelection* aSel = workshop()->selection();
636 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
637 // if there are no selected objects in the viewer, that means that the selection in another
638 // place cased this method. It is necessary to return the false value to understande in above
639 // method that delete is not processed
640 if (aSelectedObj.count() == 0)
643 // avoid delete of the objects, which are not belong to the current sketch
644 // in order to do not delete results of other sketches
645 QObjectPtrList aSketchObjects;
646 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
647 for ( ; anIt != aLast; anIt++) {
648 ObjectPtr anObject = *anIt;
649 if (mySketchMgr->isObjectOfSketch(anObject)) {
650 // sketch feature should be used in this list because workshop deletes features only
651 // results are skipped
652 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
653 aSketchObjects.append(aSketchFeature);
656 // if the selection contains only local selected presentations from other sketches,
657 // the Delete operation should not be done at all
658 if (aSketchObjects.size() == 0)
661 // 3. start operation
662 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
663 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
665 // the active nested sketch operation should be aborted unconditionally
666 // the Delete action should be additionally granted for the Sketch operation
667 // in order to do not abort/commit it
668 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
669 return true; // the objects are processed but can not be deleted
671 anOpMgr->startOperation(anOpAction);
673 // 4. delete features
674 // sketch feature should be skipped, only sub-features can be removed
675 // when sketch operation is active
676 aWorkshop->deleteFeatures(aSketchObjects);
678 anOpMgr->commitOperation();
681 bool isPartRemoved = false;
682 // Delete part with help of PartSet plugin
683 // TODO: the deleted objects has to be processed by multiselection
684 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
685 if (aObjects.size() == 1) {
686 ObjectPtr aObj = aObjects.first();
687 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
688 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
689 // Remove feature should be created in the document of the part results
690 ResultPtr aPartResult = aFeature->firstResult();
691 if (aPartResult.get()) {
692 std::shared_ptr<ModelAPI_ResultPart> aPart =
693 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
694 DocumentPtr aPartDoc = aPart->partDoc();
695 if (aPartDoc.get()) {
696 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
697 (PartSetPlugin_Remove::ID().c_str(), this);
698 if (!anOpMgr->canStartOperation(anOpAction->id()))
699 return true; // the objects are processed but can not be deleted
701 anOpMgr->startOperation(anOpAction);
703 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
706 anOpMgr->commitOperation();
707 isPartRemoved = true;
712 return isPartRemoved;
717 void PartSet_Module::onFeatureTriggered()
719 QAction* aCmd = dynamic_cast<QAction*>(sender());
720 if (aCmd->isCheckable() && aCmd->isChecked()) {
721 // 1. check whether the delete should be processed in the module
722 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
723 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
725 // in case if in the viewer nothing is displayed, the create operation should not be
726 // comitted even if all values of the feature are initialized
727 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
728 // the action information should be saved before the operation is aborted
729 // because this abort leads to update command status, which unchecks this action
730 anOperation->abort();
731 launchOperation(aCmd->data().toString());
735 ModuleBase_IModule::onFeatureTriggered();
738 void PartSet_Module::launchOperation(const QString& theCmdId)
740 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
741 // Show constraints if a constraint was anOperation
742 myHasConstraintShown = mySketchMgr->isConstraintsShown();
743 mySketchMgr->onShowConstraintsToggle(true);
745 ModuleBase_IModule::launchOperation(theCmdId);
749 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
751 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
752 if (!anAIS.IsNull()) {
753 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
754 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
755 if (!aDim.IsNull()) {
756 aCtx->SetZLayer(aDim, myVisualLayerId);
758 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
760 aCtx->SetZLayer(aCons, myVisualLayerId);
765 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
768 // it should be recomputed in order to disappear in the viewer if the corresponded object
770 //if (myCustomPrs->isActive())
771 // myCustomPrs->redisplay(theObject, false);
774 void PartSet_Module::onViewTransformed(int theTrsfType)
776 // Set length of arrows constant in pixel size
777 // if the operation is panning or rotate or panglobal then do nothing
778 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
780 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
781 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
782 if (aContext.IsNull())
785 //Handle(V3d_View) aView = aViewer->activeView();
787 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
788 XGUI_Workshop* aWorkshop = aConnector->workshop();
789 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
790 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
791 Handle(V3d_View) aView;
793 for (aV3dViewer->InitDefinedViews();
794 aV3dViewer->MoreDefinedViews();
795 aV3dViewer->NextDefinedViews()) {
796 Handle(V3d_View) aV = aV3dViewer->DefinedView();
797 double aS = aV->Scale();
805 double aLen = aView->Convert(20);
807 double aPrevLen = SketcherPrs_Tools::getArrowSize();
808 SketcherPrs_Tools::setArrowSize(aLen);
809 const double aPrevScale = aViewer->Scale(aViewer->activeView());
810 const double aCurScale = aViewer->activeView()->Camera()->Scale();
811 aViewer->SetScale(aViewer->activeView(), aCurScale);
812 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
813 bool isModified = false;
814 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
815 foreach (AISObjectPtr aAIS, aPrsList) {
816 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
818 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
819 if (!aDim.IsNull()) {
820 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
821 aContext->Redisplay(aDim, false);
826 aDisplayer->updateViewer();
829 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
831 bool isRedisplayed = false;
832 if (myCustomPrs->isActive())
833 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
835 return isRedisplayed;
838 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
840 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
842 //QLineEdit* aLabel = aOB->activeDocLabel();
843 //QPalette aPalet = aLabel->palette();
844 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
845 //aLabel->setPalette(aPalet);
846 aOB->treeView()->setExpandsOnDoubleClick(false);
847 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
848 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
849 #ifdef ModuleDataModel
850 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
851 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
852 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
853 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
858 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
861 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
863 /// If last line finished on vertex the lines creation sequence has to be break
864 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
865 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
866 // if there is an active widget, find the presented object in it
868 anActiveWidget = aPanel->preselectionWidget();
870 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
872 if (aWidgetValidated)
873 anObject = aWidgetValidated->findPresentedObject(theAIS);
878 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
880 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
881 int aSelected = aObjects.size();
882 SessionPtr aMgr = ModelAPI_Session::get();
883 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
884 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
885 if (aSelected == 1) {
886 bool hasResult = false;
887 bool hasFeature = false;
888 bool hasParameter = false;
889 bool hasSubFeature = false;
890 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
892 ObjectPtr aObject = aObjects.first();
894 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
895 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
896 bool isPart = aPart.get() ||
897 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
899 DocumentPtr aPartDoc;
901 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
903 if (aPart.get()) // this may be null is Part feature is disabled
904 aPartDoc = aPart->partDoc();
906 theMenu->addAction(aActivatePartAction);
907 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
909 } else if (aObject->document() == aMgr->activeDocument()) {
910 if (hasParameter || hasFeature)
911 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
914 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
916 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
917 } else { // If feature is 0 the it means that selected root object (document)
918 theMenu->addAction(aActivatePartSetAction);
919 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
921 } else if (aSelected == 0) {
922 // if there is no selection then it means that upper label is selected
923 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
924 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
925 theMenu->addAction(aActivatePartSetAction);
926 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
928 bool aNotDeactivate = (myWorkshop->currentOperation() == 0);
929 if (!aNotDeactivate) {
930 aActivatePartAction->setEnabled(false);
931 aActivatePartSetAction->setEnabled(false);
935 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
937 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
938 // Do not change activation of parts if an operation active
939 static QStringList aAllowActivationList;
940 if (aAllowActivationList.isEmpty())
941 aAllowActivationList <<
942 QString(PartSetPlugin_Part::ID().c_str()) <<
943 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
944 QString(PartSetPlugin_Remove::ID().c_str());
945 if (myWorkshop->currentOperation() &&
946 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
948 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
949 XGUI_Workshop* aWorkshop = aConnector->workshop();
950 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
951 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
952 QPalette aPalet = aLabel->palette();
954 SessionPtr aMgr = ModelAPI_Session::get();
955 DocumentPtr aActiveDoc = aMgr->activeDocument();
956 #ifdef ModuleDataModel
957 QModelIndex aOldIndex = myDataModel->activePartTree();
958 DocumentPtr aDoc = aMgr->moduleDocument();
959 if (aActiveDoc == aDoc) {
960 if (aOldIndex.isValid())
961 aTreeView->setExpanded(aOldIndex, false);
962 myDataModel->deactivatePart();
963 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
965 std::string aGrpName = ModelAPI_ResultPart::group();
966 for (int i = 0; i < aDoc->size(aGrpName); i++) {
967 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
968 if (aPart->partDoc() == aActiveDoc) {
969 QModelIndex aIndex = myDataModel->partIndex(aPart);
970 if (myDataModel->activatePart(aIndex)) {
971 if (aOldIndex.isValid())
972 aTreeView->setExpanded(aOldIndex, false);
973 aTreeView->setExpanded(myDataModel->activePartTree(), true);
974 aPalet.setColor(QPalette::Text, Qt::black);
981 if (aActivePartIndex.isValid())
982 aTreeView->setExpanded(aActivePartIndex, false);
983 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
984 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
985 if (aActivePartIndex.isValid())
986 aTreeView->setExpanded(aActivePartIndex, true);
988 aLabel->setPalette(aPalet);
989 aWorkshop->updateCommandStatus();
991 // Update displayed objects in order to update active color
992 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
993 QObjectPtrList aObjects = aDisplayer->displayedObjects();
995 foreach(ObjectPtr aObj, aObjects) {
996 //TODO: replace by redisplay event.
997 aHidden = !aObj->data() || !aObj->data()->isValid() ||
998 aObj->isDisabled() || (!aObj->isDisplayed());
1000 aDisplayer->redisplay(aObj, false);
1002 aDisplayer->updateViewer();
1006 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1008 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1010 SessionPtr aMgr = ModelAPI_Session::get();
1011 if (!theIndex.isValid()) {
1012 aMgr->setActiveDocument(aMgr->moduleDocument());
1015 if (theIndex.column() != 0) // Use only first column
1017 #ifdef ModuleDataModel
1018 ObjectPtr aObj = myDataModel->object(theIndex);
1020 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1021 XGUI_Workshop* aWorkshop = aConnector->workshop();
1022 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1023 // De not use non editable Indexes
1024 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1026 ObjectPtr aObj = aDataModel->object(theIndex);
1029 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1030 if (!aPart.get()) { // Probably this is Feature
1031 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1032 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1033 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1036 if (aPart.get()) { // if this is a part
1037 if (aPart->partDoc() == aMgr->activeDocument()) {
1038 myMenuMgr->activatePartSet();
1046 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1048 // z layer is created for all started operations in order to visualize operation AIS presentation
1050 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1051 if (aContext.IsNull())
1054 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1055 if (myVisualLayerId == 0) {
1056 if (myVisualLayerId == 0)
1057 aViewer->AddZLayer(myVisualLayerId);
1059 TColStd_SequenceOfInteger aZList;
1060 aViewer->GetAllZLayers(aZList);
1061 bool aFound = false;
1062 for (int i = 1; i <= aZList.Length(); i++) {
1063 if (aZList(i) == myVisualLayerId) {
1069 aViewer->AddZLayer(myVisualLayerId);
1071 // if there is an active operation with validated widget,
1072 // the filters of this widget should be activated in the created view
1073 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1075 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1076 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1077 if (anActiveWidget) {
1078 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1080 if (aWidgetValidated)
1081 aWidgetValidated->activateFilters(true);