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>
58 #include <SketchPlugin_Feature.h>
59 #include <SketchPlugin_Sketch.h>
60 #include <SketchPlugin_Line.h>
61 #include <SketchPlugin_Arc.h>
62 #include <SketchPlugin_Circle.h>
63 #include <SketchPlugin_Point.h>
64 #include <SketchPlugin_ConstraintLength.h>
65 #include <SketchPlugin_ConstraintDistance.h>
66 #include <SketchPlugin_ConstraintParallel.h>
67 #include <SketchPlugin_ConstraintPerpendicular.h>
68 #include <SketchPlugin_ConstraintRadius.h>
70 #include <SketcherPrs_SymbolPrs.h>
71 #include <SketcherPrs_Tools.h>
73 #include <Events_Loop.h>
74 #include <Config_PropManager.h>
75 #include <Config_Keywords.h>
77 #include <StdSelect_TypeOfFace.hxx>
78 #include <TopoDS_Vertex.hxx>
80 #include <TopoDS_Shape.hxx>
81 #include <BRep_Tool.hxx>
82 #include <AIS_Dimension.hxx>
85 #include <QMouseEvent>
88 #include <QApplication>
89 #include <QMessageBox>
90 #include <QMainWindow>
93 #include <GeomAlgoAPI_FaceBuilder.h>
94 #include <GeomDataAPI_Dir.h>
96 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
104 /*!Create and return new instance of XGUI_Module*/
105 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
107 return new PartSet_Module(theWshop);
110 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
111 : ModuleBase_IModule(theWshop),
112 myRestartingMode(RM_None), myVisualLayerId(0)
114 mySketchMgr = new PartSet_SketcherMgr(this);
115 myDataModel = new PartSet_DocumentDataModel(this);
117 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
118 XGUI_Workshop* aWorkshop = aConnector->workshop();
120 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
121 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
122 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
123 this, SLOT(onOperationActivatedByPreselection()));
125 ModuleBase_IViewer* aViewer = theWshop->viewer();
126 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
127 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
128 connect(aViewer, SIGNAL(viewTransformed(int)),
129 SLOT(onViewTransformed(int)));
131 myMenuMgr = new PartSet_MenuMgr(this);
133 Events_Loop* aLoop = Events_Loop::loop();
134 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
136 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
137 mySelectionFilters.Append(new PartSet_FilterInfinite());
140 PartSet_Module::~PartSet_Module()
142 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
143 for (; aIt.More(); aIt.Next()) {
144 Handle(SelectMgr_Filter) aFilter = aIt.Value();
145 if (!aFilter.IsNull())
150 void PartSet_Module::activateSelectionFilters()
152 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
153 for (; aIt.More(); aIt.Next()) {
154 Handle(SelectMgr_Filter) aFilter = aIt.Value();
155 if (!aFilter.IsNull())
156 myWorkshop->viewer()->addSelectionFilter(aFilter);
160 void PartSet_Module::deactivateSelectionFilters()
162 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
163 for (; aIt.More(); aIt.Next()) {
164 Handle(SelectMgr_Filter) aFilter = aIt.Value();
165 if (!aFilter.IsNull())
166 myWorkshop->viewer()->removeSelectionFilter(aFilter);
170 void PartSet_Module::registerValidators()
172 //Registering of validators
173 SessionPtr aMgr = ModelAPI_Session::get();
174 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
175 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
176 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
177 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
178 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
179 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
180 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
181 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
182 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
183 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
184 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
186 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
187 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
189 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
190 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
192 aFactory->registerValidator("GeomValidators_ConstructionComposite",
193 new GeomValidators_ConstructionComposite);
195 aFactory->registerValidator("GeomValidators_ZeroOffset",
196 new GeomValidators_ZeroOffset);
198 aFactory->registerValidator("PartSet_SketchEntityValidator",
199 new PartSet_SketchEntityValidator);
201 aFactory->registerValidator("PartSet_SameTypeAttr",
202 new PartSet_SameTypeAttrValidator);
205 void PartSet_Module::registerFilters()
207 //Registering of selection filters
208 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
209 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
212 void PartSet_Module::registerProperties()
214 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
216 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
217 Config_Prop::Integer, SKETCH_WIDTH);
220 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
222 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
223 mySketchMgr->commitNestedSketch(theOperation);
226 if (theOperation->isEditOperation())
228 // the selection is cleared after commit the create operation
229 // in order to do not use the same selected objects in the restarted operation
230 // for common behaviour, the selection is cleared even if the operation is not restarted
231 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
232 if (!aContext.IsNull())
233 aContext->ClearSelected();
235 /// Restart sketcher operations automatically
236 FeaturePtr aFeature = theOperation->feature();
237 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
238 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
239 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
240 myRestartingMode == RM_EmptyFeatureUsed)) {
241 myLastOperationId = theOperation->id();
242 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
244 launchOperation(myLastOperationId);
246 breakOperationSequence();
249 void PartSet_Module::breakOperationSequence()
251 myLastOperationId = "";
252 myLastFeature = FeaturePtr();
253 myRestartingMode = RM_None;
256 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
258 breakOperationSequence();
261 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
263 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
264 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
265 aViewer->AddZLayer(myVisualLayerId);
266 mySketchMgr->startSketch(theOperation);
268 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
269 mySketchMgr->startNestedSketch(theOperation);
273 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
275 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
276 mySketchMgr->stopSketch(theOperation);
277 Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
278 aViewer->RemoveZLayer(myVisualLayerId);
281 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
282 mySketchMgr->stopNestedSketch(theOperation);
286 ModuleBase_Operation* PartSet_Module::currentOperation() const
288 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
289 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
290 return anOpMgr->currentOperation();
293 bool PartSet_Module::canUndo() const
295 bool aCanUndo = false;
296 SessionPtr aMgr = ModelAPI_Session::get();
297 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
298 aCanUndo = !aMgr->isOperation();
299 if (!aCanUndo) // check the enable state additionally by sketch manager
300 aCanUndo = aMgr->canUndo();
305 bool PartSet_Module::canRedo() const
307 bool aCanRedo = false;
308 SessionPtr aMgr = ModelAPI_Session::get();
309 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
310 aCanRedo = !aMgr->isOperation();
311 if (!aCanRedo) // check the enable state additionally by sketch manager
312 aCanRedo = aMgr->canRedo();
317 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
319 // the sketch manager put the restriction to the objects display
320 return mySketchMgr->canDisplayObject(theObject);
323 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
325 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
327 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
328 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
329 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
330 if (isSketchOp || isNestedOp) {
331 // in active sketch operation it is possible to activate operation object in selection
332 // in the edit operation, e.g. points of the line can be moved when the line is edited
333 aCanActivate = aCanActivate || anOperation->isEditOperation();
338 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
340 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
343 void PartSet_Module::activeSelectionModes(QIntList& theModes)
346 if (mySketchMgr->activeSketch().get())
347 PartSet_SketcherMgr::sketchSelectionModes(theModes);
350 bool PartSet_Module::isMouseOverWindow()
352 return mySketchMgr->isMouseOverWindow();
355 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
357 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
358 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
359 // we have to manually activate the sketch label in edit mode
360 aPanel->activateWidget(aPanel->modelWidgets().first());
364 // Restart last operation type
365 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
366 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
367 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
368 // Initialise new line with first point equal to end of previous
369 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
371 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
372 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
373 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
375 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
376 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
377 aWgt->attributeID(), aPoint->x(), aPoint->y());
378 aPanel->activateNextWidget(aPnt2dWgt);
383 // Start editing constraint
384 if (theOperation->isEditOperation()) {
385 // TODO: #391 - to be removed
386 std::string aId = theOperation->id().toStdString();
387 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
388 PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
389 // Find and activate widget for management of point for dimension line position
390 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
391 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
392 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
394 aPanel->activateWidget(aPntWgt);
404 void PartSet_Module::onSelectionChanged()
406 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
410 bool isSketcherOp = false;
411 // An edit operation is enable only if the current opeation is the sketch operation
412 if (mySketchMgr->activeSketch()) {
413 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
414 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
417 // Editing of constraints can be done on selection
418 ModuleBase_ISelection* aSelect = myWorkshop->selection();
419 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
420 if (aSelected.size() == 1) {
421 ModuleBase_ViewerPrs aPrs = aSelected.first();
422 ObjectPtr aObject = aPrs.object();
423 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
425 std::string aId = aFeature->getKind();
426 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
427 (aId == SketchPlugin_ConstraintLength::ID()) ||
428 (aId == SketchPlugin_ConstraintDistance::ID())) {
429 editFeature(aFeature);
436 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
438 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
439 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
440 anOpMgr->onKeyReleased(theEvent);
443 void PartSet_Module::onEnterReleased()
445 myRestartingMode = RM_EmptyFeatureUsed;
448 void PartSet_Module::onOperationActivatedByPreselection()
450 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
451 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
452 // Set final definitions if they are necessary
453 //propertyPanelDefined(aOperation);
455 /// Commit sketcher operations automatically
456 anOperation->commit();
460 void PartSet_Module::onNoMoreWidgets()
462 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
464 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
465 if (myRestartingMode != RM_Forbided)
466 myRestartingMode = RM_LastFeatureUsed;
467 anOperation->commit();
472 void PartSet_Module::onVertexSelected()
474 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
475 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
476 /// If last line finished on vertex the lines creation sequence has to be break
477 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
478 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
479 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
480 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
481 bool aFoundWidget = false;
482 bool aFoundObligatory = false;
483 for (; anIt != aLast && !aFoundObligatory; anIt++) {
485 aFoundWidget = *anIt == anActiveWidget;
487 aFoundObligatory = (*anIt)->isObligatory();
489 if (!aFoundObligatory)
490 myRestartingMode = RM_Forbided;
494 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
495 Config_WidgetAPI* theWidgetApi, std::string theParentId)
497 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
498 XGUI_Workshop* aWorkshop = aConnector->workshop();
499 ModuleBase_ModelWidget* aWgt = NULL;
500 if (theType == "sketch-start-label") {
501 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent,
502 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
503 aLabelWgt->setWorkshop(aWorkshop);
504 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
505 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
506 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
507 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
509 } else if (theType == "sketch-2dpoint_selector") {
510 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
511 aPointWgt->setWorkshop(aWorkshop);
512 aPointWgt->setSketch(mySketchMgr->activeSketch());
513 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
515 } else if (theType == "point2ddistance") {
516 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
517 aDistanceWgt->setWorkshop(aWorkshop);
518 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
520 } else if(theType == "point2dangle") {
521 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent, theWidgetApi, theParentId);
522 anAngleWgt->setWorkshop(aWorkshop);
523 anAngleWgt->setSketch(mySketchMgr->activeSketch());
525 } else if (theType == "sketch_shape_selector") {
526 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
527 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
528 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
529 aWgt = aShapeSelectorWgt;
530 } else if (theType == "sketch_multi_selector") {
531 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
532 new PartSet_WidgetMultiSelector(theParent, workshop(), theWidgetApi, theParentId);
533 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
534 aWgt = aShapeSelectorWgt;
535 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
536 aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
537 } else if (theType == "export_file_selector") {
538 aWgt = new PartSet_WidgetFileSelector(theParent, workshop(), theWidgetApi, theParentId);
539 } else if (theType == "sketch_launcher") {
540 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
546 bool PartSet_Module::deleteObjects()
548 SessionPtr aMgr = ModelAPI_Session::get();
549 // 1. check whether the delete should be processed in the module
550 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
551 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
552 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
553 if (isSketchOp || isNestedOp) {
554 // 2. find selected presentations
555 // selected objects should be collected before the current operation abort because
556 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
557 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
558 XGUI_Workshop* aWorkshop = aConnector->workshop();
559 ModuleBase_ISelection* aSel = workshop()->selection();
560 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
561 // if there are no selected objects in the viewer, that means that the selection in another
562 // place cased this method. It is necessary to return the false value to understande in above
563 // method that delete is not processed
564 if (aSelectedObj.count() == 0)
567 // avoid delete of the objects, which are not belong to the current sketch
568 // in order to do not delete results of other sketches
569 QObjectPtrList aSketchObjects;
570 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
571 for ( ; anIt != aLast; anIt++) {
572 ObjectPtr anObject = *anIt;
573 if (mySketchMgr->isObjectOfSketch(anObject)) {
574 // sketch feature should be used in this list because workshop deletes features only
575 // results are skipped
576 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
577 aSketchObjects.append(aSketchFeature);
580 // if the selection contains only local selected presentations from other sketches,
581 // the Delete operation should not be done at all
582 if (aSketchObjects.size() == 0)
585 // the active nested sketch operation should be aborted unconditionally
587 anOperation->abort();
589 // 3. start operation
590 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
591 aMgr->startOperation(aDescription.toStdString());
593 // 4. delete features
594 // sketch feature should be skipped, only sub-features can be removed
595 // when sketch operation is active
596 std::set<FeaturePtr> anIgnoredFeatures;
597 anIgnoredFeatures.insert(mySketchMgr->activeSketch());
598 aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
601 aWorkshop->displayer()->updateViewer();
602 aMgr->finishOperation();
604 bool isPartRemoved = false;
605 // Delete part with help of PartSet plugin
606 // TODO: the deleted objects has to be processed by multiselection
607 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
608 if (aObjects.size() == 1) {
609 ObjectPtr aObj = aObjects.first();
610 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
611 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
612 // Remove feature should be created in the document of the part results
613 ResultPtr aPartResult = aFeature->firstResult();
614 if (aPartResult.get()) {
615 std::shared_ptr<ModelAPI_ResultPart> aPart =
616 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
617 DocumentPtr aPartDoc = aPart->partDoc();
618 if (aPartDoc.get()) {
619 aMgr->startOperation(PartSetPlugin_Remove::ID());
620 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
622 aMgr->finishOperation();
623 isPartRemoved = true;
628 return isPartRemoved;
633 void PartSet_Module::onFeatureTriggered()
635 SessionPtr aMgr = ModelAPI_Session::get();
636 // 1. check whether the delete should be processed in the module
637 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
638 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
640 // in case if in the viewer nothing is displayed, the create operation should not be
641 // comitted even if all values of the feature are initialized
642 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
643 QAction* aCmd = dynamic_cast<QAction*>(sender());
644 //Do nothing on uncheck
645 if (aCmd->isCheckable() && !aCmd->isChecked())
648 // the action information should be saved before the operation is aborted
649 // because this abort leads to update command status, which unchecks this action
650 anOperation->abort();
652 launchOperation(aCmd->data().toString());
655 ModuleBase_IModule::onFeatureTriggered();
658 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
660 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
661 if (!anAIS.IsNull()) {
662 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
663 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
664 if (!aDim.IsNull()) {
665 aCtx->SetZLayer(aDim, myVisualLayerId);
667 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
669 aCtx->SetZLayer(aCons, myVisualLayerId);
674 void PartSet_Module::onViewTransformed(int theTrsfType)
676 // Set length of arrows constant in pixel size
677 // if the operation is panning or rotate or panglobal then do nothing
678 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
681 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
682 //Handle(V3d_View) aView = aViewer->activeView();
684 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
685 XGUI_Workshop* aWorkshop = aConnector->workshop();
686 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
687 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
689 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
690 Handle(V3d_View) aView;
692 for (aV3dViewer->InitDefinedViews();
693 aV3dViewer->MoreDefinedViews();
694 aV3dViewer->NextDefinedViews()) {
695 Handle(V3d_View) aV = aV3dViewer->DefinedView();
696 double aS = aV->Scale();
704 double aLen = aView->Convert(20);
706 double aPrevLen = SketcherPrs_Tools::getArrowSize();
707 SketcherPrs_Tools::setArrowSize(aLen);
708 const double aPrevScale = aViewer->Scale(aViewer->activeView());
709 const double aCurScale = aViewer->activeView()->Camera()->Scale();
710 aViewer->SetScale(aViewer->activeView(), aCurScale);
711 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
712 bool isModified = false;
713 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
714 foreach (AISObjectPtr aAIS, aPrsList) {
715 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
717 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
718 if (!aDim.IsNull()) {
719 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
720 aContext->Redisplay(aDim, false);
725 aDisplayer->updateViewer();
729 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
731 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
733 QLineEdit* aLabel = aOB->activeDocLabel();
734 QPalette aPalet = aLabel->palette();
735 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
736 aLabel->setPalette(aPalet);
737 aOB->treeView()->setExpandsOnDoubleClick(false);
738 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
739 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
740 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
741 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
742 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
743 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
748 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
750 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
751 int aSelected = aObjects.size();
752 SessionPtr aMgr = ModelAPI_Session::get();
753 if (aSelected == 1) {
754 bool hasResult = false;
755 bool hasFeature = false;
756 bool hasParameter = false;
757 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
759 ObjectPtr aObject = aObjects.first();
761 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
762 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
763 bool isPart = aPart.get() ||
764 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
766 DocumentPtr aPartDoc;
768 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
770 if (aPart.get()) // this may be null is Part feature is disabled
771 aPartDoc = aPart->partDoc();
772 if (aMgr->activeDocument() == aPartDoc)
773 theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
775 theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
776 } else if (aObject->document() == aMgr->activeDocument()) {
777 if (hasParameter || hasFeature)
778 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
780 } else { // If feature is 0 the it means that selected root object (document)
781 if (aMgr->activeDocument() != aMgr->moduleDocument())
782 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
784 } else if (aSelected == 0) {
785 // if there is no selection then it means that upper label is selected
786 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
787 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
788 if (aMgr->activeDocument() != aMgr->moduleDocument())
789 theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
793 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
795 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
796 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
797 XGUI_Workshop* aWorkshop = aConnector->workshop();
798 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
799 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
800 QPalette aPalet = aLabel->palette();
802 SessionPtr aMgr = ModelAPI_Session::get();
803 DocumentPtr aActiveDoc = aMgr->activeDocument();
804 DocumentPtr aDoc = aMgr->moduleDocument();
805 QModelIndex aOldIndex = myDataModel->activePartTree();
806 if (aActiveDoc == aDoc) {
807 if (aOldIndex.isValid())
808 aTreeView->setExpanded(aOldIndex, false);
809 myDataModel->deactivatePart();
810 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
812 std::string aGrpName = ModelAPI_ResultPart::group();
813 for (int i = 0; i < aDoc->size(aGrpName); i++) {
814 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
815 if (aPart->partDoc() == aActiveDoc) {
816 QModelIndex aIndex = myDataModel->partIndex(aPart);
817 if (myDataModel->activatePart(aIndex)) {
818 if (aOldIndex.isValid())
819 aTreeView->setExpanded(aOldIndex, false);
820 aTreeView->setExpanded(myDataModel->activePartTree(), true);
821 aPalet.setColor(QPalette::Text, Qt::black);
827 aLabel->setPalette(aPalet);
828 aWorkshop->updateCommandStatus();
830 // Update displayed objects in order to update active color
831 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
832 QObjectPtrList aObjects = aDisplayer->displayedObjects();
833 foreach(ObjectPtr aObj, aObjects)
834 aDisplayer->redisplay(aObj, false);
835 aDisplayer->updateViewer();
839 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
841 SessionPtr aMgr = ModelAPI_Session::get();
842 if (!theIndex.isValid()) {
843 aMgr->setActiveDocument(aMgr->moduleDocument());
846 if (theIndex.column() != 0) // Use only first column
848 ObjectPtr aObj = myDataModel->object(theIndex);
849 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
850 if (!aPart.get()) { // Probably this is Feature
851 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
852 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
853 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
856 if (aPart.get()) { // if this is a part
857 if (aPart->partDoc() == aMgr->activeDocument()) {
858 aMgr->setActiveDocument(aMgr->moduleDocument());