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 <PartSetPlugin_Remove.h>
19 #include <PartSetPlugin_Part.h>
21 #include <ModuleBase_Operation.h>
22 #include <ModuleBase_IViewer.h>
23 #include <ModuleBase_IViewWindow.h>
24 #include <ModuleBase_IPropertyPanel.h>
25 #include <ModuleBase_WidgetEditor.h>
26 #include <ModuleBase_FilterFactory.h>
27 #include <ModuleBase_Tools.h>
28 #include <GeomValidators_ShapeType.h>
30 #include <GeomValidators_Face.h>
31 #include <GeomValidators_ConstructionComposite.h>
32 #include <GeomValidators_ZeroOffset.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Data.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_ShapeValidator.h>
42 #include <GeomDataAPI_Point2D.h>
43 #include <GeomDataAPI_Point.h>
44 #include <GeomDataAPI_Dir.h>
46 #include <XGUI_Displayer.h>
47 #include <XGUI_Workshop.h>
48 #include <XGUI_OperationMgr.h>
49 #include <XGUI_PropertyPanel.h>
50 #include <XGUI_ModuleConnector.h>
51 #include <XGUI_ContextMenuMgr.h>
52 #include <XGUI_Tools.h>
53 #include <XGUI_ObjectsBrowser.h>
55 #include <SketchPlugin_Feature.h>
56 #include <SketchPlugin_Sketch.h>
57 #include <SketchPlugin_Line.h>
58 #include <SketchPlugin_Arc.h>
59 #include <SketchPlugin_Circle.h>
60 #include <SketchPlugin_Point.h>
61 #include <SketchPlugin_ConstraintLength.h>
62 #include <SketchPlugin_ConstraintDistance.h>
63 #include <SketchPlugin_ConstraintParallel.h>
64 #include <SketchPlugin_ConstraintPerpendicular.h>
65 #include <SketchPlugin_ConstraintRadius.h>
67 #include <SketcherPrs_SymbolPrs.h>
68 #include <SketcherPrs_Tools.h>
70 #include <Events_Loop.h>
71 #include <Config_PropManager.h>
72 #include <Config_Keywords.h>
74 #include <StdSelect_TypeOfFace.hxx>
75 #include <TopoDS_Vertex.hxx>
77 #include <TopoDS_Shape.hxx>
78 #include <BRep_Tool.hxx>
79 #include <AIS_Dimension.hxx>
82 #include <QMouseEvent>
85 #include <QApplication>
86 #include <QMessageBox>
87 #include <QMainWindow>
90 #include <GeomAlgoAPI_FaceBuilder.h>
91 #include <GeomDataAPI_Dir.h>
99 /*!Create and return new instance of XGUI_Module*/
100 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
102 return new PartSet_Module(theWshop);
105 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
106 : ModuleBase_IModule(theWshop),
107 myRestartingMode(RM_None), myVisualLayerId(0)
109 mySketchMgr = new PartSet_SketcherMgr(this);
110 myDataModel = new PartSet_DocumentDataModel(this);
112 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
113 XGUI_Workshop* aWorkshop = aConnector->workshop();
115 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
116 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
117 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
118 this, SLOT(onOperationActivatedByPreselection()));
120 ModuleBase_IViewer* aViewer = theWshop->viewer();
121 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
122 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
123 connect(aViewer, SIGNAL(viewTransformed(int)),
124 SLOT(onViewTransformed(int)));
126 myMenuMgr = new PartSet_MenuMgr(this);
128 Events_Loop* aLoop = Events_Loop::loop();
129 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
131 if (myDocumentShapeFilter.IsNull())
132 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
133 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
135 if (myFilterInfinite.IsNull())
136 myFilterInfinite = new PartSet_FilterInfinite();
137 myWorkshop->viewer()->addSelectionFilter(myFilterInfinite);
140 PartSet_Module::~PartSet_Module()
142 if (!myDocumentShapeFilter.IsNull())
143 myDocumentShapeFilter.Nullify();
144 if (!myFilterInfinite.IsNull())
145 myFilterInfinite.Nullify();
148 void PartSet_Module::registerValidators()
150 //Registering of validators
151 SessionPtr aMgr = ModelAPI_Session::get();
152 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
153 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
154 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
155 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
156 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
157 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
158 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
159 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
160 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
161 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
162 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
164 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
165 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
167 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
168 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
170 aFactory->registerValidator("GeomValidators_ConstructionComposite",
171 new GeomValidators_ConstructionComposite);
173 aFactory->registerValidator("GeomValidators_ZeroOffset",
174 new GeomValidators_ZeroOffset);
176 aFactory->registerValidator("PartSet_SketchEntityValidator",
177 new PartSet_SketchEntityValidator);
179 aFactory->registerValidator("PartSet_SameTypeAttr",
180 new PartSet_SameTypeAttrValidator);
183 void PartSet_Module::registerFilters()
185 //Registering of selection filters
186 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
187 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
190 void PartSet_Module::registerProperties()
192 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
194 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
195 Config_Prop::Integer, SKETCH_WIDTH);
198 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
200 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
201 mySketchMgr->commitNestedSketch(theOperation);
204 if (theOperation->isEditOperation())
206 // the selection is cleared after commit the create operation
207 // in order to do not use the same selected objects in the restarted operation
208 // for common behaviour, the selection is cleared even if the operation is not restarted
209 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
210 if (!aContext.IsNull())
211 aContext->ClearSelected();
213 /// Restart sketcher operations automatically
214 FeaturePtr aFeature = theOperation->feature();
215 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
216 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
217 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
218 myRestartingMode == RM_EmptyFeatureUsed)) {
219 myLastOperationId = theOperation->id();
220 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
222 launchOperation(myLastOperationId);
224 breakOperationSequence();
227 void PartSet_Module::breakOperationSequence()
229 myLastOperationId = "";
230 myLastFeature = FeaturePtr();
231 myRestartingMode = RM_None;
234 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
236 breakOperationSequence();
239 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
241 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
242 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
243 aViewer->AddZLayer(myVisualLayerId);
244 mySketchMgr->startSketch(theOperation);
246 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
247 mySketchMgr->startNestedSketch(theOperation);
251 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
253 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
254 mySketchMgr->stopSketch(theOperation);
255 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
256 aViewer->RemoveZLayer(myVisualLayerId);
259 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
260 mySketchMgr->stopNestedSketch(theOperation);
264 ModuleBase_Operation* PartSet_Module::currentOperation() const
266 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
267 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
268 return anOpMgr->currentOperation();
271 bool PartSet_Module::canUndo() const
273 bool aCanUndo = false;
274 SessionPtr aMgr = ModelAPI_Session::get();
275 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
276 aCanUndo = !aMgr->isOperation();
277 if (!aCanUndo) // check the enable state additionally by sketch manager
278 aCanUndo = aMgr->canUndo();
283 bool PartSet_Module::canRedo() const
285 bool aCanRedo = false;
286 SessionPtr aMgr = ModelAPI_Session::get();
287 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
288 aCanRedo = !aMgr->isOperation();
289 if (!aCanRedo) // check the enable state additionally by sketch manager
290 aCanRedo = aMgr->canRedo();
295 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
297 // the sketch manager put the restriction to the objects display
298 return mySketchMgr->canDisplayObject(theObject);
301 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
303 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
305 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
306 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
307 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
308 if (isSketchOp || isNestedOp) {
309 // in active sketch operation it is possible to activate operation object in selection
310 // in the edit operation, e.g. points of the line can be moved when the line is edited
311 aCanActivate = aCanActivate || anOperation->isEditOperation();
316 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
318 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
321 void PartSet_Module::activeSelectionModes(QIntList& theModes)
324 if (mySketchMgr->activeSketch().get())
325 PartSet_SketcherMgr::sketchSelectionModes(theModes);
328 bool PartSet_Module::isMouseOverWindow()
330 return mySketchMgr->isMouseOverWindow();
333 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
335 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
336 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
337 // we have to manually activate the sketch label in edit mode
338 aPanel->activateWidget(aPanel->modelWidgets().first());
342 // Restart last operation type
343 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
344 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
345 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
346 // Initialise new line with first point equal to end of previous
347 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
349 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
350 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
351 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
353 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
354 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
355 aWgt->attributeID(), aPoint->x(), aPoint->y());
356 aPanel->activateNextWidget(aPnt2dWgt);
361 // Start editing constraint
362 if (theOperation->isEditOperation()) {
363 // TODO: #391 - to be removed
364 std::string aId = theOperation->id().toStdString();
365 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
366 PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
367 // Find and activate widget for management of point for dimension line position
368 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
369 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
370 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
372 aPanel->activateWidget(aPntWgt);
382 void PartSet_Module::onSelectionChanged()
384 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
388 bool isSketcherOp = false;
389 // An edit operation is enable only if the current opeation is the sketch operation
390 if (mySketchMgr->activeSketch()) {
391 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
392 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
395 // Editing of constraints can be done on selection
396 ModuleBase_ISelection* aSelect = myWorkshop->selection();
397 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
398 if (aSelected.size() == 1) {
399 ModuleBase_ViewerPrs aPrs = aSelected.first();
400 ObjectPtr aObject = aPrs.object();
401 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
403 std::string aId = aFeature->getKind();
404 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
405 (aId == SketchPlugin_ConstraintLength::ID()) ||
406 (aId == SketchPlugin_ConstraintDistance::ID())) {
407 editFeature(aFeature);
414 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
416 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
417 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
418 anOpMgr->onKeyReleased(theEvent);
421 void PartSet_Module::onEnterReleased()
423 myRestartingMode = RM_EmptyFeatureUsed;
426 void PartSet_Module::onOperationActivatedByPreselection()
428 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
429 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
430 // Set final definitions if they are necessary
431 //propertyPanelDefined(aOperation);
433 /// Commit sketcher operations automatically
434 anOperation->commit();
438 void PartSet_Module::onNoMoreWidgets()
440 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
442 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
443 if (myRestartingMode != RM_Forbided)
444 myRestartingMode = RM_LastFeatureUsed;
445 anOperation->commit();
450 void PartSet_Module::onVertexSelected()
452 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
453 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
454 /// If last line finished on vertex the lines creation sequence has to be break
455 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
456 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
457 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
458 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
459 bool aFoundWidget = false;
460 bool aFoundObligatory = false;
461 for (; anIt != aLast && !aFoundObligatory; anIt++) {
463 aFoundWidget = *anIt == anActiveWidget;
465 aFoundObligatory = (*anIt)->isObligatory();
467 if (!aFoundObligatory)
468 myRestartingMode = RM_Forbided;
472 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
473 Config_WidgetAPI* theWidgetApi, std::string theParentId)
475 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
476 XGUI_Workshop* aWorkshop = aConnector->workshop();
477 ModuleBase_ModelWidget* aWgt = NULL;
478 if (theType == "sketch-start-label") {
479 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent,
480 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
481 aLabelWgt->setWorkshop(aWorkshop);
482 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
483 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
484 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
485 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
487 } else if (theType == "sketch-2dpoint_selector") {
488 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
489 aPointWgt->setWorkshop(aWorkshop);
490 aPointWgt->setSketch(mySketchMgr->activeSketch());
491 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
493 } else if (theType == "point2ddistance") {
494 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
495 aDistanceWgt->setWorkshop(aWorkshop);
496 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
498 } else if(theType == "point2dangle") {
499 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent, theWidgetApi, theParentId);
500 anAngleWgt->setWorkshop(aWorkshop);
501 anAngleWgt->setSketch(mySketchMgr->activeSketch());
503 } else if (theType == "sketch_shape_selector") {
504 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
505 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
506 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
507 aWgt = aShapeSelectorWgt;
508 } else if (theType == "sketch_multi_selector") {
509 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
510 new PartSet_WidgetMultiSelector(theParent, workshop(), theWidgetApi, theParentId);
511 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
512 aWgt = aShapeSelectorWgt;
513 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
514 aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
515 } else if (theType == "export_file_selector") {
516 aWgt = new PartSet_WidgetFileSelector(theParent, workshop(), theWidgetApi, theParentId);
517 } else if (theType == "sketch_launcher") {
518 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
524 bool PartSet_Module::deleteObjects()
526 SessionPtr aMgr = ModelAPI_Session::get();
527 // 1. check whether the delete should be processed in the module
528 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
529 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
530 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
531 if (isSketchOp || isNestedOp) {
532 // 2. find selected presentations
533 // selected objects should be collected before the current operation abort because
534 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
535 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
536 XGUI_Workshop* aWorkshop = aConnector->workshop();
537 ModuleBase_ISelection* aSel = workshop()->selection();
538 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
539 // if there are no selected objects in the viewer, that means that the selection in another
540 // place cased this method. It is necessary to return the false value to understande in above
541 // method that delete is not processed
542 if (aSelectedObj.count() == 0)
545 // avoid delete of the objects, which are not belong to the current sketch
546 // in order to do not delete results of other sketches
547 QObjectPtrList aSketchObjects;
548 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
549 for ( ; anIt != aLast; anIt++) {
550 ObjectPtr anObject = *anIt;
551 if (mySketchMgr->isObjectOfSketch(anObject)) {
552 // sketch feature should be used in this list because workshop deletes features only
553 // results are skipped
554 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
555 aSketchObjects.append(aSketchFeature);
558 // if the selection contains only local selected presentations from other sketches,
559 // the Delete operation should not be done at all
560 if (aSketchObjects.size() == 0)
563 // the active nested sketch operation should be aborted unconditionally
565 anOperation->abort();
567 // 3. start operation
568 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
569 aMgr->startOperation(aDescription.toStdString());
571 // 4. delete features
572 // sketch feature should be skipped, only sub-features can be removed
573 // when sketch operation is active
574 std::set<FeaturePtr> anIgnoredFeatures;
575 anIgnoredFeatures.insert(mySketchMgr->activeSketch());
576 aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
579 aWorkshop->displayer()->updateViewer();
580 aMgr->finishOperation();
582 bool isPartRemoved = false;
583 // Delete part with help of PartSet plugin
584 // TODO: the deleted objects has to be processed by multiselection
585 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
586 if (aObjects.size() == 1) {
587 ObjectPtr aObj = aObjects.first();
588 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
589 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
590 // Remove feature should be created in the document of the part results
591 ResultPtr aPartResult = aFeature->firstResult();
592 if (aPartResult.get()) {
593 std::shared_ptr<ModelAPI_ResultPart> aPart =
594 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
595 DocumentPtr aPartDoc = aPart->partDoc();
596 if (aPartDoc.get()) {
597 aMgr->startOperation(PartSetPlugin_Remove::ID());
598 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
600 aMgr->finishOperation();
601 isPartRemoved = true;
606 return isPartRemoved;
611 void PartSet_Module::onFeatureTriggered()
613 SessionPtr aMgr = ModelAPI_Session::get();
614 // 1. check whether the delete should be processed in the module
615 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
616 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
618 // in case if in the viewer nothing is displayed, the create operation should not be
619 // comitted even if all values of the feature are initialized
620 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
621 QAction* aCmd = dynamic_cast<QAction*>(sender());
622 //Do nothing on uncheck
623 if (aCmd->isCheckable() && !aCmd->isChecked())
626 // the action information should be saved before the operation is aborted
627 // because this abort leads to update command status, which unchecks this action
628 anOperation->abort();
630 launchOperation(aCmd->data().toString());
633 ModuleBase_IModule::onFeatureTriggered();
636 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
638 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
639 if (!anAIS.IsNull()) {
640 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
641 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
642 if (!aDim.IsNull()) {
643 aCtx->SetZLayer(aDim, myVisualLayerId);
645 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
647 aCtx->SetZLayer(aCons, myVisualLayerId);
652 void PartSet_Module::onViewTransformed(int theTrsfType)
654 // Set length of arrows constant in pixel size
655 // if the operation is panning or rotate or panglobal then do nothing
656 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
659 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
660 //Handle(V3d_View) aView = aViewer->activeView();
662 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
663 XGUI_Workshop* aWorkshop = aConnector->workshop();
664 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
665 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
667 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
668 Handle(V3d_View) aView;
670 for (aV3dViewer->InitDefinedViews();
671 aV3dViewer->MoreDefinedViews();
672 aV3dViewer->NextDefinedViews()) {
673 Handle(V3d_View) aV = aV3dViewer->DefinedView();
674 double aS = aV->Scale();
682 double aLen = aView->Convert(20);
684 double aPrevLen = SketcherPrs_Tools::getArrowSize();
685 SketcherPrs_Tools::setArrowSize(aLen);
686 const double aPrevScale = aViewer->Scale(aViewer->activeView());
687 const double aCurScale = aViewer->activeView()->Camera()->Scale();
688 aViewer->SetScale(aViewer->activeView(), aCurScale);
689 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
690 bool isModified = false;
691 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
692 foreach (AISObjectPtr aAIS, aPrsList) {
693 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
695 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
696 if (!aDim.IsNull()) {
697 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
698 aContext->Redisplay(aDim, false);
703 aDisplayer->updateViewer();
707 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
709 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
711 QLineEdit* aLabel = aOB->activeDocLabel();
712 QPalette aPalet = aLabel->palette();
713 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
714 aLabel->setPalette(aPalet);
715 aOB->treeView()->setExpandsOnDoubleClick(false);
716 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
717 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
718 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
719 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
720 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
721 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
726 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
728 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
729 int aSelected = aObjects.size();
730 SessionPtr aMgr = ModelAPI_Session::get();
731 if (aSelected == 1) {
732 bool hasResult = false;
733 bool hasFeature = false;
734 bool hasParameter = false;
735 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
737 ObjectPtr aObject = aObjects.first();
739 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
740 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
741 bool isPart = aPart.get() ||
742 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
744 DocumentPtr aPartDoc;
746 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
748 if (aPart.get()) // this may be null is Part feature is disabled
749 aPartDoc = aPart->partDoc();
750 if (aMgr->activeDocument() == aPartDoc)
751 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
753 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
754 } else if (aObject->document() == aMgr->activeDocument()) {
755 if (hasParameter || hasFeature)
756 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
758 } else { // If feature is 0 the it means that selected root object (document)
759 if (aMgr->activeDocument() != aMgr->moduleDocument())
760 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
762 } else if (aSelected == 0) {
763 // if there is no selection then it means that upper label is selected
764 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
765 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
766 if (aMgr->activeDocument() != aMgr->moduleDocument())
767 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
771 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
773 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
774 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
775 XGUI_Workshop* aWorkshop = aConnector->workshop();
776 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
777 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
778 QPalette aPalet = aLabel->palette();
780 SessionPtr aMgr = ModelAPI_Session::get();
781 DocumentPtr aActiveDoc = aMgr->activeDocument();
782 DocumentPtr aDoc = aMgr->moduleDocument();
783 QModelIndex aOldIndex = myDataModel->activePartTree();
784 if (aActiveDoc == aDoc) {
785 if (aOldIndex.isValid())
786 aTreeView->setExpanded(aOldIndex, false);
787 myDataModel->deactivatePart();
788 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
790 std::string aGrpName = ModelAPI_ResultPart::group();
791 for (int i = 0; i < aDoc->size(aGrpName); i++) {
792 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
793 if (aPart->partDoc() == aActiveDoc) {
794 QModelIndex aIndex = myDataModel->partIndex(aPart);
795 if (myDataModel->activatePart(aIndex)) {
796 if (aOldIndex.isValid())
797 aTreeView->setExpanded(aOldIndex, false);
798 aTreeView->setExpanded(myDataModel->activePartTree(), true);
799 aPalet.setColor(QPalette::Text, Qt::black);
805 aLabel->setPalette(aPalet);
806 aWorkshop->updateCommandStatus();
808 // Update displayed objects in order to update active color
809 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
810 QObjectPtrList aObjects = aDisplayer->displayedObjects();
811 foreach(ObjectPtr aObj, aObjects)
812 aDisplayer->redisplay(aObj, false);
813 aDisplayer->updateViewer();
817 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
819 SessionPtr aMgr = ModelAPI_Session::get();
820 if (!theIndex.isValid()) {
821 aMgr->setActiveDocument(aMgr->moduleDocument());
824 if (theIndex.column() != 0) // Use only first column
826 ObjectPtr aObj = myDataModel->object(theIndex);
827 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
828 if (!aPart.get()) { // Probably this is Feature
829 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
830 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
831 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
834 if (aPart.get()) { // if this is a part
835 if (aPart->partDoc() == aMgr->activeDocument()) {
836 aMgr->setActiveDocument(aMgr->moduleDocument());