1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_Module.h"
4 #include "PartSet_WidgetSketchLabel.h"
5 #include "PartSet_Validators.h"
6 #include "PartSet_Tools.h"
7 #include "PartSet_WidgetPoint2d.h"
8 #include "PartSet_WidgetPoint2dDistance.h"
9 #include "PartSet_WidgetShapeSelector.h"
10 #include "PartSet_WidgetPoint2dAngle.h"
11 #include "PartSet_WidgetMultiSelector.h"
12 #include "PartSet_WidgetEditor.h"
13 #include "PartSet_WidgetFileSelector.h"
14 #include "PartSet_WidgetSketchCreator.h"
15 #include "PartSet_SketcherMgr.h"
16 #include "PartSet_MenuMgr.h"
17 #include "PartSet_CustomPrs.h"
18 #include "PartSet_IconFactory.h"
20 #include "PartSet_Filters.h"
21 #include "PartSet_FilterInfinite.h"
23 #include <PartSetPlugin_Remove.h>
24 #include <PartSetPlugin_Part.h>
25 #include <PartSetPlugin_Duplicate.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_OperationAction.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_WidgetEditor.h>
33 #include <ModuleBase_WidgetValidated.h>
34 #include <ModuleBase_FilterFactory.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_OperationFeature.h>
38 #include <GeomValidators_ShapeType.h>
39 #include <GeomValidators_Finite.h>
40 #include <GeomValidators_Face.h>
41 #include <GeomValidators_ConstructionComposite.h>
42 #include <GeomValidators_ZeroOffset.h>
43 #include <GeomValidators_BooleanArguments.h>
44 #include <GeomValidators_Different.h>
45 #include <GeomValidators_PartitionArguments.h>
48 #include <ModelAPI_Object.h>
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_Validator.h>
51 #include <ModelAPI_Data.h>
52 #include <ModelAPI_Session.h>
53 #include <GeomValidators_DifferentShapes.h>
54 #include <ModelAPI_ResultBody.h>
55 #include <ModelAPI_AttributeString.h>
57 #include <GeomDataAPI_Point2D.h>
58 #include <GeomDataAPI_Point.h>
59 #include <GeomDataAPI_Dir.h>
61 #include <XGUI_Displayer.h>
62 #include <XGUI_Workshop.h>
63 #include <XGUI_OperationMgr.h>
64 #include <XGUI_PropertyPanel.h>
65 #include <XGUI_ModuleConnector.h>
66 #include <XGUI_ContextMenuMgr.h>
67 #include <XGUI_Tools.h>
68 #include <XGUI_ObjectsBrowser.h>
69 #include <XGUI_SelectionMgr.h>
70 #include <XGUI_DataModel.h>
71 #include <XGUI_ErrorMgr.h>
73 #include <SketchPlugin_Feature.h>
74 #include <SketchPlugin_Sketch.h>
75 #include <SketchPlugin_Line.h>
76 #include <SketchPlugin_Arc.h>
77 #include <SketchPlugin_Circle.h>
78 #include <SketchPlugin_Point.h>
79 #include <SketchPlugin_ConstraintAngle.h>
80 #include <SketchPlugin_ConstraintLength.h>
81 #include <SketchPlugin_ConstraintDistance.h>
82 #include <SketchPlugin_ConstraintParallel.h>
83 #include <SketchPlugin_ConstraintPerpendicular.h>
84 #include <SketchPlugin_ConstraintRadius.h>
86 #include <SketcherPrs_SymbolPrs.h>
87 #include <SketcherPrs_Tools.h>
89 #include <Events_Loop.h>
90 #include <Config_PropManager.h>
91 #include <Config_Keywords.h>
93 #include <StdSelect_TypeOfFace.hxx>
94 #include <TopoDS_Vertex.hxx>
96 #include <TopoDS_Shape.hxx>
97 #include <BRep_Tool.hxx>
98 #include <AIS_Dimension.hxx>
101 #include <QMouseEvent>
104 #include <QApplication>
105 #include <QMessageBox>
106 #include <QMainWindow>
109 #include <GeomAlgoAPI_FaceBuilder.h>
110 #include <GeomDataAPI_Dir.h>
112 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
118 /*!Create and return new instance of XGUI_Module*/
119 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
121 return new PartSet_Module(theWshop);
124 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
125 : ModuleBase_IModule(theWshop),
126 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
128 new PartSet_IconFactory();
130 mySketchMgr = new PartSet_SketcherMgr(this);
132 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
133 XGUI_Workshop* aWorkshop = aConnector->workshop();
135 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
136 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
137 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
138 this, SLOT(onOperationActivatedByPreselection()));
140 ModuleBase_IViewer* aViewer = theWshop->viewer();
141 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
142 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
143 connect(aViewer, SIGNAL(viewTransformed(int)),
144 SLOT(onViewTransformed(int)));
145 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
146 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
148 myMenuMgr = new PartSet_MenuMgr(this);
149 myCustomPrs = new PartSet_CustomPrs(theWshop);
151 Events_Loop* aLoop = Events_Loop::loop();
152 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
154 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
155 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
158 PartSet_Module::~PartSet_Module()
160 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
161 for (; aIt.More(); aIt.Next()) {
162 Handle(SelectMgr_Filter) aFilter = aIt.Value();
163 if (!aFilter.IsNull())
169 void PartSet_Module::activateSelectionFilters()
171 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
172 for (; aIt.More(); aIt.Next()) {
173 Handle(SelectMgr_Filter) aFilter = aIt.Value();
174 if (!aFilter.IsNull())
175 myWorkshop->viewer()->addSelectionFilter(aFilter);
179 void PartSet_Module::deactivateSelectionFilters()
181 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
182 for (; aIt.More(); aIt.Next()) {
183 Handle(SelectMgr_Filter) aFilter = aIt.Value();
184 if (!aFilter.IsNull())
185 myWorkshop->viewer()->removeSelectionFilter(aFilter);
189 void PartSet_Module::registerValidators()
191 //Registering of validators
192 SessionPtr aMgr = ModelAPI_Session::get();
193 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
194 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
195 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
196 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
197 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
198 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
199 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
200 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
201 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
202 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
203 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
204 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
205 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
206 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
208 aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
209 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
210 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
211 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
213 aFactory->registerValidator("GeomValidators_ConstructionComposite",
214 new GeomValidators_ConstructionComposite);
216 aFactory->registerValidator("GeomValidators_ZeroOffset",
217 new GeomValidators_ZeroOffset);
219 aFactory->registerValidator("GeomValidators_BooleanArguments",
220 new GeomValidators_BooleanArguments);
222 aFactory->registerValidator("PartSet_SketchEntityValidator",
223 new PartSet_SketchEntityValidator);
225 aFactory->registerValidator("GeomValidators_Different",
226 new GeomValidators_Different);
228 aFactory->registerValidator("GeomValidators_PartitionArguments",
229 new GeomValidators_PartitionArguments);
232 void PartSet_Module::registerFilters()
234 //Registering of selection filters
235 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
236 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
239 void PartSet_Module::registerProperties()
241 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
243 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
244 Config_Prop::Integer, SKETCH_WIDTH);
247 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
249 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
250 mySketchMgr->commitNestedSketch(theOperation);
253 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
254 if (!aFOperation || aFOperation->isEditOperation())
256 // the selection is cleared after commit the create operation
257 // in order to do not use the same selected objects in the restarted operation
258 // for common behaviour, the selection is cleared even if the operation is not restarted
259 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
260 XGUI_Workshop* aWorkshop = aConnector->workshop();
261 aWorkshop->selector()->clearSelection();
263 /// Restart sketcher operations automatically
264 FeaturePtr aFeature = aFOperation->feature();
265 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
266 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
267 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
268 myRestartingMode == RM_EmptyFeatureUsed)) {
269 myLastOperationId = aFOperation->id();
270 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
271 if (!sketchMgr()->sketchSolverError())
272 launchOperation(myLastOperationId);
274 breakOperationSequence();
277 void PartSet_Module::breakOperationSequence()
279 myLastOperationId = "";
280 myLastFeature = FeaturePtr();
281 myRestartingMode = RM_None;
284 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
286 breakOperationSequence();
289 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
291 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
292 mySketchMgr->startSketch(theOperation);
294 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
295 mySketchMgr->startNestedSketch(theOperation);
298 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
300 myCustomPrs->activate(aFOperation->feature(), true);
303 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
305 ModuleBase_IModule::onOperationResumed(theOperation);
307 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
309 myCustomPrs->activate(aFOperation->feature(), true);
312 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
314 bool isModified = myCustomPrs->deactivate(false);
316 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
317 mySketchMgr->stopSketch(theOperation);
319 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
320 mySketchMgr->stopNestedSketch(theOperation);
323 //VSV: Viewer is updated on feature update and redisplay
325 // XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
326 // XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
327 // aDisplayer->updateViewer();
329 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
332 ModuleBase_Operation* PartSet_Module::currentOperation() const
334 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
335 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
336 return anOpMgr->currentOperation();
339 bool PartSet_Module::canUndo() const
341 bool aCanUndo = false;
342 SessionPtr aMgr = ModelAPI_Session::get();
343 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
344 aCanUndo = !aMgr->isOperation();
345 if (!aCanUndo) // check the enable state additionally by sketch manager
346 aCanUndo = aMgr->canUndo();
351 bool PartSet_Module::canRedo() const
353 bool aCanRedo = false;
354 SessionPtr aMgr = ModelAPI_Session::get();
355 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
356 aCanRedo = !aMgr->isOperation();
357 if (!aCanRedo) // check the enable state additionally by sketch manager
358 aCanRedo = aMgr->canRedo();
363 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
366 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
367 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
369 // part features are removed in the PartSet module only.
370 if (aFeature->getKind() == PartSetPlugin_Part::ID())
377 bool PartSet_Module::canCommitOperation() const
379 return mySketchMgr->canCommitOperation();
382 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
384 // the sketch manager put the restriction to the objects erase
385 return mySketchMgr->canEraseObject(theObject);
388 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
390 // the sketch manager put the restriction to the objects display
391 return mySketchMgr->canDisplayObject(theObject);
394 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
396 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
398 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
399 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
400 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
401 if (isSketchOp || isNestedOp) {
402 // in active sketch operation it is possible to activate operation object in selection
403 // in the edit operation, e.g. points of the line can be moved when the line is edited
404 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
405 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
410 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
412 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
415 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
417 myMenuMgr->updateViewerMenu(theStdActions);
420 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
422 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
424 if (anError.isEmpty())
425 anError = sketchMgr()->getFeatureError(theFeature);
427 if (anError.isEmpty()) {
428 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
429 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
431 if (anOpMgr->isValidationLocked()) {
432 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
433 (anOpMgr->currentOperation());
434 if (!aFOperation || theFeature == aFOperation->feature())
435 anError = "Validation is locked by the current operation";
441 void PartSet_Module::activeSelectionModes(QIntList& theModes)
444 if (mySketchMgr->activeSketch().get())
445 PartSet_SketcherMgr::sketchSelectionModes(theModes);
448 bool PartSet_Module::isMouseOverWindow()
450 return mySketchMgr->isMouseOverWindow();
453 void PartSet_Module::closeDocument()
458 void PartSet_Module::clearViewer()
460 myCustomPrs->clearPrs();
462 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
463 XGUI_Workshop* aWorkshop = aConnector->workshop();
464 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
465 aDisplayer->deactivateSelectionFilters();
468 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
470 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
474 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
475 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
476 // we have to manually activate the sketch label in edit mode
477 aPanel->activateWidget(aPanel->modelWidgets().first());
481 // Restart last operation type
482 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
483 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
484 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
485 // Initialise new line with first point equal to end of previous
486 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
488 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
489 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
490 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
492 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
493 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
494 aWgt->attributeID(), aPoint->x(), aPoint->y());
495 aPanel->activateNextWidget(aPnt2dWgt);
503 void PartSet_Module::onSelectionChanged()
505 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
509 bool isSketcherOp = false;
510 // An edit operation is enable only if the current opeation is the sketch operation
511 if (mySketchMgr->activeSketch()) {
512 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
513 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
516 // Editing of constraints can be done on selection
517 ModuleBase_ISelection* aSelect = myWorkshop->selection();
518 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
519 if (aSelected.size() == 1) {
520 ModuleBase_ViewerPrs aPrs = aSelected.first();
521 ObjectPtr aObject = aPrs.object();
522 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
524 std::string aId = aFeature->getKind();
525 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
526 (aId == SketchPlugin_ConstraintLength::ID()) ||
527 (aId == SketchPlugin_ConstraintDistance::ID()) ||
528 (aId == SketchPlugin_ConstraintAngle::ID())) {
529 editFeature(aFeature);
536 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
538 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
539 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
540 anOpMgr->onKeyReleased(theEvent);
543 void PartSet_Module::onEnterReleased()
545 myRestartingMode = RM_EmptyFeatureUsed;
548 void PartSet_Module::onOperationActivatedByPreselection()
550 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
551 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
552 // Set final definitions if they are necessary
553 //propertyPanelDefined(aOperation);
555 /// Commit sketcher operations automatically
556 anOperation->commit();
560 void PartSet_Module::onNoMoreWidgets()
562 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
564 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
565 if (myRestartingMode != RM_Forbided)
566 myRestartingMode = RM_LastFeatureUsed;
567 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
568 XGUI_Workshop* aWorkshop = aConnector->workshop();
569 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
570 // do nothing if the feature can not be applyed
571 if (anOpMgr->isApplyEnabled())
572 anOperation->commit();
577 void PartSet_Module::onVertexSelected()
579 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
580 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
581 /// If last line finished on vertex the lines creation sequence has to be break
582 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
583 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
584 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
585 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
586 bool aFoundWidget = false;
587 bool aFoundObligatory = false;
588 for (; anIt != aLast && !aFoundObligatory; anIt++) {
590 aFoundWidget = *anIt == anActiveWidget;
592 aFoundObligatory = (*anIt)->isObligatory();
594 if (!aFoundObligatory)
595 myRestartingMode = RM_Forbided;
599 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
600 Config_WidgetAPI* theWidgetApi, std::string theParentId)
602 ModuleBase_IWorkshop* aWorkshop = workshop();
603 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
604 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
605 ModuleBase_ModelWidget* aWgt = NULL;
606 if (theType == "sketch-start-label") {
607 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
608 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
609 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
610 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
611 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
612 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
614 } else if (theType == "sketch-2dpoint_selector") {
615 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
616 theWidgetApi, theParentId);
617 aPointWgt->setSketch(mySketchMgr->activeSketch());
618 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
620 } else if (theType == "point2ddistance") {
621 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
622 aWorkshop, theWidgetApi, theParentId);
623 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
625 } else if(theType == "point2dangle") {
626 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
627 aWorkshop, theWidgetApi, theParentId);
628 anAngleWgt->setSketch(mySketchMgr->activeSketch());
630 } else if (theType == "sketch_shape_selector") {
631 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
632 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
633 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
634 aWgt = aShapeSelectorWgt;
635 } else if (theType == "sketch_multi_selector") {
636 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
637 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
638 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
639 aWgt = aShapeSelectorWgt;
640 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
641 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
642 } else if (theType == "export_file_selector") {
643 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
644 } else if (theType == "sketch_launcher") {
645 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
651 bool PartSet_Module::deleteObjects()
653 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
654 XGUI_Workshop* aWorkshop = aConnector->workshop();
655 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
657 //SessionPtr aMgr = ModelAPI_Session::get();
658 // 1. check whether the delete should be processed in the module
659 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
660 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
661 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
662 if (isSketchOp || isNestedOp) {
663 // 2. find selected presentations
664 // selected objects should be collected before the current operation abort because
665 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
666 ModuleBase_ISelection* aSel = workshop()->selection();
667 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
668 // if there are no selected objects in the viewer, that means that the selection in another
669 // place cased this method. It is necessary to return the false value to understande in above
670 // method that delete is not processed
671 if (aSelectedObj.count() == 0)
674 // avoid delete of the objects, which are not belong to the current sketch
675 // in order to do not delete results of other sketches
676 QObjectPtrList aSketchObjects;
677 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
678 for ( ; anIt != aLast; anIt++) {
679 ObjectPtr anObject = *anIt;
680 if (mySketchMgr->isObjectOfSketch(anObject)) {
681 // sketch feature should be used in this list because workshop deletes features only
682 // results are skipped
683 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
684 aSketchObjects.append(aSketchFeature);
687 // if the selection contains only local selected presentations from other sketches,
688 // the Delete operation should not be done at all
689 if (aSketchObjects.size() == 0)
692 // 3. start operation
693 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
694 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
696 // the active nested sketch operation should be aborted unconditionally
697 // the Delete action should be additionally granted for the Sketch operation
698 // in order to do not abort/commit it
699 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
700 return true; // the objects are processed but can not be deleted
702 anOpMgr->startOperation(anOpAction);
704 // 4. delete features
705 // sketch feature should be skipped, only sub-features can be removed
706 // when sketch operation is active
707 aWorkshop->deleteFeatures(aSketchObjects);
709 anOpMgr->commitOperation();
712 bool isPartRemoved = false;
713 // Delete part with help of PartSet plugin
714 // TODO: the deleted objects has to be processed by multiselection
715 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
716 if (aObjects.size() == 1) {
717 ObjectPtr aObj = aObjects.first();
718 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
719 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
720 // Remove feature should be created in the document of the part results
721 ResultPtr aPartResult = aFeature->firstResult();
722 if (aPartResult.get()) {
723 std::shared_ptr<ModelAPI_ResultPart> aPart =
724 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
725 DocumentPtr aPartDoc = aPart->partDoc();
726 if (aPartDoc.get()) {
727 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
728 (PartSetPlugin_Remove::ID().c_str(), this);
729 if (!anOpMgr->canStartOperation(anOpAction->id()))
730 return true; // the objects are processed but can not be deleted
732 anOpMgr->startOperation(anOpAction);
734 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
737 anOpMgr->commitOperation();
738 isPartRemoved = true;
743 return isPartRemoved;
748 void PartSet_Module::onFeatureTriggered()
750 QAction* aCmd = dynamic_cast<QAction*>(sender());
751 if (aCmd->isCheckable() && aCmd->isChecked()) {
752 // 1. check whether the delete should be processed in the module
753 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
754 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
756 // in case if in the viewer nothing is displayed, the create operation should not be
757 // comitted even if all values of the feature are initialized
758 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
759 // the action information should be saved before the operation is aborted
760 // because this abort leads to update command status, which unchecks this action
761 anOperation->abort();
762 launchOperation(aCmd->data().toString());
766 ModuleBase_IModule::onFeatureTriggered();
769 void PartSet_Module::launchOperation(const QString& theCmdId)
771 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
772 // Show constraints if a constraint was anOperation
773 myHasConstraintShown = mySketchMgr->isConstraintsShown();
774 mySketchMgr->onShowConstraintsToggle(true);
776 ModuleBase_IModule::launchOperation(theCmdId);
780 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
782 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
783 if (!anAIS.IsNull()) {
784 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
785 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
786 if (!aDim.IsNull()) {
787 aCtx->SetZLayer(aDim, myVisualLayerId);
789 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
791 aCtx->SetZLayer(aCons, myVisualLayerId);
796 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
799 // it should be recomputed in order to disappear in the viewer if the corresponded object
801 //if (myCustomPrs->isActive())
802 // myCustomPrs->redisplay(theObject, false);
805 void PartSet_Module::onViewTransformed(int theTrsfType)
807 // Set length of arrows constant in pixel size
808 // if the operation is panning or rotate or panglobal then do nothing
809 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
811 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
812 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
813 if (aContext.IsNull())
816 //Handle(V3d_View) aView = aViewer->activeView();
818 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
819 XGUI_Workshop* aWorkshop = aConnector->workshop();
820 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
821 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
822 Handle(V3d_View) aView;
824 for (aV3dViewer->InitDefinedViews();
825 aV3dViewer->MoreDefinedViews();
826 aV3dViewer->NextDefinedViews()) {
827 Handle(V3d_View) aV = aV3dViewer->DefinedView();
828 double aS = aV->Scale();
836 double aLen = aView->Convert(20);
838 double aPrevLen = SketcherPrs_Tools::getArrowSize();
839 SketcherPrs_Tools::setArrowSize(aLen);
840 const double aPrevScale = aViewer->Scale(aViewer->activeView());
841 const double aCurScale = aViewer->activeView()->Camera()->Scale();
842 aViewer->SetScale(aViewer->activeView(), aCurScale);
843 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
844 bool isModified = false;
845 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
846 foreach (AISObjectPtr aAIS, aPrsList) {
847 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
849 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
850 if (!aDim.IsNull()) {
851 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
852 aContext->Redisplay(aDim, false);
857 aDisplayer->updateViewer();
860 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
862 bool isRedisplayed = false;
863 if (myCustomPrs->isActive())
864 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
866 return isRedisplayed;
869 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
871 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
873 //QLineEdit* aLabel = aOB->activeDocLabel();
874 //QPalette aPalet = aLabel->palette();
875 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
876 //aLabel->setPalette(aPalet);
877 aOB->treeView()->setExpandsOnDoubleClick(false);
878 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
879 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
883 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
886 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
888 /// If last line finished on vertex the lines creation sequence has to be break
889 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
890 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
891 // if there is an active widget, find the presented object in it
893 anActiveWidget = aPanel->preselectionWidget();
895 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
897 if (aWidgetValidated)
898 anObject = aWidgetValidated->findPresentedObject(theAIS);
903 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
905 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
906 int aSelected = aObjects.size();
907 SessionPtr aMgr = ModelAPI_Session::get();
908 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
909 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
911 ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
912 if (aSelected == 1) {
913 bool hasResult = false;
914 bool hasFeature = false;
915 bool hasParameter = false;
916 bool hasSubFeature = false;
917 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
919 ObjectPtr aObject = aObjects.first();
921 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
922 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
923 bool isPart = aPart.get() ||
924 (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
926 DocumentPtr aPartDoc;
928 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
930 if (aPart.get()) // this may be null is Part feature is disabled
931 aPartDoc = aPart->partDoc();
933 theMenu->addAction(aActivatePartAction);
934 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
936 } else if (aObject->document() == aMgr->activeDocument()) {
937 if (hasParameter || hasFeature) {
938 myMenuMgr->action("EDIT_CMD")->setEnabled(true);
939 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
940 if (aCurrentOp && aFeature.get()) {
941 if (aCurrentOp->id().toStdString() == aFeature->getKind())
942 myMenuMgr->action("EDIT_CMD")->setEnabled(false);
947 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
949 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
950 } else { // If feature is 0 the it means that selected root object (document)
951 theMenu->addAction(aActivatePartSetAction);
952 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
954 } else if (aSelected == 0) {
955 // if there is no selection then it means that upper label is selected
956 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
957 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
958 theMenu->addAction(aActivatePartSetAction);
959 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
961 bool aNotDeactivate = (aCurrentOp == 0);
962 if (!aNotDeactivate) {
963 aActivatePartAction->setEnabled(false);
964 aActivatePartSetAction->setEnabled(false);
968 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
970 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
971 // Do not change activation of parts if an operation active
972 static QStringList aAllowActivationList;
973 if (aAllowActivationList.isEmpty())
974 aAllowActivationList <<
975 QString(PartSetPlugin_Part::ID().c_str()) <<
976 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
977 QString(PartSetPlugin_Remove::ID().c_str());
978 if (myWorkshop->currentOperation() &&
979 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
981 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
982 XGUI_Workshop* aWorkshop = aConnector->workshop();
983 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
984 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
985 QPalette aPalet = aLabel->palette();
987 SessionPtr aMgr = ModelAPI_Session::get();
988 DocumentPtr aActiveDoc = aMgr->activeDocument();
989 if (aActivePartIndex.isValid())
990 aTreeView->setExpanded(aActivePartIndex, false);
991 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
992 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
993 if (aActivePartIndex.isValid())
994 aTreeView->setExpanded(aActivePartIndex, true);
996 aLabel->setPalette(aPalet);
997 aWorkshop->updateCommandStatus();
999 // Update displayed objects in order to update active color
1000 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1001 QObjectPtrList aObjects = aDisplayer->displayedObjects();
1003 foreach(ObjectPtr aObj, aObjects) {
1004 //TODO: replace by redisplay event.
1005 aHidden = !aObj->data() || !aObj->data()->isValid() ||
1006 aObj->isDisabled() || (!aObj->isDisplayed());
1008 aDisplayer->redisplay(aObj, false);
1010 aDisplayer->updateViewer();
1014 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1016 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1018 SessionPtr aMgr = ModelAPI_Session::get();
1019 if (!theIndex.isValid()) {
1020 aMgr->setActiveDocument(aMgr->moduleDocument());
1023 if (theIndex.column() != 0) // Use only first column
1026 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1027 XGUI_Workshop* aWorkshop = aConnector->workshop();
1028 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1029 // De not use non editable Indexes
1030 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1032 ObjectPtr aObj = aDataModel->object(theIndex);
1034 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1035 if (!aPart.get()) { // Probably this is Feature
1036 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1037 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1038 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1041 if (aPart.get()) { // if this is a part
1042 if (aPart->partDoc() == aMgr->activeDocument()) {
1043 myMenuMgr->activatePartSet();
1051 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1053 // z layer is created for all started operations in order to visualize operation AIS presentation
1055 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1056 if (aContext.IsNull())
1059 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1060 if (myVisualLayerId == 0) {
1061 if (myVisualLayerId == 0)
1062 aViewer->AddZLayer(myVisualLayerId);
1064 TColStd_SequenceOfInteger aZList;
1065 aViewer->GetAllZLayers(aZList);
1066 bool aFound = false;
1067 for (int i = 1; i <= aZList.Length(); i++) {
1068 if (aZList(i) == myVisualLayerId) {
1074 aViewer->AddZLayer(myVisualLayerId);
1076 // if there is an active operation with validated widget,
1077 // the filters of this widget should be activated in the created view
1078 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1080 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1081 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1082 if (anActiveWidget) {
1083 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1085 if (aWidgetValidated)
1086 aWidgetValidated->activateFilters(true);