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::isEntity(const std::string& theId)
645 return (theId == SketchPlugin_Line::ID()) ||
646 (theId == SketchPlugin_Point::ID()) ||
647 (theId == SketchPlugin_Arc::ID()) ||
648 (theId == SketchPlugin_Circle::ID());
651 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
653 std::string aId = theOperation ? theOperation->id().toStdString() : "";
655 return (aId == SketchPlugin_ConstraintLength::ID()) ||
656 (aId == SketchPlugin_ConstraintDistance::ID()) ||
657 (aId == SketchPlugin_ConstraintRadius::ID());
660 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
662 // Display all sketcher sub-Objects
663 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
664 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
665 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
667 // Hide sketcher result
668 std::list<ResultPtr> aResults = myCurrentSketch->results();
669 std::list<ResultPtr>::const_iterator aIt;
670 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
671 aDisplayer->erase((*aIt), false);
673 aDisplayer->erase(myCurrentSketch, false);
675 // Display sketcher objects
676 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
677 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
678 std::list<ResultPtr> aResults = aFeature->results();
679 std::list<ResultPtr>::const_iterator aIt;
680 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
681 aDisplayer->display((*aIt), false);
683 aDisplayer->display(aFeature, false);
686 if (myPlaneFilter.IsNull())
687 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
689 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
690 if (theOperation->isEditOperation()) {
691 // If it is editing of sketch then it means that plane is already defined
692 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
693 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
695 aDisplayer->updateViewer();
698 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
700 myIsMouseOverWindow = false;
701 myIsConstraintsShown = true;
702 // the objects activated in the sketch should be deactivated in order to do not have the specific
703 // sketch selection mode activated on objects in neutral point of the application(no started operation)
704 activateObjectsInSketchMode(false);
706 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
707 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
709 DataPtr aData = myCurrentSketch->data();
710 if ((!aData) || (!aData->isValid())) {
711 // The sketch was aborted
712 myCurrentSketch = CompositeFeaturePtr();
713 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
715 // Erase all sketcher objects
716 QStringList aSketchIds = sketchOperationIdList();
717 QObjectPtrList aObjects = aDisplayer->displayedObjects();
718 foreach (ObjectPtr aObj, aObjects) {
719 DataPtr aObjData = aObj->data();
720 if ((!aObjData) || (!aObjData->isValid()))
721 aDisplayer->erase(aObj);
725 // Hide all sketcher sub-Objects
726 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
727 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
728 std::list<ResultPtr> aResults = aFeature->results();
729 std::list<ResultPtr>::const_iterator aIt;
730 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
731 aDisplayer->erase((*aIt), false);
733 aDisplayer->erase(aFeature, false);
735 // Display sketcher result
736 std::list<ResultPtr> aResults = myCurrentSketch->results();
737 std::list<ResultPtr>::const_iterator aIt;
738 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
739 aDisplayer->display((*aIt), false);
741 aDisplayer->display(myCurrentSketch);
743 myCurrentSketch = CompositeFeaturePtr();
744 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
745 aDisplayer->updateViewer();
748 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
750 if (constraintsIdList().contains(theOperation->id())) {
751 // Show constraints if a constraint was created
752 onShowConstraintsToggle(true);
754 connectToPropertyPanel(true);
757 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
759 connectToPropertyPanel(false);
760 myIsPropertyPanelValueChanged = false;
761 myIsMouseOverViewProcessed = true;
763 // the sketch objects selection should be activated in order to select any sketch
765 activateObjectsInSketchMode(true);
768 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
770 if (isNestedCreateOperation(theOperation))
771 visualizeFeature(theOperation, true);
775 bool PartSet_SketcherMgr::canUndo() const
777 return isNestedCreateOperation(getCurrentOperation());
780 bool PartSet_SketcherMgr::canRedo() const
782 return isNestedCreateOperation(getCurrentOperation());
785 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
787 bool aCanDisplay = true;
789 bool aHasActiveSketch = activeSketch().get() != NULL;
790 if (aHasActiveSketch) {
791 // 1. the sketch feature should not be displayed during the sketch active operation
792 // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
793 // nested features can be visualized
794 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
795 if (aFeature.get() != NULL && aFeature == activeSketch())
798 else { // there are no an active sketch
799 // 2. sketch sub-features should not be visualized if the sketch operation is not active
800 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
801 if (aFeature.get() != NULL) {
802 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
803 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
804 if (aSketchFeature.get())
809 // 3. the method should not filter the objects, which are not related to the current operation.
810 // The object is filtered just if it is a current operation feature or this feature result
811 bool isObjectFound = false;
812 ModuleBase_Operation* anOperation = getCurrentOperation();
814 FeaturePtr aFeature = anOperation->feature();
815 if (aFeature.get()) {
816 std::list<ResultPtr> aResults = aFeature->results();
817 if (theObject == aFeature)
818 isObjectFound = true;
820 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
821 for (; anIt != aLast; anIt++) {
822 isObjectFound = *anIt == theObject;
830 // 4. For created nested feature operation do not display the created feature if
831 // the mouse curstor leaves the OCC window.
832 // The correction cases, which ignores this condition:
833 // a. the property panel values modification
834 // b. the popup menu activated
835 // c. widget editor control
836 #ifndef DEBUG_DO_NOT_BY_ENTER
837 if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
838 ModuleBase_Operation* aOperation = getCurrentOperation();
839 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
840 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
841 ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
842 // the active widget editor should not influence here. The presentation should be visible always
843 // when this widget is active.
844 if (!anEditorWdg && !myIsPopupMenuActive) {
845 // during a nested create operation, the feature is redisplayed only if the mouse over view
846 // of there was a value modified in the property panel after the mouse left the view
847 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
855 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
857 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
859 // after the plane is selected in the sketch, the sketch selection should be activated
860 // it can not be performed in the sketch label widget because, we don't need to switch off
861 // the selection by any label deactivation, but need to switch it off by stop the sketch
862 activateObjectsInSketchMode(true);
865 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
866 const FeaturePtr& theSketch,
867 ModuleBase_IWorkshop* theWorkshop,
868 FeatureToSelectionMap& theSelection)
870 if (theFeature.get() == NULL)
873 std::set<AttributePtr> aSelectedAttributes;
874 std::set<ResultPtr> aSelectedResults;
876 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
877 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
878 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
879 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
881 std::list<ResultPtr> aResults = theFeature->results();
882 std::list<ResultPtr>::const_iterator aIt;
883 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
885 ResultPtr aResult = *aIt;
886 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
887 if (aAISObj.get() == NULL)
889 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
890 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
892 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
893 aContext->SelectedOwner());
894 if (aBRepOwner.IsNull())
896 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
897 aBRepOwner->Selectable());
901 if (aBRepOwner->HasShape()) {
902 const TopoDS_Shape& aShape = aBRepOwner->Shape();
903 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
904 if (aShapeType == TopAbs_VERTEX) {
905 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
907 if (aPntAttr.get() != NULL)
908 aSelectedAttributes.insert(aPntAttr);
910 else if (aShapeType == TopAbs_EDGE &&
911 aSelectedResults.find(aResult) == aSelectedResults.end()) {
912 aSelectedResults.insert(aResult);
917 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
920 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
921 const FeaturePtr& theSketch,
922 ModuleBase_IWorkshop* theWorkshop,
923 const FeatureToSelectionMap& theSelection,
924 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
926 if (theFeature.get() == NULL)
929 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
930 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
931 std::set<ResultPtr> aSelectedResults = anIt.value().second;
933 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
934 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
935 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
936 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
938 // 1. found the feature's owners. Check the AIS objects of the constructions
939 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
940 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
941 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
943 SelectMgr_IndexedMapOfOwner aSelectedOwners;
944 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
945 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
946 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
947 if (!anOwner.IsNull())
948 anOwnersToSelect.Add(anOwner);
952 // 2. found the feature results's owners
953 std::list<ResultPtr> aResults = theFeature->results();
954 std::list<ResultPtr>::const_iterator aIt;
955 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
957 ResultPtr aResult = *aIt;
958 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
959 if (aAISObj.get() == NULL)
961 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
963 SelectMgr_IndexedMapOfOwner aSelectedOwners;
964 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
965 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
966 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
967 if ( anOwner.IsNull() || !anOwner->HasShape() )
969 const TopoDS_Shape& aShape = anOwner->Shape();
970 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
971 if (aShapeType == TopAbs_VERTEX) {
972 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
973 if (aPntAttr.get() != NULL &&
974 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
975 anOwnersToSelect.Add(anOwner);
978 else if (aShapeType == TopAbs_EDGE) {
979 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
980 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
981 anOwnersToSelect.FindIndex(anOwner) <= 0)
982 anOwnersToSelect.Add(anOwner);
988 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
990 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
991 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
992 XGUI_Workshop* aWorkshop = aConnector->workshop();
993 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
994 if (aPropertyPanel) {
995 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
996 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
998 connect(aWidget, SIGNAL(beforeValuesChanged()),
999 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1000 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1001 connect(aWidget, SIGNAL(afterValuesChanged()),
1002 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1005 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1006 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1007 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1008 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1009 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1015 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1017 return myModule->workshop()->currentOperation();
1020 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1021 const bool isToDisplay)
1023 #ifdef DEBUG_DO_NOT_BY_ENTER
1027 if (!theOperation || theOperation->isEditOperation())
1030 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1031 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1032 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1034 // 1. change visibility of the object itself, here the presentable object is processed,
1035 // e.g. constraints features
1036 FeaturePtr aFeature = theOperation->feature();
1037 std::list<ResultPtr> aResults = aFeature->results();
1039 aDisplayer->display(aFeature, false);
1041 aDisplayer->erase(aFeature, false);
1043 // change visibility of the object results, e.g. non-constraint features
1044 std::list<ResultPtr>::const_iterator aIt;
1045 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1047 aDisplayer->display(*aIt, false);
1050 aDisplayer->erase(*aIt, false);
1053 aDisplayer->updateViewer();
1056 void PartSet_SketcherMgr::activateObjectsInSketchMode(const bool isActive)
1058 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1059 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1060 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1064 aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
1065 aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
1066 aModes.append(SketcherPrs_Tools::Sel_Constraint);
1067 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
1068 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
1070 aDisplayer->activateObjects(aModes);
1073 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1075 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1076 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1077 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1079 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1080 if (theHighlightedOnly) {
1081 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1084 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1086 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1087 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1090 // 1. it is necessary to save current selection in order to restore it after the features moving
1091 myCurrentSelection.clear();
1092 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1093 aLast = aFeature2AttributeMap.end();
1094 for (; anIt != aLast; anIt++) {
1095 FeaturePtr aFeature = anIt.key();
1096 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1098 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1101 void PartSet_SketcherMgr::restoreSelection()
1103 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1104 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1105 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1106 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1107 aSLast = myCurrentSelection.end();
1108 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1109 for (; aSIt != aSLast; aSIt++) {
1110 anOwnersToSelect.Clear();
1111 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1113 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1117 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1119 if (myIsConstraintsShown == theOn)
1121 if (myCurrentSketch.get() == NULL)
1124 myIsConstraintsShown = theOn;
1126 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1127 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1128 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1130 const QStringList& aConstrIds = constraintsIdList();
1131 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1132 FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1133 if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1134 if (myIsConstraintsShown)
1135 aDisplayer->display(aSubFeature, false);
1137 aDisplayer->erase(aSubFeature, false);
1140 aDisplayer->updateViewer();