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 "ModuleBase_WidgetValidated.h"
8 #include "PartSet_WidgetPoint2d.h"
9 #include "PartSet_WidgetPoint2dDistance.h"
10 #include "PartSet_WidgetShapeSelector.h"
11 #include "PartSet_WidgetPoint2dAngle.h"
12 #include "PartSet_WidgetMultiSelector.h"
13 #include "PartSet_WidgetEditor.h"
14 #include "PartSet_WidgetFileSelector.h"
15 #include "PartSet_WidgetSketchCreator.h"
16 #include "PartSet_SketcherMgr.h"
17 #include "PartSet_MenuMgr.h"
18 #include <PartSet_CustomPrs.h>
20 #include "PartSet_Filters.h"
21 #include "PartSet_FilterInfinite.h"
23 #include <PartSetPlugin_Remove.h>
24 #include <PartSetPlugin_Part.h>
26 #include <ModuleBase_Operation.h>
27 #include <ModuleBase_IViewer.h>
28 #include <ModuleBase_IViewWindow.h>
29 #include <ModuleBase_IPropertyPanel.h>
30 #include <ModuleBase_WidgetEditor.h>
31 #include <ModuleBase_FilterFactory.h>
32 #include <ModuleBase_Tools.h>
33 #include <GeomValidators_ShapeType.h>
35 #include <GeomValidators_Face.h>
36 #include <GeomValidators_ConstructionComposite.h>
37 #include <GeomValidators_ZeroOffset.h>
38 #include <GeomValidators_BooleanArguments.h>
41 #include <ModelAPI_Object.h>
42 #include <ModelAPI_Events.h>
43 #include <ModelAPI_Validator.h>
44 #include <ModelAPI_Data.h>
45 #include <ModelAPI_Session.h>
46 #include <ModelAPI_ShapeValidator.h>
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Dir.h>
52 #include <XGUI_Displayer.h>
53 #include <XGUI_Workshop.h>
54 #include <XGUI_OperationMgr.h>
55 #include <XGUI_PropertyPanel.h>
56 #include <XGUI_ModuleConnector.h>
57 #include <XGUI_ContextMenuMgr.h>
58 #include <XGUI_Tools.h>
59 #include <XGUI_ObjectsBrowser.h>
60 #include <XGUI_SelectionMgr.h>
62 #include <SketchPlugin_Feature.h>
63 #include <SketchPlugin_Sketch.h>
64 #include <SketchPlugin_Line.h>
65 #include <SketchPlugin_Arc.h>
66 #include <SketchPlugin_Circle.h>
67 #include <SketchPlugin_Point.h>
68 #include <SketchPlugin_ConstraintLength.h>
69 #include <SketchPlugin_ConstraintDistance.h>
70 #include <SketchPlugin_ConstraintParallel.h>
71 #include <SketchPlugin_ConstraintPerpendicular.h>
72 #include <SketchPlugin_ConstraintRadius.h>
74 #include <SketcherPrs_SymbolPrs.h>
75 #include <SketcherPrs_Tools.h>
77 #include <Events_Loop.h>
78 #include <Config_PropManager.h>
79 #include <Config_Keywords.h>
81 #include <StdSelect_TypeOfFace.hxx>
82 #include <TopoDS_Vertex.hxx>
84 #include <TopoDS_Shape.hxx>
85 #include <BRep_Tool.hxx>
86 #include <AIS_Dimension.hxx>
89 #include <QMouseEvent>
92 #include <QApplication>
93 #include <QMessageBox>
94 #include <QMainWindow>
97 #include <GeomAlgoAPI_FaceBuilder.h>
98 #include <GeomDataAPI_Dir.h>
100 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
108 /*!Create and return new instance of XGUI_Module*/
109 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
111 return new PartSet_Module(theWshop);
114 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
115 : ModuleBase_IModule(theWshop),
116 myRestartingMode(RM_None), myVisualLayerId(0)
118 mySketchMgr = new PartSet_SketcherMgr(this);
119 myDataModel = new PartSet_DocumentDataModel(this);
121 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
122 XGUI_Workshop* aWorkshop = aConnector->workshop();
124 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
125 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
126 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
127 this, SLOT(onOperationActivatedByPreselection()));
129 ModuleBase_IViewer* aViewer = theWshop->viewer();
130 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
131 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
132 connect(aViewer, SIGNAL(viewTransformed(int)),
133 SLOT(onViewTransformed(int)));
135 myMenuMgr = new PartSet_MenuMgr(this);
136 myCustomPrs = new PartSet_CustomPrs(theWshop);
138 Events_Loop* aLoop = Events_Loop::loop();
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
141 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
142 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
145 PartSet_Module::~PartSet_Module()
147 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
148 for (; aIt.More(); aIt.Next()) {
149 Handle(SelectMgr_Filter) aFilter = aIt.Value();
150 if (!aFilter.IsNull())
155 void PartSet_Module::activateSelectionFilters()
157 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
158 for (; aIt.More(); aIt.Next()) {
159 Handle(SelectMgr_Filter) aFilter = aIt.Value();
160 if (!aFilter.IsNull())
161 myWorkshop->viewer()->addSelectionFilter(aFilter);
165 void PartSet_Module::deactivateSelectionFilters()
167 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
168 for (; aIt.More(); aIt.Next()) {
169 Handle(SelectMgr_Filter) aFilter = aIt.Value();
170 if (!aFilter.IsNull())
171 myWorkshop->viewer()->removeSelectionFilter(aFilter);
175 void PartSet_Module::registerValidators()
177 //Registering of validators
178 SessionPtr aMgr = ModelAPI_Session::get();
179 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
180 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
181 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
182 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
183 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
184 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
185 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
186 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
187 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
188 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
189 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
191 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
192 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
194 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
195 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
197 aFactory->registerValidator("GeomValidators_ConstructionComposite",
198 new GeomValidators_ConstructionComposite);
200 aFactory->registerValidator("GeomValidators_ZeroOffset",
201 new GeomValidators_ZeroOffset);
203 aFactory->registerValidator("GeomValidators_BooleanArguments",
204 new GeomValidators_BooleanArguments);
206 aFactory->registerValidator("PartSet_SketchEntityValidator",
207 new PartSet_SketchEntityValidator);
209 aFactory->registerValidator("PartSet_SameTypeAttr",
210 new PartSet_SameTypeAttrValidator);
213 void PartSet_Module::registerFilters()
215 //Registering of selection filters
216 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
217 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
220 void PartSet_Module::registerProperties()
222 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
224 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
225 Config_Prop::Integer, SKETCH_WIDTH);
228 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
230 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
231 mySketchMgr->commitNestedSketch(theOperation);
234 if (theOperation->isEditOperation())
236 // the selection is cleared after commit the create operation
237 // in order to do not use the same selected objects in the restarted operation
238 // for common behaviour, the selection is cleared even if the operation is not restarted
239 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
240 XGUI_Workshop* aWorkshop = aConnector->workshop();
241 aWorkshop->selector()->clearSelection();
243 /// Restart sketcher operations automatically
244 FeaturePtr aFeature = theOperation->feature();
245 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
246 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
247 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
248 myRestartingMode == RM_EmptyFeatureUsed)) {
249 myLastOperationId = theOperation->id();
250 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
252 launchOperation(myLastOperationId);
254 breakOperationSequence();
257 void PartSet_Module::breakOperationSequence()
259 myLastOperationId = "";
260 myLastFeature = FeaturePtr();
261 myRestartingMode = RM_None;
264 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
266 breakOperationSequence();
269 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
271 // z layer is created for all started operations in order to visualize operation AIS presentation
273 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
274 aViewer->AddZLayer(myVisualLayerId);
276 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
277 mySketchMgr->startSketch(theOperation);
279 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
280 mySketchMgr->startNestedSketch(theOperation);
283 myCustomPrs->activate(theOperation->feature());
286 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
288 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
289 mySketchMgr->stopSketch(theOperation);
291 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
292 mySketchMgr->stopNestedSketch(theOperation);
294 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
295 aViewer->RemoveZLayer(myVisualLayerId);
297 myCustomPrs->deactivate();
300 ModuleBase_Operation* PartSet_Module::currentOperation() const
302 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
303 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
304 return anOpMgr->currentOperation();
307 bool PartSet_Module::canUndo() const
309 bool aCanUndo = false;
310 SessionPtr aMgr = ModelAPI_Session::get();
311 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
312 aCanUndo = !aMgr->isOperation();
313 if (!aCanUndo) // check the enable state additionally by sketch manager
314 aCanUndo = aMgr->canUndo();
319 bool PartSet_Module::canRedo() const
321 bool aCanRedo = false;
322 SessionPtr aMgr = ModelAPI_Session::get();
323 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
324 aCanRedo = !aMgr->isOperation();
325 if (!aCanRedo) // check the enable state additionally by sketch manager
326 aCanRedo = aMgr->canRedo();
331 bool PartSet_Module::canCommitOperation() const
333 return mySketchMgr->canCommitOperation();
336 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
338 // the sketch manager put the restriction to the objects display
339 return mySketchMgr->canDisplayObject(theObject);
342 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
344 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
346 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
347 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
348 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
349 if (isSketchOp || isNestedOp) {
350 // in active sketch operation it is possible to activate operation object in selection
351 // in the edit operation, e.g. points of the line can be moved when the line is edited
352 aCanActivate = aCanActivate || anOperation->isEditOperation();
357 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
359 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
362 void PartSet_Module::activeSelectionModes(QIntList& theModes)
365 if (mySketchMgr->activeSketch().get())
366 PartSet_SketcherMgr::sketchSelectionModes(theModes);
369 bool PartSet_Module::isMouseOverWindow()
371 return mySketchMgr->isMouseOverWindow();
374 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
376 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
377 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
378 // we have to manually activate the sketch label in edit mode
379 aPanel->activateWidget(aPanel->modelWidgets().first());
383 // Restart last operation type
384 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
385 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
386 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
387 // Initialise new line with first point equal to end of previous
388 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
390 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
391 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
392 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
394 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
395 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
396 aWgt->attributeID(), aPoint->x(), aPoint->y());
397 aPanel->activateNextWidget(aPnt2dWgt);
405 void PartSet_Module::onSelectionChanged()
407 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
411 bool isSketcherOp = false;
412 // An edit operation is enable only if the current opeation is the sketch operation
413 if (mySketchMgr->activeSketch()) {
414 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
415 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
418 // Editing of constraints can be done on selection
419 ModuleBase_ISelection* aSelect = myWorkshop->selection();
420 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
421 if (aSelected.size() == 1) {
422 ModuleBase_ViewerPrs aPrs = aSelected.first();
423 ObjectPtr aObject = aPrs.object();
424 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
426 std::string aId = aFeature->getKind();
427 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
428 (aId == SketchPlugin_ConstraintLength::ID()) ||
429 (aId == SketchPlugin_ConstraintDistance::ID())) {
430 editFeature(aFeature);
437 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
439 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
440 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
441 anOpMgr->onKeyReleased(theEvent);
444 void PartSet_Module::onEnterReleased()
446 myRestartingMode = RM_EmptyFeatureUsed;
449 void PartSet_Module::onOperationActivatedByPreselection()
451 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
452 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
453 // Set final definitions if they are necessary
454 //propertyPanelDefined(aOperation);
456 /// Commit sketcher operations automatically
457 anOperation->commit();
461 void PartSet_Module::onNoMoreWidgets()
463 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
465 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
466 if (myRestartingMode != RM_Forbided)
467 myRestartingMode = RM_LastFeatureUsed;
468 anOperation->commit();
473 void PartSet_Module::onVertexSelected()
475 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
476 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
477 /// If last line finished on vertex the lines creation sequence has to be break
478 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
479 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
480 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
481 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
482 bool aFoundWidget = false;
483 bool aFoundObligatory = false;
484 for (; anIt != aLast && !aFoundObligatory; anIt++) {
486 aFoundWidget = *anIt == anActiveWidget;
488 aFoundObligatory = (*anIt)->isObligatory();
490 if (!aFoundObligatory)
491 myRestartingMode = RM_Forbided;
495 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
496 Config_WidgetAPI* theWidgetApi, std::string theParentId)
498 ModuleBase_IWorkshop* aWorkshop = workshop();
499 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
500 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
501 ModuleBase_ModelWidget* aWgt = NULL;
502 if (theType == "sketch-start-label") {
503 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
504 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
505 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
506 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
507 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
508 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
510 } else if (theType == "sketch-2dpoint_selector") {
511 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
512 theWidgetApi, theParentId);
513 aPointWgt->setSketch(mySketchMgr->activeSketch());
514 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
516 } else if (theType == "point2ddistance") {
517 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
518 aWorkshop, theWidgetApi, theParentId);
519 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
521 } else if(theType == "point2dangle") {
522 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
523 aWorkshop, theWidgetApi, theParentId);
524 anAngleWgt->setSketch(mySketchMgr->activeSketch());
526 } else if (theType == "sketch_shape_selector") {
527 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
528 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
529 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
530 aWgt = aShapeSelectorWgt;
531 } else if (theType == "sketch_multi_selector") {
532 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
533 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
534 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
535 aWgt = aShapeSelectorWgt;
536 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
537 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
538 } else if (theType == "export_file_selector") {
539 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
540 } else if (theType == "sketch_launcher") {
541 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
547 bool PartSet_Module::deleteObjects()
549 SessionPtr aMgr = ModelAPI_Session::get();
550 // 1. check whether the delete should be processed in the module
551 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
552 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
553 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
554 if (isSketchOp || isNestedOp) {
555 // 2. find selected presentations
556 // selected objects should be collected before the current operation abort because
557 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
558 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
559 XGUI_Workshop* aWorkshop = aConnector->workshop();
560 ModuleBase_ISelection* aSel = workshop()->selection();
561 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
562 // if there are no selected objects in the viewer, that means that the selection in another
563 // place cased this method. It is necessary to return the false value to understande in above
564 // method that delete is not processed
565 if (aSelectedObj.count() == 0)
568 // avoid delete of the objects, which are not belong to the current sketch
569 // in order to do not delete results of other sketches
570 QObjectPtrList aSketchObjects;
571 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
572 for ( ; anIt != aLast; anIt++) {
573 ObjectPtr anObject = *anIt;
574 if (mySketchMgr->isObjectOfSketch(anObject)) {
575 // sketch feature should be used in this list because workshop deletes features only
576 // results are skipped
577 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
578 aSketchObjects.append(aSketchFeature);
581 // if the selection contains only local selected presentations from other sketches,
582 // the Delete operation should not be done at all
583 if (aSketchObjects.size() == 0)
586 // the active nested sketch operation should be aborted unconditionally
588 anOperation->abort();
590 // 3. start operation
591 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
592 aMgr->startOperation(aDescription.toStdString());
594 // 4. delete features
595 // sketch feature should be skipped, only sub-features can be removed
596 // when sketch operation is active
597 std::set<FeaturePtr> anIgnoredFeatures;
598 anIgnoredFeatures.insert(mySketchMgr->activeSketch());
599 aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
602 aWorkshop->displayer()->updateViewer();
603 aMgr->finishOperation();
605 bool isPartRemoved = false;
606 // Delete part with help of PartSet plugin
607 // TODO: the deleted objects has to be processed by multiselection
608 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
609 if (aObjects.size() == 1) {
610 ObjectPtr aObj = aObjects.first();
611 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
612 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
613 // Remove feature should be created in the document of the part results
614 ResultPtr aPartResult = aFeature->firstResult();
615 if (aPartResult.get()) {
616 std::shared_ptr<ModelAPI_ResultPart> aPart =
617 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
618 DocumentPtr aPartDoc = aPart->partDoc();
619 if (aPartDoc.get()) {
620 aMgr->startOperation(PartSetPlugin_Remove::ID());
621 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
623 aMgr->finishOperation();
624 isPartRemoved = true;
629 return isPartRemoved;
634 void PartSet_Module::onFeatureTriggered()
636 SessionPtr aMgr = ModelAPI_Session::get();
637 // 1. check whether the delete should be processed in the module
638 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
639 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
641 // in case if in the viewer nothing is displayed, the create operation should not be
642 // comitted even if all values of the feature are initialized
643 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
644 QAction* aCmd = dynamic_cast<QAction*>(sender());
645 //Do nothing on uncheck
646 if (aCmd->isCheckable() && !aCmd->isChecked())
649 // the action information should be saved before the operation is aborted
650 // because this abort leads to update command status, which unchecks this action
651 anOperation->abort();
653 launchOperation(aCmd->data().toString());
656 ModuleBase_IModule::onFeatureTriggered();
659 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
661 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
662 if (!anAIS.IsNull()) {
663 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
664 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
665 if (!aDim.IsNull()) {
666 aCtx->SetZLayer(aDim, myVisualLayerId);
668 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
670 aCtx->SetZLayer(aCons, myVisualLayerId);
675 void PartSet_Module::onViewTransformed(int theTrsfType)
677 // Set length of arrows constant in pixel size
678 // if the operation is panning or rotate or panglobal then do nothing
679 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
682 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
683 //Handle(V3d_View) aView = aViewer->activeView();
685 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
686 XGUI_Workshop* aWorkshop = aConnector->workshop();
687 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
688 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
690 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
691 Handle(V3d_View) aView;
693 for (aV3dViewer->InitDefinedViews();
694 aV3dViewer->MoreDefinedViews();
695 aV3dViewer->NextDefinedViews()) {
696 Handle(V3d_View) aV = aV3dViewer->DefinedView();
697 double aS = aV->Scale();
705 double aLen = aView->Convert(20);
707 double aPrevLen = SketcherPrs_Tools::getArrowSize();
708 SketcherPrs_Tools::setArrowSize(aLen);
709 const double aPrevScale = aViewer->Scale(aViewer->activeView());
710 const double aCurScale = aViewer->activeView()->Camera()->Scale();
711 aViewer->SetScale(aViewer->activeView(), aCurScale);
712 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
713 bool isModified = false;
714 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
715 foreach (AISObjectPtr aAIS, aPrsList) {
716 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
718 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
719 if (!aDim.IsNull()) {
720 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
721 aContext->Redisplay(aDim, false);
726 aDisplayer->updateViewer();
729 void PartSet_Module::customizeObject(ObjectPtr theObject)
731 if (myCustomPrs->isActive())
732 myCustomPrs->customize(theObject);
735 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
737 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
739 QLineEdit* aLabel = aOB->activeDocLabel();
740 QPalette aPalet = aLabel->palette();
741 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
742 aLabel->setPalette(aPalet);
743 aOB->treeView()->setExpandsOnDoubleClick(false);
744 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
745 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
746 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
747 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
748 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
749 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
753 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
756 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
758 /// If last line finished on vertex the lines creation sequence has to be break
759 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
760 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
761 // if there is an active widget, find the presented object in it
763 anActiveWidget = aPanel->preselectionWidget();
765 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
767 if (aWidgetValidated)
768 anObject = aWidgetValidated->findPresentedObject(theAIS);
773 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
775 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
776 int aSelected = aObjects.size();
777 SessionPtr aMgr = ModelAPI_Session::get();
778 if (aSelected == 1) {
779 bool hasResult = false;
780 bool hasFeature = false;
781 bool hasParameter = false;
782 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
784 ObjectPtr aObject = aObjects.first();
786 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
787 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
788 bool isPart = aPart.get() ||
789 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
791 DocumentPtr aPartDoc;
793 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
795 if (aPart.get()) // this may be null is Part feature is disabled
796 aPartDoc = aPart->partDoc();
797 if (aMgr->activeDocument() == aPartDoc)
798 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
800 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
801 } else if (aObject->document() == aMgr->activeDocument()) {
802 if (hasParameter || hasFeature)
803 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
805 } else { // If feature is 0 the it means that selected root object (document)
806 if (aMgr->activeDocument() != aMgr->moduleDocument())
807 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
809 } else if (aSelected == 0) {
810 // if there is no selection then it means that upper label is selected
811 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
812 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
813 if (aMgr->activeDocument() != aMgr->moduleDocument())
814 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
816 bool aCanDeactivate = (myWorkshop->currentOperation() == 0);
817 myMenuMgr->action("ACTIVATE_PARTSET_CMD")->setEnabled(aCanDeactivate);
818 myMenuMgr->action("DEACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
819 myMenuMgr->action("ACTIVATE_PART_CMD")->setEnabled(aCanDeactivate);
822 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
824 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
825 // Do not change activation of parts if an operation active
826 if (myWorkshop->currentOperation() && myWorkshop->currentOperation()->id().toStdString() != PartSetPlugin_Part::ID())
828 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
829 XGUI_Workshop* aWorkshop = aConnector->workshop();
830 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
831 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
832 QPalette aPalet = aLabel->palette();
834 SessionPtr aMgr = ModelAPI_Session::get();
835 DocumentPtr aActiveDoc = aMgr->activeDocument();
836 DocumentPtr aDoc = aMgr->moduleDocument();
837 QModelIndex aOldIndex = myDataModel->activePartTree();
838 if (aActiveDoc == aDoc) {
839 if (aOldIndex.isValid())
840 aTreeView->setExpanded(aOldIndex, false);
841 myDataModel->deactivatePart();
842 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
844 std::string aGrpName = ModelAPI_ResultPart::group();
845 for (int i = 0; i < aDoc->size(aGrpName); i++) {
846 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
847 if (aPart->partDoc() == aActiveDoc) {
848 QModelIndex aIndex = myDataModel->partIndex(aPart);
849 if (myDataModel->activatePart(aIndex)) {
850 if (aOldIndex.isValid())
851 aTreeView->setExpanded(aOldIndex, false);
852 aTreeView->setExpanded(myDataModel->activePartTree(), true);
853 aPalet.setColor(QPalette::Text, Qt::black);
859 aLabel->setPalette(aPalet);
860 aWorkshop->updateCommandStatus();
862 // Update displayed objects in order to update active color
863 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
864 QObjectPtrList aObjects = aDisplayer->displayedObjects();
865 foreach(ObjectPtr aObj, aObjects)
866 aDisplayer->redisplay(aObj, false);
867 aDisplayer->updateViewer();
871 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
873 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
875 SessionPtr aMgr = ModelAPI_Session::get();
876 if (!theIndex.isValid()) {
877 aMgr->setActiveDocument(aMgr->moduleDocument());
880 if (theIndex.column() != 0) // Use only first column
882 ObjectPtr aObj = myDataModel->object(theIndex);
883 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
884 if (!aPart.get()) { // Probably this is Feature
885 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
886 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
887 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
890 if (aPart.get()) { // if this is a part
891 if (aPart->partDoc() == aMgr->activeDocument()) {
892 aMgr->setActiveDocument(aMgr->moduleDocument());