1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_Module.h"
4 #include "PartSet_WidgetSketchLabel.h"
5 #include "PartSet_Validators.h"
6 #include "PartSet_Tools.h"
7 #include "PartSet_WidgetPoint2d.h"
8 #include "PartSet_WidgetPoint2dDistance.h"
9 #include "PartSet_WidgetShapeSelector.h"
10 #include "PartSet_WidgetPoint2dAngle.h"
11 #include "PartSet_WidgetMultiSelector.h"
12 #include "PartSet_WidgetEditor.h"
13 #include "PartSet_WidgetFileSelector.h"
14 #include "PartSet_WidgetSketchCreator.h"
15 #include "PartSet_SketcherMgr.h"
16 #include "PartSet_MenuMgr.h"
17 #include "PartSet_CustomPrs.h"
18 #include "PartSet_IconFactory.h"
20 #include "PartSet_Filters.h"
21 #include "PartSet_FilterInfinite.h"
23 #include <PartSetPlugin_Remove.h>
24 #include <PartSetPlugin_Part.h>
25 #include <PartSetPlugin_Duplicate.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_OperationAction.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_WidgetEditor.h>
33 #include <ModuleBase_WidgetValidated.h>
34 #include <ModuleBase_FilterFactory.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_OperationFeature.h>
38 #include <GeomValidators_ShapeType.h>
39 #include <GeomValidators_Finite.h>
40 #include <GeomValidators_Face.h>
41 #include <GeomValidators_ConstructionComposite.h>
42 #include <GeomValidators_ZeroOffset.h>
43 #include <GeomValidators_BooleanArguments.h>
44 #include <GeomValidators_Different.h>
47 #include <ModelAPI_Object.h>
48 #include <ModelAPI_Events.h>
49 #include <ModelAPI_Validator.h>
50 #include <ModelAPI_Data.h>
51 #include <ModelAPI_Session.h>
52 #include <GeomValidators_DifferentShapes.h>
53 #include <ModelAPI_ResultBody.h>
54 #include <ModelAPI_AttributeString.h>
56 #include <GeomDataAPI_Point2D.h>
57 #include <GeomDataAPI_Point.h>
58 #include <GeomDataAPI_Dir.h>
60 #include <XGUI_Displayer.h>
61 #include <XGUI_Workshop.h>
62 #include <XGUI_OperationMgr.h>
63 #include <XGUI_PropertyPanel.h>
64 #include <XGUI_ModuleConnector.h>
65 #include <XGUI_ContextMenuMgr.h>
66 #include <XGUI_Tools.h>
67 #include <XGUI_ObjectsBrowser.h>
68 #include <XGUI_SelectionMgr.h>
69 #include <XGUI_DataModel.h>
70 #include <XGUI_ErrorMgr.h>
72 #include <SketchPlugin_Feature.h>
73 #include <SketchPlugin_Sketch.h>
74 #include <SketchPlugin_Line.h>
75 #include <SketchPlugin_Arc.h>
76 #include <SketchPlugin_Circle.h>
77 #include <SketchPlugin_Point.h>
78 #include <SketchPlugin_ConstraintAngle.h>
79 #include <SketchPlugin_ConstraintLength.h>
80 #include <SketchPlugin_ConstraintDistance.h>
81 #include <SketchPlugin_ConstraintParallel.h>
82 #include <SketchPlugin_ConstraintPerpendicular.h>
83 #include <SketchPlugin_ConstraintRadius.h>
85 #include <SketcherPrs_SymbolPrs.h>
86 #include <SketcherPrs_Tools.h>
88 #include <Events_Loop.h>
89 #include <Config_PropManager.h>
90 #include <Config_Keywords.h>
92 #include <StdSelect_TypeOfFace.hxx>
93 #include <TopoDS_Vertex.hxx>
95 #include <TopoDS_Shape.hxx>
96 #include <BRep_Tool.hxx>
97 #include <AIS_Dimension.hxx>
100 #include <QMouseEvent>
103 #include <QApplication>
104 #include <QMessageBox>
105 #include <QMainWindow>
108 #include <GeomAlgoAPI_FaceBuilder.h>
109 #include <GeomDataAPI_Dir.h>
111 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
117 /*!Create and return new instance of XGUI_Module*/
118 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
120 return new PartSet_Module(theWshop);
123 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
124 : ModuleBase_IModule(theWshop),
125 myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
127 new PartSet_IconFactory();
129 mySketchMgr = new PartSet_SketcherMgr(this);
130 #ifdef ModuleDataModel
131 myDataModel = new PartSet_DocumentDataModel(this);
134 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
135 XGUI_Workshop* aWorkshop = aConnector->workshop();
137 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
138 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
139 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
140 this, SLOT(onOperationActivatedByPreselection()));
142 ModuleBase_IViewer* aViewer = theWshop->viewer();
143 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
144 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
145 connect(aViewer, SIGNAL(viewTransformed(int)),
146 SLOT(onViewTransformed(int)));
147 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
148 SLOT(onViewCreated(ModuleBase_IViewWindow*)));
150 myMenuMgr = new PartSet_MenuMgr(this);
151 myCustomPrs = new PartSet_CustomPrs(theWshop);
153 Events_Loop* aLoop = Events_Loop::loop();
154 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
156 mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
157 mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
160 PartSet_Module::~PartSet_Module()
162 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
163 for (; aIt.More(); aIt.Next()) {
164 Handle(SelectMgr_Filter) aFilter = aIt.Value();
165 if (!aFilter.IsNull())
171 void PartSet_Module::activateSelectionFilters()
173 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
174 for (; aIt.More(); aIt.Next()) {
175 Handle(SelectMgr_Filter) aFilter = aIt.Value();
176 if (!aFilter.IsNull())
177 myWorkshop->viewer()->addSelectionFilter(aFilter);
181 void PartSet_Module::deactivateSelectionFilters()
183 SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
184 for (; aIt.More(); aIt.Next()) {
185 Handle(SelectMgr_Filter) aFilter = aIt.Value();
186 if (!aFilter.IsNull())
187 myWorkshop->viewer()->removeSelectionFilter(aFilter);
191 void PartSet_Module::registerValidators()
193 //Registering of validators
194 SessionPtr aMgr = ModelAPI_Session::get();
195 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
196 aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
197 aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
198 aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
199 aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
200 aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
201 aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
202 aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
203 aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
204 aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
205 aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
206 aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
207 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
208 aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
210 aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
211 aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
212 aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
213 aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
215 aFactory->registerValidator("GeomValidators_ConstructionComposite",
216 new GeomValidators_ConstructionComposite);
218 aFactory->registerValidator("GeomValidators_ZeroOffset",
219 new GeomValidators_ZeroOffset);
221 aFactory->registerValidator("GeomValidators_BooleanArguments",
222 new GeomValidators_BooleanArguments);
224 aFactory->registerValidator("PartSet_SketchEntityValidator",
225 new PartSet_SketchEntityValidator);
227 aFactory->registerValidator("GeomValidators_Different",
228 new GeomValidators_Different);
231 void PartSet_Module::registerFilters()
233 //Registering of selection filters
234 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
235 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
238 void PartSet_Module::registerProperties()
240 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
242 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
243 Config_Prop::Integer, SKETCH_WIDTH);
246 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation)
248 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
249 mySketchMgr->commitNestedSketch(theOperation);
252 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
253 if (!aFOperation || aFOperation->isEditOperation())
255 // the selection is cleared after commit the create operation
256 // in order to do not use the same selected objects in the restarted operation
257 // for common behaviour, the selection is cleared even if the operation is not restarted
258 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
259 XGUI_Workshop* aWorkshop = aConnector->workshop();
260 aWorkshop->selector()->clearSelection();
262 /// Restart sketcher operations automatically
263 FeaturePtr aFeature = aFOperation->feature();
264 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
265 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
266 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
267 myRestartingMode == RM_EmptyFeatureUsed)) {
268 myLastOperationId = aFOperation->id();
269 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
270 if (!sketchMgr()->sketchSolverError())
271 launchOperation(myLastOperationId);
273 breakOperationSequence();
276 void PartSet_Module::breakOperationSequence()
278 myLastOperationId = "";
279 myLastFeature = FeaturePtr();
280 myRestartingMode = RM_None;
283 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
285 breakOperationSequence();
288 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
290 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
291 mySketchMgr->startSketch(theOperation);
293 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
294 mySketchMgr->startNestedSketch(theOperation);
297 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
299 myCustomPrs->activate(aFOperation->feature(), true);
302 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
304 ModuleBase_IModule::onOperationResumed(theOperation);
306 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
308 myCustomPrs->activate(aFOperation->feature(), true);
311 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
313 bool isModified = myCustomPrs->deactivate(false);
315 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
316 mySketchMgr->stopSketch(theOperation);
318 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
319 mySketchMgr->stopNestedSketch(theOperation);
323 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
324 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
325 aDisplayer->updateViewer();
327 mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
330 ModuleBase_Operation* PartSet_Module::currentOperation() const
332 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
333 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
334 return anOpMgr->currentOperation();
337 bool PartSet_Module::canUndo() const
339 bool aCanUndo = false;
340 SessionPtr aMgr = ModelAPI_Session::get();
341 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
342 aCanUndo = !aMgr->isOperation();
343 if (!aCanUndo) // check the enable state additionally by sketch manager
344 aCanUndo = aMgr->canUndo();
349 bool PartSet_Module::canRedo() const
351 bool aCanRedo = false;
352 SessionPtr aMgr = ModelAPI_Session::get();
353 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
354 aCanRedo = !aMgr->isOperation();
355 if (!aCanRedo) // check the enable state additionally by sketch manager
356 aCanRedo = aMgr->canRedo();
361 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
364 if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
365 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
367 // part features are removed in the PartSet module only.
368 if (aFeature->getKind() == PartSetPlugin_Part::ID())
375 bool PartSet_Module::canCommitOperation() const
377 return mySketchMgr->canCommitOperation();
380 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
382 // the sketch manager put the restriction to the objects erase
383 return mySketchMgr->canEraseObject(theObject);
386 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
388 // the sketch manager put the restriction to the objects display
389 return mySketchMgr->canDisplayObject(theObject);
392 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
394 bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
396 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
397 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
398 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
399 if (isSketchOp || isNestedOp) {
400 // in active sketch operation it is possible to activate operation object in selection
401 // in the edit operation, e.g. points of the line can be moved when the line is edited
402 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
403 aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
408 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
410 return myMenuMgr->addViewerMenu(theMenu, theStdActions);
413 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
415 myMenuMgr->updateViewerMenu(theStdActions);
418 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
420 QString anError = ModuleBase_IModule::getFeatureError(theFeature);
422 if (anError.isEmpty())
423 anError = sketchMgr()->getFeatureError(theFeature);
425 if (anError.isEmpty()) {
426 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
427 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
429 if (anOpMgr->isValidationLocked()) {
430 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
431 (anOpMgr->currentOperation());
432 if (!aFOperation || theFeature == aFOperation->feature())
433 anError = "Validation is locked by the current operation";
439 void PartSet_Module::activeSelectionModes(QIntList& theModes)
442 if (mySketchMgr->activeSketch().get())
443 PartSet_SketcherMgr::sketchSelectionModes(theModes);
446 bool PartSet_Module::isMouseOverWindow()
448 return mySketchMgr->isMouseOverWindow();
451 void PartSet_Module::closeDocument()
456 void PartSet_Module::clearViewer()
458 myCustomPrs->clearPrs();
461 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
463 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
467 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
468 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
469 // we have to manually activate the sketch label in edit mode
470 aPanel->activateWidget(aPanel->modelWidgets().first());
474 // Restart last operation type
475 if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
476 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
477 if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
478 // Initialise new line with first point equal to end of previous
479 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
481 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
482 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
483 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
485 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
486 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(),
487 aWgt->attributeID(), aPoint->x(), aPoint->y());
488 aPanel->activateNextWidget(aPnt2dWgt);
496 void PartSet_Module::onSelectionChanged()
498 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
502 bool isSketcherOp = false;
503 // An edit operation is enable only if the current opeation is the sketch operation
504 if (mySketchMgr->activeSketch()) {
505 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
506 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
509 // Editing of constraints can be done on selection
510 ModuleBase_ISelection* aSelect = myWorkshop->selection();
511 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
512 if (aSelected.size() == 1) {
513 ModuleBase_ViewerPrs aPrs = aSelected.first();
514 ObjectPtr aObject = aPrs.object();
515 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
517 std::string aId = aFeature->getKind();
518 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
519 (aId == SketchPlugin_ConstraintLength::ID()) ||
520 (aId == SketchPlugin_ConstraintDistance::ID()) ||
521 (aId == SketchPlugin_ConstraintAngle::ID())) {
522 editFeature(aFeature);
529 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
531 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
532 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
533 anOpMgr->onKeyReleased(theEvent);
536 void PartSet_Module::onEnterReleased()
538 myRestartingMode = RM_EmptyFeatureUsed;
541 void PartSet_Module::onOperationActivatedByPreselection()
543 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
544 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
545 // Set final definitions if they are necessary
546 //propertyPanelDefined(aOperation);
548 /// Commit sketcher operations automatically
549 anOperation->commit();
553 void PartSet_Module::onNoMoreWidgets()
555 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
557 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
558 if (myRestartingMode != RM_Forbided)
559 myRestartingMode = RM_LastFeatureUsed;
560 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
561 XGUI_Workshop* aWorkshop = aConnector->workshop();
562 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
563 // do nothing if the feature can not be applyed
564 if (anOpMgr->isApplyEnabled())
565 anOperation->commit();
570 void PartSet_Module::onVertexSelected()
572 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
573 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
574 /// If last line finished on vertex the lines creation sequence has to be break
575 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
576 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
577 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
578 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
579 bool aFoundWidget = false;
580 bool aFoundObligatory = false;
581 for (; anIt != aLast && !aFoundObligatory; anIt++) {
583 aFoundWidget = *anIt == anActiveWidget;
585 aFoundObligatory = (*anIt)->isObligatory();
587 if (!aFoundObligatory)
588 myRestartingMode = RM_Forbided;
592 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
593 Config_WidgetAPI* theWidgetApi, std::string theParentId)
595 ModuleBase_IWorkshop* aWorkshop = workshop();
596 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
597 XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
598 ModuleBase_ModelWidget* aWgt = NULL;
599 if (theType == "sketch-start-label") {
600 PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
601 theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
602 connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
603 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
604 connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
605 mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
607 } else if (theType == "sketch-2dpoint_selector") {
608 PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
609 theWidgetApi, theParentId);
610 aPointWgt->setSketch(mySketchMgr->activeSketch());
611 connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
613 } else if (theType == "point2ddistance") {
614 PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
615 aWorkshop, theWidgetApi, theParentId);
616 aDistanceWgt->setSketch(mySketchMgr->activeSketch());
618 } else if(theType == "point2dangle") {
619 PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
620 aWorkshop, theWidgetApi, theParentId);
621 anAngleWgt->setSketch(mySketchMgr->activeSketch());
623 } else if (theType == "sketch_shape_selector") {
624 PartSet_WidgetShapeSelector* aShapeSelectorWgt =
625 new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
626 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
627 aWgt = aShapeSelectorWgt;
628 } else if (theType == "sketch_multi_selector") {
629 PartSet_WidgetMultiSelector* aShapeSelectorWgt =
630 new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
631 aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
632 aWgt = aShapeSelectorWgt;
633 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
634 aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
635 } else if (theType == "export_file_selector") {
636 aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
637 } else if (theType == "sketch_launcher") {
638 aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
644 bool PartSet_Module::deleteObjects()
646 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
647 XGUI_Workshop* aWorkshop = aConnector->workshop();
648 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
650 //SessionPtr aMgr = ModelAPI_Session::get();
651 // 1. check whether the delete should be processed in the module
652 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
653 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
654 isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
655 if (isSketchOp || isNestedOp) {
656 // 2. find selected presentations
657 // selected objects should be collected before the current operation abort because
658 // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
659 ModuleBase_ISelection* aSel = workshop()->selection();
660 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
661 // if there are no selected objects in the viewer, that means that the selection in another
662 // place cased this method. It is necessary to return the false value to understande in above
663 // method that delete is not processed
664 if (aSelectedObj.count() == 0)
667 // avoid delete of the objects, which are not belong to the current sketch
668 // in order to do not delete results of other sketches
669 QObjectPtrList aSketchObjects;
670 QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
671 for ( ; anIt != aLast; anIt++) {
672 ObjectPtr anObject = *anIt;
673 if (mySketchMgr->isObjectOfSketch(anObject)) {
674 // sketch feature should be used in this list because workshop deletes features only
675 // results are skipped
676 FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
677 aSketchObjects.append(aSketchFeature);
680 // if the selection contains only local selected presentations from other sketches,
681 // the Delete operation should not be done at all
682 if (aSketchObjects.size() == 0)
685 // 3. start operation
686 QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
687 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
689 // the active nested sketch operation should be aborted unconditionally
690 // the Delete action should be additionally granted for the Sketch operation
691 // in order to do not abort/commit it
692 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
693 return true; // the objects are processed but can not be deleted
695 anOpMgr->startOperation(anOpAction);
697 // 4. delete features
698 // sketch feature should be skipped, only sub-features can be removed
699 // when sketch operation is active
700 aWorkshop->deleteFeatures(aSketchObjects);
702 anOpMgr->commitOperation();
705 bool isPartRemoved = false;
706 // Delete part with help of PartSet plugin
707 // TODO: the deleted objects has to be processed by multiselection
708 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
709 if (aObjects.size() == 1) {
710 ObjectPtr aObj = aObjects.first();
711 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
712 if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
713 // Remove feature should be created in the document of the part results
714 ResultPtr aPartResult = aFeature->firstResult();
715 if (aPartResult.get()) {
716 std::shared_ptr<ModelAPI_ResultPart> aPart =
717 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
718 DocumentPtr aPartDoc = aPart->partDoc();
719 if (aPartDoc.get()) {
720 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
721 (PartSetPlugin_Remove::ID().c_str(), this);
722 if (!anOpMgr->canStartOperation(anOpAction->id()))
723 return true; // the objects are processed but can not be deleted
725 anOpMgr->startOperation(anOpAction);
727 FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
730 anOpMgr->commitOperation();
731 isPartRemoved = true;
736 return isPartRemoved;
741 void PartSet_Module::onFeatureTriggered()
743 QAction* aCmd = dynamic_cast<QAction*>(sender());
744 if (aCmd->isCheckable() && aCmd->isChecked()) {
745 // 1. check whether the delete should be processed in the module
746 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
747 bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
749 // in case if in the viewer nothing is displayed, the create operation should not be
750 // comitted even if all values of the feature are initialized
751 if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
752 // the action information should be saved before the operation is aborted
753 // because this abort leads to update command status, which unchecks this action
754 anOperation->abort();
755 launchOperation(aCmd->data().toString());
759 ModuleBase_IModule::onFeatureTriggered();
762 void PartSet_Module::launchOperation(const QString& theCmdId)
764 if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
765 // Show constraints if a constraint was anOperation
766 myHasConstraintShown = mySketchMgr->isConstraintsShown();
767 mySketchMgr->onShowConstraintsToggle(true);
769 ModuleBase_IModule::launchOperation(theCmdId);
773 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
775 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
776 if (!anAIS.IsNull()) {
777 Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
778 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
779 if (!aDim.IsNull()) {
780 aCtx->SetZLayer(aDim, myVisualLayerId);
782 Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
784 aCtx->SetZLayer(aCons, myVisualLayerId);
789 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
792 // it should be recomputed in order to disappear in the viewer if the corresponded object
794 //if (myCustomPrs->isActive())
795 // myCustomPrs->redisplay(theObject, false);
798 void PartSet_Module::onViewTransformed(int theTrsfType)
800 // Set length of arrows constant in pixel size
801 // if the operation is panning or rotate or panglobal then do nothing
802 if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
804 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
805 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
806 if (aContext.IsNull())
809 //Handle(V3d_View) aView = aViewer->activeView();
811 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
812 XGUI_Workshop* aWorkshop = aConnector->workshop();
813 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
814 Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
815 Handle(V3d_View) aView;
817 for (aV3dViewer->InitDefinedViews();
818 aV3dViewer->MoreDefinedViews();
819 aV3dViewer->NextDefinedViews()) {
820 Handle(V3d_View) aV = aV3dViewer->DefinedView();
821 double aS = aV->Scale();
829 double aLen = aView->Convert(20);
831 double aPrevLen = SketcherPrs_Tools::getArrowSize();
832 SketcherPrs_Tools::setArrowSize(aLen);
833 const double aPrevScale = aViewer->Scale(aViewer->activeView());
834 const double aCurScale = aViewer->activeView()->Camera()->Scale();
835 aViewer->SetScale(aViewer->activeView(), aCurScale);
836 SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
837 bool isModified = false;
838 QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
839 foreach (AISObjectPtr aAIS, aPrsList) {
840 Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
842 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
843 if (!aDim.IsNull()) {
844 aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
845 aContext->Redisplay(aDim, false);
850 aDisplayer->updateViewer();
853 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
855 bool isRedisplayed = false;
856 if (myCustomPrs->isActive())
857 isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
859 return isRedisplayed;
862 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
864 XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
866 //QLineEdit* aLabel = aOB->activeDocLabel();
867 //QPalette aPalet = aLabel->palette();
868 //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
869 //aLabel->setPalette(aPalet);
870 aOB->treeView()->setExpandsOnDoubleClick(false);
871 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
872 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
873 #ifdef ModuleDataModel
874 connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)),
875 SLOT(onTreeViewDoubleClick(const QModelIndex&)));
876 connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
877 myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
882 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
885 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
887 /// If last line finished on vertex the lines creation sequence has to be break
888 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
889 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
890 // if there is an active widget, find the presented object in it
892 anActiveWidget = aPanel->preselectionWidget();
894 ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
896 if (aWidgetValidated)
897 anObject = aWidgetValidated->findPresentedObject(theAIS);
902 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
904 QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
905 int aSelected = aObjects.size();
906 SessionPtr aMgr = ModelAPI_Session::get();
907 QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
908 QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
909 if (aSelected == 1) {
910 bool hasResult = false;
911 bool hasFeature = false;
912 bool hasParameter = false;
913 bool hasSubFeature = false;
914 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
916 ObjectPtr aObject = aObjects.first();
918 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
919 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
920 bool isPart = aPart.get() ||
921 (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
923 DocumentPtr aPartDoc;
925 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
927 if (aPart.get()) // this may be null is Part feature is disabled
928 aPartDoc = aPart->partDoc();
930 theMenu->addAction(aActivatePartAction);
931 aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
933 } else if (aObject->document() == aMgr->activeDocument()) {
934 if (hasParameter || hasFeature)
935 theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
938 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
940 theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
941 } else { // If feature is 0 the it means that selected root object (document)
942 theMenu->addAction(aActivatePartSetAction);
943 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
945 } else if (aSelected == 0) {
946 // if there is no selection then it means that upper label is selected
947 QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
948 if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
949 theMenu->addAction(aActivatePartSetAction);
950 aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
952 bool aNotDeactivate = (myWorkshop->currentOperation() == 0);
953 if (!aNotDeactivate) {
954 aActivatePartAction->setEnabled(false);
955 aActivatePartSetAction->setEnabled(false);
959 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
961 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
962 // Do not change activation of parts if an operation active
963 static QStringList aAllowActivationList;
964 if (aAllowActivationList.isEmpty())
965 aAllowActivationList <<
966 QString(PartSetPlugin_Part::ID().c_str()) <<
967 QString(PartSetPlugin_Duplicate::ID().c_str()) <<
968 QString(PartSetPlugin_Remove::ID().c_str());
969 if (myWorkshop->currentOperation() &&
970 (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
972 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
973 XGUI_Workshop* aWorkshop = aConnector->workshop();
974 XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
975 QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
976 QPalette aPalet = aLabel->palette();
978 SessionPtr aMgr = ModelAPI_Session::get();
979 DocumentPtr aActiveDoc = aMgr->activeDocument();
980 #ifdef ModuleDataModel
981 QModelIndex aOldIndex = myDataModel->activePartTree();
982 DocumentPtr aDoc = aMgr->moduleDocument();
983 if (aActiveDoc == aDoc) {
984 if (aOldIndex.isValid())
985 aTreeView->setExpanded(aOldIndex, false);
986 myDataModel->deactivatePart();
987 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
989 std::string aGrpName = ModelAPI_ResultPart::group();
990 for (int i = 0; i < aDoc->size(aGrpName); i++) {
991 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
992 if (aPart->partDoc() == aActiveDoc) {
993 QModelIndex aIndex = myDataModel->partIndex(aPart);
994 if (myDataModel->activatePart(aIndex)) {
995 if (aOldIndex.isValid())
996 aTreeView->setExpanded(aOldIndex, false);
997 aTreeView->setExpanded(myDataModel->activePartTree(), true);
998 aPalet.setColor(QPalette::Text, Qt::black);
1005 // Problem with MPV: At first time on creation it doesn't work because Part feature
1006 // creation event will be sent after
1007 if (aActivePartIndex.isValid())
1008 aTreeView->setExpanded(aActivePartIndex, false);
1009 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1010 aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1011 if (aActivePartIndex.isValid())
1012 aTreeView->setExpanded(aActivePartIndex, true);
1014 aLabel->setPalette(aPalet);
1015 aWorkshop->updateCommandStatus();
1017 // Update displayed objects in order to update active color
1018 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1019 QObjectPtrList aObjects = aDisplayer->displayedObjects();
1020 foreach(ObjectPtr aObj, aObjects)
1021 aDisplayer->redisplay(aObj, false);
1022 aDisplayer->updateViewer();
1026 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1028 if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1030 SessionPtr aMgr = ModelAPI_Session::get();
1031 if (!theIndex.isValid()) {
1032 aMgr->setActiveDocument(aMgr->moduleDocument());
1035 if (theIndex.column() != 0) // Use only first column
1037 #ifdef ModuleDataModel
1038 ObjectPtr aObj = myDataModel->object(theIndex);
1040 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1041 XGUI_Workshop* aWorkshop = aConnector->workshop();
1042 XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1043 // De not use non editable Indexes
1044 if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1046 ObjectPtr aObj = aDataModel->object(theIndex);
1049 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1050 if (!aPart.get()) { // Probably this is Feature
1051 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1052 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1053 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1056 if (aPart.get()) { // if this is a part
1057 if (aPart->partDoc() == aMgr->activeDocument()) {
1058 myMenuMgr->activatePartSet();
1066 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1068 // z layer is created for all started operations in order to visualize operation AIS presentation
1070 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1071 if (aContext.IsNull())
1074 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1075 if (myVisualLayerId == 0) {
1076 if (myVisualLayerId == 0)
1077 aViewer->AddZLayer(myVisualLayerId);
1079 TColStd_SequenceOfInteger aZList;
1080 aViewer->GetAllZLayers(aZList);
1081 bool aFound = false;
1082 for (int i = 1; i <= aZList.Length(); i++) {
1083 if (aZList(i) == myVisualLayerId) {
1089 aViewer->AddZLayer(myVisualLayerId);