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>
47 #include <ModelAPI_Object.h>
48 #include <ModelAPI_Events.h>
49 #include <ModelAPI_Validator.h>
50 #include <ModelAPI_Data.h>
51 #include <ModelAPI_Session.h>
52 #include <GeomValidators_DifferentShapes.h>
53 #include <ModelAPI_ResultBody.h>
55 #include <GeomDataAPI_Point2D.h>
56 #include <GeomDataAPI_Point.h>
57 #include <GeomDataAPI_Dir.h>
59 #include <XGUI_Displayer.h>
60 #include <XGUI_Workshop.h>
61 #include <XGUI_OperationMgr.h>
62 #include <XGUI_PropertyPanel.h>
63 #include <XGUI_ModuleConnector.h>
64 #include <XGUI_ContextMenuMgr.h>
65 #include <XGUI_Tools.h>
66 #include <XGUI_ObjectsBrowser.h>
67 #include <XGUI_SelectionMgr.h>
68 #include <XGUI_DataModel.h>
70 #include <SketchPlugin_Feature.h>
71 #include <SketchPlugin_Sketch.h>
72 #include <SketchPlugin_Line.h>
73 #include <SketchPlugin_Arc.h>
74 #include <SketchPlugin_Circle.h>
75 #include <SketchPlugin_Point.h>
76 #include <SketchPlugin_ConstraintAngle.h>
77 #include <SketchPlugin_ConstraintLength.h>
78 #include <SketchPlugin_ConstraintDistance.h>
79 #include <SketchPlugin_ConstraintParallel.h>
80 #include <SketchPlugin_ConstraintPerpendicular.h>
81 #include <SketchPlugin_ConstraintRadius.h>
83 #include <SketcherPrs_SymbolPrs.h>
84 #include <SketcherPrs_Tools.h>
86 #include <Events_Loop.h>
87 #include <Config_PropManager.h>
88 #include <Config_Keywords.h>
90 #include <StdSelect_TypeOfFace.hxx>
91 #include <TopoDS_Vertex.hxx>
93 #include <TopoDS_Shape.hxx>
94 #include <BRep_Tool.hxx>
95 #include <AIS_Dimension.hxx>
98 #include <QMouseEvent>
101 #include <QApplication>
102 #include <QMessageBox>
103 #include <QMainWindow>
106 #include <GeomAlgoAPI_FaceBuilder.h>
107 #include <GeomDataAPI_Dir.h>
109 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
115 /*!Create and return new instance of XGUI_Module*/
116 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
118 return new PartSet_Module(theWshop);
121 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
122 : ModuleBase_IModule(theWshop),
123 myRestartingMode(RM_None), myVisualLayerId(0)
125 new PartSet_IconFactory();
127 mySketchMgr = new PartSet_SketcherMgr(this);
128 #ifdef ModuleDataModel
129 myDataModel = new PartSet_DocumentDataModel(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);
229 void PartSet_Module::registerFilters()
231 //Registering of selection filters
232 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
233 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
236 void PartSet_Module::registerProperties()
238 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
240 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
241 Config_Prop::Integer, SKETCH_WIDTH);
244 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
246 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
247 mySketchMgr->commitNestedSketch(theOperation);
250 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
251 if (!aFOperation || aFOperation->isEditOperation())
253 // the selection is cleared after commit the create operation
254 // in order to do not use the same selected objects in the restarted operation
255 // for common behaviour, the selection is cleared even if the operation is not restarted
256 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
257 XGUI_Workshop* aWorkshop = aConnector->workshop();
258 aWorkshop->selector()->clearSelection();
260 /// Restart sketcher operations automatically
261 FeaturePtr aFeature = aFOperation->feature();
262 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
263 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
264 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
265 myRestartingMode == RM_EmptyFeatureUsed)) {
266 myLastOperationId = aFOperation->id();
267 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
268 if (!sketchMgr()->sketchSolverError())
269 launchOperation(myLastOperationId);
271 breakOperationSequence();
274 void PartSet_Module::breakOperationSequence()
276 myLastOperationId = "";
277 myLastFeature = FeaturePtr();
278 myRestartingMode = RM_None;
281 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
283 breakOperationSequence();
286 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
288 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
289 mySketchMgr->startSketch(theOperation);
291 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
292 mySketchMgr->startNestedSketch(theOperation);
295 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
297 myCustomPrs->activate(aFOperation->feature(), true);
300 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
302 ModuleBase_IModule::onOperationResumed(theOperation);
304 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
306 myCustomPrs->activate(aFOperation->feature(), true);
309 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
311 bool isModified = myCustomPrs->deactivate(false);
313 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
314 mySketchMgr->stopSketch(theOperation);
316 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
317 mySketchMgr->stopNestedSketch(theOperation);
321 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
322 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
323 aDisplayer->updateViewer();
327 ModuleBase_Operation* PartSet_Module::currentOperation() const
329 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
330 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
331 return anOpMgr->currentOperation();
334 bool PartSet_Module::canUndo() const
336 bool aCanUndo = false;
337 SessionPtr aMgr = ModelAPI_Session::get();
338 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
339 aCanUndo = !aMgr->isOperation();
340 if (!aCanUndo) // check the enable state additionally by sketch manager
341 aCanUndo = aMgr->canUndo();
346 bool PartSet_Module::canRedo() const
348 bool aCanRedo = false;
349 SessionPtr aMgr = ModelAPI_Session::get();
350 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
351 aCanRedo = !aMgr->isOperation();
352 if (!aCanRedo) // check the enable state additionally by sketch manager
353 aCanRedo = aMgr->canRedo();
358 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
361 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
362 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
364 // part features are removed in the PartSet module only.
365 if (aFeature->getKind() == PartSetPlugin_Part::ID())
372 bool PartSet_Module::canCommitOperation() const
374 return mySketchMgr->canCommitOperation();
377 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
379 // the sketch manager put the restriction to the objects erase
380 return mySketchMgr->canEraseObject(theObject);
383 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
385 // the sketch manager put the restriction to the objects display
386 return mySketchMgr->canDisplayObject(theObject);
389 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
391 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
393 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
394 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
395 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
396 if (isSketchOp || isNestedOp) {
397 // in active sketch operation it is possible to activate operation object in selection
398 // in the edit operation, e.g. points of the line can be moved when the line is edited
399 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
400 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
405 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
407 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
410 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
412 myMenuMgr->updateViewerMenu(theStdActions);
416 void PartSet_Module::activeSelectionModes(QIntList& theModes)
419 if (mySketchMgr->activeSketch().get())
420 PartSet_SketcherMgr::sketchSelectionModes(theModes);
423 bool PartSet_Module::isMouseOverWindow()
425 return mySketchMgr->isMouseOverWindow();
428 void PartSet_Module::closeDocument()
433 void PartSet_Module::clearViewer()
435 myCustomPrs->clearPrs();
438 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
440 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
444 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
445 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
446 // we have to manually activate the sketch label in edit mode
447 aPanel->activateWidget(aPanel->modelWidgets().first());
451 // Restart last operation type
452 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
453 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
454 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
455 // Initialise new line with first point equal to end of previous
456 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
458 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
459 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
460 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
462 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
463 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
464 aWgt->attributeID(), aPoint->x(), aPoint->y());
465 aPanel->activateNextWidget(aPnt2dWgt);
473 void PartSet_Module::onSelectionChanged()
475 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
479 bool isSketcherOp = false;
480 // An edit operation is enable only if the current opeation is the sketch operation
481 if (mySketchMgr->activeSketch()) {
482 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
483 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
486 // Editing of constraints can be done on selection
487 ModuleBase_ISelection* aSelect = myWorkshop->selection();
488 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
489 if (aSelected.size() == 1) {
490 ModuleBase_ViewerPrs aPrs = aSelected.first();
491 ObjectPtr aObject = aPrs.object();
492 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
494 std::string aId = aFeature->getKind();
495 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
496 (aId == SketchPlugin_ConstraintLength::ID()) ||
497 (aId == SketchPlugin_ConstraintDistance::ID()) ||
498 (aId == SketchPlugin_ConstraintAngle::ID())) {
499 editFeature(aFeature);
506 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
508 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
509 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
510 anOpMgr->onKeyReleased(theEvent);
513 void PartSet_Module::onEnterReleased()
515 myRestartingMode = RM_EmptyFeatureUsed;
518 void PartSet_Module::onOperationActivatedByPreselection()
520 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
521 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
522 // Set final definitions if they are necessary
523 //propertyPanelDefined(aOperation);
525 /// Commit sketcher operations automatically
526 anOperation->commit();
530 void PartSet_Module::onNoMoreWidgets()
532 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
534 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
535 if (myRestartingMode != RM_Forbided)
536 myRestartingMode = RM_LastFeatureUsed;
537 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
538 XGUI_Workshop* aWorkshop = aConnector->workshop();
539 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
540 // do nothing if the feature can not be applyed
541 if (anOpMgr->isApplyEnabled())
542 anOperation->commit();
547 void PartSet_Module::onVertexSelected()
549 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
550 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
551 /// If last line finished on vertex the lines creation sequence has to be break
552 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
553 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
554 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
555 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
556 bool aFoundWidget = false;
557 bool aFoundObligatory = false;
558 for (; anIt != aLast && !aFoundObligatory; anIt++) {
560 aFoundWidget = *anIt == anActiveWidget;
562 aFoundObligatory = (*anIt)->isObligatory();
564 if (!aFoundObligatory)
565 myRestartingMode = RM_Forbided;
569 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
570 Config_WidgetAPI* theWidgetApi, std::string theParentId)
572 ModuleBase_IWorkshop* aWorkshop = workshop();
573 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
574 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
575 ModuleBase_ModelWidget* aWgt = NULL;
576 if (theType == "sketch-start-label") {
577 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
578 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
579 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
580 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
581 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
582 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
584 } else if (theType == "sketch-2dpoint_selector") {
585 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
586 theWidgetApi, theParentId);
587 aPointWgt->setSketch(mySketchMgr->activeSketch());
588 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
590 } else if (theType == "point2ddistance") {
591 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
592 aWorkshop, theWidgetApi, theParentId);
593 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
595 } else if(theType == "point2dangle") {
596 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
597 aWorkshop, theWidgetApi, theParentId);
598 anAngleWgt->setSketch(mySketchMgr->activeSketch());
600 } else if (theType == "sketch_shape_selector") {
601 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
602 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
603 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
604 aWgt = aShapeSelectorWgt;
605 } else if (theType == "sketch_multi_selector") {
606 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
607 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
608 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
609 aWgt = aShapeSelectorWgt;
610 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
611 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
612 } else if (theType == "export_file_selector") {
613 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
614 } else if (theType == "sketch_launcher") {
615 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
621 bool PartSet_Module::deleteObjects()
623 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
624 XGUI_Workshop* aWorkshop = aConnector->workshop();
625 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
627 //SessionPtr aMgr = ModelAPI_Session::get();
628 // 1. check whether the delete should be processed in the module
629 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
630 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
631 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
632 if (isSketchOp || isNestedOp) {
633 // 2. find selected presentations
634 // selected objects should be collected before the current operation abort because
635 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
636 ModuleBase_ISelection* aSel = workshop()->selection();
637 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
638 // if there are no selected objects in the viewer, that means that the selection in another
639 // place cased this method. It is necessary to return the false value to understande in above
640 // method that delete is not processed
641 if (aSelectedObj.count() == 0)
644 // avoid delete of the objects, which are not belong to the current sketch
645 // in order to do not delete results of other sketches
646 QObjectPtrList aSketchObjects;
647 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
648 for ( ; anIt != aLast; anIt++) {
649 ObjectPtr anObject = *anIt;
650 if (mySketchMgr->isObjectOfSketch(anObject)) {
651 // sketch feature should be used in this list because workshop deletes features only
652 // results are skipped
653 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
654 aSketchObjects.append(aSketchFeature);
657 // if the selection contains only local selected presentations from other sketches,
658 // the Delete operation should not be done at all
659 if (aSketchObjects.size() == 0)
662 // 3. start operation
663 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
664 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
666 // the active nested sketch operation should be aborted unconditionally
667 // the Delete action should be additionally granted for the Sketch operation
668 // in order to do not abort/commit it
669 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
670 return true; // the objects are processed but can not be deleted
672 anOpMgr->startOperation(anOpAction);
674 // 4. delete features
675 // sketch feature should be skipped, only sub-features can be removed
676 // when sketch operation is active
677 aWorkshop->deleteFeatures(aSketchObjects);
679 anOpMgr->commitOperation();
682 bool isPartRemoved = false;
683 // Delete part with help of PartSet plugin
684 // TODO: the deleted objects has to be processed by multiselection
685 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
686 if (aObjects.size() == 1) {
687 ObjectPtr aObj = aObjects.first();
688 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
689 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
690 // Remove feature should be created in the document of the part results
691 ResultPtr aPartResult = aFeature->firstResult();
692 if (aPartResult.get()) {
693 std::shared_ptr<ModelAPI_ResultPart> aPart =
694 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
695 DocumentPtr aPartDoc = aPart->partDoc();
696 if (aPartDoc.get()) {
697 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
698 (PartSetPlugin_Remove::ID().c_str(), this);
699 if (!anOpMgr->canStartOperation(anOpAction->id()))
700 return true; // the objects are processed but can not be deleted
702 anOpMgr->startOperation(anOpAction);
704 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
707 anOpMgr->commitOperation();
708 isPartRemoved = true;
713 return isPartRemoved;
718 void PartSet_Module::onFeatureTriggered()
720 QAction* aCmd = dynamic_cast<QAction*>(sender());
721 if (aCmd->isCheckable() && aCmd->isChecked()) {
722 // 1. check whether the delete should be processed in the module
723 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
724 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
726 // in case if in the viewer nothing is displayed, the create operation should not be
727 // comitted even if all values of the feature are initialized
728 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
729 // the action information should be saved before the operation is aborted
730 // because this abort leads to update command status, which unchecks this action
731 anOperation->abort();
732 launchOperation(aCmd->data().toString());
736 ModuleBase_IModule::onFeatureTriggered();
739 void PartSet_Module::launchOperation(const QString& theCmdId)
741 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
742 // Show constraints if a constraint was anOperation
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))
781 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
782 //Handle(V3d_View) aView = aViewer->activeView();
784 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
785 XGUI_Workshop* aWorkshop = aConnector->workshop();
786 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
787 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
789 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
790 Handle(V3d_View) aView;
792 for (aV3dViewer->InitDefinedViews();
793 aV3dViewer->MoreDefinedViews();
794 aV3dViewer->NextDefinedViews()) {
795 Handle(V3d_View) aV = aV3dViewer->DefinedView();
796 double aS = aV->Scale();
804 double aLen = aView->Convert(20);
806 double aPrevLen = SketcherPrs_Tools::getArrowSize();
807 SketcherPrs_Tools::setArrowSize(aLen);
808 const double aPrevScale = aViewer->Scale(aViewer->activeView());
809 const double aCurScale = aViewer->activeView()->Camera()->Scale();
810 aViewer->SetScale(aViewer->activeView(), aCurScale);
811 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
812 bool isModified = false;
813 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
814 foreach (AISObjectPtr aAIS, aPrsList) {
815 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
817 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
818 if (!aDim.IsNull()) {
819 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
820 aContext->Redisplay(aDim, false);
825 aDisplayer->updateViewer();
828 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
830 bool isRedisplayed = false;
831 if (myCustomPrs->isActive())
832 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
834 return isRedisplayed;
837 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
839 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
841 //QLineEdit* aLabel = aOB->activeDocLabel();
842 //QPalette aPalet = aLabel->palette();
843 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
844 //aLabel->setPalette(aPalet);
845 aOB->treeView()->setExpandsOnDoubleClick(false);
846 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
847 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
848 #ifdef ModuleDataModel
849 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
850 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
851 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
852 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
857 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
860 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
862 /// If last line finished on vertex the lines creation sequence has to be break
863 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
864 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
865 // if there is an active widget, find the presented object in it
867 anActiveWidget = aPanel->preselectionWidget();
869 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
871 if (aWidgetValidated)
872 anObject = aWidgetValidated->findPresentedObject(theAIS);
877 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
879 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
880 int aSelected = aObjects.size();
881 SessionPtr aMgr = ModelAPI_Session::get();
882 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
883 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
884 if (aSelected == 1) {
885 bool hasResult = false;
886 bool hasFeature = false;
887 bool hasParameter = false;
888 bool hasSubFeature = false;
889 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
891 ObjectPtr aObject = aObjects.first();
893 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
894 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
895 bool isPart = aPart.get() ||
896 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
898 DocumentPtr aPartDoc;
900 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
902 if (aPart.get()) // this may be null is Part feature is disabled
903 aPartDoc = aPart->partDoc();
905 theMenu->addAction(aActivatePartAction);
906 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
908 } else if (aObject->document() == aMgr->activeDocument()) {
909 if (hasParameter || hasFeature)
910 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
913 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
915 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
916 } else { // If feature is 0 the it means that selected root object (document)
917 theMenu->addAction(aActivatePartSetAction);
918 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
920 } else if (aSelected == 0) {
921 // if there is no selection then it means that upper label is selected
922 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
923 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
924 theMenu->addAction(aActivatePartSetAction);
925 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
927 bool aNotDeactivate = (myWorkshop->currentOperation() == 0);
928 if (!aNotDeactivate) {
929 aActivatePartAction->setEnabled(false);
930 aActivatePartSetAction->setEnabled(false);
934 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
936 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
937 // Do not change activation of parts if an operation active
938 static QStringList aAllowActivationList;
939 if (aAllowActivationList.isEmpty())
940 aAllowActivationList <<
941 QString(PartSetPlugin_Part::ID().c_str()) <<
942 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
943 QString(PartSetPlugin_Remove::ID().c_str());
944 if (myWorkshop->currentOperation() &&
945 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
947 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
948 XGUI_Workshop* aWorkshop = aConnector->workshop();
949 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
950 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
951 QPalette aPalet = aLabel->palette();
953 SessionPtr aMgr = ModelAPI_Session::get();
954 DocumentPtr aActiveDoc = aMgr->activeDocument();
955 #ifdef ModuleDataModel
956 QModelIndex aOldIndex = myDataModel->activePartTree();
957 DocumentPtr aDoc = aMgr->moduleDocument();
958 if (aActiveDoc == aDoc) {
959 if (aOldIndex.isValid())
960 aTreeView->setExpanded(aOldIndex, false);
961 myDataModel->deactivatePart();
962 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
964 std::string aGrpName = ModelAPI_ResultPart::group();
965 for (int i = 0; i < aDoc->size(aGrpName); i++) {
966 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
967 if (aPart->partDoc() == aActiveDoc) {
968 QModelIndex aIndex = myDataModel->partIndex(aPart);
969 if (myDataModel->activatePart(aIndex)) {
970 if (aOldIndex.isValid())
971 aTreeView->setExpanded(aOldIndex, false);
972 aTreeView->setExpanded(myDataModel->activePartTree(), true);
973 aPalet.setColor(QPalette::Text, Qt::black);
980 // Problem with MPV: At first time on creation it doesn't work because Part feature
981 // creation event will be sent after
982 if (aActivePartIndex.isValid())
983 aTreeView->setExpanded(aActivePartIndex, false);
984 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
985 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
986 if (aActivePartIndex.isValid())
987 aTreeView->setExpanded(aActivePartIndex, true);
989 aLabel->setPalette(aPalet);
990 aWorkshop->updateCommandStatus();
992 // Update displayed objects in order to update active color
993 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
994 QObjectPtrList aObjects = aDisplayer->displayedObjects();
995 foreach(ObjectPtr aObj, aObjects)
996 aDisplayer->redisplay(aObj, false);
997 aDisplayer->updateViewer();
1001 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1003 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1005 SessionPtr aMgr = ModelAPI_Session::get();
1006 if (!theIndex.isValid()) {
1007 aMgr->setActiveDocument(aMgr->moduleDocument());
1010 if (theIndex.column() != 0) // Use only first column
1012 #ifdef ModuleDataModel
1013 ObjectPtr aObj = myDataModel->object(theIndex);
1015 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1016 XGUI_Workshop* aWorkshop = aConnector->workshop();
1017 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1018 // De not use non editable Indexes
1019 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1021 ObjectPtr aObj = aDataModel->object(theIndex);
1024 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1025 if (!aPart.get()) { // Probably this is Feature
1026 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1027 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1028 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1031 if (aPart.get()) { // if this is a part
1032 if (aPart->partDoc() == aMgr->activeDocument()) {
1033 myMenuMgr->activatePartSet();
1041 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1043 // z layer is created for all started operations in order to visualize operation AIS presentation
1045 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->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);