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()))
167 myIsMouseOverViewProcessed = false;
168 myIsMouseOverWindow = false;
170 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
171 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
172 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
173 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
174 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
175 // obtained after reset value
176 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
177 ModuleBase_Operation* aOperation = getCurrentOperation();
178 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
179 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
183 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
185 // hides the presentation of the current operation feature
186 myIsPropertyPanelValueChanged = false;
187 // the feature is to be erased here, but it is correct to call canDisplayObject because
188 // there can be additional check (e.g. editor widget in distance constraint)
189 visualizeFeature(aOperation, canDisplayObject());
192 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
194 if (isNestedCreateOperation(getCurrentOperation()))
196 // it is necessary to save current selection in order to restore it after the values are modifed
199 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
200 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
201 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
202 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
205 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
207 if (isNestedCreateOperation(getCurrentOperation()))
209 // it is necessary to restore current selection in order to restore it after the values are modified
211 myCurrentSelection.clear();
213 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
214 // viewer happens by deselect/select the modified objects. The flag should be restored after
215 // the selection processing. The update viewer should be also called.
216 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
217 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
218 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
219 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
220 aDisplayer->updateViewer();
223 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
225 if (!isNestedCreateOperation(getCurrentOperation()))
228 // visualize the current operation feature
229 myIsPropertyPanelValueChanged = true;
230 ModuleBase_Operation* aOperation = getCurrentOperation();
231 // the feature is to be erased here, but it is correct to call canDisplayObject because
232 // there can be additional check (e.g. editor widget in distance constraint)
233 visualizeFeature(aOperation, canDisplayObject());
236 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
238 get2dPoint(theWnd, theEvent, myClickedPoint);
240 if (!(theEvent->buttons() & Qt::LeftButton))
243 // Clear dragging mode
244 myIsDragging = false;
246 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
247 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
248 if (!aViewer->canDragByMouse())
251 ModuleBase_Operation* aOperation = getCurrentOperation();
252 if (aOperation && aOperation->isEditOperation()) {
253 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
254 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
255 // If the current widget is a selector, do do nothing, it processes the mouse press
256 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
261 // Use only for sketch operations
262 if (aOperation && myCurrentSketch) {
263 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
266 bool isSketcher = isSketchOperation(aOperation);
267 bool isSketchOpe = isNestedSketchOperation(aOperation);
269 // Avoid non-sketch operations
270 if ((!isSketchOpe) && (!isSketcher))
273 bool isEditing = aOperation->isEditOperation();
275 // Ignore creation sketch operation
276 if ((!isSketcher) && (!isEditing))
279 // MoveTo in order to highlight current object
280 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
282 // Remember highlighted objects for editing
283 ModuleBase_ISelection* aSelect = aWorkshop->selection();
285 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
286 storeSelection(!aHasShift);
288 if (myCurrentSelection.empty()) {
289 if (isSketchOpe && (!isSketcher))
290 // commit previous operation
291 if (!aOperation->commit())
298 get2dPoint(theWnd, theEvent, myCurrentPoint);
302 } else if (isSketchOpe && isEditing) {
303 // If selected another object commit current result
304 aOperation->commit();
307 get2dPoint(theWnd, theEvent, myCurrentPoint);
310 // This is necessary in order to finalize previous operation
311 QApplication::processEvents();
317 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
319 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
320 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
321 if (!aViewer->canDragByMouse())
323 ModuleBase_Operation* aOp = getCurrentOperation();
325 if (isNestedSketchOperation(aOp)) {
326 get2dPoint(theWnd, theEvent, myClickedPoint);
328 // Only for sketcher operations
332 myCurrentSelection.clear();
333 // Reselect edited object
334 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
335 if (theEvent->modifiers() & Qt::ShiftModifier)
336 aViewer->AISContext()->ShiftSelect();
338 aViewer->AISContext()->Select();
344 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
345 myIsDragging = false;
348 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
350 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
351 myIsMouseOverViewProcessed = true;
352 // 1. perform the widget mouse move functionality and display the presentation
353 ModuleBase_Operation* aOperation = getCurrentOperation();
354 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
355 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
356 // the mouse move should be processed in the widget, if it can in order to visualize correct
357 // presentation. These widgets correct the feature attribute according to the mouse position
358 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
360 aPoint2DWdg->onMouseMove(theWnd, theEvent);
362 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
365 aDistanceWdg->onMouseMove(theWnd, theEvent);
367 // the feature is to be erased here, but it is correct to call canDisplayObject because
368 // there can be additional check (e.g. editor widget in distance constraint)
369 visualizeFeature(aOperation, canDisplayObject());
372 myClickedPoint.clear();
375 // 1. the current selection is saved in the mouse press method in order to restore it after moving
376 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
377 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
378 // deselected). This flag should be restored in the slot, processed the mouse release signal.
379 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
380 aViewer->enableSelection(false);
382 ModuleBase_Operation* aOperation = getCurrentOperation();
385 if (isSketchOperation(aOperation))
386 return; // No edit operation activated
388 Handle(V3d_View) aView = theWnd->v3dView();
389 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
391 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
392 double dX = aX - myCurrentPoint.myCurX;
393 double dY = aY - myCurrentPoint.myCurY;
395 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
396 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
397 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
398 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
399 // viewer happens by deselect/select the modified objects. The flag should be restored after
400 // the selection processing. The update viewer should be also called.
401 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
403 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
404 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
405 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
406 aLast = myCurrentSelection.end();
407 // 4. the features and attributes modification(move)
408 for (; anIt != aLast; anIt++) {
409 FeaturePtr aFeature = anIt.key();
411 std::set<AttributePtr> anAttributes = anIt.value().first;
412 // Process selection by attribute: the priority to the attribute
413 if (!anAttributes.empty()) {
414 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
415 anAttLast = anAttributes.end();
416 for (; anAttIt != anAttLast; anAttIt++) {
417 AttributePtr anAttr = *anAttIt;
418 if (anAttr.get() == NULL)
420 std::string aAttrId = anAttr->id();
421 DataPtr aData = aFeature->data();
422 if (aData.get() != NULL) {
423 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
424 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
425 if (aPoint.get() != NULL) {
426 bool isImmutable = aPoint->setImmutable(true);
427 aPoint->move(dX, dY);
428 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
429 aPoint->setImmutable(isImmutable);
434 // Process selection by feature
435 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
436 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
437 if (aSketchFeature) {
438 aSketchFeature->move(dX, dY);
439 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
443 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
444 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
446 // 5. it is necessary to save current selection in order to restore it after the features moving
448 // 6. restore the update viewer flag and call this update
449 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
450 aDisplayer->updateViewer();
453 myCurrentPoint.setValue(aX, aY);
457 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
459 ModuleBase_Operation* aOperation = getCurrentOperation();
460 if (aOperation && aOperation->isEditOperation()) {
461 std::string aId = aOperation->id().toStdString();
462 if (isDistanceOperation(aOperation))
464 // Activate dimension value editing on double click
465 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
466 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
467 // Find corresponded widget to activate value editing
468 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
469 if (aWgt->attributeID() == "ConstraintValue") {
478 void PartSet_SketcherMgr::onApplicationStarted()
480 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
481 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
482 XGUI_Workshop* aWorkshop = aConnector->workshop();
483 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
484 if (aPropertyPanel) {
485 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
486 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
489 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
490 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
491 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
493 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
494 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
495 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
498 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
500 if (!myClickedPoint.myIsInitialized)
503 ModuleBase_Operation* aOperation = getCurrentOperation();
504 // the distance constraint feature should not use the clickedd point
505 // this is workaround in order to don't throw down the flyout point value,
506 // set by execute() method of these type of features
507 if (isDistanceOperation(aOperation))
510 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
512 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
516 void PartSet_SketcherMgr::onBeforeContextMenu()
518 myIsPopupMenuActive = true;
521 void PartSet_SketcherMgr::onAfterContextMenu()
523 myIsPopupMenuActive = false;
526 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
529 Handle(V3d_View) aView = theWnd->v3dView();
530 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
532 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
533 thePoint.setValue(aX, anY);
536 void PartSet_SketcherMgr::launchEditing()
538 // there should be activate the vertex selection mode because the edit can happens by the selected
541 aModes << TopAbs_VERTEX << TopAbs_EDGE;
542 // TODO: #391 - to be uncommented
543 /*aModes.append(AIS_DSM_Text);
544 aModes.append(AIS_DSM_Line);
545 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
546 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
548 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
549 aConnector->activateSubShapesSelection(aModes);
551 if (!myCurrentSelection.empty()) {
552 FeaturePtr aFeature = myCurrentSelection.begin().key();
553 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
554 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
556 myModule->editFeature(aSPFeature);
563 QStringList PartSet_SketcherMgr::sketchOperationIdList()
565 static QStringList aIds;
566 if (aIds.size() == 0) {
567 aIds << SketchPlugin_Line::ID().c_str();
568 aIds << SketchPlugin_Point::ID().c_str();
569 aIds << SketchPlugin_Arc::ID().c_str();
570 aIds << SketchPlugin_Circle::ID().c_str();
571 aIds << SketchPlugin_ConstraintLength::ID().c_str();
572 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
573 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
574 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
575 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
576 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
581 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
583 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
586 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
588 return theOperation &&
589 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
592 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
594 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
597 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
599 std::string aId = theOperation ? theOperation->id().toStdString() : "";
601 return (aId == SketchPlugin_Line::ID()) ||
602 (aId == SketchPlugin_Point::ID()) ||
603 (aId == SketchPlugin_Arc::ID()) ||
604 (aId == SketchPlugin_Circle::ID());
607 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
609 std::string aId = theOperation ? theOperation->id().toStdString() : "";
611 return (aId == SketchPlugin_ConstraintLength::ID()) ||
612 (aId == SketchPlugin_ConstraintDistance::ID()) ||
613 (aId == SketchPlugin_ConstraintRadius::ID());
616 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
618 // Display all sketcher sub-Objects
619 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
620 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
621 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
623 // Hide sketcher result
624 std::list<ResultPtr> aResults = myCurrentSketch->results();
625 std::list<ResultPtr>::const_iterator aIt;
626 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
627 aDisplayer->erase((*aIt), false);
629 aDisplayer->erase(myCurrentSketch, false);
631 // Display sketcher objects
632 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
633 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
634 std::list<ResultPtr> aResults = aFeature->results();
635 std::list<ResultPtr>::const_iterator aIt;
636 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
637 aDisplayer->display((*aIt), false);
639 aDisplayer->display(aFeature, false);
642 if (myPlaneFilter.IsNull())
643 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
645 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
646 if (theOperation->isEditOperation()) {
647 // If it is editing of sketch then it means that plane is already defined
648 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
649 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
651 aDisplayer->updateViewer();
654 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
656 myIsMouseOverWindow = false;
658 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
659 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
661 DataPtr aData = myCurrentSketch->data();
662 if ((!aData) || (!aData->isValid())) {
663 // The sketch was aborted
664 myCurrentSketch = CompositeFeaturePtr();
665 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
667 // Erase all sketcher objects
668 QStringList aSketchIds = sketchOperationIdList();
669 QObjectPtrList aObjects = aDisplayer->displayedObjects();
670 foreach (ObjectPtr aObj, aObjects) {
671 DataPtr aObjData = aObj->data();
672 if ((!aObjData) || (!aObjData->isValid()))
673 aDisplayer->erase(aObj);
677 // Hide all sketcher sub-Objects
678 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
679 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
680 std::list<ResultPtr> aResults = aFeature->results();
681 std::list<ResultPtr>::const_iterator aIt;
682 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
683 aDisplayer->erase((*aIt), false);
685 aDisplayer->erase(aFeature, false);
687 // Display sketcher result
688 std::list<ResultPtr> aResults = myCurrentSketch->results();
689 std::list<ResultPtr>::const_iterator aIt;
690 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
691 aDisplayer->display((*aIt), false);
693 aDisplayer->display(myCurrentSketch);
695 myCurrentSketch = CompositeFeaturePtr();
696 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
697 aDisplayer->updateViewer();
700 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
702 connectToPropertyPanel(true);
705 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
707 connectToPropertyPanel(false);
708 myIsPropertyPanelValueChanged = false;
709 myIsMouseOverViewProcessed = true;
712 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
714 if (isNestedCreateOperation(theOperation))
715 visualizeFeature(theOperation, true);
718 bool PartSet_SketcherMgr::canUndo() const
720 return isNestedCreateOperation(getCurrentOperation());
723 bool PartSet_SketcherMgr::canRedo() const
725 return isNestedCreateOperation(getCurrentOperation());
728 bool PartSet_SketcherMgr::canDisplayObject() const
730 bool aCanDisplay = true;
731 if (!isNestedCreateOperation(getCurrentOperation()))
734 ModuleBase_Operation* aOperation = getCurrentOperation();
735 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
736 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
737 // the active widget editor should not influence here. The presentation should be visible always
738 // when this widget is active.
740 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
745 if (myIsPopupMenuActive)
748 // during a nested create operation, the feature is redisplayed only if the mouse over view
749 // of there was a value modified in the property panel after the mouse left the view
750 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
754 bool PartSet_SketcherMgr::canChangeConstruction(bool& isConstruction) const
756 bool anEnabled = false;
757 ModuleBase_Operation* anOperation = getCurrentOperation();
759 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
760 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
764 QObjectPtrList anObjects;
765 // 1. change construction type of a created feature
766 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
767 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
768 anObjects.append(anOperation->feature());
771 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
772 anOperation->abort();
773 // 2. change construction type of selected sketch entities
774 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
775 anObjects = aSelection->selectedPresentations();
777 anEnabled = anObjects.size() > 0;
779 bool isNotConstructedFound = false;
780 if (anObjects.size() > 0) {
781 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
782 for (; anIt != aLast && !isNotConstructedFound; anIt++) {
783 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
784 if (aFeature.get() != NULL) {
785 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
786 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
787 if (aSketchFeature.get() != NULL) {
788 std::string anAttribute = SketchPlugin_SketchEntity::CONSTRUCTION_ID();
790 std::shared_ptr<ModelAPI_AttributeBoolean> aConstructionAttr =
791 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
792 isNotConstructedFound = !aConstructionAttr->value();
797 isConstruction = anObjects.size() && !isNotConstructedFound;
801 void PartSet_SketcherMgr::setConstruction(const bool isChecked)
803 ModuleBase_Operation* anOperation = getCurrentOperation();
805 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
806 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
810 QObjectPtrList anObjects;
811 bool isUseTransaction = false;
812 // 1. change construction type of a created feature
813 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
814 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
815 anObjects.append(anOperation->feature());
818 isUseTransaction = true;
819 // 2. change construction type of selected sketch entities
820 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
821 anObjects = aSelection->selectedPresentations();
824 QAction* anAction = myModule->action("CONSTRUCTION_CMD");
825 SessionPtr aMgr = ModelAPI_Session::get();
826 if (isUseTransaction) {
827 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
828 anOperation->abort();
829 aMgr->startOperation(anAction->text().toStdString());
833 if (anObjects.size() > 0) {
834 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
835 for (; anIt != aLast; anIt++) {
836 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
837 if (aFeature.get() != NULL) {
838 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
839 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
840 if (aSketchFeature.get() != NULL) {
841 std::string anAttribute = SketchPlugin_SketchEntity::CONSTRUCTION_ID();
843 std::shared_ptr<ModelAPI_AttributeBoolean> aConstructionAttr =
844 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
845 aConstructionAttr->setValue(isChecked);
850 if (isUseTransaction) {
851 aMgr->finishOperation();
853 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
857 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
859 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
862 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
863 const FeaturePtr& theSketch,
864 ModuleBase_IWorkshop* theWorkshop,
865 FeatureToSelectionMap& theSelection)
867 if (theFeature.get() == NULL)
870 std::set<AttributePtr> aSelectedAttributes;
871 std::set<ResultPtr> aSelectedResults;
873 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
874 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
875 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
876 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
878 std::list<ResultPtr> aResults = theFeature->results();
879 std::list<ResultPtr>::const_iterator aIt;
880 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
882 ResultPtr aResult = *aIt;
883 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
884 if (aAISObj.get() == NULL)
886 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
887 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
889 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
890 aContext->SelectedOwner());
891 if (aBRepOwner.IsNull())
893 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
894 aBRepOwner->Selectable());
898 if (aBRepOwner->HasShape()) {
899 const TopoDS_Shape& aShape = aBRepOwner->Shape();
900 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
901 if (aShapeType == TopAbs_VERTEX) {
902 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
904 if (aPntAttr.get() != NULL)
905 aSelectedAttributes.insert(aPntAttr);
907 else if (aShapeType == TopAbs_EDGE &&
908 aSelectedResults.find(aResult) == aSelectedResults.end()) {
909 aSelectedResults.insert(aResult);
914 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
917 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
918 const FeaturePtr& theSketch,
919 ModuleBase_IWorkshop* theWorkshop,
920 const FeatureToSelectionMap& theSelection,
921 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
923 if (theFeature.get() == NULL)
926 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
927 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
928 std::set<ResultPtr> aSelectedResults = anIt.value().second;
930 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
931 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
932 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
933 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
935 // 1. found the feature's owners. Check the AIS objects of the constructions
936 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
937 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
938 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
940 SelectMgr_IndexedMapOfOwner aSelectedOwners;
941 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
942 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
943 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
944 if (!anOwner.IsNull())
945 anOwnersToSelect.Add(anOwner);
949 // 2. found the feature results's owners
950 std::list<ResultPtr> aResults = theFeature->results();
951 std::list<ResultPtr>::const_iterator aIt;
952 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
954 ResultPtr aResult = *aIt;
955 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
956 if (aAISObj.get() == NULL)
958 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
960 SelectMgr_IndexedMapOfOwner aSelectedOwners;
961 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
962 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
963 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
964 if ( anOwner.IsNull() || !anOwner->HasShape() )
966 const TopoDS_Shape& aShape = anOwner->Shape();
967 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
968 if (aShapeType == TopAbs_VERTEX) {
969 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
970 if (aPntAttr.get() != NULL &&
971 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
972 anOwnersToSelect.Add(anOwner);
975 else if (aShapeType == TopAbs_EDGE) {
976 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
977 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
978 anOwnersToSelect.FindIndex(anOwner) <= 0)
979 anOwnersToSelect.Add(anOwner);
985 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
987 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
988 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
989 XGUI_Workshop* aWorkshop = aConnector->workshop();
990 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
991 if (aPropertyPanel) {
992 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
993 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
995 connect(aWidget, SIGNAL(beforeValuesChanged()),
996 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
997 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
998 connect(aWidget, SIGNAL(afterValuesChanged()),
999 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1002 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1003 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1004 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1005 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1006 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1012 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1014 return myModule->workshop()->currentOperation();
1017 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1018 const bool isToDisplay)
1020 if (!theOperation || theOperation->isEditOperation())
1023 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1024 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1025 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1027 // 1. change visibility of the object itself, here the presentable object is processed,
1028 // e.g. constraints features
1029 FeaturePtr aFeature = theOperation->feature();
1030 std::list<ResultPtr> aResults = aFeature->results();
1032 aDisplayer->display(aFeature, false);
1034 aDisplayer->erase(aFeature, false);
1036 // change visibility of the object results, e.g. non-constraint features
1037 std::list<ResultPtr>::const_iterator aIt;
1038 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1040 aDisplayer->display(*aIt, false);
1043 aDisplayer->erase(*aIt, false);
1046 aDisplayer->updateViewer();
1049 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1051 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1052 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1053 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1055 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1056 if (theHighlightedOnly) {
1057 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1060 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1062 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1063 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1066 // 1. it is necessary to save current selection in order to restore it after the features moving
1067 myCurrentSelection.clear();
1068 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1069 aLast = aFeature2AttributeMap.end();
1070 for (; anIt != aLast; anIt++) {
1071 FeaturePtr aFeature = anIt.key();
1072 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1074 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1077 void PartSet_SketcherMgr::restoreSelection()
1079 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1080 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1081 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1082 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1083 aSLast = myCurrentSelection.end();
1084 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1085 for (; aSIt != aSLast; aSIt++) {
1086 anOwnersToSelect.Clear();
1087 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1089 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);