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 <SelectMgr_IndexedMapOfOwner.hxx>
61 #include <StdSelect_BRepOwner.hxx>
63 //#include <AIS_DimensionSelectionMode.hxx>
64 //#include <AIS_Shape.hxx>
66 #include <ModelAPI_Events.h>
67 #include <ModelAPI_Session.h>
69 #include <QMouseEvent>
70 #include <QApplication>
73 /// Returns list of unique objects by sum of objects from List1 and List2
74 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
75 const QList<ModuleBase_ViewerPrs>& theList2)
77 QList<ModuleBase_ViewerPrs> aRes;
78 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
79 if (!aRes.contains(aPrs))
82 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
83 if (!aRes.contains(aPrs))
89 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
90 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
91 const FeaturePtr theSketch)
93 QList<ModuleBase_ViewerPrs> aRes;
95 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
96 aLast = theList.end();
97 for (; anIt != aLast; anIt++)
99 ModuleBase_ViewerPrs aPrs = *anIt;
100 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
101 if (aFeature.get() == NULL)
104 QList<AttributePtr> anAttributes;
105 if (theFeature2AttributeMap.contains(aFeature)) {
106 anAttributes = theFeature2AttributeMap[aFeature];
109 TopoDS_Shape aShape = aPrs.shape();
110 if (!aShape.IsNull()) {
111 if (aShape.ShapeType() == TopAbs_VERTEX) {
112 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
113 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
114 anAttributes.push_back(anAttr);
117 theFeature2AttributeMap[aFeature] = anAttributes;
124 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
125 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
126 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
127 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
128 myIsPopupMenuActive(false), myIsConstraintsShown(true)
130 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
131 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
133 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
135 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
136 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
138 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
139 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
141 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
142 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
144 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
145 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
147 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
148 XGUI_Workshop* aWorkshop = aConnector->workshop();
149 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
152 PartSet_SketcherMgr::~PartSet_SketcherMgr()
154 if (!myPlaneFilter.IsNull())
155 myPlaneFilter.Nullify();
158 void PartSet_SketcherMgr::onEnterViewPort()
160 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
161 // created feature because it should be done in onMouseMove(). Some widgets watch
162 // the mouse move and use the cursor position to update own values. If the presentaion is
163 // redisplayed before this update, the feature presentation jumps from reset value to current.
164 myIsMouseOverWindow = true;
165 myIsPropertyPanelValueChanged = false;
167 if (!isNestedCreateOperation(getCurrentOperation()))
171 void PartSet_SketcherMgr::onLeaveViewPort()
173 myIsMouseOverViewProcessed = false;
174 myIsMouseOverWindow = false;
175 myIsPropertyPanelValueChanged = false;
177 if (!isNestedCreateOperation(getCurrentOperation()))
179 // the method should be performed if the popup menu is called,
180 // the reset of the current widget should not happen
181 if (myIsPopupMenuActive)
184 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
185 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
186 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
187 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
188 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
189 // obtained after reset value
190 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
191 ModuleBase_Operation* aOperation = getCurrentOperation();
192 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
193 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
197 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
199 // hides the presentation of the current operation feature
200 //myIsPropertyPanelValueChanged = false;
201 // the feature is to be erased here, but it is correct to call canDisplayObject because
202 // there can be additional check (e.g. editor widget in distance constraint)
203 FeaturePtr aFeature = getCurrentOperation()->feature();
204 visualizeFeature(aOperation, canDisplayObject(aFeature));
207 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
209 if (isNestedCreateOperation(getCurrentOperation()))
211 // it is necessary to save current selection in order to restore it after the values are modifed
214 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
215 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
216 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
217 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
220 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
222 if (isNestedCreateOperation(getCurrentOperation()))
224 // it is necessary to restore current selection in order to restore it after the values are modified
226 myCurrentSelection.clear();
228 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
229 // viewer happens by deselect/select the modified objects. The flag should be restored after
230 // the selection processing. The update viewer should be also called.
231 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
232 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
233 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
234 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
235 aDisplayer->updateViewer();
238 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
240 if (!isNestedCreateOperation(getCurrentOperation()))
243 // visualize the current operation feature
244 myIsPropertyPanelValueChanged = true;
245 ModuleBase_Operation* aOperation = getCurrentOperation();
246 // the feature is to be erased here, but it is correct to call canDisplayObject because
247 // there can be additional check (e.g. editor widget in distance constraint)
248 FeaturePtr aFeature = getCurrentOperation()->feature();
249 visualizeFeature(aOperation, canDisplayObject(aFeature));
252 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
254 get2dPoint(theWnd, theEvent, myClickedPoint);
256 if (!(theEvent->buttons() & Qt::LeftButton))
259 // Clear dragging mode
260 myIsDragging = false;
262 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
263 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
264 if (!aViewer->canDragByMouse())
267 ModuleBase_Operation* aOperation = getCurrentOperation();
268 if (aOperation && aOperation->isEditOperation()) {
269 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
270 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
271 // If the current widget is a selector, do do nothing, it processes the mouse press
272 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
277 // Use only for sketch operations
278 if (aOperation && myCurrentSketch) {
279 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
282 bool isSketcher = isSketchOperation(aOperation);
283 bool isSketchOpe = isNestedSketchOperation(aOperation);
285 // Avoid non-sketch operations
286 if ((!isSketchOpe) && (!isSketcher))
289 bool isEditing = aOperation->isEditOperation();
291 // Ignore creation sketch operation
292 if ((!isSketcher) && (!isEditing))
295 // MoveTo in order to highlight current object
296 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
298 // Remember highlighted objects for editing
299 ModuleBase_ISelection* aSelect = aWorkshop->selection();
301 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
302 storeSelection(!aHasShift);
304 if (myCurrentSelection.empty()) {
305 if (isSketchOpe && (!isSketcher))
306 // commit previous operation
307 if (!aOperation->commit())
314 get2dPoint(theWnd, theEvent, myCurrentPoint);
318 } else if (isSketchOpe && isEditing) {
319 // If selected another object commit current result
320 aOperation->commit();
323 get2dPoint(theWnd, theEvent, myCurrentPoint);
326 // This is necessary in order to finalize previous operation
327 QApplication::processEvents();
333 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
335 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
336 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
337 if (!aViewer->canDragByMouse())
339 ModuleBase_Operation* aOp = getCurrentOperation();
341 if (isNestedSketchOperation(aOp)) {
342 get2dPoint(theWnd, theEvent, myClickedPoint);
344 // Only for sketcher operations
348 myCurrentSelection.clear();
349 // Reselect edited object
350 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
351 if (theEvent->modifiers() & Qt::ShiftModifier)
352 aViewer->AISContext()->ShiftSelect();
354 aViewer->AISContext()->Select();
360 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
361 myIsDragging = false;
364 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
366 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
367 myIsMouseOverViewProcessed = true;
368 // 1. perform the widget mouse move functionality and display the presentation
369 ModuleBase_Operation* aOperation = getCurrentOperation();
370 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
371 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
372 // the mouse move should be processed in the widget, if it can in order to visualize correct
373 // presentation. These widgets correct the feature attribute according to the mouse position
374 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
376 aPoint2DWdg->onMouseMove(theWnd, theEvent);
378 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
381 aDistanceWdg->onMouseMove(theWnd, theEvent);
383 // the feature is to be erased here, but it is correct to call canDisplayObject because
384 // there can be additional check (e.g. editor widget in distance constraint)
385 FeaturePtr aFeature = getCurrentOperation()->feature();
386 visualizeFeature(aOperation, canDisplayObject(aFeature));
389 myClickedPoint.clear();
392 // 1. the current selection is saved in the mouse press method in order to restore it after moving
393 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
394 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
395 // deselected). This flag should be restored in the slot, processed the mouse release signal.
396 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
397 aViewer->enableSelection(false);
399 ModuleBase_Operation* aOperation = getCurrentOperation();
402 if (isSketchOperation(aOperation))
403 return; // No edit operation activated
405 Handle(V3d_View) aView = theWnd->v3dView();
406 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
408 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
409 double dX = aX - myCurrentPoint.myCurX;
410 double dY = aY - myCurrentPoint.myCurY;
412 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
413 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
414 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
415 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
416 // viewer happens by deselect/select the modified objects. The flag should be restored after
417 // the selection processing. The update viewer should be also called.
418 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
420 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
421 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
422 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
423 aLast = myCurrentSelection.end();
424 // 4. the features and attributes modification(move)
425 for (; anIt != aLast; anIt++) {
426 FeaturePtr aFeature = anIt.key();
428 std::set<AttributePtr> anAttributes = anIt.value().first;
429 // Process selection by attribute: the priority to the attribute
430 if (!anAttributes.empty()) {
431 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
432 anAttLast = anAttributes.end();
433 for (; anAttIt != anAttLast; anAttIt++) {
434 AttributePtr anAttr = *anAttIt;
435 if (anAttr.get() == NULL)
437 std::string aAttrId = anAttr->id();
438 DataPtr aData = aFeature->data();
439 if (aData.get() != NULL) {
440 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
441 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
442 if (aPoint.get() != NULL) {
443 bool isImmutable = aPoint->setImmutable(true);
444 aPoint->move(dX, dY);
445 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
446 aPoint->setImmutable(isImmutable);
451 // Process selection by feature
452 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
453 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
454 if (aSketchFeature) {
455 aSketchFeature->move(dX, dY);
456 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
460 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
461 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
463 // 5. it is necessary to save current selection in order to restore it after the features moving
465 // 6. restore the update viewer flag and call this update
466 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
467 aDisplayer->updateViewer();
470 myCurrentPoint.setValue(aX, aY);
474 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
476 ModuleBase_Operation* aOperation = getCurrentOperation();
477 if (aOperation && aOperation->isEditOperation()) {
478 std::string aId = aOperation->id().toStdString();
479 if (isDistanceOperation(aOperation))
481 // Activate dimension value editing on double click
482 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
483 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
484 // Find corresponded widget to activate value editing
485 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
486 if (aWgt->attributeID() == "ConstraintValue") {
495 void PartSet_SketcherMgr::onApplicationStarted()
497 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
498 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
499 XGUI_Workshop* aWorkshop = aConnector->workshop();
500 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
501 if (aPropertyPanel) {
502 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
503 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
506 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
507 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
508 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
510 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
511 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
512 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
515 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
517 if (!myClickedPoint.myIsInitialized)
520 ModuleBase_Operation* aOperation = getCurrentOperation();
521 // the distance constraint feature should not use the clickedd point
522 // this is workaround in order to don't throw down the flyout point value,
523 // set by execute() method of these type of features
524 if (isDistanceOperation(aOperation))
527 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
529 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
533 void PartSet_SketcherMgr::onBeforeContextMenu()
535 myIsPopupMenuActive = true;
538 void PartSet_SketcherMgr::onAfterContextMenu()
540 myIsPopupMenuActive = false;
543 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
546 Handle(V3d_View) aView = theWnd->v3dView();
547 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
549 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
550 thePoint.setValue(aX, anY);
553 void PartSet_SketcherMgr::launchEditing()
555 // there should be activate the vertex selection mode because the edit can happens by the selected
558 aModes << TopAbs_VERTEX << TopAbs_EDGE;
559 // TODO: #391 - to be uncommented
560 /*aModes.append(AIS_DSM_Text);
561 aModes.append(AIS_DSM_Line);
562 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
563 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
565 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
566 aConnector->activateSubShapesSelection(aModes);
568 if (!myCurrentSelection.empty()) {
569 FeaturePtr aFeature = myCurrentSelection.begin().key();
570 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
571 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
573 myModule->editFeature(aSPFeature);
580 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
582 static QStringList aIds;
583 if (aIds.size() == 0) {
584 aIds << SketchPlugin_Line::ID().c_str();
585 aIds << SketchPlugin_Point::ID().c_str();
586 aIds << SketchPlugin_Arc::ID().c_str();
587 aIds << SketchPlugin_Circle::ID().c_str();
588 aIds << SketchPlugin_ConstraintLength::ID().c_str();
589 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
590 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
591 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
592 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
593 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
594 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
595 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
596 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
597 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
598 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
599 aIds << SketchPlugin_ConstraintFillet::ID().c_str();
600 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
605 const QStringList& PartSet_SketcherMgr::constraintsIdList()
607 static QStringList aIds;
608 if (aIds.size() == 0) {
609 aIds << SketchPlugin_ConstraintLength::ID().c_str();
610 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
611 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
612 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
613 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
614 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
615 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
616 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
617 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
618 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
619 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
620 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
626 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
628 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
631 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
633 return theOperation &&
634 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
637 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
639 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
642 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
644 std::string aId = theOperation ? theOperation->id().toStdString() : "";
646 return (aId == SketchPlugin_Line::ID()) ||
647 (aId == SketchPlugin_Point::ID()) ||
648 (aId == SketchPlugin_Arc::ID()) ||
649 (aId == SketchPlugin_Circle::ID());
652 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
654 std::string aId = theOperation ? theOperation->id().toStdString() : "";
656 return (aId == SketchPlugin_ConstraintLength::ID()) ||
657 (aId == SketchPlugin_ConstraintDistance::ID()) ||
658 (aId == SketchPlugin_ConstraintRadius::ID());
661 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
663 // Display all sketcher sub-Objects
664 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
665 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
666 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
668 // Hide sketcher result
669 std::list<ResultPtr> aResults = myCurrentSketch->results();
670 std::list<ResultPtr>::const_iterator aIt;
671 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
672 aDisplayer->erase((*aIt), false);
674 aDisplayer->erase(myCurrentSketch, false);
676 // Display sketcher objects
677 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
678 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
679 std::list<ResultPtr> aResults = aFeature->results();
680 std::list<ResultPtr>::const_iterator aIt;
681 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
682 aDisplayer->display((*aIt), false);
684 aDisplayer->display(aFeature, false);
687 if (myPlaneFilter.IsNull())
688 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
690 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
691 if (theOperation->isEditOperation()) {
692 // If it is editing of sketch then it means that plane is already defined
693 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
694 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
696 aDisplayer->updateViewer();
699 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
701 myIsMouseOverWindow = false;
702 myIsConstraintsShown = true;
704 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
705 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
707 DataPtr aData = myCurrentSketch->data();
708 if ((!aData) || (!aData->isValid())) {
709 // The sketch was aborted
710 myCurrentSketch = CompositeFeaturePtr();
711 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
713 // Erase all sketcher objects
714 QStringList aSketchIds = sketchOperationIdList();
715 QObjectPtrList aObjects = aDisplayer->displayedObjects();
716 foreach (ObjectPtr aObj, aObjects) {
717 DataPtr aObjData = aObj->data();
718 if ((!aObjData) || (!aObjData->isValid()))
719 aDisplayer->erase(aObj);
723 // Hide all sketcher sub-Objects
724 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
725 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
726 std::list<ResultPtr> aResults = aFeature->results();
727 std::list<ResultPtr>::const_iterator aIt;
728 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
729 aDisplayer->erase((*aIt), false);
731 aDisplayer->erase(aFeature, false);
733 // Display sketcher result
734 std::list<ResultPtr> aResults = myCurrentSketch->results();
735 std::list<ResultPtr>::const_iterator aIt;
736 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
737 aDisplayer->display((*aIt), false);
739 aDisplayer->display(myCurrentSketch);
741 myCurrentSketch = CompositeFeaturePtr();
742 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
743 aDisplayer->updateViewer();
746 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
748 if (constraintsIdList().contains(theOperation->id())) {
749 // Show constraints if a constraint was created
750 onShowConstraintsToggle(true);
752 connectToPropertyPanel(true);
755 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
757 connectToPropertyPanel(false);
758 myIsPropertyPanelValueChanged = false;
759 myIsMouseOverViewProcessed = 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>());
831 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
832 const FeaturePtr& theSketch,
833 ModuleBase_IWorkshop* theWorkshop,
834 FeatureToSelectionMap& theSelection)
836 if (theFeature.get() == NULL)
839 std::set<AttributePtr> aSelectedAttributes;
840 std::set<ResultPtr> aSelectedResults;
842 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
843 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
844 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
845 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
847 std::list<ResultPtr> aResults = theFeature->results();
848 std::list<ResultPtr>::const_iterator aIt;
849 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
851 ResultPtr aResult = *aIt;
852 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
853 if (aAISObj.get() == NULL)
855 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
856 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
858 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
859 aContext->SelectedOwner());
860 if (aBRepOwner.IsNull())
862 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
863 aBRepOwner->Selectable());
867 if (aBRepOwner->HasShape()) {
868 const TopoDS_Shape& aShape = aBRepOwner->Shape();
869 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
870 if (aShapeType == TopAbs_VERTEX) {
871 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
873 if (aPntAttr.get() != NULL)
874 aSelectedAttributes.insert(aPntAttr);
876 else if (aShapeType == TopAbs_EDGE &&
877 aSelectedResults.find(aResult) == aSelectedResults.end()) {
878 aSelectedResults.insert(aResult);
883 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
886 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
887 const FeaturePtr& theSketch,
888 ModuleBase_IWorkshop* theWorkshop,
889 const FeatureToSelectionMap& theSelection,
890 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
892 if (theFeature.get() == NULL)
895 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
896 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
897 std::set<ResultPtr> aSelectedResults = anIt.value().second;
899 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
900 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
901 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
902 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
904 // 1. found the feature's owners. Check the AIS objects of the constructions
905 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
906 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
907 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
909 SelectMgr_IndexedMapOfOwner aSelectedOwners;
910 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
911 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
912 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
913 if (!anOwner.IsNull())
914 anOwnersToSelect.Add(anOwner);
918 // 2. found the feature results's owners
919 std::list<ResultPtr> aResults = theFeature->results();
920 std::list<ResultPtr>::const_iterator aIt;
921 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
923 ResultPtr aResult = *aIt;
924 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
925 if (aAISObj.get() == NULL)
927 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
929 SelectMgr_IndexedMapOfOwner aSelectedOwners;
930 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
931 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
932 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
933 if ( anOwner.IsNull() || !anOwner->HasShape() )
935 const TopoDS_Shape& aShape = anOwner->Shape();
936 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
937 if (aShapeType == TopAbs_VERTEX) {
938 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
939 if (aPntAttr.get() != NULL &&
940 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
941 anOwnersToSelect.Add(anOwner);
944 else if (aShapeType == TopAbs_EDGE) {
945 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
946 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
947 anOwnersToSelect.FindIndex(anOwner) <= 0)
948 anOwnersToSelect.Add(anOwner);
954 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
956 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
957 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
958 XGUI_Workshop* aWorkshop = aConnector->workshop();
959 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
960 if (aPropertyPanel) {
961 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
962 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
964 connect(aWidget, SIGNAL(beforeValuesChanged()),
965 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
966 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
967 connect(aWidget, SIGNAL(afterValuesChanged()),
968 this, SLOT(onAfterValuesChangedInPropertyPanel()));
971 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
972 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
973 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
974 disconnect(aWidget, SIGNAL(afterValuesChanged()),
975 this, SLOT(onAfterValuesChangedInPropertyPanel()));
981 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
983 return myModule->workshop()->currentOperation();
986 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
987 const bool isToDisplay)
989 if (!theOperation || theOperation->isEditOperation())
992 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
993 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
994 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
996 // 1. change visibility of the object itself, here the presentable object is processed,
997 // e.g. constraints features
998 FeaturePtr aFeature = theOperation->feature();
999 std::list<ResultPtr> aResults = aFeature->results();
1001 aDisplayer->display(aFeature, false);
1003 aDisplayer->erase(aFeature, false);
1005 // change visibility of the object results, e.g. non-constraint features
1006 std::list<ResultPtr>::const_iterator aIt;
1007 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1009 aDisplayer->display(*aIt, false);
1012 aDisplayer->erase(*aIt, false);
1015 aDisplayer->updateViewer();
1018 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1020 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1021 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1022 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1024 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1025 if (theHighlightedOnly) {
1026 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1029 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1031 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1032 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1035 // 1. it is necessary to save current selection in order to restore it after the features moving
1036 myCurrentSelection.clear();
1037 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1038 aLast = aFeature2AttributeMap.end();
1039 for (; anIt != aLast; anIt++) {
1040 FeaturePtr aFeature = anIt.key();
1041 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1043 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1046 void PartSet_SketcherMgr::restoreSelection()
1048 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1049 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1050 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1051 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1052 aSLast = myCurrentSelection.end();
1053 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1054 for (; aSIt != aSLast; aSIt++) {
1055 anOwnersToSelect.Clear();
1056 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1058 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1062 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1064 if (myIsConstraintsShown == theOn)
1066 if (myCurrentSketch.get() == NULL)
1069 myIsConstraintsShown = theOn;
1071 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1072 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1073 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1075 const QStringList& aConstrIds = constraintsIdList();
1076 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1077 FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1078 if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1079 if (myIsConstraintsShown)
1080 aDisplayer->display(aSubFeature, false);
1082 aDisplayer->erase(aSubFeature, false);
1085 aDisplayer->updateViewer();