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>
54 #include <SketchPlugin_ConstraintTangent.h>
56 #include <SelectMgr_IndexedMapOfOwner.hxx>
57 #include <StdSelect_BRepOwner.hxx>
59 //#include <AIS_DimensionSelectionMode.hxx>
60 //#include <AIS_Shape.hxx>
62 #include <ModelAPI_Events.h>
63 #include <ModelAPI_Session.h>
65 #include <QMouseEvent>
66 #include <QApplication>
69 /// Returns list of unique objects by sum of objects from List1 and List2
70 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
71 const QList<ModuleBase_ViewerPrs>& theList2)
73 QList<ModuleBase_ViewerPrs> aRes;
74 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
75 if (!aRes.contains(aPrs))
78 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
79 if (!aRes.contains(aPrs))
85 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
86 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
87 const FeaturePtr theSketch)
89 QList<ModuleBase_ViewerPrs> aRes;
91 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
92 aLast = theList.end();
93 for (; anIt != aLast; anIt++)
95 ModuleBase_ViewerPrs aPrs = *anIt;
96 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
97 if (aFeature.get() == NULL)
100 QList<AttributePtr> anAttributes;
101 if (theFeature2AttributeMap.contains(aFeature)) {
102 anAttributes = theFeature2AttributeMap[aFeature];
105 TopoDS_Shape aShape = aPrs.shape();
106 if (!aShape.IsNull()) {
107 if (aShape.ShapeType() == TopAbs_VERTEX) {
108 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
109 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
110 anAttributes.push_back(anAttr);
113 theFeature2AttributeMap[aFeature] = anAttributes;
120 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
121 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
122 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
123 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
124 myIsPopupMenuActive(false)
126 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
127 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
129 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
131 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
132 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
134 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
135 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
137 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
138 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
140 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
141 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
143 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
144 XGUI_Workshop* aWorkshop = aConnector->workshop();
145 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
148 PartSet_SketcherMgr::~PartSet_SketcherMgr()
150 if (!myPlaneFilter.IsNull())
151 myPlaneFilter.Nullify();
154 void PartSet_SketcherMgr::onEnterViewPort()
156 if (!isNestedCreateOperation(getCurrentOperation()))
158 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
159 // created feature because it should be done in onMouseMove(). Some widgets watch
160 // the mouse move and use the cursor position to update own values. If the presentaion is
161 // redisplayed before this update, the feature presentation jumps from reset value to current.
162 myIsMouseOverWindow = true;
163 myIsPropertyPanelValueChanged = false;
166 void PartSet_SketcherMgr::onLeaveViewPort()
168 if (!isNestedCreateOperation(getCurrentOperation()))
170 // the method should be performed if the popup menu is called,
171 // the reset of the current widget should not happen
172 if (myIsPopupMenuActive)
175 myIsMouseOverViewProcessed = false;
176 myIsMouseOverWindow = false;
178 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
179 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
180 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
181 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
182 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
183 // obtained after reset value
184 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
185 ModuleBase_Operation* aOperation = getCurrentOperation();
186 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
187 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
191 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
193 // hides the presentation of the current operation feature
194 myIsPropertyPanelValueChanged = false;
195 // the feature is to be erased here, but it is correct to call canDisplayObject because
196 // there can be additional check (e.g. editor widget in distance constraint)
197 visualizeFeature(aOperation, canDisplayObject());
200 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
202 if (isNestedCreateOperation(getCurrentOperation()))
204 // it is necessary to save current selection in order to restore it after the values are modifed
207 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
208 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
209 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
210 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
213 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
215 if (isNestedCreateOperation(getCurrentOperation()))
217 // it is necessary to restore current selection in order to restore it after the values are modified
219 myCurrentSelection.clear();
221 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
222 // viewer happens by deselect/select the modified objects. The flag should be restored after
223 // the selection processing. The update viewer should be also called.
224 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
225 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
226 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
227 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
228 aDisplayer->updateViewer();
231 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
233 if (!isNestedCreateOperation(getCurrentOperation()))
236 // visualize the current operation feature
237 myIsPropertyPanelValueChanged = true;
238 ModuleBase_Operation* aOperation = getCurrentOperation();
239 // the feature is to be erased here, but it is correct to call canDisplayObject because
240 // there can be additional check (e.g. editor widget in distance constraint)
241 visualizeFeature(aOperation, canDisplayObject());
244 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
246 get2dPoint(theWnd, theEvent, myClickedPoint);
248 if (!(theEvent->buttons() & Qt::LeftButton))
251 // Clear dragging mode
252 myIsDragging = false;
254 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
255 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
256 if (!aViewer->canDragByMouse())
259 ModuleBase_Operation* aOperation = getCurrentOperation();
260 if (aOperation && aOperation->isEditOperation()) {
261 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
262 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
263 // If the current widget is a selector, do do nothing, it processes the mouse press
264 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
269 // Use only for sketch operations
270 if (aOperation && myCurrentSketch) {
271 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
274 bool isSketcher = isSketchOperation(aOperation);
275 bool isSketchOpe = isNestedSketchOperation(aOperation);
277 // Avoid non-sketch operations
278 if ((!isSketchOpe) && (!isSketcher))
281 bool isEditing = aOperation->isEditOperation();
283 // Ignore creation sketch operation
284 if ((!isSketcher) && (!isEditing))
287 // MoveTo in order to highlight current object
288 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
290 // Remember highlighted objects for editing
291 ModuleBase_ISelection* aSelect = aWorkshop->selection();
293 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
294 storeSelection(!aHasShift);
296 if (myCurrentSelection.empty()) {
297 if (isSketchOpe && (!isSketcher))
298 // commit previous operation
299 if (!aOperation->commit())
306 get2dPoint(theWnd, theEvent, myCurrentPoint);
310 } else if (isSketchOpe && isEditing) {
311 // If selected another object commit current result
312 aOperation->commit();
315 get2dPoint(theWnd, theEvent, myCurrentPoint);
318 // This is necessary in order to finalize previous operation
319 QApplication::processEvents();
325 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
327 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
328 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
329 if (!aViewer->canDragByMouse())
331 ModuleBase_Operation* aOp = getCurrentOperation();
333 if (isNestedSketchOperation(aOp)) {
334 get2dPoint(theWnd, theEvent, myClickedPoint);
336 // Only for sketcher operations
340 myCurrentSelection.clear();
341 // Reselect edited object
342 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
343 if (theEvent->modifiers() & Qt::ShiftModifier)
344 aViewer->AISContext()->ShiftSelect();
346 aViewer->AISContext()->Select();
352 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
353 myIsDragging = false;
356 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
358 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
359 myIsMouseOverViewProcessed = true;
360 // 1. perform the widget mouse move functionality and display the presentation
361 ModuleBase_Operation* aOperation = getCurrentOperation();
362 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
363 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
364 // the mouse move should be processed in the widget, if it can in order to visualize correct
365 // presentation. These widgets correct the feature attribute according to the mouse position
366 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
368 aPoint2DWdg->onMouseMove(theWnd, theEvent);
370 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
373 aDistanceWdg->onMouseMove(theWnd, theEvent);
375 // the feature is to be erased here, but it is correct to call canDisplayObject because
376 // there can be additional check (e.g. editor widget in distance constraint)
377 visualizeFeature(aOperation, canDisplayObject());
380 myClickedPoint.clear();
383 // 1. the current selection is saved in the mouse press method in order to restore it after moving
384 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
385 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
386 // deselected). This flag should be restored in the slot, processed the mouse release signal.
387 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
388 aViewer->enableSelection(false);
390 ModuleBase_Operation* aOperation = getCurrentOperation();
393 if (isSketchOperation(aOperation))
394 return; // No edit operation activated
396 Handle(V3d_View) aView = theWnd->v3dView();
397 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
399 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
400 double dX = aX - myCurrentPoint.myCurX;
401 double dY = aY - myCurrentPoint.myCurY;
403 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
404 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
405 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
406 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
407 // viewer happens by deselect/select the modified objects. The flag should be restored after
408 // the selection processing. The update viewer should be also called.
409 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
411 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
412 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
413 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
414 aLast = myCurrentSelection.end();
415 // 4. the features and attributes modification(move)
416 for (; anIt != aLast; anIt++) {
417 FeaturePtr aFeature = anIt.key();
419 std::set<AttributePtr> anAttributes = anIt.value().first;
420 // Process selection by attribute: the priority to the attribute
421 if (!anAttributes.empty()) {
422 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
423 anAttLast = anAttributes.end();
424 for (; anAttIt != anAttLast; anAttIt++) {
425 AttributePtr anAttr = *anAttIt;
426 if (anAttr.get() == NULL)
428 std::string aAttrId = anAttr->id();
429 DataPtr aData = aFeature->data();
430 if (aData.get() != NULL) {
431 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
432 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
433 if (aPoint.get() != NULL) {
434 bool isImmutable = aPoint->setImmutable(true);
435 aPoint->move(dX, dY);
436 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
437 aPoint->setImmutable(isImmutable);
442 // Process selection by feature
443 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
444 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
445 if (aSketchFeature) {
446 aSketchFeature->move(dX, dY);
447 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
451 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
452 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
454 // 5. it is necessary to save current selection in order to restore it after the features moving
456 // 6. restore the update viewer flag and call this update
457 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
458 aDisplayer->updateViewer();
461 myCurrentPoint.setValue(aX, aY);
465 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
467 ModuleBase_Operation* aOperation = getCurrentOperation();
468 if (aOperation && aOperation->isEditOperation()) {
469 std::string aId = aOperation->id().toStdString();
470 if (isDistanceOperation(aOperation))
472 // Activate dimension value editing on double click
473 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
474 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
475 // Find corresponded widget to activate value editing
476 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
477 if (aWgt->attributeID() == "ConstraintValue") {
486 void PartSet_SketcherMgr::onApplicationStarted()
488 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
489 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
490 XGUI_Workshop* aWorkshop = aConnector->workshop();
491 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
492 if (aPropertyPanel) {
493 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
494 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
497 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
498 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
499 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
501 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
502 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
503 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
506 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
508 if (!myClickedPoint.myIsInitialized)
511 ModuleBase_Operation* aOperation = getCurrentOperation();
512 // the distance constraint feature should not use the clickedd point
513 // this is workaround in order to don't throw down the flyout point value,
514 // set by execute() method of these type of features
515 if (isDistanceOperation(aOperation))
518 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
520 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
524 void PartSet_SketcherMgr::onBeforeContextMenu()
526 myIsPopupMenuActive = true;
529 void PartSet_SketcherMgr::onAfterContextMenu()
531 myIsPopupMenuActive = false;
534 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
537 Handle(V3d_View) aView = theWnd->v3dView();
538 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
540 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
541 thePoint.setValue(aX, anY);
544 void PartSet_SketcherMgr::launchEditing()
546 // there should be activate the vertex selection mode because the edit can happens by the selected
549 aModes << TopAbs_VERTEX << TopAbs_EDGE;
550 // TODO: #391 - to be uncommented
551 /*aModes.append(AIS_DSM_Text);
552 aModes.append(AIS_DSM_Line);
553 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
554 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
556 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
557 aConnector->activateSubShapesSelection(aModes);
559 if (!myCurrentSelection.empty()) {
560 FeaturePtr aFeature = myCurrentSelection.begin().key();
561 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
562 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
564 myModule->editFeature(aSPFeature);
571 QStringList PartSet_SketcherMgr::sketchOperationIdList()
573 static QStringList aIds;
574 if (aIds.size() == 0) {
575 aIds << SketchPlugin_Line::ID().c_str();
576 aIds << SketchPlugin_Point::ID().c_str();
577 aIds << SketchPlugin_Arc::ID().c_str();
578 aIds << SketchPlugin_Circle::ID().c_str();
579 aIds << SketchPlugin_ConstraintLength::ID().c_str();
580 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
581 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
582 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
583 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
584 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
585 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
586 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
587 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
588 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
593 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
595 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
598 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
600 return theOperation &&
601 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
604 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
606 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
609 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
611 std::string aId = theOperation ? theOperation->id().toStdString() : "";
613 return (aId == SketchPlugin_Line::ID()) ||
614 (aId == SketchPlugin_Point::ID()) ||
615 (aId == SketchPlugin_Arc::ID()) ||
616 (aId == SketchPlugin_Circle::ID());
619 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
621 std::string aId = theOperation ? theOperation->id().toStdString() : "";
623 return (aId == SketchPlugin_ConstraintLength::ID()) ||
624 (aId == SketchPlugin_ConstraintDistance::ID()) ||
625 (aId == SketchPlugin_ConstraintRadius::ID());
628 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
630 // Display all sketcher sub-Objects
631 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
632 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
633 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
635 // Hide sketcher result
636 std::list<ResultPtr> aResults = myCurrentSketch->results();
637 std::list<ResultPtr>::const_iterator aIt;
638 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
639 aDisplayer->erase((*aIt), false);
641 aDisplayer->erase(myCurrentSketch, false);
643 // Display sketcher objects
644 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
645 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
646 std::list<ResultPtr> aResults = aFeature->results();
647 std::list<ResultPtr>::const_iterator aIt;
648 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
649 aDisplayer->display((*aIt), false);
651 aDisplayer->display(aFeature, false);
654 if (myPlaneFilter.IsNull())
655 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
657 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
658 if (theOperation->isEditOperation()) {
659 // If it is editing of sketch then it means that plane is already defined
660 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
661 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
663 aDisplayer->updateViewer();
666 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
668 myIsMouseOverWindow = false;
670 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
671 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
673 DataPtr aData = myCurrentSketch->data();
674 if ((!aData) || (!aData->isValid())) {
675 // The sketch was aborted
676 myCurrentSketch = CompositeFeaturePtr();
677 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
679 // Erase all sketcher objects
680 QStringList aSketchIds = sketchOperationIdList();
681 QObjectPtrList aObjects = aDisplayer->displayedObjects();
682 foreach (ObjectPtr aObj, aObjects) {
683 DataPtr aObjData = aObj->data();
684 if ((!aObjData) || (!aObjData->isValid()))
685 aDisplayer->erase(aObj);
689 // Hide all sketcher sub-Objects
690 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
691 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
692 std::list<ResultPtr> aResults = aFeature->results();
693 std::list<ResultPtr>::const_iterator aIt;
694 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
695 aDisplayer->erase((*aIt), false);
697 aDisplayer->erase(aFeature, false);
699 // Display sketcher result
700 std::list<ResultPtr> aResults = myCurrentSketch->results();
701 std::list<ResultPtr>::const_iterator aIt;
702 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
703 aDisplayer->display((*aIt), false);
705 aDisplayer->display(myCurrentSketch);
707 myCurrentSketch = CompositeFeaturePtr();
708 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
709 aDisplayer->updateViewer();
712 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
714 connectToPropertyPanel(true);
717 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
719 connectToPropertyPanel(false);
720 myIsPropertyPanelValueChanged = false;
721 myIsMouseOverViewProcessed = true;
724 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
726 if (isNestedCreateOperation(theOperation))
727 visualizeFeature(theOperation, true);
730 bool PartSet_SketcherMgr::canUndo() const
732 return isNestedCreateOperation(getCurrentOperation());
735 bool PartSet_SketcherMgr::canRedo() const
737 return isNestedCreateOperation(getCurrentOperation());
740 bool PartSet_SketcherMgr::canDisplayObject() const
742 bool aCanDisplay = true;
743 if (!isNestedCreateOperation(getCurrentOperation()))
746 ModuleBase_Operation* aOperation = getCurrentOperation();
747 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
748 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
749 // the active widget editor should not influence here. The presentation should be visible always
750 // when this widget is active.
752 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
757 if (myIsPopupMenuActive)
760 // during a nested create operation, the feature is redisplayed only if the mouse over view
761 // of there was a value modified in the property panel after the mouse left the view
762 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
766 bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
768 bool anEnabled = false;
769 ModuleBase_Operation* anOperation = getCurrentOperation();
771 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
772 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
776 QObjectPtrList anObjects;
777 // 1. change auxiliary type of a created feature
778 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
779 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
780 anObjects.append(anOperation->feature());
783 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
784 anOperation->abort();
785 // 2. change auxiliary type of selected sketch entities
786 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
787 anObjects = aSelection->selectedPresentations();
789 anEnabled = anObjects.size() > 0;
791 bool isNotAuxiliaryFound = false;
792 if (anObjects.size() > 0) {
793 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
794 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
795 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
796 if (aFeature.get() != NULL) {
797 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
798 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
799 if (aSketchFeature.get() != NULL) {
800 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
802 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
803 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
804 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
809 theValue = anObjects.size() && !isNotAuxiliaryFound;
813 void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
815 ModuleBase_Operation* anOperation = getCurrentOperation();
817 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
818 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
822 QObjectPtrList anObjects;
823 bool isUseTransaction = false;
824 // 1. change auxiliary type of a created feature
825 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
826 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
827 anObjects.append(anOperation->feature());
830 isUseTransaction = true;
831 // 2. change auxiliary type of selected sketch entities
832 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
833 anObjects = aSelection->selectedPresentations();
836 QAction* anAction = myModule->action("AUXILIARY_CMD");
837 SessionPtr aMgr = ModelAPI_Session::get();
838 if (isUseTransaction) {
839 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
840 anOperation->abort();
841 aMgr->startOperation(anAction->text().toStdString());
845 if (anObjects.size() > 0) {
846 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
847 for (; anIt != aLast; anIt++) {
848 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
849 if (aFeature.get() != NULL) {
850 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
851 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
852 if (aSketchFeature.get() != NULL) {
853 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
855 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
856 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
857 anAuxiliaryAttr->setValue(isChecked);
862 if (isUseTransaction) {
863 aMgr->finishOperation();
865 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
869 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
871 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
874 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
875 const FeaturePtr& theSketch,
876 ModuleBase_IWorkshop* theWorkshop,
877 FeatureToSelectionMap& theSelection)
879 if (theFeature.get() == NULL)
882 std::set<AttributePtr> aSelectedAttributes;
883 std::set<ResultPtr> aSelectedResults;
885 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
886 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
887 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
888 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
890 std::list<ResultPtr> aResults = theFeature->results();
891 std::list<ResultPtr>::const_iterator aIt;
892 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
894 ResultPtr aResult = *aIt;
895 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
896 if (aAISObj.get() == NULL)
898 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
899 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
901 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
902 aContext->SelectedOwner());
903 if (aBRepOwner.IsNull())
905 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
906 aBRepOwner->Selectable());
910 if (aBRepOwner->HasShape()) {
911 const TopoDS_Shape& aShape = aBRepOwner->Shape();
912 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
913 if (aShapeType == TopAbs_VERTEX) {
914 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
916 if (aPntAttr.get() != NULL)
917 aSelectedAttributes.insert(aPntAttr);
919 else if (aShapeType == TopAbs_EDGE &&
920 aSelectedResults.find(aResult) == aSelectedResults.end()) {
921 aSelectedResults.insert(aResult);
926 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
929 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
930 const FeaturePtr& theSketch,
931 ModuleBase_IWorkshop* theWorkshop,
932 const FeatureToSelectionMap& theSelection,
933 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
935 if (theFeature.get() == NULL)
938 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
939 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
940 std::set<ResultPtr> aSelectedResults = anIt.value().second;
942 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
943 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
944 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
945 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
947 // 1. found the feature's owners. Check the AIS objects of the constructions
948 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
949 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
950 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
952 SelectMgr_IndexedMapOfOwner aSelectedOwners;
953 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
954 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
955 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
956 if (!anOwner.IsNull())
957 anOwnersToSelect.Add(anOwner);
961 // 2. found the feature results's owners
962 std::list<ResultPtr> aResults = theFeature->results();
963 std::list<ResultPtr>::const_iterator aIt;
964 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
966 ResultPtr aResult = *aIt;
967 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
968 if (aAISObj.get() == NULL)
970 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
972 SelectMgr_IndexedMapOfOwner aSelectedOwners;
973 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
974 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
975 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
976 if ( anOwner.IsNull() || !anOwner->HasShape() )
978 const TopoDS_Shape& aShape = anOwner->Shape();
979 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
980 if (aShapeType == TopAbs_VERTEX) {
981 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
982 if (aPntAttr.get() != NULL &&
983 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
984 anOwnersToSelect.Add(anOwner);
987 else if (aShapeType == TopAbs_EDGE) {
988 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
989 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
990 anOwnersToSelect.FindIndex(anOwner) <= 0)
991 anOwnersToSelect.Add(anOwner);
997 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
999 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1000 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1001 XGUI_Workshop* aWorkshop = aConnector->workshop();
1002 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1003 if (aPropertyPanel) {
1004 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1005 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1007 connect(aWidget, SIGNAL(beforeValuesChanged()),
1008 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1009 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1010 connect(aWidget, SIGNAL(afterValuesChanged()),
1011 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1014 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1015 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1016 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1017 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1018 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1024 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1026 return myModule->workshop()->currentOperation();
1029 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1030 const bool isToDisplay)
1032 if (!theOperation || theOperation->isEditOperation())
1035 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1036 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1037 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1039 // 1. change visibility of the object itself, here the presentable object is processed,
1040 // e.g. constraints features
1041 FeaturePtr aFeature = theOperation->feature();
1042 std::list<ResultPtr> aResults = aFeature->results();
1044 aDisplayer->display(aFeature, false);
1046 aDisplayer->erase(aFeature, false);
1048 // change visibility of the object results, e.g. non-constraint features
1049 std::list<ResultPtr>::const_iterator aIt;
1050 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1052 aDisplayer->display(*aIt, false);
1055 aDisplayer->erase(*aIt, false);
1058 aDisplayer->updateViewer();
1061 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1063 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1064 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1065 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1067 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1068 if (theHighlightedOnly) {
1069 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1072 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1074 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1075 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1078 // 1. it is necessary to save current selection in order to restore it after the features moving
1079 myCurrentSelection.clear();
1080 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1081 aLast = aFeature2AttributeMap.end();
1082 for (; anIt != aLast; anIt++) {
1083 FeaturePtr aFeature = anIt.key();
1084 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1086 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1089 void PartSet_SketcherMgr::restoreSelection()
1091 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1092 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1093 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1094 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1095 aSLast = myCurrentSelection.end();
1096 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1097 for (; aSIt != aSLast; aSIt++) {
1098 anOwnersToSelect.Clear();
1099 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1101 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);