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"
12 #include "PartSet_WidgetSketchLabel.h"
14 #include <ModuleBase_WidgetEditor.h>
16 #include <XGUI_ModuleConnector.h>
17 #include <XGUI_Displayer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_ContextMenuMgr.h>
20 #include <XGUI_Selection.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <ModuleBase_ModelWidget.h>
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_PropertyPanel.h>
25 #include <XGUI_ViewerProxy.h>
27 #include <AppElements_MainWindow.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IWorkshop.h>
31 #include <ModuleBase_IViewWindow.h>
32 #include <ModuleBase_Operation.h>
33 #include <ModuleBase_ISelection.h>
34 #include <ModuleBase_IPropertyPanel.h>
35 #include <ModuleBase_Operation.h>
37 #include <GeomDataAPI_Point2D.h>
39 #include <Events_Loop.h>
41 #include <SketchPlugin_Line.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_Point.h>
44 #include <SketchPlugin_Arc.h>
45 #include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintLength.h>
47 #include <SketchPlugin_ConstraintDistance.h>
48 #include <SketchPlugin_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintPerpendicular.h>
50 #include <SketchPlugin_ConstraintRadius.h>
51 #include <SketchPlugin_ConstraintRigid.h>
52 #include <SketchPlugin_ConstraintHorizontal.h>
53 #include <SketchPlugin_ConstraintVertical.h>
54 #include <SketchPlugin_ConstraintEqual.h>
55 #include <SketchPlugin_ConstraintTangent.h>
56 #include <SketchPlugin_ConstraintCoincidence.h>
57 #include <SketchPlugin_ConstraintFillet.h>
58 #include <SketchPlugin_ConstraintMirror.h>
60 #include <SketcherPrs_Tools.h>
62 #include <SelectMgr_IndexedMapOfOwner.hxx>
63 #include <StdSelect_BRepOwner.hxx>
65 //#include <AIS_DimensionSelectionMode.hxx>
66 #include <AIS_Shape.hxx>
68 #include <ModelAPI_Events.h>
69 #include <ModelAPI_Session.h>
71 #include <QMouseEvent>
72 #include <QApplication>
74 //#define DEBUG_DO_NOT_BY_ENTER
76 /// Returns list of unique objects by sum of objects from List1 and List2
77 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
78 const QList<ModuleBase_ViewerPrs>& theList2)
80 QList<ModuleBase_ViewerPrs> aRes;
81 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
82 if (!aRes.contains(aPrs))
85 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
86 if (!aRes.contains(aPrs))
92 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
93 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
94 const FeaturePtr theSketch)
96 QList<ModuleBase_ViewerPrs> aRes;
98 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
99 aLast = theList.end();
100 for (; anIt != aLast; anIt++)
102 ModuleBase_ViewerPrs aPrs = *anIt;
103 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
104 if (aFeature.get() == NULL)
107 QList<AttributePtr> anAttributes;
108 if (theFeature2AttributeMap.contains(aFeature)) {
109 anAttributes = theFeature2AttributeMap[aFeature];
112 TopoDS_Shape aShape = aPrs.shape();
113 if (!aShape.IsNull()) {
114 if (aShape.ShapeType() == TopAbs_VERTEX) {
115 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
116 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
117 anAttributes.push_back(anAttr);
120 theFeature2AttributeMap[aFeature] = anAttributes;
127 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
128 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
129 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
130 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
131 myIsPopupMenuActive(false), myIsConstraintsShown(true)
133 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
134 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
136 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
138 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
139 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
141 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
142 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
144 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
145 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
147 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
148 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
150 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
151 XGUI_Workshop* aWorkshop = aConnector->workshop();
152 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
155 PartSet_SketcherMgr::~PartSet_SketcherMgr()
157 if (!myPlaneFilter.IsNull())
158 myPlaneFilter.Nullify();
161 void PartSet_SketcherMgr::onEnterViewPort()
163 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
164 // created feature because it should be done in onMouseMove(). Some widgets watch
165 // the mouse move and use the cursor position to update own values. If the presentaion is
166 // redisplayed before this update, the feature presentation jumps from reset value to current.
167 myIsMouseOverWindow = true;
168 myIsPropertyPanelValueChanged = false;
170 #ifdef DEBUG_DO_NOT_BY_ENTER
173 //if (!isNestedCreateOperation(getCurrentOperation()))
177 void PartSet_SketcherMgr::onLeaveViewPort()
179 myIsMouseOverViewProcessed = false;
180 myIsMouseOverWindow = false;
181 myIsPropertyPanelValueChanged = false;
183 #ifdef DEBUG_DO_NOT_BY_ENTER
187 if (!isNestedCreateOperation(getCurrentOperation()))
189 // the method should be performed if the popup menu is called,
190 // the reset of the current widget should not happen
191 if (myIsPopupMenuActive)
194 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
195 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
196 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
197 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
198 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
199 // obtained after reset value
200 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
201 ModuleBase_Operation* aOperation = getCurrentOperation();
202 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
203 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
207 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
209 // hides the presentation of the current operation feature
210 //myIsPropertyPanelValueChanged = false;
211 // the feature is to be erased here, but it is correct to call canDisplayObject because
212 // there can be additional check (e.g. editor widget in distance constraint)
213 FeaturePtr aFeature = getCurrentOperation()->feature();
214 visualizeFeature(aOperation, canDisplayObject(aFeature));
217 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
219 if (isNestedCreateOperation(getCurrentOperation()))
221 // it is necessary to save current selection in order to restore it after the values are modifed
224 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
225 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
226 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
227 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
230 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
232 if (isNestedCreateOperation(getCurrentOperation()))
234 // it is necessary to restore current selection in order to restore it after the values are modified
236 myCurrentSelection.clear();
238 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
239 // viewer happens by deselect/select the modified objects. The flag should be restored after
240 // the selection processing. The update viewer should be also called.
241 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
242 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
243 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
244 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
245 aDisplayer->updateViewer();
248 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
250 if (!isNestedCreateOperation(getCurrentOperation()))
253 // visualize the current operation feature
254 myIsPropertyPanelValueChanged = true;
255 ModuleBase_Operation* aOperation = getCurrentOperation();
256 // the feature is to be erased here, but it is correct to call canDisplayObject because
257 // there can be additional check (e.g. editor widget in distance constraint)
258 FeaturePtr aFeature = getCurrentOperation()->feature();
259 visualizeFeature(aOperation, canDisplayObject(aFeature));
262 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
264 get2dPoint(theWnd, theEvent, myClickedPoint);
266 if (!(theEvent->buttons() & Qt::LeftButton))
269 // Clear dragging mode
270 myIsDragging = false;
272 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
273 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
274 if (!aViewer->canDragByMouse())
277 ModuleBase_Operation* aOperation = getCurrentOperation();
278 if (aOperation && aOperation->isEditOperation()) {
279 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
280 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
281 // If the current widget is a selector, do do nothing, it processes the mouse press
282 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
287 // Use only for sketch operations
288 if (aOperation && myCurrentSketch) {
289 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
292 bool isSketcher = isSketchOperation(aOperation);
293 bool isSketchOpe = isNestedSketchOperation(aOperation);
295 // Avoid non-sketch operations
296 if ((!isSketchOpe) && (!isSketcher))
299 bool isEditing = aOperation->isEditOperation();
301 // Ignore creation sketch operation
302 if ((!isSketcher) && (!isEditing))
305 // MoveTo in order to highlight current object
306 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
308 // Remember highlighted objects for editing
309 ModuleBase_ISelection* aSelect = aWorkshop->selection();
311 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
312 storeSelection(!aHasShift);
314 if (myCurrentSelection.empty()) {
315 if (isSketchOpe && (!isSketcher))
316 // commit previous operation
317 if (!aOperation->commit())
324 get2dPoint(theWnd, theEvent, myCurrentPoint);
328 } else if (isSketchOpe && isEditing) {
329 // If selected another object commit current result
330 aOperation->commit();
333 get2dPoint(theWnd, theEvent, myCurrentPoint);
336 // This is necessary in order to finalize previous operation
337 QApplication::processEvents();
343 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
345 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
346 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
347 if (!aViewer->canDragByMouse())
349 ModuleBase_Operation* aOp = getCurrentOperation();
351 if (isNestedSketchOperation(aOp)) {
352 get2dPoint(theWnd, theEvent, myClickedPoint);
354 // Only for sketcher operations
358 myCurrentSelection.clear();
359 // Reselect edited object
360 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
361 if (theEvent->modifiers() & Qt::ShiftModifier)
362 aViewer->AISContext()->ShiftSelect();
364 aViewer->AISContext()->Select();
370 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
371 myIsDragging = false;
374 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
376 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
377 myIsMouseOverViewProcessed = true;
378 // 1. perform the widget mouse move functionality and display the presentation
379 ModuleBase_Operation* aOperation = getCurrentOperation();
380 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
381 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
382 // the mouse move should be processed in the widget, if it can in order to visualize correct
383 // presentation. These widgets correct the feature attribute according to the mouse position
384 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
386 aPoint2DWdg->onMouseMove(theWnd, theEvent);
388 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
391 aDistanceWdg->onMouseMove(theWnd, theEvent);
393 // the feature is to be erased here, but it is correct to call canDisplayObject because
394 // there can be additional check (e.g. editor widget in distance constraint)
395 FeaturePtr aFeature = getCurrentOperation()->feature();
396 visualizeFeature(aOperation, canDisplayObject(aFeature));
399 myClickedPoint.clear();
402 // 1. the current selection is saved in the mouse press method in order to restore it after moving
403 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
404 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
405 // deselected). This flag should be restored in the slot, processed the mouse release signal.
406 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
407 aViewer->enableSelection(false);
409 ModuleBase_Operation* aOperation = getCurrentOperation();
412 if (isSketchOperation(aOperation))
413 return; // No edit operation activated
415 Handle(V3d_View) aView = theWnd->v3dView();
416 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
418 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
419 double dX = aX - myCurrentPoint.myCurX;
420 double dY = aY - myCurrentPoint.myCurY;
422 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
423 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
424 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
425 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
426 // viewer happens by deselect/select the modified objects. The flag should be restored after
427 // the selection processing. The update viewer should be also called.
428 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
430 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
431 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
432 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
433 aLast = myCurrentSelection.end();
434 // 4. the features and attributes modification(move)
435 for (; anIt != aLast; anIt++) {
436 FeaturePtr aFeature = anIt.key();
438 std::set<AttributePtr> anAttributes = anIt.value().first;
439 // Process selection by attribute: the priority to the attribute
440 if (!anAttributes.empty()) {
441 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
442 anAttLast = anAttributes.end();
443 for (; anAttIt != anAttLast; anAttIt++) {
444 AttributePtr anAttr = *anAttIt;
445 if (anAttr.get() == NULL)
447 std::string aAttrId = anAttr->id();
448 DataPtr aData = aFeature->data();
449 if (aData.get() != NULL) {
450 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
451 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
452 if (aPoint.get() != NULL) {
453 bool isImmutable = aPoint->setImmutable(true);
454 aPoint->move(dX, dY);
455 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
456 aPoint->setImmutable(isImmutable);
461 // Process selection by feature
462 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
463 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
464 if (aSketchFeature) {
465 aSketchFeature->move(dX, dY);
466 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
470 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
471 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
473 // 5. it is necessary to save current selection in order to restore it after the features moving
475 // 6. restore the update viewer flag and call this update
476 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
477 aDisplayer->updateViewer();
480 myCurrentPoint.setValue(aX, aY);
484 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
486 ModuleBase_Operation* aOperation = getCurrentOperation();
487 if (aOperation && aOperation->isEditOperation()) {
488 std::string aId = aOperation->id().toStdString();
489 if (isDistanceOperation(aOperation))
491 // Activate dimension value editing on double click
492 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
493 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
494 // Find corresponded widget to activate value editing
495 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
496 if (aWgt->attributeID() == "ConstraintValue") {
505 void PartSet_SketcherMgr::onApplicationStarted()
507 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
508 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
509 XGUI_Workshop* aWorkshop = aConnector->workshop();
510 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
511 if (aPropertyPanel) {
512 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
513 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
516 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
517 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
518 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
520 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
521 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
522 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
525 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
527 if (!myClickedPoint.myIsInitialized)
530 ModuleBase_Operation* aOperation = getCurrentOperation();
531 // the distance constraint feature should not use the clickedd point
532 // this is workaround in order to don't throw down the flyout point value,
533 // set by execute() method of these type of features
534 if (isDistanceOperation(aOperation))
537 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
539 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
543 void PartSet_SketcherMgr::onBeforeContextMenu()
545 myIsPopupMenuActive = true;
548 void PartSet_SketcherMgr::onAfterContextMenu()
550 myIsPopupMenuActive = false;
553 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
556 Handle(V3d_View) aView = theWnd->v3dView();
557 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
559 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
560 thePoint.setValue(aX, anY);
563 void PartSet_SketcherMgr::launchEditing()
565 // there should be activate the sketch selection mode because the edit can happens
566 // by any sketch entity or consttant selected
567 activateObjectsInSketchMode(true);
569 if (!myCurrentSelection.empty()) {
570 FeaturePtr aFeature = myCurrentSelection.begin().key();
571 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
572 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
574 myModule->editFeature(aSPFeature);
581 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
583 static QStringList aIds;
584 if (aIds.size() == 0) {
585 aIds << SketchPlugin_Line::ID().c_str();
586 aIds << SketchPlugin_Point::ID().c_str();
587 aIds << SketchPlugin_Arc::ID().c_str();
588 aIds << SketchPlugin_Circle::ID().c_str();
589 aIds << SketchPlugin_ConstraintLength::ID().c_str();
590 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
591 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
592 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
593 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
594 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
595 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
596 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
597 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
598 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
599 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
600 aIds << SketchPlugin_ConstraintFillet::ID().c_str();
601 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
606 const QStringList& PartSet_SketcherMgr::constraintsIdList()
608 static QStringList aIds;
609 if (aIds.size() == 0) {
610 aIds << SketchPlugin_ConstraintLength::ID().c_str();
611 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
612 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
613 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
614 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
615 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
616 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
617 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
618 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
619 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
620 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
621 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
627 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
629 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
632 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
634 return theOperation &&
635 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
638 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
640 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
643 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
645 std::string aId = theOperation ? theOperation->id().toStdString() : "";
647 return (aId == SketchPlugin_Line::ID()) ||
648 (aId == SketchPlugin_Point::ID()) ||
649 (aId == SketchPlugin_Arc::ID()) ||
650 (aId == SketchPlugin_Circle::ID());
653 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
655 std::string aId = theOperation ? theOperation->id().toStdString() : "";
657 return (aId == SketchPlugin_ConstraintLength::ID()) ||
658 (aId == SketchPlugin_ConstraintDistance::ID()) ||
659 (aId == SketchPlugin_ConstraintRadius::ID());
662 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
664 // Display all sketcher sub-Objects
665 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
666 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
667 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
669 // Hide sketcher result
670 std::list<ResultPtr> aResults = myCurrentSketch->results();
671 std::list<ResultPtr>::const_iterator aIt;
672 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
673 aDisplayer->erase((*aIt), false);
675 aDisplayer->erase(myCurrentSketch, false);
677 // Display sketcher 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->display((*aIt), false);
685 aDisplayer->display(aFeature, false);
688 if (myPlaneFilter.IsNull())
689 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
691 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
692 if (theOperation->isEditOperation()) {
693 // If it is editing of sketch then it means that plane is already defined
694 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
695 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
697 aDisplayer->updateViewer();
700 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
702 myIsMouseOverWindow = false;
703 myIsConstraintsShown = true;
704 // the objects activated in the sketch should be deactivated in order to do not have the specific
705 // sketch selection mode activated on objects in neutral point of the application(no started operation)
706 activateObjectsInSketchMode(false);
708 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
709 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
711 DataPtr aData = myCurrentSketch->data();
712 if ((!aData) || (!aData->isValid())) {
713 // The sketch was aborted
714 myCurrentSketch = CompositeFeaturePtr();
715 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
717 // Erase all sketcher objects
718 QStringList aSketchIds = sketchOperationIdList();
719 QObjectPtrList aObjects = aDisplayer->displayedObjects();
720 foreach (ObjectPtr aObj, aObjects) {
721 DataPtr aObjData = aObj->data();
722 if ((!aObjData) || (!aObjData->isValid()))
723 aDisplayer->erase(aObj);
727 // Hide all sketcher sub-Objects
728 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
729 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
730 std::list<ResultPtr> aResults = aFeature->results();
731 std::list<ResultPtr>::const_iterator aIt;
732 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
733 aDisplayer->erase((*aIt), false);
735 aDisplayer->erase(aFeature, false);
737 // Display sketcher result
738 std::list<ResultPtr> aResults = myCurrentSketch->results();
739 std::list<ResultPtr>::const_iterator aIt;
740 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
741 aDisplayer->display((*aIt), false);
743 aDisplayer->display(myCurrentSketch);
745 myCurrentSketch = CompositeFeaturePtr();
746 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
747 aDisplayer->updateViewer();
750 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
752 if (constraintsIdList().contains(theOperation->id())) {
753 // Show constraints if a constraint was created
754 onShowConstraintsToggle(true);
756 connectToPropertyPanel(true);
759 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
761 connectToPropertyPanel(false);
762 myIsPropertyPanelValueChanged = false;
763 myIsMouseOverViewProcessed = true;
765 // the sketch objects selection should be activated in order to select any sketch
767 activateObjectsInSketchMode(true);
770 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
772 if (isNestedCreateOperation(theOperation))
773 visualizeFeature(theOperation, true);
777 bool PartSet_SketcherMgr::canUndo() const
779 return isNestedCreateOperation(getCurrentOperation());
782 bool PartSet_SketcherMgr::canRedo() const
784 return isNestedCreateOperation(getCurrentOperation());
787 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
789 bool aCanDisplay = true;
791 bool aHasActiveSketch = activeSketch().get() != NULL;
792 if (aHasActiveSketch) {
793 // 1. the sketch feature should not be displayed during the sketch active operation
794 // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
795 // nested features can be visualized
796 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
797 if (aFeature.get() != NULL && aFeature == activeSketch())
800 else { // there are no an active sketch
801 // 2. sketch sub-features should not visualized if the sketch operation is not active
802 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
803 if (aFeature.get() != NULL) {
804 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
805 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
806 if (aSketchFeature.get())
811 // 3. For created nested feature operation do not display the created feature if
812 // the mouse curstor leaves the OCC window.
813 // The correction cases, which ignores this condition:
814 // a. the property panel values modification
815 // b. the popup menu activated
816 // c. widget editor control
817 #ifndef DEBUG_DO_NOT_BY_ENTER
818 if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
819 ModuleBase_Operation* aOperation = getCurrentOperation();
820 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
821 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
822 ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
823 // the active widget editor should not influence here. The presentation should be visible always
824 // when this widget is active.
825 if (!anEditorWdg && !myIsPopupMenuActive) {
826 // during a nested create operation, the feature is redisplayed only if the mouse over view
827 // of there was a value modified in the property panel after the mouse left the view
828 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
836 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
838 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
840 // after the plane is selected in the sketch, the sketch selection should be activated
841 // it can not be performed in the sketch label widget because, we don't need to switch off
842 // the selection by any label deactivation, but need to switch it off by stop the sketch
843 activateObjectsInSketchMode(true);
846 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
847 const FeaturePtr& theSketch,
848 ModuleBase_IWorkshop* theWorkshop,
849 FeatureToSelectionMap& theSelection)
851 if (theFeature.get() == NULL)
854 std::set<AttributePtr> aSelectedAttributes;
855 std::set<ResultPtr> aSelectedResults;
857 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
858 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
859 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
860 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
862 std::list<ResultPtr> aResults = theFeature->results();
863 std::list<ResultPtr>::const_iterator aIt;
864 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
866 ResultPtr aResult = *aIt;
867 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
868 if (aAISObj.get() == NULL)
870 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
871 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
873 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
874 aContext->SelectedOwner());
875 if (aBRepOwner.IsNull())
877 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
878 aBRepOwner->Selectable());
882 if (aBRepOwner->HasShape()) {
883 const TopoDS_Shape& aShape = aBRepOwner->Shape();
884 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
885 if (aShapeType == TopAbs_VERTEX) {
886 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
888 if (aPntAttr.get() != NULL)
889 aSelectedAttributes.insert(aPntAttr);
891 else if (aShapeType == TopAbs_EDGE &&
892 aSelectedResults.find(aResult) == aSelectedResults.end()) {
893 aSelectedResults.insert(aResult);
898 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
901 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
902 const FeaturePtr& theSketch,
903 ModuleBase_IWorkshop* theWorkshop,
904 const FeatureToSelectionMap& theSelection,
905 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
907 if (theFeature.get() == NULL)
910 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
911 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
912 std::set<ResultPtr> aSelectedResults = anIt.value().second;
914 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
915 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
916 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
917 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
919 // 1. found the feature's owners. Check the AIS objects of the constructions
920 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
921 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
922 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
924 SelectMgr_IndexedMapOfOwner aSelectedOwners;
925 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
926 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
927 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
928 if (!anOwner.IsNull())
929 anOwnersToSelect.Add(anOwner);
933 // 2. found the feature results's owners
934 std::list<ResultPtr> aResults = theFeature->results();
935 std::list<ResultPtr>::const_iterator aIt;
936 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
938 ResultPtr aResult = *aIt;
939 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
940 if (aAISObj.get() == NULL)
942 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
944 SelectMgr_IndexedMapOfOwner aSelectedOwners;
945 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
946 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
947 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
948 if ( anOwner.IsNull() || !anOwner->HasShape() )
950 const TopoDS_Shape& aShape = anOwner->Shape();
951 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
952 if (aShapeType == TopAbs_VERTEX) {
953 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
954 if (aPntAttr.get() != NULL &&
955 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
956 anOwnersToSelect.Add(anOwner);
959 else if (aShapeType == TopAbs_EDGE) {
960 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
961 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
962 anOwnersToSelect.FindIndex(anOwner) <= 0)
963 anOwnersToSelect.Add(anOwner);
969 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
971 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
972 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
973 XGUI_Workshop* aWorkshop = aConnector->workshop();
974 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
975 if (aPropertyPanel) {
976 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
977 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
979 connect(aWidget, SIGNAL(beforeValuesChanged()),
980 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
981 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
982 connect(aWidget, SIGNAL(afterValuesChanged()),
983 this, SLOT(onAfterValuesChangedInPropertyPanel()));
986 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
987 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
988 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
989 disconnect(aWidget, SIGNAL(afterValuesChanged()),
990 this, SLOT(onAfterValuesChangedInPropertyPanel()));
996 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
998 return myModule->workshop()->currentOperation();
1001 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1002 const bool isToDisplay)
1004 #ifdef DEBUG_DO_NOT_BY_ENTER
1008 if (!theOperation || theOperation->isEditOperation())
1011 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1012 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1013 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1015 // 1. change visibility of the object itself, here the presentable object is processed,
1016 // e.g. constraints features
1017 FeaturePtr aFeature = theOperation->feature();
1018 std::list<ResultPtr> aResults = aFeature->results();
1020 aDisplayer->display(aFeature, false);
1022 aDisplayer->erase(aFeature, false);
1024 // change visibility of the object results, e.g. non-constraint features
1025 std::list<ResultPtr>::const_iterator aIt;
1026 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1028 aDisplayer->display(*aIt, false);
1031 aDisplayer->erase(*aIt, false);
1034 aDisplayer->updateViewer();
1037 void PartSet_SketcherMgr::activateObjectsInSketchMode(const bool isActive)
1039 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1040 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1041 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1045 aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
1046 aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
1047 aModes.append(SketcherPrs_Tools::Sel_Constraint);
1048 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
1049 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
1051 aDisplayer->activateObjects(aModes);
1054 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1056 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1057 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1058 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1060 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1061 if (theHighlightedOnly) {
1062 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1065 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1067 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1068 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1071 // 1. it is necessary to save current selection in order to restore it after the features moving
1072 myCurrentSelection.clear();
1073 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1074 aLast = aFeature2AttributeMap.end();
1075 for (; anIt != aLast; anIt++) {
1076 FeaturePtr aFeature = anIt.key();
1077 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1079 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1082 void PartSet_SketcherMgr::restoreSelection()
1084 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1085 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1086 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1087 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1088 aSLast = myCurrentSelection.end();
1089 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1090 for (; aSIt != aSLast; aSIt++) {
1091 anOwnersToSelect.Clear();
1092 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1094 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1098 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1100 if (myIsConstraintsShown == theOn)
1102 if (myCurrentSketch.get() == NULL)
1105 myIsConstraintsShown = theOn;
1107 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1108 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1109 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1111 const QStringList& aConstrIds = constraintsIdList();
1112 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1113 FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1114 if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1115 if (myIsConstraintsShown)
1116 aDisplayer->display(aSubFeature, false);
1118 aDisplayer->erase(aSubFeature, false);
1121 aDisplayer->updateViewer();