1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_SketcherMgr.cpp
4 // Created: 19 Dec 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_WidgetPoint2dDistance.h"
11 #include "PartSet_Tools.h"
13 #include <ModuleBase_WidgetEditor.h>
15 #include <XGUI_ModuleConnector.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_Workshop.h>
18 #include <XGUI_ContextMenuMgr.h>
19 #include <XGUI_Selection.h>
20 #include <XGUI_SelectionMgr.h>
21 #include <ModuleBase_ModelWidget.h>
22 #include <XGUI_ModuleConnector.h>
23 #include <XGUI_PropertyPanel.h>
24 #include <XGUI_ViewerProxy.h>
26 #include <AppElements_MainWindow.h>
28 #include <ModuleBase_IViewer.h>
29 #include <ModuleBase_IWorkshop.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_Operation.h>
32 #include <ModuleBase_ISelection.h>
33 #include <ModuleBase_IPropertyPanel.h>
34 #include <ModuleBase_Operation.h>
36 #include <GeomDataAPI_Point2D.h>
38 #include <Events_Loop.h>
40 #include <SketchPlugin_Line.h>
41 #include <SketchPlugin_Sketch.h>
42 #include <SketchPlugin_Point.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_ConstraintLength.h>
46 #include <SketchPlugin_ConstraintDistance.h>
47 #include <SketchPlugin_ConstraintParallel.h>
48 #include <SketchPlugin_ConstraintPerpendicular.h>
49 #include <SketchPlugin_ConstraintRadius.h>
50 #include <SketchPlugin_ConstraintRigid.h>
51 #include <SketchPlugin_ConstraintHorizontal.h>
52 #include <SketchPlugin_ConstraintVertical.h>
53 #include <SketchPlugin_ConstraintEqual.h>
54 #include <SketchPlugin_ConstraintTangent.h>
55 #include <SketchPlugin_ConstraintCoincidence.h>
57 #include <SelectMgr_IndexedMapOfOwner.hxx>
58 #include <StdSelect_BRepOwner.hxx>
60 //#include <AIS_DimensionSelectionMode.hxx>
61 //#include <AIS_Shape.hxx>
63 #include <ModelAPI_Events.h>
64 #include <ModelAPI_Session.h>
66 #include <QMouseEvent>
67 #include <QApplication>
70 /// Returns list of unique objects by sum of objects from List1 and List2
71 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
72 const QList<ModuleBase_ViewerPrs>& theList2)
74 QList<ModuleBase_ViewerPrs> aRes;
75 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
76 if (!aRes.contains(aPrs))
79 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
80 if (!aRes.contains(aPrs))
86 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
87 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
88 const FeaturePtr theSketch)
90 QList<ModuleBase_ViewerPrs> aRes;
92 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
93 aLast = theList.end();
94 for (; anIt != aLast; anIt++)
96 ModuleBase_ViewerPrs aPrs = *anIt;
97 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
98 if (aFeature.get() == NULL)
101 QList<AttributePtr> anAttributes;
102 if (theFeature2AttributeMap.contains(aFeature)) {
103 anAttributes = theFeature2AttributeMap[aFeature];
106 TopoDS_Shape aShape = aPrs.shape();
107 if (!aShape.IsNull()) {
108 if (aShape.ShapeType() == TopAbs_VERTEX) {
109 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
110 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
111 anAttributes.push_back(anAttr);
114 theFeature2AttributeMap[aFeature] = anAttributes;
121 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
122 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
123 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
124 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
125 myIsPopupMenuActive(false)
127 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
128 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
130 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
132 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
133 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
135 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
136 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
138 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
139 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
141 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
142 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
144 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
145 XGUI_Workshop* aWorkshop = aConnector->workshop();
146 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
149 PartSet_SketcherMgr::~PartSet_SketcherMgr()
151 if (!myPlaneFilter.IsNull())
152 myPlaneFilter.Nullify();
155 void PartSet_SketcherMgr::onEnterViewPort()
157 if (!isNestedCreateOperation(getCurrentOperation()))
159 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
160 // created feature because it should be done in onMouseMove(). Some widgets watch
161 // the mouse move and use the cursor position to update own values. If the presentaion is
162 // redisplayed before this update, the feature presentation jumps from reset value to current.
163 myIsMouseOverWindow = true;
164 myIsPropertyPanelValueChanged = false;
167 void PartSet_SketcherMgr::onLeaveViewPort()
169 if (!isNestedCreateOperation(getCurrentOperation()))
171 // the method should be performed if the popup menu is called,
172 // the reset of the current widget should not happen
173 if (myIsPopupMenuActive)
176 myIsMouseOverViewProcessed = false;
177 myIsMouseOverWindow = false;
179 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
180 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
181 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
182 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
183 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
184 // obtained after reset value
185 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
186 ModuleBase_Operation* aOperation = getCurrentOperation();
187 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
188 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
192 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
194 // hides the presentation of the current operation feature
195 myIsPropertyPanelValueChanged = false;
196 // the feature is to be erased here, but it is correct to call canDisplayObject because
197 // there can be additional check (e.g. editor widget in distance constraint)
198 FeaturePtr aFeature = getCurrentOperation()->feature();
199 visualizeFeature(aOperation, canDisplayObject(aFeature));
202 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
204 if (isNestedCreateOperation(getCurrentOperation()))
206 // it is necessary to save current selection in order to restore it after the values are modifed
209 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
210 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
211 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
212 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
215 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
217 if (isNestedCreateOperation(getCurrentOperation()))
219 // it is necessary to restore current selection in order to restore it after the values are modified
221 myCurrentSelection.clear();
223 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
224 // viewer happens by deselect/select the modified objects. The flag should be restored after
225 // the selection processing. The update viewer should be also called.
226 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
227 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
228 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
229 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
230 aDisplayer->updateViewer();
233 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
235 if (!isNestedCreateOperation(getCurrentOperation()))
238 // visualize the current operation feature
239 myIsPropertyPanelValueChanged = true;
240 ModuleBase_Operation* aOperation = getCurrentOperation();
241 // the feature is to be erased here, but it is correct to call canDisplayObject because
242 // there can be additional check (e.g. editor widget in distance constraint)
243 FeaturePtr aFeature = getCurrentOperation()->feature();
244 visualizeFeature(aOperation, canDisplayObject(aFeature));
247 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
249 get2dPoint(theWnd, theEvent, myClickedPoint);
251 if (!(theEvent->buttons() & Qt::LeftButton))
254 // Clear dragging mode
255 myIsDragging = false;
257 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
258 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
259 if (!aViewer->canDragByMouse())
262 ModuleBase_Operation* aOperation = getCurrentOperation();
263 if (aOperation && aOperation->isEditOperation()) {
264 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
265 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
266 // If the current widget is a selector, do do nothing, it processes the mouse press
267 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
272 // Use only for sketch operations
273 if (aOperation && myCurrentSketch) {
274 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
277 bool isSketcher = isSketchOperation(aOperation);
278 bool isSketchOpe = isNestedSketchOperation(aOperation);
280 // Avoid non-sketch operations
281 if ((!isSketchOpe) && (!isSketcher))
284 bool isEditing = aOperation->isEditOperation();
286 // Ignore creation sketch operation
287 if ((!isSketcher) && (!isEditing))
290 // MoveTo in order to highlight current object
291 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
293 // Remember highlighted objects for editing
294 ModuleBase_ISelection* aSelect = aWorkshop->selection();
296 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
297 storeSelection(!aHasShift);
299 if (myCurrentSelection.empty()) {
300 if (isSketchOpe && (!isSketcher))
301 // commit previous operation
302 if (!aOperation->commit())
309 get2dPoint(theWnd, theEvent, myCurrentPoint);
313 } else if (isSketchOpe && isEditing) {
314 // If selected another object commit current result
315 aOperation->commit();
318 get2dPoint(theWnd, theEvent, myCurrentPoint);
321 // This is necessary in order to finalize previous operation
322 QApplication::processEvents();
328 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
330 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
331 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
332 if (!aViewer->canDragByMouse())
334 ModuleBase_Operation* aOp = getCurrentOperation();
336 if (isNestedSketchOperation(aOp)) {
337 get2dPoint(theWnd, theEvent, myClickedPoint);
339 // Only for sketcher operations
343 myCurrentSelection.clear();
344 // Reselect edited object
345 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
346 if (theEvent->modifiers() & Qt::ShiftModifier)
347 aViewer->AISContext()->ShiftSelect();
349 aViewer->AISContext()->Select();
355 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
356 myIsDragging = false;
359 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
361 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
362 myIsMouseOverViewProcessed = true;
363 // 1. perform the widget mouse move functionality and display the presentation
364 ModuleBase_Operation* aOperation = getCurrentOperation();
365 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
366 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
367 // the mouse move should be processed in the widget, if it can in order to visualize correct
368 // presentation. These widgets correct the feature attribute according to the mouse position
369 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
371 aPoint2DWdg->onMouseMove(theWnd, theEvent);
373 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
376 aDistanceWdg->onMouseMove(theWnd, theEvent);
378 // the feature is to be erased here, but it is correct to call canDisplayObject because
379 // there can be additional check (e.g. editor widget in distance constraint)
380 FeaturePtr aFeature = getCurrentOperation()->feature();
381 visualizeFeature(aOperation, canDisplayObject(aFeature));
384 myClickedPoint.clear();
387 // 1. the current selection is saved in the mouse press method in order to restore it after moving
388 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
389 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
390 // deselected). This flag should be restored in the slot, processed the mouse release signal.
391 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
392 aViewer->enableSelection(false);
394 ModuleBase_Operation* aOperation = getCurrentOperation();
397 if (isSketchOperation(aOperation))
398 return; // No edit operation activated
400 Handle(V3d_View) aView = theWnd->v3dView();
401 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
403 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
404 double dX = aX - myCurrentPoint.myCurX;
405 double dY = aY - myCurrentPoint.myCurY;
407 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
408 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
409 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
410 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
411 // viewer happens by deselect/select the modified objects. The flag should be restored after
412 // the selection processing. The update viewer should be also called.
413 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
415 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
416 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
417 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
418 aLast = myCurrentSelection.end();
419 // 4. the features and attributes modification(move)
420 for (; anIt != aLast; anIt++) {
421 FeaturePtr aFeature = anIt.key();
423 std::set<AttributePtr> anAttributes = anIt.value().first;
424 // Process selection by attribute: the priority to the attribute
425 if (!anAttributes.empty()) {
426 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
427 anAttLast = anAttributes.end();
428 for (; anAttIt != anAttLast; anAttIt++) {
429 AttributePtr anAttr = *anAttIt;
430 if (anAttr.get() == NULL)
432 std::string aAttrId = anAttr->id();
433 DataPtr aData = aFeature->data();
434 if (aData.get() != NULL) {
435 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
436 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
437 if (aPoint.get() != NULL) {
438 bool isImmutable = aPoint->setImmutable(true);
439 aPoint->move(dX, dY);
440 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
441 aPoint->setImmutable(isImmutable);
446 // Process selection by feature
447 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
448 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
449 if (aSketchFeature) {
450 aSketchFeature->move(dX, dY);
451 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
455 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
456 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
458 // 5. it is necessary to save current selection in order to restore it after the features moving
460 // 6. restore the update viewer flag and call this update
461 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
462 aDisplayer->updateViewer();
465 myCurrentPoint.setValue(aX, aY);
469 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
471 ModuleBase_Operation* aOperation = getCurrentOperation();
472 if (aOperation && aOperation->isEditOperation()) {
473 std::string aId = aOperation->id().toStdString();
474 if (isDistanceOperation(aOperation))
476 // Activate dimension value editing on double click
477 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
478 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
479 // Find corresponded widget to activate value editing
480 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
481 if (aWgt->attributeID() == "ConstraintValue") {
490 void PartSet_SketcherMgr::onApplicationStarted()
492 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
493 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
494 XGUI_Workshop* aWorkshop = aConnector->workshop();
495 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
496 if (aPropertyPanel) {
497 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
498 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
501 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
502 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
503 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
505 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
506 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
507 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
510 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
512 if (!myClickedPoint.myIsInitialized)
515 ModuleBase_Operation* aOperation = getCurrentOperation();
516 // the distance constraint feature should not use the clickedd point
517 // this is workaround in order to don't throw down the flyout point value,
518 // set by execute() method of these type of features
519 if (isDistanceOperation(aOperation))
522 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
524 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
528 void PartSet_SketcherMgr::onBeforeContextMenu()
530 myIsPopupMenuActive = true;
533 void PartSet_SketcherMgr::onAfterContextMenu()
535 myIsPopupMenuActive = false;
538 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
541 Handle(V3d_View) aView = theWnd->v3dView();
542 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
544 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
545 thePoint.setValue(aX, anY);
548 void PartSet_SketcherMgr::launchEditing()
550 // there should be activate the vertex selection mode because the edit can happens by the selected
553 aModes << TopAbs_VERTEX << TopAbs_EDGE;
554 // TODO: #391 - to be uncommented
555 /*aModes.append(AIS_DSM_Text);
556 aModes.append(AIS_DSM_Line);
557 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
558 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
560 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
561 aConnector->activateSubShapesSelection(aModes);
563 if (!myCurrentSelection.empty()) {
564 FeaturePtr aFeature = myCurrentSelection.begin().key();
565 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
566 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
568 myModule->editFeature(aSPFeature);
575 QStringList PartSet_SketcherMgr::sketchOperationIdList()
577 static QStringList aIds;
578 if (aIds.size() == 0) {
579 aIds << SketchPlugin_Line::ID().c_str();
580 aIds << SketchPlugin_Point::ID().c_str();
581 aIds << SketchPlugin_Arc::ID().c_str();
582 aIds << SketchPlugin_Circle::ID().c_str();
583 aIds << SketchPlugin_ConstraintLength::ID().c_str();
584 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
585 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
586 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
587 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
588 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
589 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
590 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
591 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
592 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
593 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
598 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
600 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
603 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
605 return theOperation &&
606 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
609 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
611 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
614 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
616 std::string aId = theOperation ? theOperation->id().toStdString() : "";
618 return (aId == SketchPlugin_Line::ID()) ||
619 (aId == SketchPlugin_Point::ID()) ||
620 (aId == SketchPlugin_Arc::ID()) ||
621 (aId == SketchPlugin_Circle::ID());
624 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
626 std::string aId = theOperation ? theOperation->id().toStdString() : "";
628 return (aId == SketchPlugin_ConstraintLength::ID()) ||
629 (aId == SketchPlugin_ConstraintDistance::ID()) ||
630 (aId == SketchPlugin_ConstraintRadius::ID());
633 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
635 // Display all sketcher sub-Objects
636 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
637 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
638 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
640 // Hide sketcher result
641 std::list<ResultPtr> aResults = myCurrentSketch->results();
642 std::list<ResultPtr>::const_iterator aIt;
643 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
644 aDisplayer->erase((*aIt), false);
646 aDisplayer->erase(myCurrentSketch, false);
648 // Display sketcher objects
649 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
650 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
651 std::list<ResultPtr> aResults = aFeature->results();
652 std::list<ResultPtr>::const_iterator aIt;
653 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
654 aDisplayer->display((*aIt), false);
656 aDisplayer->display(aFeature, false);
659 if (myPlaneFilter.IsNull())
660 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
662 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
663 if (theOperation->isEditOperation()) {
664 // If it is editing of sketch then it means that plane is already defined
665 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
666 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
668 aDisplayer->updateViewer();
671 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
673 myIsMouseOverWindow = false;
675 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
676 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
678 DataPtr aData = myCurrentSketch->data();
679 if ((!aData) || (!aData->isValid())) {
680 // The sketch was aborted
681 myCurrentSketch = CompositeFeaturePtr();
682 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
684 // Erase all sketcher objects
685 QStringList aSketchIds = sketchOperationIdList();
686 QObjectPtrList aObjects = aDisplayer->displayedObjects();
687 foreach (ObjectPtr aObj, aObjects) {
688 DataPtr aObjData = aObj->data();
689 if ((!aObjData) || (!aObjData->isValid()))
690 aDisplayer->erase(aObj);
694 // Hide all sketcher sub-Objects
695 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
696 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
697 std::list<ResultPtr> aResults = aFeature->results();
698 std::list<ResultPtr>::const_iterator aIt;
699 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
700 aDisplayer->erase((*aIt), false);
702 aDisplayer->erase(aFeature, false);
704 // Display sketcher result
705 std::list<ResultPtr> aResults = myCurrentSketch->results();
706 std::list<ResultPtr>::const_iterator aIt;
707 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
708 aDisplayer->display((*aIt), false);
710 aDisplayer->display(myCurrentSketch);
712 myCurrentSketch = CompositeFeaturePtr();
713 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
714 aDisplayer->updateViewer();
717 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
719 connectToPropertyPanel(true);
722 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
724 connectToPropertyPanel(false);
725 myIsPropertyPanelValueChanged = false;
726 myIsMouseOverViewProcessed = true;
729 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
731 if (isNestedCreateOperation(theOperation))
732 visualizeFeature(theOperation, true);
735 bool PartSet_SketcherMgr::canUndo() const
737 return isNestedCreateOperation(getCurrentOperation());
740 bool PartSet_SketcherMgr::canRedo() const
742 return isNestedCreateOperation(getCurrentOperation());
745 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
747 bool aCanDisplay = true;
748 // 1. the sketch feature should not be displayed during the sketch active operation
749 // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
750 // nested features can be visualized
751 CompositeFeaturePtr aSketchFeature = activeSketch();
752 if (aSketchFeature.get() != NULL) {
753 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
754 if (aFeature.get() != NULL && aFeature == aSketchFeature)
757 // 2. For created nested feature operation do not display the created feature if
758 // the mouse curstor leaves the OCC window.
759 // The correction cases, which ignores this condition:
760 // a. the property panel values modification
761 // b. the popup menu activated
762 // c. widget editor control
764 if (!isNestedCreateOperation(getCurrentOperation()))
767 ModuleBase_Operation* aOperation = getCurrentOperation();
768 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
769 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
770 // the active widget editor should not influence here. The presentation should be visible always
771 // when this widget is active.
773 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
778 if (myIsPopupMenuActive)
781 // during a nested create operation, the feature is redisplayed only if the mouse over view
782 // of there was a value modified in the property panel after the mouse left the view
783 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
788 bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
790 bool anEnabled = false;
791 ModuleBase_Operation* anOperation = getCurrentOperation();
793 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
794 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
798 QObjectPtrList anObjects;
799 // 1. change auxiliary type of a created feature
800 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
801 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
802 anObjects.append(anOperation->feature());
805 /// The operation should not be aborted here, because the method does not changed
806 /// the auxilliary state, but checks the possibility to perform this
807 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
808 /// anOperation->abort();
809 // 2. change auxiliary type of selected sketch entities
810 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
811 anObjects = aSelection->selectedPresentations();
813 anEnabled = anObjects.size() > 0;
815 bool isNotAuxiliaryFound = false;
816 if (anObjects.size() > 0) {
817 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
818 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
819 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
820 if (aFeature.get() != NULL) {
821 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
822 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
823 if (aSketchFeature.get() != NULL) {
824 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
826 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
827 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
829 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
834 theValue = anObjects.size() && !isNotAuxiliaryFound;
838 void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
840 ModuleBase_Operation* anOperation = getCurrentOperation();
842 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
843 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
847 QObjectPtrList anObjects;
848 bool isUseTransaction = false;
849 // 1. change auxiliary type of a created feature
850 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
851 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
852 anObjects.append(anOperation->feature());
855 isUseTransaction = true;
856 // 2. change auxiliary type of selected sketch entities
857 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
858 anObjects = aSelection->selectedPresentations();
861 QAction* anAction = myModule->action("AUXILIARY_CMD");
862 SessionPtr aMgr = ModelAPI_Session::get();
863 if (isUseTransaction) {
864 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
865 anOperation->abort();
866 aMgr->startOperation(anAction->text().toStdString());
870 if (anObjects.size() > 0) {
871 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
872 for (; anIt != aLast; anIt++) {
873 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
874 if (aFeature.get() != NULL) {
875 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
876 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
877 if (aSketchFeature.get() != NULL) {
878 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
880 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
881 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
883 anAuxiliaryAttr->setValue(isChecked);
888 if (isUseTransaction) {
889 aMgr->finishOperation();
891 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
895 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
897 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
900 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
901 const FeaturePtr& theSketch,
902 ModuleBase_IWorkshop* theWorkshop,
903 FeatureToSelectionMap& theSelection)
905 if (theFeature.get() == NULL)
908 std::set<AttributePtr> aSelectedAttributes;
909 std::set<ResultPtr> aSelectedResults;
911 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
912 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
913 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
914 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
916 std::list<ResultPtr> aResults = theFeature->results();
917 std::list<ResultPtr>::const_iterator aIt;
918 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
920 ResultPtr aResult = *aIt;
921 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
922 if (aAISObj.get() == NULL)
924 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
925 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
927 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
928 aContext->SelectedOwner());
929 if (aBRepOwner.IsNull())
931 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
932 aBRepOwner->Selectable());
936 if (aBRepOwner->HasShape()) {
937 const TopoDS_Shape& aShape = aBRepOwner->Shape();
938 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
939 if (aShapeType == TopAbs_VERTEX) {
940 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
942 if (aPntAttr.get() != NULL)
943 aSelectedAttributes.insert(aPntAttr);
945 else if (aShapeType == TopAbs_EDGE &&
946 aSelectedResults.find(aResult) == aSelectedResults.end()) {
947 aSelectedResults.insert(aResult);
952 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
955 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
956 const FeaturePtr& theSketch,
957 ModuleBase_IWorkshop* theWorkshop,
958 const FeatureToSelectionMap& theSelection,
959 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
961 if (theFeature.get() == NULL)
964 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
965 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
966 std::set<ResultPtr> aSelectedResults = anIt.value().second;
968 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
969 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
970 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
971 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
973 // 1. found the feature's owners. Check the AIS objects of the constructions
974 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
975 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
976 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
978 SelectMgr_IndexedMapOfOwner aSelectedOwners;
979 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
980 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
981 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
982 if (!anOwner.IsNull())
983 anOwnersToSelect.Add(anOwner);
987 // 2. found the feature results's owners
988 std::list<ResultPtr> aResults = theFeature->results();
989 std::list<ResultPtr>::const_iterator aIt;
990 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
992 ResultPtr aResult = *aIt;
993 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
994 if (aAISObj.get() == NULL)
996 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
998 SelectMgr_IndexedMapOfOwner aSelectedOwners;
999 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1000 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1001 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1002 if ( anOwner.IsNull() || !anOwner->HasShape() )
1004 const TopoDS_Shape& aShape = anOwner->Shape();
1005 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1006 if (aShapeType == TopAbs_VERTEX) {
1007 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1008 if (aPntAttr.get() != NULL &&
1009 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1010 anOwnersToSelect.Add(anOwner);
1013 else if (aShapeType == TopAbs_EDGE) {
1014 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1015 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1016 anOwnersToSelect.FindIndex(anOwner) <= 0)
1017 anOwnersToSelect.Add(anOwner);
1023 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1025 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1026 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1027 XGUI_Workshop* aWorkshop = aConnector->workshop();
1028 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1029 if (aPropertyPanel) {
1030 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1031 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1033 connect(aWidget, SIGNAL(beforeValuesChanged()),
1034 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1035 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1036 connect(aWidget, SIGNAL(afterValuesChanged()),
1037 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1040 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1041 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1042 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1043 disconnect(aWidget, SIGNAL(afterValuesChanged()),
1044 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1050 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1052 return myModule->workshop()->currentOperation();
1055 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1056 const bool isToDisplay)
1058 if (!theOperation || theOperation->isEditOperation())
1061 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1062 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1063 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1065 // 1. change visibility of the object itself, here the presentable object is processed,
1066 // e.g. constraints features
1067 FeaturePtr aFeature = theOperation->feature();
1068 std::list<ResultPtr> aResults = aFeature->results();
1070 aDisplayer->display(aFeature, false);
1072 aDisplayer->erase(aFeature, false);
1074 // change visibility of the object results, e.g. non-constraint features
1075 std::list<ResultPtr>::const_iterator aIt;
1076 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1078 aDisplayer->display(*aIt, false);
1081 aDisplayer->erase(*aIt, false);
1084 aDisplayer->updateViewer();
1087 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1089 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1090 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1091 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1093 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1094 if (theHighlightedOnly) {
1095 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1098 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1100 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1101 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1104 // 1. it is necessary to save current selection in order to restore it after the features moving
1105 myCurrentSelection.clear();
1106 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1107 aLast = aFeature2AttributeMap.end();
1108 for (; anIt != aLast; anIt++) {
1109 FeaturePtr aFeature = anIt.key();
1110 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1112 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1115 void PartSet_SketcherMgr::restoreSelection()
1117 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1118 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1119 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1120 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1121 aSLast = myCurrentSelection.end();
1122 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1123 for (; aSIt != aSLast; aSIt++) {
1124 anOwnersToSelect.Clear();
1125 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1127 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);