1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_SketcherMgr.cpp
4 // Created: 19 Dec 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_WidgetPoint2dDistance.h"
11 #include "PartSet_Tools.h"
13 #include <ModuleBase_WidgetEditor.h>
15 #include <XGUI_ModuleConnector.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_Workshop.h>
18 #include <XGUI_ContextMenuMgr.h>
19 #include <XGUI_Selection.h>
20 #include <XGUI_SelectionMgr.h>
21 #include <ModuleBase_ModelWidget.h>
22 #include <XGUI_ModuleConnector.h>
23 #include <XGUI_PropertyPanel.h>
24 #include <XGUI_ViewerProxy.h>
26 #include <AppElements_MainWindow.h>
28 #include <ModuleBase_IViewer.h>
29 #include <ModuleBase_IWorkshop.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_Operation.h>
32 #include <ModuleBase_ISelection.h>
33 #include <ModuleBase_IPropertyPanel.h>
34 #include <ModuleBase_Operation.h>
36 #include <GeomDataAPI_Point2D.h>
38 #include <Events_Loop.h>
40 #include <SketchPlugin_Line.h>
41 #include <SketchPlugin_Sketch.h>
42 #include <SketchPlugin_Point.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_ConstraintLength.h>
46 #include <SketchPlugin_ConstraintDistance.h>
47 #include <SketchPlugin_ConstraintParallel.h>
48 #include <SketchPlugin_ConstraintPerpendicular.h>
49 #include <SketchPlugin_ConstraintRadius.h>
50 #include <SketchPlugin_ConstraintRigid.h>
52 #include <SelectMgr_IndexedMapOfOwner.hxx>
53 #include <StdSelect_BRepOwner.hxx>
55 //#include <AIS_DimensionSelectionMode.hxx>
56 //#include <AIS_Shape.hxx>
58 #include <ModelAPI_Events.h>
59 #include <ModelAPI_Session.h>
61 #include <QMouseEvent>
62 #include <QApplication>
65 /// Returns list of unique objects by sum of objects from List1 and List2
66 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
67 const QList<ModuleBase_ViewerPrs>& theList2)
69 QList<ModuleBase_ViewerPrs> aRes;
70 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
71 if (!aRes.contains(aPrs))
74 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
75 if (!aRes.contains(aPrs))
81 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
82 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
83 const FeaturePtr theSketch)
85 QList<ModuleBase_ViewerPrs> aRes;
87 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
88 aLast = theList.end();
89 for (; anIt != aLast; anIt++)
91 ModuleBase_ViewerPrs aPrs = *anIt;
92 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
93 if (aFeature.get() == NULL)
96 QList<AttributePtr> anAttributes;
97 if (theFeature2AttributeMap.contains(aFeature)) {
98 anAttributes = theFeature2AttributeMap[aFeature];
101 TopoDS_Shape aShape = aPrs.shape();
102 if (!aShape.IsNull()) {
103 if (aShape.ShapeType() == TopAbs_VERTEX) {
104 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
105 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
106 anAttributes.push_back(anAttr);
109 theFeature2AttributeMap[aFeature] = anAttributes;
116 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
117 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
118 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
119 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
120 myIsPopupMenuActive(false)
122 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
123 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
125 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
127 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
128 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
130 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
131 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
133 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
134 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
136 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
137 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
139 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
140 XGUI_Workshop* aWorkshop = aConnector->workshop();
141 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
144 PartSet_SketcherMgr::~PartSet_SketcherMgr()
146 if (!myPlaneFilter.IsNull())
147 myPlaneFilter.Nullify();
150 void PartSet_SketcherMgr::onEnterViewPort()
152 if (!isNestedCreateOperation(getCurrentOperation()))
154 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
155 // created feature because it should be done in onMouseMove(). Some widgets watch
156 // the mouse move and use the cursor position to update own values. If the presentaion is
157 // redisplayed before this update, the feature presentation jumps from reset value to current.
158 myIsMouseOverWindow = true;
159 myIsPropertyPanelValueChanged = false;
162 void PartSet_SketcherMgr::onLeaveViewPort()
164 if (!isNestedCreateOperation(getCurrentOperation()))
166 // the method should be performed if the popup menu is called,
167 // the reset of the current widget should not happen
168 if (myIsPopupMenuActive)
171 myIsMouseOverViewProcessed = false;
172 myIsMouseOverWindow = false;
174 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
175 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
176 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
177 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
178 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
179 // obtained after reset value
180 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
181 ModuleBase_Operation* aOperation = getCurrentOperation();
182 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
183 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
187 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
189 // hides the presentation of the current operation feature
190 myIsPropertyPanelValueChanged = false;
191 // the feature is to be erased here, but it is correct to call canDisplayObject because
192 // there can be additional check (e.g. editor widget in distance constraint)
193 visualizeFeature(aOperation, canDisplayObject());
196 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
198 if (isNestedCreateOperation(getCurrentOperation()))
200 // it is necessary to save current selection in order to restore it after the values are modifed
203 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
204 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
205 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
206 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
209 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
211 if (isNestedCreateOperation(getCurrentOperation()))
213 // it is necessary to restore current selection in order to restore it after the values are modified
215 myCurrentSelection.clear();
217 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
218 // viewer happens by deselect/select the modified objects. The flag should be restored after
219 // the selection processing. The update viewer should be also called.
220 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
221 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
222 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
223 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
224 aDisplayer->updateViewer();
227 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
229 if (!isNestedCreateOperation(getCurrentOperation()))
232 // visualize the current operation feature
233 myIsPropertyPanelValueChanged = true;
234 ModuleBase_Operation* aOperation = getCurrentOperation();
235 // the feature is to be erased here, but it is correct to call canDisplayObject because
236 // there can be additional check (e.g. editor widget in distance constraint)
237 visualizeFeature(aOperation, canDisplayObject());
240 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
242 get2dPoint(theWnd, theEvent, myClickedPoint);
244 if (!(theEvent->buttons() & Qt::LeftButton))
247 // Clear dragging mode
248 myIsDragging = false;
250 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
251 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
252 if (!aViewer->canDragByMouse())
255 ModuleBase_Operation* aOperation = getCurrentOperation();
256 if (aOperation && aOperation->isEditOperation()) {
257 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
258 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
259 // If the current widget is a selector, do do nothing, it processes the mouse press
260 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
265 // Use only for sketch operations
266 if (aOperation && myCurrentSketch) {
267 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
270 bool isSketcher = isSketchOperation(aOperation);
271 bool isSketchOpe = isNestedSketchOperation(aOperation);
273 // Avoid non-sketch operations
274 if ((!isSketchOpe) && (!isSketcher))
277 bool isEditing = aOperation->isEditOperation();
279 // Ignore creation sketch operation
280 if ((!isSketcher) && (!isEditing))
283 // MoveTo in order to highlight current object
284 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
286 // Remember highlighted objects for editing
287 ModuleBase_ISelection* aSelect = aWorkshop->selection();
289 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
290 storeSelection(!aHasShift);
292 if (myCurrentSelection.empty()) {
293 if (isSketchOpe && (!isSketcher))
294 // commit previous operation
295 if (!aOperation->commit())
302 get2dPoint(theWnd, theEvent, myCurrentPoint);
306 } else if (isSketchOpe && isEditing) {
307 // If selected another object commit current result
308 aOperation->commit();
311 get2dPoint(theWnd, theEvent, myCurrentPoint);
314 // This is necessary in order to finalize previous operation
315 QApplication::processEvents();
321 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
323 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
324 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
325 if (!aViewer->canDragByMouse())
327 ModuleBase_Operation* aOp = getCurrentOperation();
329 if (isNestedSketchOperation(aOp)) {
330 get2dPoint(theWnd, theEvent, myClickedPoint);
332 // Only for sketcher operations
336 myCurrentSelection.clear();
337 // Reselect edited object
338 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
339 if (theEvent->modifiers() & Qt::ShiftModifier)
340 aViewer->AISContext()->ShiftSelect();
342 aViewer->AISContext()->Select();
348 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
349 myIsDragging = false;
352 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
354 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
355 myIsMouseOverViewProcessed = true;
356 // 1. perform the widget mouse move functionality and display the presentation
357 ModuleBase_Operation* aOperation = getCurrentOperation();
358 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
359 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
360 // the mouse move should be processed in the widget, if it can in order to visualize correct
361 // presentation. These widgets correct the feature attribute according to the mouse position
362 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
364 aPoint2DWdg->onMouseMove(theWnd, theEvent);
366 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
369 aDistanceWdg->onMouseMove(theWnd, theEvent);
371 // the feature is to be erased here, but it is correct to call canDisplayObject because
372 // there can be additional check (e.g. editor widget in distance constraint)
373 visualizeFeature(aOperation, canDisplayObject());
376 myClickedPoint.clear();
379 // 1. the current selection is saved in the mouse press method in order to restore it after moving
380 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
381 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
382 // deselected). This flag should be restored in the slot, processed the mouse release signal.
383 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
384 aViewer->enableSelection(false);
386 ModuleBase_Operation* aOperation = getCurrentOperation();
389 if (isSketchOperation(aOperation))
390 return; // No edit operation activated
392 Handle(V3d_View) aView = theWnd->v3dView();
393 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
395 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
396 double dX = aX - myCurrentPoint.myCurX;
397 double dY = aY - myCurrentPoint.myCurY;
399 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
400 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
401 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
402 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
403 // viewer happens by deselect/select the modified objects. The flag should be restored after
404 // the selection processing. The update viewer should be also called.
405 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
407 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
408 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
409 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
410 aLast = myCurrentSelection.end();
411 // 4. the features and attributes modification(move)
412 for (; anIt != aLast; anIt++) {
413 FeaturePtr aFeature = anIt.key();
415 std::set<AttributePtr> anAttributes = anIt.value().first;
416 // Process selection by attribute: the priority to the attribute
417 if (!anAttributes.empty()) {
418 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
419 anAttLast = anAttributes.end();
420 for (; anAttIt != anAttLast; anAttIt++) {
421 AttributePtr anAttr = *anAttIt;
422 if (anAttr.get() == NULL)
424 std::string aAttrId = anAttr->id();
425 DataPtr aData = aFeature->data();
426 if (aData.get() != NULL) {
427 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
428 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
429 if (aPoint.get() != NULL) {
430 bool isImmutable = aPoint->setImmutable(true);
431 aPoint->move(dX, dY);
432 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
433 aPoint->setImmutable(isImmutable);
438 // Process selection by feature
439 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
440 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
441 if (aSketchFeature) {
442 aSketchFeature->move(dX, dY);
443 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
447 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
448 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
450 // 5. it is necessary to save current selection in order to restore it after the features moving
452 // 6. restore the update viewer flag and call this update
453 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
454 aDisplayer->updateViewer();
457 myCurrentPoint.setValue(aX, aY);
461 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
463 ModuleBase_Operation* aOperation = getCurrentOperation();
464 if (aOperation && aOperation->isEditOperation()) {
465 std::string aId = aOperation->id().toStdString();
466 if (isDistanceOperation(aOperation))
468 // Activate dimension value editing on double click
469 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
470 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
471 // Find corresponded widget to activate value editing
472 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
473 if (aWgt->attributeID() == "ConstraintValue") {
482 void PartSet_SketcherMgr::onApplicationStarted()
484 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
485 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
486 XGUI_Workshop* aWorkshop = aConnector->workshop();
487 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
488 if (aPropertyPanel) {
489 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
490 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
493 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
494 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
495 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
497 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
498 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
499 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
502 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
504 if (!myClickedPoint.myIsInitialized)
507 ModuleBase_Operation* aOperation = getCurrentOperation();
508 // the distance constraint feature should not use the clickedd point
509 // this is workaround in order to don't throw down the flyout point value,
510 // set by execute() method of these type of features
511 if (isDistanceOperation(aOperation))
514 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
516 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
520 void PartSet_SketcherMgr::onBeforeContextMenu()
522 myIsPopupMenuActive = true;
525 void PartSet_SketcherMgr::onAfterContextMenu()
527 myIsPopupMenuActive = false;
530 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
533 Handle(V3d_View) aView = theWnd->v3dView();
534 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
536 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
537 thePoint.setValue(aX, anY);
540 void PartSet_SketcherMgr::launchEditing()
542 // there should be activate the vertex selection mode because the edit can happens by the selected
545 aModes << TopAbs_VERTEX << TopAbs_EDGE;
546 // TODO: #391 - to be uncommented
547 /*aModes.append(AIS_DSM_Text);
548 aModes.append(AIS_DSM_Line);
549 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
550 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
552 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
553 aConnector->activateSubShapesSelection(aModes);
555 if (!myCurrentSelection.empty()) {
556 FeaturePtr aFeature = myCurrentSelection.begin().key();
557 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
558 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
560 myModule->editFeature(aSPFeature);
567 QStringList PartSet_SketcherMgr::sketchOperationIdList()
569 static QStringList aIds;
570 if (aIds.size() == 0) {
571 aIds << SketchPlugin_Line::ID().c_str();
572 aIds << SketchPlugin_Point::ID().c_str();
573 aIds << SketchPlugin_Arc::ID().c_str();
574 aIds << SketchPlugin_Circle::ID().c_str();
575 aIds << SketchPlugin_ConstraintLength::ID().c_str();
576 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
577 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
578 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
579 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
580 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
585 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
587 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
590 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
592 return theOperation &&
593 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
596 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
598 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
601 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
603 std::string aId = theOperation ? theOperation->id().toStdString() : "";
605 return (aId == SketchPlugin_Line::ID()) ||
606 (aId == SketchPlugin_Point::ID()) ||
607 (aId == SketchPlugin_Arc::ID()) ||
608 (aId == SketchPlugin_Circle::ID());
611 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
613 std::string aId = theOperation ? theOperation->id().toStdString() : "";
615 return (aId == SketchPlugin_ConstraintLength::ID()) ||
616 (aId == SketchPlugin_ConstraintDistance::ID()) ||
617 (aId == SketchPlugin_ConstraintRadius::ID());
620 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
622 // Display all sketcher sub-Objects
623 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
624 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
625 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
627 // Hide sketcher result
628 std::list<ResultPtr> aResults = myCurrentSketch->results();
629 std::list<ResultPtr>::const_iterator aIt;
630 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
631 aDisplayer->erase((*aIt), false);
633 aDisplayer->erase(myCurrentSketch, false);
635 // Display sketcher objects
636 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
637 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
638 std::list<ResultPtr> aResults = aFeature->results();
639 std::list<ResultPtr>::const_iterator aIt;
640 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
641 aDisplayer->display((*aIt), false);
643 aDisplayer->display(aFeature, false);
646 if (myPlaneFilter.IsNull())
647 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
649 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
650 if (theOperation->isEditOperation()) {
651 // If it is editing of sketch then it means that plane is already defined
652 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
653 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
655 aDisplayer->updateViewer();
658 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
660 myIsMouseOverWindow = false;
662 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
663 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
665 DataPtr aData = myCurrentSketch->data();
666 if ((!aData) || (!aData->isValid())) {
667 // The sketch was aborted
668 myCurrentSketch = CompositeFeaturePtr();
669 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
671 // Erase all sketcher objects
672 QStringList aSketchIds = sketchOperationIdList();
673 QObjectPtrList aObjects = aDisplayer->displayedObjects();
674 foreach (ObjectPtr aObj, aObjects) {
675 DataPtr aObjData = aObj->data();
676 if ((!aObjData) || (!aObjData->isValid()))
677 aDisplayer->erase(aObj);
681 // Hide all sketcher sub-Objects
682 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
683 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
684 std::list<ResultPtr> aResults = aFeature->results();
685 std::list<ResultPtr>::const_iterator aIt;
686 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
687 aDisplayer->erase((*aIt), false);
689 aDisplayer->erase(aFeature, false);
691 // Display sketcher result
692 std::list<ResultPtr> aResults = myCurrentSketch->results();
693 std::list<ResultPtr>::const_iterator aIt;
694 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
695 aDisplayer->display((*aIt), false);
697 aDisplayer->display(myCurrentSketch);
699 myCurrentSketch = CompositeFeaturePtr();
700 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
701 aDisplayer->updateViewer();
704 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
706 connectToPropertyPanel(true);
709 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
711 connectToPropertyPanel(false);
712 myIsPropertyPanelValueChanged = false;
713 myIsMouseOverViewProcessed = true;
716 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
718 if (isNestedCreateOperation(theOperation))
719 visualizeFeature(theOperation, true);
722 bool PartSet_SketcherMgr::canUndo() const
724 return isNestedCreateOperation(getCurrentOperation());
727 bool PartSet_SketcherMgr::canRedo() const
729 return isNestedCreateOperation(getCurrentOperation());
732 bool PartSet_SketcherMgr::canDisplayObject() const
734 bool aCanDisplay = true;
735 if (!isNestedCreateOperation(getCurrentOperation()))
738 ModuleBase_Operation* aOperation = getCurrentOperation();
739 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
740 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
741 // the active widget editor should not influence here. The presentation should be visible always
742 // when this widget is active.
744 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
749 if (myIsPopupMenuActive)
752 // during a nested create operation, the feature is redisplayed only if the mouse over view
753 // of there was a value modified in the property panel after the mouse left the view
754 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
758 bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
760 bool anEnabled = false;
761 ModuleBase_Operation* anOperation = getCurrentOperation();
763 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
764 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
768 QObjectPtrList anObjects;
769 // 1. change auxiliary type of a created feature
770 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
771 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
772 anObjects.append(anOperation->feature());
775 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
776 anOperation->abort();
777 // 2. change auxiliary type of selected sketch entities
778 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
779 anObjects = aSelection->selectedPresentations();
781 anEnabled = anObjects.size() > 0;
783 bool isNotAuxiliaryFound = false;
784 if (anObjects.size() > 0) {
785 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
786 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
787 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
788 if (aFeature.get() != NULL) {
789 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
790 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
791 if (aSketchFeature.get() != NULL) {
792 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
794 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
795 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
796 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
801 theValue = anObjects.size() && !isNotAuxiliaryFound;
805 void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
807 ModuleBase_Operation* anOperation = getCurrentOperation();
809 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
810 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
814 QObjectPtrList anObjects;
815 bool isUseTransaction = false;
816 // 1. change auxiliary type of a created feature
817 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
818 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
819 anObjects.append(anOperation->feature());
822 isUseTransaction = true;
823 // 2. change auxiliary type of selected sketch entities
824 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
825 anObjects = aSelection->selectedPresentations();
828 QAction* anAction = myModule->action("AUXILIARY_CMD");
829 SessionPtr aMgr = ModelAPI_Session::get();
830 if (isUseTransaction) {
831 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
832 anOperation->abort();
833 aMgr->startOperation(anAction->text().toStdString());
837 if (anObjects.size() > 0) {
838 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
839 for (; anIt != aLast; anIt++) {
840 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
841 if (aFeature.get() != NULL) {
842 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
843 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
844 if (aSketchFeature.get() != NULL) {
845 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
847 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
848 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
849 anAuxiliaryAttr->setValue(isChecked);
854 if (isUseTransaction) {
855 aMgr->finishOperation();
857 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
861 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
863 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
866 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
867 const FeaturePtr& theSketch,
868 ModuleBase_IWorkshop* theWorkshop,
869 FeatureToSelectionMap& theSelection)
871 if (theFeature.get() == NULL)
874 std::set<AttributePtr> aSelectedAttributes;
875 std::set<ResultPtr> aSelectedResults;
877 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
878 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
879 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
880 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
882 std::list<ResultPtr> aResults = theFeature->results();
883 std::list<ResultPtr>::const_iterator aIt;
884 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
886 ResultPtr aResult = *aIt;
887 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
888 if (aAISObj.get() == NULL)
890 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
891 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
893 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
894 aContext->SelectedOwner());
895 if (aBRepOwner.IsNull())
897 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
898 aBRepOwner->Selectable());
902 if (aBRepOwner->HasShape()) {
903 const TopoDS_Shape& aShape = aBRepOwner->Shape();
904 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
905 if (aShapeType == TopAbs_VERTEX) {
906 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
908 if (aPntAttr.get() != NULL)
909 aSelectedAttributes.insert(aPntAttr);
911 else if (aShapeType == TopAbs_EDGE &&
912 aSelectedResults.find(aResult) == aSelectedResults.end()) {
913 aSelectedResults.insert(aResult);
918 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
921 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
922 const FeaturePtr& theSketch,
923 ModuleBase_IWorkshop* theWorkshop,
924 const FeatureToSelectionMap& theSelection,
925 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
927 if (theFeature.get() == NULL)
930 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
931 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
932 std::set<ResultPtr> aSelectedResults = anIt.value().second;
934 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
935 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
936 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
937 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
939 // 1. found the feature's owners. Check the AIS objects of the constructions
940 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
941 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
942 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
944 SelectMgr_IndexedMapOfOwner aSelectedOwners;
945 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
946 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
947 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
948 if (!anOwner.IsNull())
949 anOwnersToSelect.Add(anOwner);
953 // 2. found the feature results's owners
954 std::list<ResultPtr> aResults = theFeature->results();
955 std::list<ResultPtr>::const_iterator aIt;
956 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
958 ResultPtr aResult = *aIt;
959 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
960 if (aAISObj.get() == NULL)
962 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
964 SelectMgr_IndexedMapOfOwner aSelectedOwners;
965 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
966 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
967 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
968 if ( anOwner.IsNull() || !anOwner->HasShape() )
970 const TopoDS_Shape& aShape = anOwner->Shape();
971 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
972 if (aShapeType == TopAbs_VERTEX) {
973 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
974 if (aPntAttr.get() != NULL &&
975 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
976 anOwnersToSelect.Add(anOwner);
979 else if (aShapeType == TopAbs_EDGE) {
980 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
981 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
982 anOwnersToSelect.FindIndex(anOwner) <= 0)
983 anOwnersToSelect.Add(anOwner);
989 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
991 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
992 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
993 XGUI_Workshop* aWorkshop = aConnector->workshop();
994 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
995 if (aPropertyPanel) {
996 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
997 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
999 connect(aWidget, SIGNAL(beforeValuesChanged()),
1000 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1001 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1002 connect(aWidget, SIGNAL(afterValuesChanged()),
1003 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1006 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1007 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1008 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1009 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1010 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1016 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1018 return myModule->workshop()->currentOperation();
1021 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1022 const bool isToDisplay)
1024 if (!theOperation || theOperation->isEditOperation())
1027 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1028 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1029 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1031 // 1. change visibility of the object itself, here the presentable object is processed,
1032 // e.g. constraints features
1033 FeaturePtr aFeature = theOperation->feature();
1034 std::list<ResultPtr> aResults = aFeature->results();
1036 aDisplayer->display(aFeature, false);
1038 aDisplayer->erase(aFeature, false);
1040 // change visibility of the object results, e.g. non-constraint features
1041 std::list<ResultPtr>::const_iterator aIt;
1042 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1044 aDisplayer->display(*aIt, false);
1047 aDisplayer->erase(*aIt, false);
1050 aDisplayer->updateViewer();
1053 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1055 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1056 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1057 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1059 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1060 if (theHighlightedOnly) {
1061 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1064 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1066 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1067 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1070 // 1. it is necessary to save current selection in order to restore it after the features moving
1071 myCurrentSelection.clear();
1072 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1073 aLast = aFeature2AttributeMap.end();
1074 for (; anIt != aLast; anIt++) {
1075 FeaturePtr aFeature = anIt.key();
1076 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1078 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1081 void PartSet_SketcherMgr::restoreSelection()
1083 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1084 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1085 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1086 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1087 aSLast = myCurrentSelection.end();
1088 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1089 for (; aSIt != aSLast; aSIt++) {
1090 anOwnersToSelect.Clear();
1091 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1093 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);