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>
55 #include <SketchPlugin_ConstraintCoincidence.h>
57 #include <SelectMgr_IndexedMapOfOwner.hxx>
58 #include <StdSelect_BRepOwner.hxx>
60 //#include <AIS_DimensionSelectionMode.hxx>
61 //#include <AIS_Shape.hxx>
63 #include <ModelAPI_Events.h>
64 #include <ModelAPI_Session.h>
66 #include <QMouseEvent>
67 #include <QApplication>
70 /// Returns list of unique objects by sum of objects from List1 and List2
71 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
72 const QList<ModuleBase_ViewerPrs>& theList2)
74 QList<ModuleBase_ViewerPrs> aRes;
75 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
76 if (!aRes.contains(aPrs))
79 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
80 if (!aRes.contains(aPrs))
86 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
87 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
88 const FeaturePtr theSketch)
90 QList<ModuleBase_ViewerPrs> aRes;
92 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
93 aLast = theList.end();
94 for (; anIt != aLast; anIt++)
96 ModuleBase_ViewerPrs aPrs = *anIt;
97 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
98 if (aFeature.get() == NULL)
101 QList<AttributePtr> anAttributes;
102 if (theFeature2AttributeMap.contains(aFeature)) {
103 anAttributes = theFeature2AttributeMap[aFeature];
106 TopoDS_Shape aShape = aPrs.shape();
107 if (!aShape.IsNull()) {
108 if (aShape.ShapeType() == TopAbs_VERTEX) {
109 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
110 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
111 anAttributes.push_back(anAttr);
114 theFeature2AttributeMap[aFeature] = anAttributes;
121 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
122 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
123 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
124 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
125 myIsPopupMenuActive(false)
127 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
128 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
130 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
132 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
133 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
135 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
136 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
138 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
139 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
141 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
142 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
144 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
145 XGUI_Workshop* aWorkshop = aConnector->workshop();
146 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
149 PartSet_SketcherMgr::~PartSet_SketcherMgr()
151 if (!myPlaneFilter.IsNull())
152 myPlaneFilter.Nullify();
155 void PartSet_SketcherMgr::onEnterViewPort()
157 if (!isNestedCreateOperation(getCurrentOperation()))
159 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
160 // created feature because it should be done in onMouseMove(). Some widgets watch
161 // the mouse move and use the cursor position to update own values. If the presentaion is
162 // redisplayed before this update, the feature presentation jumps from reset value to current.
163 myIsMouseOverWindow = true;
164 myIsPropertyPanelValueChanged = false;
167 void PartSet_SketcherMgr::onLeaveViewPort()
169 if (!isNestedCreateOperation(getCurrentOperation()))
171 // the method should be performed if the popup menu is called,
172 // the reset of the current widget should not happen
173 if (myIsPopupMenuActive)
176 myIsMouseOverViewProcessed = false;
177 myIsMouseOverWindow = false;
179 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
180 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
181 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
182 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
183 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
184 // obtained after reset value
185 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
186 ModuleBase_Operation* aOperation = getCurrentOperation();
187 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
188 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
192 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
194 // hides the presentation of the current operation feature
195 myIsPropertyPanelValueChanged = false;
196 // the feature is to be erased here, but it is correct to call canDisplayObject because
197 // there can be additional check (e.g. editor widget in distance constraint)
198 visualizeFeature(aOperation, canDisplayObject());
201 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
203 if (isNestedCreateOperation(getCurrentOperation()))
205 // it is necessary to save current selection in order to restore it after the values are modifed
208 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
209 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
210 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
211 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
214 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
216 if (isNestedCreateOperation(getCurrentOperation()))
218 // it is necessary to restore current selection in order to restore it after the values are modified
220 myCurrentSelection.clear();
222 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
223 // viewer happens by deselect/select the modified objects. The flag should be restored after
224 // the selection processing. The update viewer should be also called.
225 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
226 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
227 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
228 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
229 aDisplayer->updateViewer();
232 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
234 if (!isNestedCreateOperation(getCurrentOperation()))
237 // visualize the current operation feature
238 myIsPropertyPanelValueChanged = true;
239 ModuleBase_Operation* aOperation = getCurrentOperation();
240 // the feature is to be erased here, but it is correct to call canDisplayObject because
241 // there can be additional check (e.g. editor widget in distance constraint)
242 visualizeFeature(aOperation, canDisplayObject());
245 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
247 get2dPoint(theWnd, theEvent, myClickedPoint);
249 if (!(theEvent->buttons() & Qt::LeftButton))
252 // Clear dragging mode
253 myIsDragging = false;
255 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
256 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
257 if (!aViewer->canDragByMouse())
260 ModuleBase_Operation* aOperation = getCurrentOperation();
261 if (aOperation && aOperation->isEditOperation()) {
262 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
263 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
264 // If the current widget is a selector, do do nothing, it processes the mouse press
265 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
270 // Use only for sketch operations
271 if (aOperation && myCurrentSketch) {
272 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
275 bool isSketcher = isSketchOperation(aOperation);
276 bool isSketchOpe = isNestedSketchOperation(aOperation);
278 // Avoid non-sketch operations
279 if ((!isSketchOpe) && (!isSketcher))
282 bool isEditing = aOperation->isEditOperation();
284 // Ignore creation sketch operation
285 if ((!isSketcher) && (!isEditing))
288 // MoveTo in order to highlight current object
289 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
291 // Remember highlighted objects for editing
292 ModuleBase_ISelection* aSelect = aWorkshop->selection();
294 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
295 storeSelection(!aHasShift);
297 if (myCurrentSelection.empty()) {
298 if (isSketchOpe && (!isSketcher))
299 // commit previous operation
300 if (!aOperation->commit())
307 get2dPoint(theWnd, theEvent, myCurrentPoint);
311 } else if (isSketchOpe && isEditing) {
312 // If selected another object commit current result
313 aOperation->commit();
316 get2dPoint(theWnd, theEvent, myCurrentPoint);
319 // This is necessary in order to finalize previous operation
320 QApplication::processEvents();
326 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
328 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
329 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
330 if (!aViewer->canDragByMouse())
332 ModuleBase_Operation* aOp = getCurrentOperation();
334 if (isNestedSketchOperation(aOp)) {
335 get2dPoint(theWnd, theEvent, myClickedPoint);
337 // Only for sketcher operations
341 myCurrentSelection.clear();
342 // Reselect edited object
343 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
344 if (theEvent->modifiers() & Qt::ShiftModifier)
345 aViewer->AISContext()->ShiftSelect();
347 aViewer->AISContext()->Select();
353 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
354 myIsDragging = false;
357 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
359 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
360 myIsMouseOverViewProcessed = true;
361 // 1. perform the widget mouse move functionality and display the presentation
362 ModuleBase_Operation* aOperation = getCurrentOperation();
363 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
364 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
365 // the mouse move should be processed in the widget, if it can in order to visualize correct
366 // presentation. These widgets correct the feature attribute according to the mouse position
367 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
369 aPoint2DWdg->onMouseMove(theWnd, theEvent);
371 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
374 aDistanceWdg->onMouseMove(theWnd, theEvent);
376 // the feature is to be erased here, but it is correct to call canDisplayObject because
377 // there can be additional check (e.g. editor widget in distance constraint)
378 visualizeFeature(aOperation, canDisplayObject());
381 myClickedPoint.clear();
384 // 1. the current selection is saved in the mouse press method in order to restore it after moving
385 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
386 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
387 // deselected). This flag should be restored in the slot, processed the mouse release signal.
388 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
389 aViewer->enableSelection(false);
391 ModuleBase_Operation* aOperation = getCurrentOperation();
394 if (isSketchOperation(aOperation))
395 return; // No edit operation activated
397 Handle(V3d_View) aView = theWnd->v3dView();
398 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
400 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
401 double dX = aX - myCurrentPoint.myCurX;
402 double dY = aY - myCurrentPoint.myCurY;
404 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
405 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
406 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
407 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
408 // viewer happens by deselect/select the modified objects. The flag should be restored after
409 // the selection processing. The update viewer should be also called.
410 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
412 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
413 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
414 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
415 aLast = myCurrentSelection.end();
416 // 4. the features and attributes modification(move)
417 for (; anIt != aLast; anIt++) {
418 FeaturePtr aFeature = anIt.key();
420 std::set<AttributePtr> anAttributes = anIt.value().first;
421 // Process selection by attribute: the priority to the attribute
422 if (!anAttributes.empty()) {
423 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
424 anAttLast = anAttributes.end();
425 for (; anAttIt != anAttLast; anAttIt++) {
426 AttributePtr anAttr = *anAttIt;
427 if (anAttr.get() == NULL)
429 std::string aAttrId = anAttr->id();
430 DataPtr aData = aFeature->data();
431 if (aData.get() != NULL) {
432 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
433 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
434 if (aPoint.get() != NULL) {
435 bool isImmutable = aPoint->setImmutable(true);
436 aPoint->move(dX, dY);
437 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
438 aPoint->setImmutable(isImmutable);
443 // Process selection by feature
444 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
445 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
446 if (aSketchFeature) {
447 aSketchFeature->move(dX, dY);
448 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
452 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
453 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
455 // 5. it is necessary to save current selection in order to restore it after the features moving
457 // 6. restore the update viewer flag and call this update
458 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
459 aDisplayer->updateViewer();
462 myCurrentPoint.setValue(aX, aY);
466 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
468 ModuleBase_Operation* aOperation = getCurrentOperation();
469 if (aOperation && aOperation->isEditOperation()) {
470 std::string aId = aOperation->id().toStdString();
471 if (isDistanceOperation(aOperation))
473 // Activate dimension value editing on double click
474 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
475 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
476 // Find corresponded widget to activate value editing
477 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
478 if (aWgt->attributeID() == "ConstraintValue") {
487 void PartSet_SketcherMgr::onApplicationStarted()
489 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
490 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
491 XGUI_Workshop* aWorkshop = aConnector->workshop();
492 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
493 if (aPropertyPanel) {
494 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
495 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
498 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
499 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
500 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
502 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
503 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
504 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
507 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
509 if (!myClickedPoint.myIsInitialized)
512 ModuleBase_Operation* aOperation = getCurrentOperation();
513 // the distance constraint feature should not use the clickedd point
514 // this is workaround in order to don't throw down the flyout point value,
515 // set by execute() method of these type of features
516 if (isDistanceOperation(aOperation))
519 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
521 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
525 void PartSet_SketcherMgr::onBeforeContextMenu()
527 myIsPopupMenuActive = true;
530 void PartSet_SketcherMgr::onAfterContextMenu()
532 myIsPopupMenuActive = false;
535 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
538 Handle(V3d_View) aView = theWnd->v3dView();
539 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
541 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
542 thePoint.setValue(aX, anY);
545 void PartSet_SketcherMgr::launchEditing()
547 // there should be activate the vertex selection mode because the edit can happens by the selected
550 aModes << TopAbs_VERTEX << TopAbs_EDGE;
551 // TODO: #391 - to be uncommented
552 /*aModes.append(AIS_DSM_Text);
553 aModes.append(AIS_DSM_Line);
554 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
555 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
557 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
558 aConnector->activateSubShapesSelection(aModes);
560 if (!myCurrentSelection.empty()) {
561 FeaturePtr aFeature = myCurrentSelection.begin().key();
562 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
563 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
565 myModule->editFeature(aSPFeature);
572 QStringList PartSet_SketcherMgr::sketchOperationIdList()
574 static QStringList aIds;
575 if (aIds.size() == 0) {
576 aIds << SketchPlugin_Line::ID().c_str();
577 aIds << SketchPlugin_Point::ID().c_str();
578 aIds << SketchPlugin_Arc::ID().c_str();
579 aIds << SketchPlugin_Circle::ID().c_str();
580 aIds << SketchPlugin_ConstraintLength::ID().c_str();
581 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
582 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
583 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
584 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
585 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
586 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
587 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
588 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
589 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
590 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
595 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
597 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
600 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
602 return theOperation &&
603 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
606 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
608 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
611 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
613 std::string aId = theOperation ? theOperation->id().toStdString() : "";
615 return (aId == SketchPlugin_Line::ID()) ||
616 (aId == SketchPlugin_Point::ID()) ||
617 (aId == SketchPlugin_Arc::ID()) ||
618 (aId == SketchPlugin_Circle::ID());
621 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
623 std::string aId = theOperation ? theOperation->id().toStdString() : "";
625 return (aId == SketchPlugin_ConstraintLength::ID()) ||
626 (aId == SketchPlugin_ConstraintDistance::ID()) ||
627 (aId == SketchPlugin_ConstraintRadius::ID());
630 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
632 // Display all sketcher sub-Objects
633 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
634 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
635 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
637 // Hide sketcher result
638 std::list<ResultPtr> aResults = myCurrentSketch->results();
639 std::list<ResultPtr>::const_iterator aIt;
640 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
641 aDisplayer->erase((*aIt), false);
643 aDisplayer->erase(myCurrentSketch, false);
645 // Display sketcher objects
646 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
647 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
648 std::list<ResultPtr> aResults = aFeature->results();
649 std::list<ResultPtr>::const_iterator aIt;
650 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
651 aDisplayer->display((*aIt), false);
653 aDisplayer->display(aFeature, false);
656 if (myPlaneFilter.IsNull())
657 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
659 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
660 if (theOperation->isEditOperation()) {
661 // If it is editing of sketch then it means that plane is already defined
662 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
663 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
665 aDisplayer->updateViewer();
668 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
670 myIsMouseOverWindow = false;
672 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
673 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
675 DataPtr aData = myCurrentSketch->data();
676 if ((!aData) || (!aData->isValid())) {
677 // The sketch was aborted
678 myCurrentSketch = CompositeFeaturePtr();
679 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
681 // Erase all sketcher objects
682 QStringList aSketchIds = sketchOperationIdList();
683 QObjectPtrList aObjects = aDisplayer->displayedObjects();
684 foreach (ObjectPtr aObj, aObjects) {
685 DataPtr aObjData = aObj->data();
686 if ((!aObjData) || (!aObjData->isValid()))
687 aDisplayer->erase(aObj);
691 // Hide all sketcher sub-Objects
692 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
693 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
694 std::list<ResultPtr> aResults = aFeature->results();
695 std::list<ResultPtr>::const_iterator aIt;
696 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
697 aDisplayer->erase((*aIt), false);
699 aDisplayer->erase(aFeature, false);
701 // Display sketcher result
702 std::list<ResultPtr> aResults = myCurrentSketch->results();
703 std::list<ResultPtr>::const_iterator aIt;
704 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
705 aDisplayer->display((*aIt), false);
707 aDisplayer->display(myCurrentSketch);
709 myCurrentSketch = CompositeFeaturePtr();
710 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
711 aDisplayer->updateViewer();
714 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
716 connectToPropertyPanel(true);
719 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
721 connectToPropertyPanel(false);
722 myIsPropertyPanelValueChanged = false;
723 myIsMouseOverViewProcessed = true;
726 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
728 if (isNestedCreateOperation(theOperation))
729 visualizeFeature(theOperation, true);
732 bool PartSet_SketcherMgr::canUndo() const
734 return isNestedCreateOperation(getCurrentOperation());
737 bool PartSet_SketcherMgr::canRedo() const
739 return isNestedCreateOperation(getCurrentOperation());
742 bool PartSet_SketcherMgr::canDisplayObject() const
744 bool aCanDisplay = true;
745 if (!isNestedCreateOperation(getCurrentOperation()))
748 ModuleBase_Operation* aOperation = getCurrentOperation();
749 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
750 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
751 // the active widget editor should not influence here. The presentation should be visible always
752 // when this widget is active.
754 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
759 if (myIsPopupMenuActive)
762 // during a nested create operation, the feature is redisplayed only if the mouse over view
763 // of there was a value modified in the property panel after the mouse left the view
764 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
768 bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
770 bool anEnabled = false;
771 ModuleBase_Operation* anOperation = getCurrentOperation();
773 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
774 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
778 QObjectPtrList anObjects;
779 // 1. change auxiliary type of a created feature
780 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
781 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
782 anObjects.append(anOperation->feature());
785 /// The operation should not be aborted here, because the method does not changed
786 /// the auxilliary state, but checks the possibility to perform this
787 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
788 /// anOperation->abort();
789 // 2. change auxiliary type of selected sketch entities
790 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
791 anObjects = aSelection->selectedPresentations();
793 anEnabled = anObjects.size() > 0;
795 bool isNotAuxiliaryFound = false;
796 if (anObjects.size() > 0) {
797 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
798 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
799 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
800 if (aFeature.get() != NULL) {
801 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
802 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
803 if (aSketchFeature.get() != NULL) {
804 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
806 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
807 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
809 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
814 theValue = anObjects.size() && !isNotAuxiliaryFound;
818 void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
820 ModuleBase_Operation* anOperation = getCurrentOperation();
822 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
823 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
827 QObjectPtrList anObjects;
828 bool isUseTransaction = false;
829 // 1. change auxiliary type of a created feature
830 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
831 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
832 anObjects.append(anOperation->feature());
835 isUseTransaction = true;
836 // 2. change auxiliary type of selected sketch entities
837 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
838 anObjects = aSelection->selectedPresentations();
841 QAction* anAction = myModule->action("AUXILIARY_CMD");
842 SessionPtr aMgr = ModelAPI_Session::get();
843 if (isUseTransaction) {
844 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
845 anOperation->abort();
846 aMgr->startOperation(anAction->text().toStdString());
850 if (anObjects.size() > 0) {
851 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
852 for (; anIt != aLast; anIt++) {
853 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
854 if (aFeature.get() != NULL) {
855 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
856 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
857 if (aSketchFeature.get() != NULL) {
858 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
860 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
861 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
863 anAuxiliaryAttr->setValue(isChecked);
868 if (isUseTransaction) {
869 aMgr->finishOperation();
871 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
875 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
877 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
880 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
881 const FeaturePtr& theSketch,
882 ModuleBase_IWorkshop* theWorkshop,
883 FeatureToSelectionMap& theSelection)
885 if (theFeature.get() == NULL)
888 std::set<AttributePtr> aSelectedAttributes;
889 std::set<ResultPtr> aSelectedResults;
891 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
892 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
893 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
894 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
896 std::list<ResultPtr> aResults = theFeature->results();
897 std::list<ResultPtr>::const_iterator aIt;
898 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
900 ResultPtr aResult = *aIt;
901 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
902 if (aAISObj.get() == NULL)
904 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
905 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
907 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
908 aContext->SelectedOwner());
909 if (aBRepOwner.IsNull())
911 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
912 aBRepOwner->Selectable());
916 if (aBRepOwner->HasShape()) {
917 const TopoDS_Shape& aShape = aBRepOwner->Shape();
918 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
919 if (aShapeType == TopAbs_VERTEX) {
920 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
922 if (aPntAttr.get() != NULL)
923 aSelectedAttributes.insert(aPntAttr);
925 else if (aShapeType == TopAbs_EDGE &&
926 aSelectedResults.find(aResult) == aSelectedResults.end()) {
927 aSelectedResults.insert(aResult);
932 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
935 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
936 const FeaturePtr& theSketch,
937 ModuleBase_IWorkshop* theWorkshop,
938 const FeatureToSelectionMap& theSelection,
939 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
941 if (theFeature.get() == NULL)
944 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
945 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
946 std::set<ResultPtr> aSelectedResults = anIt.value().second;
948 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
949 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
950 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
951 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
953 // 1. found the feature's owners. Check the AIS objects of the constructions
954 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
955 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
956 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
958 SelectMgr_IndexedMapOfOwner aSelectedOwners;
959 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
960 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
961 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
962 if (!anOwner.IsNull())
963 anOwnersToSelect.Add(anOwner);
967 // 2. found the feature results's owners
968 std::list<ResultPtr> aResults = theFeature->results();
969 std::list<ResultPtr>::const_iterator aIt;
970 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
972 ResultPtr aResult = *aIt;
973 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
974 if (aAISObj.get() == NULL)
976 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
978 SelectMgr_IndexedMapOfOwner aSelectedOwners;
979 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
980 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
981 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
982 if ( anOwner.IsNull() || !anOwner->HasShape() )
984 const TopoDS_Shape& aShape = anOwner->Shape();
985 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
986 if (aShapeType == TopAbs_VERTEX) {
987 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
988 if (aPntAttr.get() != NULL &&
989 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
990 anOwnersToSelect.Add(anOwner);
993 else if (aShapeType == TopAbs_EDGE) {
994 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
995 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
996 anOwnersToSelect.FindIndex(anOwner) <= 0)
997 anOwnersToSelect.Add(anOwner);
1003 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1005 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1006 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1007 XGUI_Workshop* aWorkshop = aConnector->workshop();
1008 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1009 if (aPropertyPanel) {
1010 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1011 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1013 connect(aWidget, SIGNAL(beforeValuesChanged()),
1014 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1015 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1016 connect(aWidget, SIGNAL(afterValuesChanged()),
1017 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1020 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1021 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1022 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1023 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1024 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1030 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1032 return myModule->workshop()->currentOperation();
1035 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1036 const bool isToDisplay)
1038 if (!theOperation || theOperation->isEditOperation())
1041 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1042 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1043 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1045 // 1. change visibility of the object itself, here the presentable object is processed,
1046 // e.g. constraints features
1047 FeaturePtr aFeature = theOperation->feature();
1048 std::list<ResultPtr> aResults = aFeature->results();
1050 aDisplayer->display(aFeature, false);
1052 aDisplayer->erase(aFeature, false);
1054 // change visibility of the object results, e.g. non-constraint features
1055 std::list<ResultPtr>::const_iterator aIt;
1056 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1058 aDisplayer->display(*aIt, false);
1061 aDisplayer->erase(*aIt, false);
1064 aDisplayer->updateViewer();
1067 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1069 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1070 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1071 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1073 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1074 if (theHighlightedOnly) {
1075 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1078 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1080 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1081 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1084 // 1. it is necessary to save current selection in order to restore it after the features moving
1085 myCurrentSelection.clear();
1086 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1087 aLast = aFeature2AttributeMap.end();
1088 for (; anIt != aLast; anIt++) {
1089 FeaturePtr aFeature = anIt.key();
1090 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1092 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1095 void PartSet_SketcherMgr::restoreSelection()
1097 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1098 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1099 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1100 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1101 aSLast = myCurrentSelection.end();
1102 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1103 for (; aSIt != aSLast; aSIt++) {
1104 anOwnersToSelect.Clear();
1105 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1107 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);