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>
75 /// Returns list of unique objects by sum of objects from List1 and List2
76 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
77 const QList<ModuleBase_ViewerPrs>& theList2)
79 QList<ModuleBase_ViewerPrs> aRes;
80 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
81 if (!aRes.contains(aPrs))
84 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
85 if (!aRes.contains(aPrs))
91 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
92 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
93 const FeaturePtr theSketch)
95 QList<ModuleBase_ViewerPrs> aRes;
97 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
98 aLast = theList.end();
99 for (; anIt != aLast; anIt++)
101 ModuleBase_ViewerPrs aPrs = *anIt;
102 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
103 if (aFeature.get() == NULL)
106 QList<AttributePtr> anAttributes;
107 if (theFeature2AttributeMap.contains(aFeature)) {
108 anAttributes = theFeature2AttributeMap[aFeature];
111 TopoDS_Shape aShape = aPrs.shape();
112 if (!aShape.IsNull()) {
113 if (aShape.ShapeType() == TopAbs_VERTEX) {
114 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
115 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
116 anAttributes.push_back(anAttr);
119 theFeature2AttributeMap[aFeature] = anAttributes;
126 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
127 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
128 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
129 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
130 myIsPopupMenuActive(false), myIsConstraintsShown(true)
132 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
133 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
135 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
137 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
138 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
140 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
141 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
143 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
144 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
146 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
147 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
149 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
150 XGUI_Workshop* aWorkshop = aConnector->workshop();
151 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
154 PartSet_SketcherMgr::~PartSet_SketcherMgr()
156 if (!myPlaneFilter.IsNull())
157 myPlaneFilter.Nullify();
160 void PartSet_SketcherMgr::onEnterViewPort()
162 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
163 // created feature because it should be done in onMouseMove(). Some widgets watch
164 // the mouse move and use the cursor position to update own values. If the presentaion is
165 // redisplayed before this update, the feature presentation jumps from reset value to current.
166 myIsMouseOverWindow = true;
167 myIsPropertyPanelValueChanged = false;
169 if (!isNestedCreateOperation(getCurrentOperation()))
173 void PartSet_SketcherMgr::onLeaveViewPort()
175 myIsMouseOverViewProcessed = false;
176 myIsMouseOverWindow = false;
177 myIsPropertyPanelValueChanged = false;
179 if (!isNestedCreateOperation(getCurrentOperation()))
181 // the method should be performed if the popup menu is called,
182 // the reset of the current widget should not happen
183 if (myIsPopupMenuActive)
186 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
187 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
188 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
189 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
190 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
191 // obtained after reset value
192 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
193 ModuleBase_Operation* aOperation = getCurrentOperation();
194 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
195 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
199 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
201 // hides the presentation of the current operation feature
202 //myIsPropertyPanelValueChanged = false;
203 // the feature is to be erased here, but it is correct to call canDisplayObject because
204 // there can be additional check (e.g. editor widget in distance constraint)
205 FeaturePtr aFeature = getCurrentOperation()->feature();
206 visualizeFeature(aOperation, canDisplayObject(aFeature));
209 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
211 if (isNestedCreateOperation(getCurrentOperation()))
213 // it is necessary to save current selection in order to restore it after the values are modifed
216 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
217 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
218 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
219 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
222 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
224 if (isNestedCreateOperation(getCurrentOperation()))
226 // it is necessary to restore current selection in order to restore it after the values are modified
228 myCurrentSelection.clear();
230 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
231 // viewer happens by deselect/select the modified objects. The flag should be restored after
232 // the selection processing. The update viewer should be also called.
233 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
234 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
235 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
236 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
237 aDisplayer->updateViewer();
240 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
242 if (!isNestedCreateOperation(getCurrentOperation()))
245 // visualize the current operation feature
246 myIsPropertyPanelValueChanged = true;
247 ModuleBase_Operation* aOperation = getCurrentOperation();
248 // the feature is to be erased here, but it is correct to call canDisplayObject because
249 // there can be additional check (e.g. editor widget in distance constraint)
250 FeaturePtr aFeature = getCurrentOperation()->feature();
251 visualizeFeature(aOperation, canDisplayObject(aFeature));
254 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
256 get2dPoint(theWnd, theEvent, myClickedPoint);
258 if (!(theEvent->buttons() & Qt::LeftButton))
261 // Clear dragging mode
262 myIsDragging = false;
264 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
265 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
266 if (!aViewer->canDragByMouse())
269 ModuleBase_Operation* aOperation = getCurrentOperation();
270 if (aOperation && aOperation->isEditOperation()) {
271 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
272 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
273 // If the current widget is a selector, do do nothing, it processes the mouse press
274 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
279 // Use only for sketch operations
280 if (aOperation && myCurrentSketch) {
281 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
284 bool isSketcher = isSketchOperation(aOperation);
285 bool isSketchOpe = isNestedSketchOperation(aOperation);
287 // Avoid non-sketch operations
288 if ((!isSketchOpe) && (!isSketcher))
291 bool isEditing = aOperation->isEditOperation();
293 // Ignore creation sketch operation
294 if ((!isSketcher) && (!isEditing))
297 // MoveTo in order to highlight current object
298 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
300 // Remember highlighted objects for editing
301 ModuleBase_ISelection* aSelect = aWorkshop->selection();
303 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
304 storeSelection(!aHasShift);
306 if (myCurrentSelection.empty()) {
307 if (isSketchOpe && (!isSketcher))
308 // commit previous operation
309 if (!aOperation->commit())
316 get2dPoint(theWnd, theEvent, myCurrentPoint);
320 } else if (isSketchOpe && isEditing) {
321 // If selected another object commit current result
322 aOperation->commit();
325 get2dPoint(theWnd, theEvent, myCurrentPoint);
328 // This is necessary in order to finalize previous operation
329 QApplication::processEvents();
335 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
337 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
338 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
339 if (!aViewer->canDragByMouse())
341 ModuleBase_Operation* aOp = getCurrentOperation();
343 if (isNestedSketchOperation(aOp)) {
344 get2dPoint(theWnd, theEvent, myClickedPoint);
346 // Only for sketcher operations
350 myCurrentSelection.clear();
351 // Reselect edited object
352 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
353 if (theEvent->modifiers() & Qt::ShiftModifier)
354 aViewer->AISContext()->ShiftSelect();
356 aViewer->AISContext()->Select();
362 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
363 myIsDragging = false;
366 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
368 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
369 myIsMouseOverViewProcessed = true;
370 // 1. perform the widget mouse move functionality and display the presentation
371 ModuleBase_Operation* aOperation = getCurrentOperation();
372 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
373 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
374 // the mouse move should be processed in the widget, if it can in order to visualize correct
375 // presentation. These widgets correct the feature attribute according to the mouse position
376 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
378 aPoint2DWdg->onMouseMove(theWnd, theEvent);
380 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
383 aDistanceWdg->onMouseMove(theWnd, theEvent);
385 // the feature is to be erased here, but it is correct to call canDisplayObject because
386 // there can be additional check (e.g. editor widget in distance constraint)
387 FeaturePtr aFeature = getCurrentOperation()->feature();
388 visualizeFeature(aOperation, canDisplayObject(aFeature));
391 myClickedPoint.clear();
394 // 1. the current selection is saved in the mouse press method in order to restore it after moving
395 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
396 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
397 // deselected). This flag should be restored in the slot, processed the mouse release signal.
398 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
399 aViewer->enableSelection(false);
401 ModuleBase_Operation* aOperation = getCurrentOperation();
404 if (isSketchOperation(aOperation))
405 return; // No edit operation activated
407 Handle(V3d_View) aView = theWnd->v3dView();
408 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
410 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
411 double dX = aX - myCurrentPoint.myCurX;
412 double dY = aY - myCurrentPoint.myCurY;
414 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
415 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
416 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
417 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
418 // viewer happens by deselect/select the modified objects. The flag should be restored after
419 // the selection processing. The update viewer should be also called.
420 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
422 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
423 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
424 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
425 aLast = myCurrentSelection.end();
426 // 4. the features and attributes modification(move)
427 for (; anIt != aLast; anIt++) {
428 FeaturePtr aFeature = anIt.key();
430 std::set<AttributePtr> anAttributes = anIt.value().first;
431 // Process selection by attribute: the priority to the attribute
432 if (!anAttributes.empty()) {
433 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
434 anAttLast = anAttributes.end();
435 for (; anAttIt != anAttLast; anAttIt++) {
436 AttributePtr anAttr = *anAttIt;
437 if (anAttr.get() == NULL)
439 std::string aAttrId = anAttr->id();
440 DataPtr aData = aFeature->data();
441 if (aData.get() != NULL) {
442 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
443 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
444 if (aPoint.get() != NULL) {
445 bool isImmutable = aPoint->setImmutable(true);
446 aPoint->move(dX, dY);
447 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
448 aPoint->setImmutable(isImmutable);
453 // Process selection by feature
454 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
455 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
456 if (aSketchFeature) {
457 aSketchFeature->move(dX, dY);
458 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
462 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
463 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
465 // 5. it is necessary to save current selection in order to restore it after the features moving
467 // 6. restore the update viewer flag and call this update
468 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
469 aDisplayer->updateViewer();
472 myCurrentPoint.setValue(aX, aY);
476 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
478 ModuleBase_Operation* aOperation = getCurrentOperation();
479 if (aOperation && aOperation->isEditOperation()) {
480 std::string aId = aOperation->id().toStdString();
481 if (isDistanceOperation(aOperation))
483 // Activate dimension value editing on double click
484 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
485 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
486 // Find corresponded widget to activate value editing
487 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
488 if (aWgt->attributeID() == "ConstraintValue") {
497 void PartSet_SketcherMgr::onApplicationStarted()
499 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
500 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
501 XGUI_Workshop* aWorkshop = aConnector->workshop();
502 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
503 if (aPropertyPanel) {
504 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
505 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
508 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
509 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
510 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
512 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
513 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
514 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
517 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
519 if (!myClickedPoint.myIsInitialized)
522 ModuleBase_Operation* aOperation = getCurrentOperation();
523 // the distance constraint feature should not use the clickedd point
524 // this is workaround in order to don't throw down the flyout point value,
525 // set by execute() method of these type of features
526 if (isDistanceOperation(aOperation))
529 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
531 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
535 void PartSet_SketcherMgr::onBeforeContextMenu()
537 myIsPopupMenuActive = true;
540 void PartSet_SketcherMgr::onAfterContextMenu()
542 myIsPopupMenuActive = false;
545 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
548 Handle(V3d_View) aView = theWnd->v3dView();
549 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
551 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
552 thePoint.setValue(aX, anY);
555 void PartSet_SketcherMgr::launchEditing()
557 // there should be activate the sketch selection mode because the edit can happens
558 // by any sketch entity or consttant selected
559 activateObjectsInSketchMode(true);
561 if (!myCurrentSelection.empty()) {
562 FeaturePtr aFeature = myCurrentSelection.begin().key();
563 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
564 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
566 myModule->editFeature(aSPFeature);
573 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
575 static QStringList aIds;
576 if (aIds.size() == 0) {
577 aIds << SketchPlugin_Line::ID().c_str();
578 aIds << SketchPlugin_Point::ID().c_str();
579 aIds << SketchPlugin_Arc::ID().c_str();
580 aIds << SketchPlugin_Circle::ID().c_str();
581 aIds << SketchPlugin_ConstraintLength::ID().c_str();
582 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
583 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
584 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
585 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
586 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
587 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
588 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
589 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
590 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
591 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
592 aIds << SketchPlugin_ConstraintFillet::ID().c_str();
593 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
598 const QStringList& PartSet_SketcherMgr::constraintsIdList()
600 static QStringList aIds;
601 if (aIds.size() == 0) {
602 aIds << SketchPlugin_ConstraintLength::ID().c_str();
603 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
604 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
605 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
606 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
607 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
608 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
609 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
610 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
611 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
612 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
613 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
619 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
621 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
624 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
626 return theOperation &&
627 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
630 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
632 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
635 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
637 std::string aId = theOperation ? theOperation->id().toStdString() : "";
639 return (aId == SketchPlugin_Line::ID()) ||
640 (aId == SketchPlugin_Point::ID()) ||
641 (aId == SketchPlugin_Arc::ID()) ||
642 (aId == SketchPlugin_Circle::ID());
645 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
647 std::string aId = theOperation ? theOperation->id().toStdString() : "";
649 return (aId == SketchPlugin_ConstraintLength::ID()) ||
650 (aId == SketchPlugin_ConstraintDistance::ID()) ||
651 (aId == SketchPlugin_ConstraintRadius::ID());
654 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
656 // Display all sketcher sub-Objects
657 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
658 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
659 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
661 // Hide sketcher result
662 std::list<ResultPtr> aResults = myCurrentSketch->results();
663 std::list<ResultPtr>::const_iterator aIt;
664 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
665 aDisplayer->erase((*aIt), false);
667 aDisplayer->erase(myCurrentSketch, false);
669 // Display sketcher objects
670 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
671 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
672 std::list<ResultPtr> aResults = aFeature->results();
673 std::list<ResultPtr>::const_iterator aIt;
674 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
675 aDisplayer->display((*aIt), false);
677 aDisplayer->display(aFeature, false);
680 if (myPlaneFilter.IsNull())
681 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
683 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
684 if (theOperation->isEditOperation()) {
685 // If it is editing of sketch then it means that plane is already defined
686 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
687 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
689 aDisplayer->updateViewer();
692 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
694 myIsMouseOverWindow = false;
695 myIsConstraintsShown = true;
696 // the objects activated in the sketch should be deactivated in order to do not have the specific
697 // sketch selection mode activated on objects in neutral point of the application(no started operation)
698 activateObjectsInSketchMode(false);
700 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
701 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
703 DataPtr aData = myCurrentSketch->data();
704 if ((!aData) || (!aData->isValid())) {
705 // The sketch was aborted
706 myCurrentSketch = CompositeFeaturePtr();
707 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
709 // Erase all sketcher objects
710 QStringList aSketchIds = sketchOperationIdList();
711 QObjectPtrList aObjects = aDisplayer->displayedObjects();
712 foreach (ObjectPtr aObj, aObjects) {
713 DataPtr aObjData = aObj->data();
714 if ((!aObjData) || (!aObjData->isValid()))
715 aDisplayer->erase(aObj);
719 // Hide all sketcher sub-Objects
720 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
721 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
722 std::list<ResultPtr> aResults = aFeature->results();
723 std::list<ResultPtr>::const_iterator aIt;
724 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
725 aDisplayer->erase((*aIt), false);
727 aDisplayer->erase(aFeature, false);
729 // Display sketcher result
730 std::list<ResultPtr> aResults = myCurrentSketch->results();
731 std::list<ResultPtr>::const_iterator aIt;
732 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
733 aDisplayer->display((*aIt), false);
735 aDisplayer->display(myCurrentSketch);
737 myCurrentSketch = CompositeFeaturePtr();
738 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
739 aDisplayer->updateViewer();
742 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
744 if (constraintsIdList().contains(theOperation->id())) {
745 // Show constraints if a constraint was created
746 onShowConstraintsToggle(true);
748 connectToPropertyPanel(true);
751 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
753 connectToPropertyPanel(false);
754 myIsPropertyPanelValueChanged = false;
755 myIsMouseOverViewProcessed = true;
757 // the sketch objects selection should be activated in order to select any sketch
759 activateObjectsInSketchMode(true);
762 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
764 if (isNestedCreateOperation(theOperation))
765 visualizeFeature(theOperation, true);
769 bool PartSet_SketcherMgr::canUndo() const
771 return isNestedCreateOperation(getCurrentOperation());
774 bool PartSet_SketcherMgr::canRedo() const
776 return isNestedCreateOperation(getCurrentOperation());
779 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
781 bool aCanDisplay = true;
783 bool aHasActiveSketch = activeSketch().get() != NULL;
784 if (aHasActiveSketch) {
785 // 1. the sketch feature should not be displayed during the sketch active operation
786 // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
787 // nested features can be visualized
788 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
789 if (aFeature.get() != NULL && aFeature == activeSketch())
792 else { // there are no an active sketch
793 // 2. sketch sub-features should not visualized if the sketch operatio is not active
794 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
795 if (aFeature.get() != NULL) {
796 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
797 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
798 if (aSketchFeature.get())
803 // 3. For created nested feature operation do not display the created feature if
804 // the mouse curstor leaves the OCC window.
805 // The correction cases, which ignores this condition:
806 // a. the property panel values modification
807 // b. the popup menu activated
808 // c. widget editor control
809 if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
810 ModuleBase_Operation* aOperation = getCurrentOperation();
811 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
812 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
813 ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
814 // the active widget editor should not influence here. The presentation should be visible always
815 // when this widget is active.
816 if (!anEditorWdg && !myIsPopupMenuActive) {
817 // during a nested create operation, the feature is redisplayed only if the mouse over view
818 // of there was a value modified in the property panel after the mouse left the view
819 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
826 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
828 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
830 // after the plane is selected in the sketch, the sketch selection should be activated
831 // it can not be performed in the sketch label widget because, we don't need to switch off
832 // the selection by any label deactivation, but need to switch it off by stop the sketch
833 activateObjectsInSketchMode(true);
836 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
837 const FeaturePtr& theSketch,
838 ModuleBase_IWorkshop* theWorkshop,
839 FeatureToSelectionMap& theSelection)
841 if (theFeature.get() == NULL)
844 std::set<AttributePtr> aSelectedAttributes;
845 std::set<ResultPtr> aSelectedResults;
847 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
848 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
849 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
850 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
852 std::list<ResultPtr> aResults = theFeature->results();
853 std::list<ResultPtr>::const_iterator aIt;
854 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
856 ResultPtr aResult = *aIt;
857 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
858 if (aAISObj.get() == NULL)
860 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
861 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
863 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
864 aContext->SelectedOwner());
865 if (aBRepOwner.IsNull())
867 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
868 aBRepOwner->Selectable());
872 if (aBRepOwner->HasShape()) {
873 const TopoDS_Shape& aShape = aBRepOwner->Shape();
874 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
875 if (aShapeType == TopAbs_VERTEX) {
876 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
878 if (aPntAttr.get() != NULL)
879 aSelectedAttributes.insert(aPntAttr);
881 else if (aShapeType == TopAbs_EDGE &&
882 aSelectedResults.find(aResult) == aSelectedResults.end()) {
883 aSelectedResults.insert(aResult);
888 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
891 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
892 const FeaturePtr& theSketch,
893 ModuleBase_IWorkshop* theWorkshop,
894 const FeatureToSelectionMap& theSelection,
895 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
897 if (theFeature.get() == NULL)
900 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
901 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
902 std::set<ResultPtr> aSelectedResults = anIt.value().second;
904 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
905 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
906 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
907 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
909 // 1. found the feature's owners. Check the AIS objects of the constructions
910 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
911 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
912 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
914 SelectMgr_IndexedMapOfOwner aSelectedOwners;
915 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
916 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
917 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
918 if (!anOwner.IsNull())
919 anOwnersToSelect.Add(anOwner);
923 // 2. found the feature results's owners
924 std::list<ResultPtr> aResults = theFeature->results();
925 std::list<ResultPtr>::const_iterator aIt;
926 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
928 ResultPtr aResult = *aIt;
929 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
930 if (aAISObj.get() == NULL)
932 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
934 SelectMgr_IndexedMapOfOwner aSelectedOwners;
935 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
936 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
937 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
938 if ( anOwner.IsNull() || !anOwner->HasShape() )
940 const TopoDS_Shape& aShape = anOwner->Shape();
941 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
942 if (aShapeType == TopAbs_VERTEX) {
943 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
944 if (aPntAttr.get() != NULL &&
945 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
946 anOwnersToSelect.Add(anOwner);
949 else if (aShapeType == TopAbs_EDGE) {
950 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
951 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
952 anOwnersToSelect.FindIndex(anOwner) <= 0)
953 anOwnersToSelect.Add(anOwner);
959 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
961 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
962 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
963 XGUI_Workshop* aWorkshop = aConnector->workshop();
964 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
965 if (aPropertyPanel) {
966 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
967 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
969 connect(aWidget, SIGNAL(beforeValuesChanged()),
970 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
971 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
972 connect(aWidget, SIGNAL(afterValuesChanged()),
973 this, SLOT(onAfterValuesChangedInPropertyPanel()));
976 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
977 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
978 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
979 disconnect(aWidget, SIGNAL(afterValuesChanged()),
980 this, SLOT(onAfterValuesChangedInPropertyPanel()));
986 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
988 return myModule->workshop()->currentOperation();
991 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
992 const bool isToDisplay)
994 if (!theOperation || theOperation->isEditOperation())
997 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
998 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
999 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1001 // 1. change visibility of the object itself, here the presentable object is processed,
1002 // e.g. constraints features
1003 FeaturePtr aFeature = theOperation->feature();
1004 std::list<ResultPtr> aResults = aFeature->results();
1006 aDisplayer->display(aFeature, false);
1008 aDisplayer->erase(aFeature, false);
1010 // change visibility of the object results, e.g. non-constraint features
1011 std::list<ResultPtr>::const_iterator aIt;
1012 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1014 aDisplayer->display(*aIt, false);
1017 aDisplayer->erase(*aIt, false);
1020 aDisplayer->updateViewer();
1023 void PartSet_SketcherMgr::activateObjectsInSketchMode(const bool isActive)
1025 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1026 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1027 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1031 aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
1032 aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
1033 aModes.append(SketcherPrs_Tools::Sel_Constraint);
1034 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
1035 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
1037 aDisplayer->activateObjects(aModes);
1040 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1042 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1043 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1044 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1046 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1047 if (theHighlightedOnly) {
1048 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1051 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1053 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1054 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1057 // 1. it is necessary to save current selection in order to restore it after the features moving
1058 myCurrentSelection.clear();
1059 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1060 aLast = aFeature2AttributeMap.end();
1061 for (; anIt != aLast; anIt++) {
1062 FeaturePtr aFeature = anIt.key();
1063 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1065 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1068 void PartSet_SketcherMgr::restoreSelection()
1070 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1071 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1072 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1073 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1074 aSLast = myCurrentSelection.end();
1075 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1076 for (; aSIt != aSLast; aSIt++) {
1077 anOwnersToSelect.Clear();
1078 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1080 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1084 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1086 if (myIsConstraintsShown == theOn)
1088 if (myCurrentSketch.get() == NULL)
1091 myIsConstraintsShown = theOn;
1093 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1094 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1095 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1097 const QStringList& aConstrIds = constraintsIdList();
1098 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1099 FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1100 if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1101 if (myIsConstraintsShown)
1102 aDisplayer->display(aSubFeature, false);
1104 aDisplayer->erase(aSubFeature, false);
1107 aDisplayer->updateViewer();