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>
51 #include <SketchPlugin_ConstraintHorizontal.h>
52 #include <SketchPlugin_ConstraintVertical.h>
53 #include <SketchPlugin_ConstraintEqual.h>
55 #include <SelectMgr_IndexedMapOfOwner.hxx>
56 #include <StdSelect_BRepOwner.hxx>
58 //#include <AIS_DimensionSelectionMode.hxx>
59 //#include <AIS_Shape.hxx>
61 #include <ModelAPI_Events.h>
62 #include <ModelAPI_Session.h>
64 #include <QMouseEvent>
65 #include <QApplication>
68 /// Returns list of unique objects by sum of objects from List1 and List2
69 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
70 const QList<ModuleBase_ViewerPrs>& theList2)
72 QList<ModuleBase_ViewerPrs> aRes;
73 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
74 if (!aRes.contains(aPrs))
77 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
78 if (!aRes.contains(aPrs))
84 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
85 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
86 const FeaturePtr theSketch)
88 QList<ModuleBase_ViewerPrs> aRes;
90 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
91 aLast = theList.end();
92 for (; anIt != aLast; anIt++)
94 ModuleBase_ViewerPrs aPrs = *anIt;
95 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
96 if (aFeature.get() == NULL)
99 QList<AttributePtr> anAttributes;
100 if (theFeature2AttributeMap.contains(aFeature)) {
101 anAttributes = theFeature2AttributeMap[aFeature];
104 TopoDS_Shape aShape = aPrs.shape();
105 if (!aShape.IsNull()) {
106 if (aShape.ShapeType() == TopAbs_VERTEX) {
107 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
108 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
109 anAttributes.push_back(anAttr);
112 theFeature2AttributeMap[aFeature] = anAttributes;
119 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
120 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
121 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
122 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
123 myIsPopupMenuActive(false)
125 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
126 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
128 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
130 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
131 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
133 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
134 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
136 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
137 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
139 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
140 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
142 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
143 XGUI_Workshop* aWorkshop = aConnector->workshop();
144 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
147 PartSet_SketcherMgr::~PartSet_SketcherMgr()
149 if (!myPlaneFilter.IsNull())
150 myPlaneFilter.Nullify();
153 void PartSet_SketcherMgr::onEnterViewPort()
155 if (!isNestedCreateOperation(getCurrentOperation()))
157 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
158 // created feature because it should be done in onMouseMove(). Some widgets watch
159 // the mouse move and use the cursor position to update own values. If the presentaion is
160 // redisplayed before this update, the feature presentation jumps from reset value to current.
161 myIsMouseOverWindow = true;
162 myIsPropertyPanelValueChanged = false;
165 void PartSet_SketcherMgr::onLeaveViewPort()
167 if (!isNestedCreateOperation(getCurrentOperation()))
169 // the method should be performed if the popup menu is called,
170 // the reset of the current widget should not happen
171 if (myIsPopupMenuActive)
174 myIsMouseOverViewProcessed = false;
175 myIsMouseOverWindow = false;
177 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
178 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
179 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
180 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
181 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
182 // obtained after reset value
183 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
184 ModuleBase_Operation* aOperation = getCurrentOperation();
185 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
186 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
190 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
192 // hides the presentation of the current operation feature
193 myIsPropertyPanelValueChanged = false;
194 // the feature is to be erased here, but it is correct to call canDisplayObject because
195 // there can be additional check (e.g. editor widget in distance constraint)
196 visualizeFeature(aOperation, canDisplayObject());
199 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
201 if (isNestedCreateOperation(getCurrentOperation()))
203 // it is necessary to save current selection in order to restore it after the values are modifed
206 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
207 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
208 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
209 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
212 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
214 if (isNestedCreateOperation(getCurrentOperation()))
216 // it is necessary to restore current selection in order to restore it after the values are modified
218 myCurrentSelection.clear();
220 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
221 // viewer happens by deselect/select the modified objects. The flag should be restored after
222 // the selection processing. The update viewer should be also called.
223 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
224 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
225 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
226 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
227 aDisplayer->updateViewer();
230 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
232 if (!isNestedCreateOperation(getCurrentOperation()))
235 // visualize the current operation feature
236 myIsPropertyPanelValueChanged = true;
237 ModuleBase_Operation* aOperation = getCurrentOperation();
238 // the feature is to be erased here, but it is correct to call canDisplayObject because
239 // there can be additional check (e.g. editor widget in distance constraint)
240 visualizeFeature(aOperation, canDisplayObject());
243 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
245 get2dPoint(theWnd, theEvent, myClickedPoint);
247 if (!(theEvent->buttons() & Qt::LeftButton))
250 // Clear dragging mode
251 myIsDragging = false;
253 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
254 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
255 if (!aViewer->canDragByMouse())
258 ModuleBase_Operation* aOperation = getCurrentOperation();
259 if (aOperation && aOperation->isEditOperation()) {
260 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
261 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
262 // If the current widget is a selector, do do nothing, it processes the mouse press
263 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
268 // Use only for sketch operations
269 if (aOperation && myCurrentSketch) {
270 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
273 bool isSketcher = isSketchOperation(aOperation);
274 bool isSketchOpe = isNestedSketchOperation(aOperation);
276 // Avoid non-sketch operations
277 if ((!isSketchOpe) && (!isSketcher))
280 bool isEditing = aOperation->isEditOperation();
282 // Ignore creation sketch operation
283 if ((!isSketcher) && (!isEditing))
286 // MoveTo in order to highlight current object
287 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
289 // Remember highlighted objects for editing
290 ModuleBase_ISelection* aSelect = aWorkshop->selection();
292 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
293 storeSelection(!aHasShift);
295 if (myCurrentSelection.empty()) {
296 if (isSketchOpe && (!isSketcher))
297 // commit previous operation
298 if (!aOperation->commit())
305 get2dPoint(theWnd, theEvent, myCurrentPoint);
309 } else if (isSketchOpe && isEditing) {
310 // If selected another object commit current result
311 aOperation->commit();
314 get2dPoint(theWnd, theEvent, myCurrentPoint);
317 // This is necessary in order to finalize previous operation
318 QApplication::processEvents();
324 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
326 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
327 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
328 if (!aViewer->canDragByMouse())
330 ModuleBase_Operation* aOp = getCurrentOperation();
332 if (isNestedSketchOperation(aOp)) {
333 get2dPoint(theWnd, theEvent, myClickedPoint);
335 // Only for sketcher operations
339 myCurrentSelection.clear();
340 // Reselect edited object
341 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
342 if (theEvent->modifiers() & Qt::ShiftModifier)
343 aViewer->AISContext()->ShiftSelect();
345 aViewer->AISContext()->Select();
351 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
352 myIsDragging = false;
355 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
357 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
358 myIsMouseOverViewProcessed = true;
359 // 1. perform the widget mouse move functionality and display the presentation
360 ModuleBase_Operation* aOperation = getCurrentOperation();
361 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
362 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
363 // the mouse move should be processed in the widget, if it can in order to visualize correct
364 // presentation. These widgets correct the feature attribute according to the mouse position
365 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
367 aPoint2DWdg->onMouseMove(theWnd, theEvent);
369 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
372 aDistanceWdg->onMouseMove(theWnd, theEvent);
374 // the feature is to be erased here, but it is correct to call canDisplayObject because
375 // there can be additional check (e.g. editor widget in distance constraint)
376 visualizeFeature(aOperation, canDisplayObject());
379 myClickedPoint.clear();
382 // 1. the current selection is saved in the mouse press method in order to restore it after moving
383 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
384 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
385 // deselected). This flag should be restored in the slot, processed the mouse release signal.
386 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
387 aViewer->enableSelection(false);
389 ModuleBase_Operation* aOperation = getCurrentOperation();
392 if (isSketchOperation(aOperation))
393 return; // No edit operation activated
395 Handle(V3d_View) aView = theWnd->v3dView();
396 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
398 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
399 double dX = aX - myCurrentPoint.myCurX;
400 double dY = aY - myCurrentPoint.myCurY;
402 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
403 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
404 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
405 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
406 // viewer happens by deselect/select the modified objects. The flag should be restored after
407 // the selection processing. The update viewer should be also called.
408 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
410 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
411 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
412 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
413 aLast = myCurrentSelection.end();
414 // 4. the features and attributes modification(move)
415 for (; anIt != aLast; anIt++) {
416 FeaturePtr aFeature = anIt.key();
418 std::set<AttributePtr> anAttributes = anIt.value().first;
419 // Process selection by attribute: the priority to the attribute
420 if (!anAttributes.empty()) {
421 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
422 anAttLast = anAttributes.end();
423 for (; anAttIt != anAttLast; anAttIt++) {
424 AttributePtr anAttr = *anAttIt;
425 if (anAttr.get() == NULL)
427 std::string aAttrId = anAttr->id();
428 DataPtr aData = aFeature->data();
429 if (aData.get() != NULL) {
430 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
431 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
432 if (aPoint.get() != NULL) {
433 bool isImmutable = aPoint->setImmutable(true);
434 aPoint->move(dX, dY);
435 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
436 aPoint->setImmutable(isImmutable);
441 // Process selection by feature
442 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
443 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
444 if (aSketchFeature) {
445 aSketchFeature->move(dX, dY);
446 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
450 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
451 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
453 // 5. it is necessary to save current selection in order to restore it after the features moving
455 // 6. restore the update viewer flag and call this update
456 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
457 aDisplayer->updateViewer();
460 myCurrentPoint.setValue(aX, aY);
464 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
466 ModuleBase_Operation* aOperation = getCurrentOperation();
467 if (aOperation && aOperation->isEditOperation()) {
468 std::string aId = aOperation->id().toStdString();
469 if (isDistanceOperation(aOperation))
471 // Activate dimension value editing on double click
472 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
473 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
474 // Find corresponded widget to activate value editing
475 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
476 if (aWgt->attributeID() == "ConstraintValue") {
485 void PartSet_SketcherMgr::onApplicationStarted()
487 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
488 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
489 XGUI_Workshop* aWorkshop = aConnector->workshop();
490 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
491 if (aPropertyPanel) {
492 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
493 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
496 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
497 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
498 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
500 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
501 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
502 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
505 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
507 if (!myClickedPoint.myIsInitialized)
510 ModuleBase_Operation* aOperation = getCurrentOperation();
511 // the distance constraint feature should not use the clickedd point
512 // this is workaround in order to don't throw down the flyout point value,
513 // set by execute() method of these type of features
514 if (isDistanceOperation(aOperation))
517 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
519 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
523 void PartSet_SketcherMgr::onBeforeContextMenu()
525 myIsPopupMenuActive = true;
528 void PartSet_SketcherMgr::onAfterContextMenu()
530 myIsPopupMenuActive = false;
533 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
536 Handle(V3d_View) aView = theWnd->v3dView();
537 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
539 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
540 thePoint.setValue(aX, anY);
543 void PartSet_SketcherMgr::launchEditing()
545 // there should be activate the vertex selection mode because the edit can happens by the selected
548 aModes << TopAbs_VERTEX << TopAbs_EDGE;
549 // TODO: #391 - to be uncommented
550 /*aModes.append(AIS_DSM_Text);
551 aModes.append(AIS_DSM_Line);
552 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
553 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
555 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
556 aConnector->activateSubShapesSelection(aModes);
558 if (!myCurrentSelection.empty()) {
559 FeaturePtr aFeature = myCurrentSelection.begin().key();
560 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
561 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
563 myModule->editFeature(aSPFeature);
570 QStringList PartSet_SketcherMgr::sketchOperationIdList()
572 static QStringList aIds;
573 if (aIds.size() == 0) {
574 aIds << SketchPlugin_Line::ID().c_str();
575 aIds << SketchPlugin_Point::ID().c_str();
576 aIds << SketchPlugin_Arc::ID().c_str();
577 aIds << SketchPlugin_Circle::ID().c_str();
578 aIds << SketchPlugin_ConstraintLength::ID().c_str();
579 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
580 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
581 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
582 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
583 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
584 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
585 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
586 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
591 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
593 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
596 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
598 return theOperation &&
599 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
602 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
604 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
607 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
609 std::string aId = theOperation ? theOperation->id().toStdString() : "";
611 return (aId == SketchPlugin_Line::ID()) ||
612 (aId == SketchPlugin_Point::ID()) ||
613 (aId == SketchPlugin_Arc::ID()) ||
614 (aId == SketchPlugin_Circle::ID());
617 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
619 std::string aId = theOperation ? theOperation->id().toStdString() : "";
621 return (aId == SketchPlugin_ConstraintLength::ID()) ||
622 (aId == SketchPlugin_ConstraintDistance::ID()) ||
623 (aId == SketchPlugin_ConstraintRadius::ID());
626 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
628 // Display all sketcher sub-Objects
629 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
630 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
631 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
633 // Hide sketcher result
634 std::list<ResultPtr> aResults = myCurrentSketch->results();
635 std::list<ResultPtr>::const_iterator aIt;
636 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
637 aDisplayer->erase((*aIt), false);
639 aDisplayer->erase(myCurrentSketch, false);
641 // Display sketcher objects
642 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
643 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
644 std::list<ResultPtr> aResults = aFeature->results();
645 std::list<ResultPtr>::const_iterator aIt;
646 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
647 aDisplayer->display((*aIt), false);
649 aDisplayer->display(aFeature, false);
652 if (myPlaneFilter.IsNull())
653 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
655 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
656 if (theOperation->isEditOperation()) {
657 // If it is editing of sketch then it means that plane is already defined
658 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
659 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
661 aDisplayer->updateViewer();
664 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
666 myIsMouseOverWindow = false;
668 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
669 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
671 DataPtr aData = myCurrentSketch->data();
672 if ((!aData) || (!aData->isValid())) {
673 // The sketch was aborted
674 myCurrentSketch = CompositeFeaturePtr();
675 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
677 // Erase all sketcher objects
678 QStringList aSketchIds = sketchOperationIdList();
679 QObjectPtrList aObjects = aDisplayer->displayedObjects();
680 foreach (ObjectPtr aObj, aObjects) {
681 DataPtr aObjData = aObj->data();
682 if ((!aObjData) || (!aObjData->isValid()))
683 aDisplayer->erase(aObj);
687 // Hide all sketcher sub-Objects
688 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
689 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
690 std::list<ResultPtr> aResults = aFeature->results();
691 std::list<ResultPtr>::const_iterator aIt;
692 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
693 aDisplayer->erase((*aIt), false);
695 aDisplayer->erase(aFeature, false);
697 // Display sketcher result
698 std::list<ResultPtr> aResults = myCurrentSketch->results();
699 std::list<ResultPtr>::const_iterator aIt;
700 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
701 aDisplayer->display((*aIt), false);
703 aDisplayer->display(myCurrentSketch);
705 myCurrentSketch = CompositeFeaturePtr();
706 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
707 aDisplayer->updateViewer();
710 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
712 connectToPropertyPanel(true);
715 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
717 connectToPropertyPanel(false);
718 myIsPropertyPanelValueChanged = false;
719 myIsMouseOverViewProcessed = true;
722 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
724 if (isNestedCreateOperation(theOperation))
725 visualizeFeature(theOperation, true);
728 bool PartSet_SketcherMgr::canUndo() const
730 return isNestedCreateOperation(getCurrentOperation());
733 bool PartSet_SketcherMgr::canRedo() const
735 return isNestedCreateOperation(getCurrentOperation());
738 bool PartSet_SketcherMgr::canDisplayObject() const
740 bool aCanDisplay = true;
741 if (!isNestedCreateOperation(getCurrentOperation()))
744 ModuleBase_Operation* aOperation = getCurrentOperation();
745 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
746 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
747 // the active widget editor should not influence here. The presentation should be visible always
748 // when this widget is active.
750 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
755 if (myIsPopupMenuActive)
758 // during a nested create operation, the feature is redisplayed only if the mouse over view
759 // of there was a value modified in the property panel after the mouse left the view
760 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
764 bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
766 bool anEnabled = false;
767 ModuleBase_Operation* anOperation = getCurrentOperation();
769 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
770 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
774 QObjectPtrList anObjects;
775 // 1. change auxiliary type of a created feature
776 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
777 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
778 anObjects.append(anOperation->feature());
781 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
782 anOperation->abort();
783 // 2. change auxiliary type of selected sketch entities
784 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
785 anObjects = aSelection->selectedPresentations();
787 anEnabled = anObjects.size() > 0;
789 bool isNotAuxiliaryFound = false;
790 if (anObjects.size() > 0) {
791 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
792 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
793 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
794 if (aFeature.get() != NULL) {
795 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
796 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
797 if (aSketchFeature.get() != NULL) {
798 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
800 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
801 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
802 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
807 theValue = anObjects.size() && !isNotAuxiliaryFound;
811 void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
813 ModuleBase_Operation* anOperation = getCurrentOperation();
815 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
816 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
820 QObjectPtrList anObjects;
821 bool isUseTransaction = false;
822 // 1. change auxiliary type of a created feature
823 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
824 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
825 anObjects.append(anOperation->feature());
828 isUseTransaction = true;
829 // 2. change auxiliary type of selected sketch entities
830 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
831 anObjects = aSelection->selectedPresentations();
834 QAction* anAction = myModule->action("AUXILIARY_CMD");
835 SessionPtr aMgr = ModelAPI_Session::get();
836 if (isUseTransaction) {
837 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
838 anOperation->abort();
839 aMgr->startOperation(anAction->text().toStdString());
843 if (anObjects.size() > 0) {
844 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
845 for (; anIt != aLast; anIt++) {
846 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
847 if (aFeature.get() != NULL) {
848 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
849 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
850 if (aSketchFeature.get() != NULL) {
851 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
853 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
854 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
855 anAuxiliaryAttr->setValue(isChecked);
860 if (isUseTransaction) {
861 aMgr->finishOperation();
863 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
867 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
869 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
872 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
873 const FeaturePtr& theSketch,
874 ModuleBase_IWorkshop* theWorkshop,
875 FeatureToSelectionMap& theSelection)
877 if (theFeature.get() == NULL)
880 std::set<AttributePtr> aSelectedAttributes;
881 std::set<ResultPtr> aSelectedResults;
883 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
884 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
885 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
886 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
888 std::list<ResultPtr> aResults = theFeature->results();
889 std::list<ResultPtr>::const_iterator aIt;
890 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
892 ResultPtr aResult = *aIt;
893 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
894 if (aAISObj.get() == NULL)
896 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
897 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
899 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
900 aContext->SelectedOwner());
901 if (aBRepOwner.IsNull())
903 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
904 aBRepOwner->Selectable());
908 if (aBRepOwner->HasShape()) {
909 const TopoDS_Shape& aShape = aBRepOwner->Shape();
910 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
911 if (aShapeType == TopAbs_VERTEX) {
912 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
914 if (aPntAttr.get() != NULL)
915 aSelectedAttributes.insert(aPntAttr);
917 else if (aShapeType == TopAbs_EDGE &&
918 aSelectedResults.find(aResult) == aSelectedResults.end()) {
919 aSelectedResults.insert(aResult);
924 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
927 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
928 const FeaturePtr& theSketch,
929 ModuleBase_IWorkshop* theWorkshop,
930 const FeatureToSelectionMap& theSelection,
931 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
933 if (theFeature.get() == NULL)
936 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
937 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
938 std::set<ResultPtr> aSelectedResults = anIt.value().second;
940 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
941 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
942 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
943 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
945 // 1. found the feature's owners. Check the AIS objects of the constructions
946 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
947 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
948 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
950 SelectMgr_IndexedMapOfOwner aSelectedOwners;
951 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
952 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
953 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
954 if (!anOwner.IsNull())
955 anOwnersToSelect.Add(anOwner);
959 // 2. found the feature results's owners
960 std::list<ResultPtr> aResults = theFeature->results();
961 std::list<ResultPtr>::const_iterator aIt;
962 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
964 ResultPtr aResult = *aIt;
965 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
966 if (aAISObj.get() == NULL)
968 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
970 SelectMgr_IndexedMapOfOwner aSelectedOwners;
971 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
972 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
973 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
974 if ( anOwner.IsNull() || !anOwner->HasShape() )
976 const TopoDS_Shape& aShape = anOwner->Shape();
977 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
978 if (aShapeType == TopAbs_VERTEX) {
979 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
980 if (aPntAttr.get() != NULL &&
981 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
982 anOwnersToSelect.Add(anOwner);
985 else if (aShapeType == TopAbs_EDGE) {
986 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
987 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
988 anOwnersToSelect.FindIndex(anOwner) <= 0)
989 anOwnersToSelect.Add(anOwner);
995 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
997 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
998 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
999 XGUI_Workshop* aWorkshop = aConnector->workshop();
1000 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1001 if (aPropertyPanel) {
1002 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1003 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1005 connect(aWidget, SIGNAL(beforeValuesChanged()),
1006 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1007 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1008 connect(aWidget, SIGNAL(afterValuesChanged()),
1009 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1012 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1013 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1014 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1015 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1016 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1022 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1024 return myModule->workshop()->currentOperation();
1027 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1028 const bool isToDisplay)
1030 if (!theOperation || theOperation->isEditOperation())
1033 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1034 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1035 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1037 // 1. change visibility of the object itself, here the presentable object is processed,
1038 // e.g. constraints features
1039 FeaturePtr aFeature = theOperation->feature();
1040 std::list<ResultPtr> aResults = aFeature->results();
1042 aDisplayer->display(aFeature, false);
1044 aDisplayer->erase(aFeature, false);
1046 // change visibility of the object results, e.g. non-constraint features
1047 std::list<ResultPtr>::const_iterator aIt;
1048 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1050 aDisplayer->display(*aIt, false);
1053 aDisplayer->erase(*aIt, false);
1056 aDisplayer->updateViewer();
1059 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1061 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1062 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1063 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1065 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1066 if (theHighlightedOnly) {
1067 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1070 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1072 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1073 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1076 // 1. it is necessary to save current selection in order to restore it after the features moving
1077 myCurrentSelection.clear();
1078 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1079 aLast = aFeature2AttributeMap.end();
1080 for (; anIt != aLast; anIt++) {
1081 FeaturePtr aFeature = anIt.key();
1082 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1084 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1087 void PartSet_SketcherMgr::restoreSelection()
1089 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1090 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1091 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1092 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1093 aSLast = myCurrentSelection.end();
1094 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1095 for (; aSIt != aSLast; aSIt++) {
1096 anOwnersToSelect.Clear();
1097 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1099 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);