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"
18 #include "PartSet_Filters.h"
19 #include "PartSet_FilterInfinite.h"
21 #include <PartSetPlugin_Remove.h>
22 #include <PartSetPlugin_Part.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_IViewWindow.h>
27 #include <ModuleBase_IPropertyPanel.h>
28 #include <ModuleBase_WidgetEditor.h>
29 #include <ModuleBase_FilterFactory.h>
30 #include <ModuleBase_Tools.h>
31 #include <GeomValidators_ShapeType.h>
33 #include <GeomValidators_Face.h>
34 #include <GeomValidators_ConstructionComposite.h>
35 #include <GeomValidators_ZeroOffset.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_ShapeValidator.h>
45 #include <GeomDataAPI_Point2D.h>
46 #include <GeomDataAPI_Point.h>
47 #include <GeomDataAPI_Dir.h>
49 #include <XGUI_Displayer.h>
50 #include <XGUI_Workshop.h>
51 #include <XGUI_OperationMgr.h>
52 #include <XGUI_PropertyPanel.h>
53 #include <XGUI_ModuleConnector.h>
54 #include <XGUI_ContextMenuMgr.h>
55 #include <XGUI_Tools.h>
56 #include <XGUI_ObjectsBrowser.h>
57 #include <XGUI_SelectionMgr.h>
59 #include <SketchPlugin_Feature.h>
60 #include <SketchPlugin_Sketch.h>
61 #include <SketchPlugin_Line.h>
62 #include <SketchPlugin_Arc.h>
63 #include <SketchPlugin_Circle.h>
64 #include <SketchPlugin_Point.h>
65 #include <SketchPlugin_ConstraintLength.h>
66 #include <SketchPlugin_ConstraintDistance.h>
67 #include <SketchPlugin_ConstraintParallel.h>
68 #include <SketchPlugin_ConstraintPerpendicular.h>
69 #include <SketchPlugin_ConstraintRadius.h>
71 #include <SketcherPrs_SymbolPrs.h>
72 #include <SketcherPrs_Tools.h>
74 #include <Events_Loop.h>
75 #include <Config_PropManager.h>
76 #include <Config_Keywords.h>
78 #include <StdSelect_TypeOfFace.hxx>
79 #include <TopoDS_Vertex.hxx>
81 #include <TopoDS_Shape.hxx>
82 #include <BRep_Tool.hxx>
83 #include <AIS_Dimension.hxx>
86 #include <QMouseEvent>
89 #include <QApplication>
90 #include <QMessageBox>
91 #include <QMainWindow>
94 #include <GeomAlgoAPI_FaceBuilder.h>
95 #include <GeomDataAPI_Dir.h>
97 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
105 /*!Create and return new instance of XGUI_Module*/
106 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
108 return new PartSet_Module(theWshop);
111 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
112 : ModuleBase_IModule(theWshop),
113 myRestartingMode(RM_None), myVisualLayerId(0)
115 mySketchMgr = new PartSet_SketcherMgr(this);
116 myDataModel = new PartSet_DocumentDataModel(this);
118 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
119 XGUI_Workshop* aWorkshop = aConnector->workshop();
121 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
122 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
123 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
124 this, SLOT(onOperationActivatedByPreselection()));
126 ModuleBase_IViewer* aViewer = theWshop->viewer();
127 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
128 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
129 connect(aViewer, SIGNAL(viewTransformed(int)),
130 SLOT(onViewTransformed(int)));
132 myMenuMgr = new PartSet_MenuMgr(this);
134 Events_Loop* aLoop = Events_Loop::loop();
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
137 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
138 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
141 PartSet_Module::~PartSet_Module()
143 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
144 for (; aIt.More(); aIt.Next()) {
145 Handle(SelectMgr_Filter) aFilter = aIt.Value();
146 if (!aFilter.IsNull())
151 void PartSet_Module::activateSelectionFilters()
153 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
154 for (; aIt.More(); aIt.Next()) {
155 Handle(SelectMgr_Filter) aFilter = aIt.Value();
156 if (!aFilter.IsNull())
157 myWorkshop->viewer()->addSelectionFilter(aFilter);
161 void PartSet_Module::deactivateSelectionFilters()
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()->removeSelectionFilter(aFilter);
171 void PartSet_Module::registerValidators()
173 //Registering of validators
174 SessionPtr aMgr = ModelAPI_Session::get();
175 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
176 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
177 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
178 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
179 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
180 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
181 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
182 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
183 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
184 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
185 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
187 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
188 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
190 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
191 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
193 aFactory->registerValidator("GeomValidators_ConstructionComposite",
194 new GeomValidators_ConstructionComposite);
196 aFactory->registerValidator("GeomValidators_ZeroOffset",
197 new GeomValidators_ZeroOffset);
199 aFactory->registerValidator("PartSet_SketchEntityValidator",
200 new PartSet_SketchEntityValidator);
202 aFactory->registerValidator("PartSet_SameTypeAttr",
203 new PartSet_SameTypeAttrValidator);
206 void PartSet_Module::registerFilters()
208 //Registering of selection filters
209 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
210 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
213 void PartSet_Module::registerProperties()
215 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
217 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
218 Config_Prop::Integer, SKETCH_WIDTH);
221 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
223 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
224 mySketchMgr->commitNestedSketch(theOperation);
227 if (theOperation->isEditOperation())
229 // the selection is cleared after commit the create operation
230 // in order to do not use the same selected objects in the restarted operation
231 // for common behaviour, the selection is cleared even if the operation is not restarted
232 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
233 XGUI_Workshop* aWorkshop = aConnector->workshop();
234 aWorkshop->selector()->clearSelection();
236 /// Restart sketcher operations automatically
237 FeaturePtr aFeature = theOperation->feature();
238 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
239 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
240 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
241 myRestartingMode == RM_EmptyFeatureUsed)) {
242 myLastOperationId = theOperation->id();
243 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
245 launchOperation(myLastOperationId);
247 breakOperationSequence();
250 void PartSet_Module::breakOperationSequence()
252 myLastOperationId = "";
253 myLastFeature = FeaturePtr();
254 myRestartingMode = RM_None;
257 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
259 breakOperationSequence();
262 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
264 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
265 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
266 aViewer->AddZLayer(myVisualLayerId);
267 mySketchMgr->startSketch(theOperation);
269 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
270 mySketchMgr->startNestedSketch(theOperation);
274 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
276 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
277 mySketchMgr->stopSketch(theOperation);
278 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
279 aViewer->RemoveZLayer(myVisualLayerId);
282 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
283 mySketchMgr->stopNestedSketch(theOperation);
287 ModuleBase_Operation* PartSet_Module::currentOperation() const
289 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
290 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
291 return anOpMgr->currentOperation();
294 bool PartSet_Module::canUndo() const
296 bool aCanUndo = false;
297 SessionPtr aMgr = ModelAPI_Session::get();
298 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
299 aCanUndo = !aMgr->isOperation();
300 if (!aCanUndo) // check the enable state additionally by sketch manager
301 aCanUndo = aMgr->canUndo();
306 bool PartSet_Module::canRedo() const
308 bool aCanRedo = false;
309 SessionPtr aMgr = ModelAPI_Session::get();
310 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
311 aCanRedo = !aMgr->isOperation();
312 if (!aCanRedo) // check the enable state additionally by sketch manager
313 aCanRedo = aMgr->canRedo();
318 bool PartSet_Module::canCommitOperation() const
320 return mySketchMgr->canCommitOperation();
323 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
325 // the sketch manager put the restriction to the objects display
326 return mySketchMgr->canDisplayObject(theObject);
329 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
331 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
333 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
334 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
335 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
336 if (isSketchOp || isNestedOp) {
337 // in active sketch operation it is possible to activate operation object in selection
338 // in the edit operation, e.g. points of the line can be moved when the line is edited
339 aCanActivate = aCanActivate || anOperation->isEditOperation();
344 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
346 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
349 void PartSet_Module::activeSelectionModes(QIntList& theModes)
352 if (mySketchMgr->activeSketch().get())
353 PartSet_SketcherMgr::sketchSelectionModes(theModes);
356 bool PartSet_Module::isMouseOverWindow()
358 return mySketchMgr->isMouseOverWindow();
361 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
363 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
364 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
365 // we have to manually activate the sketch label in edit mode
366 aPanel->activateWidget(aPanel->modelWidgets().first());
370 // Restart last operation type
371 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
372 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
373 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
374 // Initialise new line with first point equal to end of previous
375 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
377 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
378 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
379 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
381 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
382 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
383 aWgt->attributeID(), aPoint->x(), aPoint->y());
384 aPanel->activateNextWidget(aPnt2dWgt);
392 void PartSet_Module::onSelectionChanged()
394 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
398 bool isSketcherOp = false;
399 // An edit operation is enable only if the current opeation is the sketch operation
400 if (mySketchMgr->activeSketch()) {
401 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
402 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
405 // Editing of constraints can be done on selection
406 ModuleBase_ISelection* aSelect = myWorkshop->selection();
407 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
408 if (aSelected.size() == 1) {
409 ModuleBase_ViewerPrs aPrs = aSelected.first();
410 ObjectPtr aObject = aPrs.object();
411 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
413 std::string aId = aFeature->getKind();
414 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
415 (aId == SketchPlugin_ConstraintLength::ID()) ||
416 (aId == SketchPlugin_ConstraintDistance::ID())) {
417 editFeature(aFeature);
424 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
426 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
427 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
428 anOpMgr->onKeyReleased(theEvent);
431 void PartSet_Module::onEnterReleased()
433 myRestartingMode = RM_EmptyFeatureUsed;
436 void PartSet_Module::onOperationActivatedByPreselection()
438 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
439 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
440 // Set final definitions if they are necessary
441 //propertyPanelDefined(aOperation);
443 /// Commit sketcher operations automatically
444 anOperation->commit();
448 void PartSet_Module::onNoMoreWidgets()
450 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
452 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
453 if (myRestartingMode != RM_Forbided)
454 myRestartingMode = RM_LastFeatureUsed;
455 anOperation->commit();
460 void PartSet_Module::onVertexSelected()
462 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
463 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
464 /// If last line finished on vertex the lines creation sequence has to be break
465 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
466 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
467 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
468 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
469 bool aFoundWidget = false;
470 bool aFoundObligatory = false;
471 for (; anIt != aLast && !aFoundObligatory; anIt++) {
473 aFoundWidget = *anIt == anActiveWidget;
475 aFoundObligatory = (*anIt)->isObligatory();
477 if (!aFoundObligatory)
478 myRestartingMode = RM_Forbided;
482 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
483 Config_WidgetAPI* theWidgetApi, std::string theParentId)
485 ModuleBase_IWorkshop* aWorkshop = workshop();
486 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
487 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
488 ModuleBase_ModelWidget* aWgt = NULL;
489 if (theType == "sketch-start-label") {
490 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent,
491 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
492 aLabelWgt->setWorkshop(aXUIWorkshop);
493 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
494 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
495 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
496 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
498 } else if (theType == "sketch-2dpoint_selector") {
499 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
500 theWidgetApi, theParentId);
501 aPointWgt->setSketch(mySketchMgr->activeSketch());
502 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
504 } else if (theType == "point2ddistance") {
505 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
506 aWorkshop, theWidgetApi, theParentId);
507 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
509 } else if(theType == "point2dangle") {
510 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
511 aWorkshop, theWidgetApi, theParentId);
512 anAngleWgt->setSketch(mySketchMgr->activeSketch());
514 } else if (theType == "sketch_shape_selector") {
515 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
516 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
517 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
518 aWgt = aShapeSelectorWgt;
519 } else if (theType == "sketch_multi_selector") {
520 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
521 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
522 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
523 aWgt = aShapeSelectorWgt;
524 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
525 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
526 } else if (theType == "export_file_selector") {
527 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
528 } else if (theType == "sketch_launcher") {
529 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
535 bool PartSet_Module::deleteObjects()
537 SessionPtr aMgr = ModelAPI_Session::get();
538 // 1. check whether the delete should be processed in the module
539 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
540 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
541 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
542 if (isSketchOp || isNestedOp) {
543 // 2. find selected presentations
544 // selected objects should be collected before the current operation abort because
545 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
546 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
547 XGUI_Workshop* aWorkshop = aConnector->workshop();
548 ModuleBase_ISelection* aSel = workshop()->selection();
549 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
550 // if there are no selected objects in the viewer, that means that the selection in another
551 // place cased this method. It is necessary to return the false value to understande in above
552 // method that delete is not processed
553 if (aSelectedObj.count() == 0)
556 // avoid delete of the objects, which are not belong to the current sketch
557 // in order to do not delete results of other sketches
558 QObjectPtrList aSketchObjects;
559 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
560 for ( ; anIt != aLast; anIt++) {
561 ObjectPtr anObject = *anIt;
562 if (mySketchMgr->isObjectOfSketch(anObject)) {
563 // sketch feature should be used in this list because workshop deletes features only
564 // results are skipped
565 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
566 aSketchObjects.append(aSketchFeature);
569 // if the selection contains only local selected presentations from other sketches,
570 // the Delete operation should not be done at all
571 if (aSketchObjects.size() == 0)
574 // the active nested sketch operation should be aborted unconditionally
576 anOperation->abort();
578 // 3. start operation
579 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
580 aMgr->startOperation(aDescription.toStdString());
582 // 4. delete features
583 // sketch feature should be skipped, only sub-features can be removed
584 // when sketch operation is active
585 std::set<FeaturePtr> anIgnoredFeatures;
586 anIgnoredFeatures.insert(mySketchMgr->activeSketch());
587 aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
590 aWorkshop->displayer()->updateViewer();
591 aMgr->finishOperation();
593 bool isPartRemoved = false;
594 // Delete part with help of PartSet plugin
595 // TODO: the deleted objects has to be processed by multiselection
596 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
597 if (aObjects.size() == 1) {
598 ObjectPtr aObj = aObjects.first();
599 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
600 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
601 // Remove feature should be created in the document of the part results
602 ResultPtr aPartResult = aFeature->firstResult();
603 if (aPartResult.get()) {
604 std::shared_ptr<ModelAPI_ResultPart> aPart =
605 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
606 DocumentPtr aPartDoc = aPart->partDoc();
607 if (aPartDoc.get()) {
608 aMgr->startOperation(PartSetPlugin_Remove::ID());
609 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
611 aMgr->finishOperation();
612 isPartRemoved = true;
617 return isPartRemoved;
622 void PartSet_Module::onFeatureTriggered()
624 SessionPtr aMgr = ModelAPI_Session::get();
625 // 1. check whether the delete should be processed in the module
626 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
627 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
629 // in case if in the viewer nothing is displayed, the create operation should not be
630 // comitted even if all values of the feature are initialized
631 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
632 QAction* aCmd = dynamic_cast<QAction*>(sender());
633 //Do nothing on uncheck
634 if (aCmd->isCheckable() && !aCmd->isChecked())
637 // the action information should be saved before the operation is aborted
638 // because this abort leads to update command status, which unchecks this action
639 anOperation->abort();
641 launchOperation(aCmd->data().toString());
644 ModuleBase_IModule::onFeatureTriggered();
647 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
649 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
650 if (!anAIS.IsNull()) {
651 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
652 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
653 if (!aDim.IsNull()) {
654 aCtx->SetZLayer(aDim, myVisualLayerId);
656 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
658 aCtx->SetZLayer(aCons, myVisualLayerId);
663 void PartSet_Module::onViewTransformed(int theTrsfType)
665 // Set length of arrows constant in pixel size
666 // if the operation is panning or rotate or panglobal then do nothing
667 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
670 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
671 //Handle(V3d_View) aView = aViewer->activeView();
673 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
674 XGUI_Workshop* aWorkshop = aConnector->workshop();
675 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
676 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
678 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
679 Handle(V3d_View) aView;
681 for (aV3dViewer->InitDefinedViews();
682 aV3dViewer->MoreDefinedViews();
683 aV3dViewer->NextDefinedViews()) {
684 Handle(V3d_View) aV = aV3dViewer->DefinedView();
685 double aS = aV->Scale();
693 double aLen = aView->Convert(20);
695 double aPrevLen = SketcherPrs_Tools::getArrowSize();
696 SketcherPrs_Tools::setArrowSize(aLen);
697 const double aPrevScale = aViewer->Scale(aViewer->activeView());
698 const double aCurScale = aViewer->activeView()->Camera()->Scale();
699 aViewer->SetScale(aViewer->activeView(), aCurScale);
700 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
701 bool isModified = false;
702 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
703 foreach (AISObjectPtr aAIS, aPrsList) {
704 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
706 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
707 if (!aDim.IsNull()) {
708 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
709 aContext->Redisplay(aDim, false);
714 aDisplayer->updateViewer();
718 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
720 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
722 QLineEdit* aLabel = aOB->activeDocLabel();
723 QPalette aPalet = aLabel->palette();
724 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
725 aLabel->setPalette(aPalet);
726 aOB->treeView()->setExpandsOnDoubleClick(false);
727 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
728 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
729 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
730 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
731 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
732 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
737 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
739 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
740 int aSelected = aObjects.size();
741 SessionPtr aMgr = ModelAPI_Session::get();
742 if (aSelected == 1) {
743 bool hasResult = false;
744 bool hasFeature = false;
745 bool hasParameter = false;
746 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
748 ObjectPtr aObject = aObjects.first();
750 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
751 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
752 bool isPart = aPart.get() ||
753 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
755 DocumentPtr aPartDoc;
757 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
759 if (aPart.get()) // this may be null is Part feature is disabled
760 aPartDoc = aPart->partDoc();
761 if (aMgr->activeDocument() == aPartDoc)
762 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
764 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
765 } else if (aObject->document() == aMgr->activeDocument()) {
766 if (hasParameter || hasFeature)
767 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
769 } else { // If feature is 0 the it means that selected root object (document)
770 if (aMgr->activeDocument() != aMgr->moduleDocument())
771 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
773 } else if (aSelected == 0) {
774 // if there is no selection then it means that upper label is selected
775 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
776 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
777 if (aMgr->activeDocument() != aMgr->moduleDocument())
778 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
782 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
784 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
785 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
786 XGUI_Workshop* aWorkshop = aConnector->workshop();
787 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
788 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
789 QPalette aPalet = aLabel->palette();
791 SessionPtr aMgr = ModelAPI_Session::get();
792 DocumentPtr aActiveDoc = aMgr->activeDocument();
793 DocumentPtr aDoc = aMgr->moduleDocument();
794 QModelIndex aOldIndex = myDataModel->activePartTree();
795 if (aActiveDoc == aDoc) {
796 if (aOldIndex.isValid())
797 aTreeView->setExpanded(aOldIndex, false);
798 myDataModel->deactivatePart();
799 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
801 std::string aGrpName = ModelAPI_ResultPart::group();
802 for (int i = 0; i < aDoc->size(aGrpName); i++) {
803 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
804 if (aPart->partDoc() == aActiveDoc) {
805 QModelIndex aIndex = myDataModel->partIndex(aPart);
806 if (myDataModel->activatePart(aIndex)) {
807 if (aOldIndex.isValid())
808 aTreeView->setExpanded(aOldIndex, false);
809 aTreeView->setExpanded(myDataModel->activePartTree(), true);
810 aPalet.setColor(QPalette::Text, Qt::black);
816 aLabel->setPalette(aPalet);
817 aWorkshop->updateCommandStatus();
819 // Update displayed objects in order to update active color
820 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
821 QObjectPtrList aObjects = aDisplayer->displayedObjects();
822 foreach(ObjectPtr aObj, aObjects)
823 aDisplayer->redisplay(aObj, false);
824 aDisplayer->updateViewer();
828 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
830 SessionPtr aMgr = ModelAPI_Session::get();
831 if (!theIndex.isValid()) {
832 aMgr->setActiveDocument(aMgr->moduleDocument());
835 if (theIndex.column() != 0) // Use only first column
837 ObjectPtr aObj = myDataModel->object(theIndex);
838 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
839 if (!aPart.get()) { // Probably this is Feature
840 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
841 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
842 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
845 if (aPart.get()) { // if this is a part
846 if (aPart->partDoc() == aMgr->activeDocument()) {
847 aMgr->setActiveDocument(aMgr->moduleDocument());