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 <XGUI_ModuleConnector.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Selection.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <XGUI_ModuleConnector.h>
20 #include <XGUI_PropertyPanel.h>
21 #include <XGUI_ViewerProxy.h>
23 #include <AppElements_MainWindow.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_IViewWindow.h>
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_ISelection.h>
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_Operation.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <Events_Loop.h>
37 #include <SketchPlugin_Line.h>
38 #include <SketchPlugin_Sketch.h>
39 #include <SketchPlugin_Point.h>
40 #include <SketchPlugin_Arc.h>
41 #include <SketchPlugin_Circle.h>
42 #include <SketchPlugin_ConstraintLength.h>
43 #include <SketchPlugin_ConstraintDistance.h>
44 #include <SketchPlugin_ConstraintParallel.h>
45 #include <SketchPlugin_ConstraintPerpendicular.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
49 #include <SelectMgr_IndexedMapOfOwner.hxx>
50 #include <StdSelect_BRepOwner.hxx>
52 //#include <AIS_DimensionSelectionMode.hxx>
53 //#include <AIS_Shape.hxx>
55 #include <ModelAPI_Events.h>
57 #include <QMouseEvent>
58 #include <QApplication>
61 /// Returns list of unique objects by sum of objects from List1 and List2
62 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
63 const QList<ModuleBase_ViewerPrs>& theList2)
65 QList<ModuleBase_ViewerPrs> aRes;
66 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
67 if (!aRes.contains(aPrs))
70 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
71 if (!aRes.contains(aPrs))
77 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
78 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
79 const FeaturePtr theSketch)
81 QList<ModuleBase_ViewerPrs> aRes;
83 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
84 aLast = theList.end();
85 for (; anIt != aLast; anIt++)
87 ModuleBase_ViewerPrs aPrs = *anIt;
88 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
89 if (aFeature.get() == NULL)
92 QList<AttributePtr> anAttributes;
93 if (theFeature2AttributeMap.contains(aFeature)) {
94 anAttributes = theFeature2AttributeMap[aFeature];
97 TopoDS_Shape aShape = aPrs.shape();
98 if (!aShape.IsNull()) {
99 if (aShape.ShapeType() == TopAbs_VERTEX) {
100 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
101 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
102 anAttributes.push_back(anAttr);
105 theFeature2AttributeMap[aFeature] = anAttributes;
112 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
113 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
114 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
115 myIsMouseOverViewProcessed(true)
117 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
118 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
120 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
122 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
123 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
125 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
126 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
128 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
129 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
131 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
132 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
134 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
135 XGUI_Workshop* aWorkshop = aConnector->workshop();
136 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
139 PartSet_SketcherMgr::~PartSet_SketcherMgr()
141 if (!myPlaneFilter.IsNull())
142 myPlaneFilter.Nullify();
145 void PartSet_SketcherMgr::onEnterViewPort()
147 if (!isNestedCreateOperation(getCurrentOperation()))
149 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
150 // created feature because it should be done in onMouseMove(). Some widgets watch
151 // the mouse move and use the cursor position to update own values. If the presentaion is
152 // redisplayed before this update, the feature presentation jumps from reset value to current.
153 myIsMouseOverWindow = true;
154 myIsPropertyPanelValueChanged = false;
157 void PartSet_SketcherMgr::onLeaveViewPort()
159 if (!isNestedCreateOperation(getCurrentOperation()))
162 myIsMouseOverViewProcessed = false;
163 myIsMouseOverWindow = false;
165 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
166 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
167 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
168 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
169 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
170 // obtained after reset value
171 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
172 ModuleBase_Operation* aOperation = getCurrentOperation();
173 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
174 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
178 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
180 // hides the presentation of the current operation feature
181 myIsPropertyPanelValueChanged = false;
182 visualizeFeature(aOperation, false);
185 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
187 if (!isNestedCreateOperation(getCurrentOperation()))
190 // visualize the current operation feature
191 myIsPropertyPanelValueChanged = true;
192 ModuleBase_Operation* aOperation = getCurrentOperation();
193 visualizeFeature(aOperation, true);
196 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
198 get2dPoint(theWnd, theEvent, myClickedPoint);
200 if (!(theEvent->buttons() & Qt::LeftButton))
203 // Clear dragging mode
204 myIsDragging = false;
206 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
207 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
208 if (!aViewer->canDragByMouse())
211 ModuleBase_Operation* aOperation = getCurrentOperation();
212 if (aOperation && aOperation->isEditOperation()) {
213 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
214 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
215 // If the current widget is a selector, do do nothing, it processes the mouse press
216 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
221 // Use only for sketch operations
222 if (aOperation && myCurrentSketch) {
223 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
226 bool isSketcher = isSketchOperation(aOperation);
227 bool isSketchOpe = isNestedSketchOperation(aOperation);
229 // Avoid non-sketch operations
230 if ((!isSketchOpe) && (!isSketcher))
233 bool isEditing = aOperation->isEditOperation();
235 // Ignore creation sketch operation
236 if ((!isSketcher) && (!isEditing))
239 // MoveTo in order to highlight current object
240 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
242 // Remember highlighted objects for editing
243 ModuleBase_ISelection* aSelect = aWorkshop->selection();
244 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
245 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
246 myFeature2AttributeMap.clear();
248 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
250 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
251 fillFeature2Attribute(aSelected, myFeature2AttributeMap, myCurrentSketch);
254 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
257 if (myFeature2AttributeMap.empty()) {
258 if (isSketchOpe && (!isSketcher))
259 // commit previous operation
260 if (!aOperation->commit())
267 get2dPoint(theWnd, theEvent, myCurrentPoint);
271 } else if (isSketchOpe && isEditing) {
272 // If selected another object commit current result
273 aOperation->commit();
276 get2dPoint(theWnd, theEvent, myCurrentPoint);
279 // This is necessary in order to finalize previous operation
280 QApplication::processEvents();
286 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
288 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
289 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
290 if (!aViewer->canDragByMouse())
292 ModuleBase_Operation* aOp = getCurrentOperation();
294 if (isNestedSketchOperation(aOp)) {
295 get2dPoint(theWnd, theEvent, myClickedPoint);
297 // Only for sketcher operations
301 myFeature2AttributeMap.clear();
303 // Reselect edited object
304 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
305 if (theEvent->modifiers() & Qt::ShiftModifier)
306 aViewer->AISContext()->ShiftSelect();
308 aViewer->AISContext()->Select();
314 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
315 myIsDragging = false;
318 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
320 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
321 myIsMouseOverViewProcessed = true;
322 // 1. perform the widget mouse move functionality and display the presentation
323 ModuleBase_Operation* aOperation = getCurrentOperation();
324 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
325 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
326 // the mouse move should be processed in the widget, if it can in order to visualize correct
327 // presentation. These widgets correct the feature attribute according to the mouse position
328 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
330 aPoint2DWdg->onMouseMove(theWnd, theEvent);
332 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
335 aDistanceWdg->onMouseMove(theWnd, theEvent);
337 visualizeFeature(aOperation, true);
340 myClickedPoint.clear();
343 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
344 // 1. it is necessary to save current selection in order to restore it after the features moving
345 FeatureToSelectionMap aCurrentSelection;
346 getCurrentSelection(myFeature2AttributeMap, myCurrentSketch, aWorkshop, aCurrentSelection);
348 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
349 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
350 // deselected). This flag should be restored in the slot, processed the mouse release signal.
351 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
352 aViewer->enableSelection(false);
354 ModuleBase_Operation* aOperation = getCurrentOperation();
357 if (isSketchOperation(aOperation))
358 return; // No edit operation activated
360 Handle(V3d_View) aView = theWnd->v3dView();
361 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
363 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
364 double dX = aX - myCurrentPoint.myCurX;
365 double dY = aY - myCurrentPoint.myCurY;
367 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
368 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
369 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
370 // viewer happens by deselect/select the modified objects. The flag should be restored after
371 // the selection processing. The update viewer should be also called.
372 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
374 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
375 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
377 FeatureToAttributesMap::const_iterator anIt = myFeature2AttributeMap.begin(),
378 aLast = myFeature2AttributeMap.end();
379 // 4. the features and attributes modification(move)
380 for (; anIt != aLast; anIt++) {
381 FeaturePtr aFeature = anIt.key();
383 AttributeList anAttributes = anIt.value();
384 // Process selection by attribute: the priority to the attribute
385 if (!anAttributes.empty()) {
386 AttributeList::const_iterator anAttIt = anAttributes.begin(), anAttLast = anAttributes.end();
387 for (; anAttIt != anAttLast; anAttIt++) {
388 AttributePtr anAttr = *anAttIt;
389 if (anAttr.get() == NULL)
391 std::string aAttrId = anAttr->id();
392 DataPtr aData = aFeature->data();
393 if (aData.get() != NULL) {
394 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
395 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
396 if (aPoint.get() != NULL) {
397 bool isImmutable = aPoint->setImmutable(true);
398 aPoint->move(dX, dY);
399 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
400 aPoint->setImmutable(isImmutable);
405 // Process selection by feature
406 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
407 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
408 if (aSketchFeature) {
409 aSketchFeature->move(dX, dY);
410 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
414 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
415 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
417 // 5. it is necessary to save current selection in order to restore it after the features moving
418 FeatureToSelectionMap::const_iterator aSIt = aCurrentSelection.begin(),
419 aSLast = aCurrentSelection.end();
420 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
421 for (; aSIt != aSLast; aSIt++) {
422 anOwnersToSelect.Clear();
423 getSelectionOwners(aSIt->first, myCurrentSketch, aWorkshop, aCurrentSelection,
425 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
428 // 6. restore the update viewer flag and call this update
429 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
430 aDisplayer->updateViewer();
432 myCurrentPoint.setValue(aX, aY);
436 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
438 ModuleBase_Operation* aOperation = getCurrentOperation();
439 if (aOperation && aOperation->isEditOperation()) {
440 std::string aId = aOperation->id().toStdString();
441 if (isDistanceOperation(aOperation))
443 // Activate dimension value editing on double click
444 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
445 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
446 // Find corresponded widget to activate value editing
447 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
448 if (aWgt->attributeID() == "ConstraintValue") {
457 void PartSet_SketcherMgr::onApplicationStarted()
459 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
460 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
461 XGUI_Workshop* aWorkshop = aConnector->workshop();
462 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
463 if (aPropertyPanel) {
464 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
465 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
468 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
469 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
470 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
473 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
475 if (!myClickedPoint.myIsInitialized)
478 ModuleBase_Operation* aOperation = getCurrentOperation();
479 // the distance constraint feature should not use the clickedd point
480 // this is workaround in order to don't throw down the flyout point value,
481 // set by execute() method of these type of features
482 if (isDistanceOperation(aOperation))
485 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
487 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
491 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
494 Handle(V3d_View) aView = theWnd->v3dView();
495 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
497 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
498 thePoint.setValue(aX, anY);
501 void PartSet_SketcherMgr::launchEditing()
503 // there should be activate the vertex selection mode because the edit can happens by the selected
506 aModes << TopAbs_VERTEX << TopAbs_EDGE;
507 // TODO: #391 - to be uncommented
508 /*aModes.append(AIS_DSM_Text);
509 aModes.append(AIS_DSM_Line);
510 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
511 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
513 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
514 aConnector->activateSubShapesSelection(aModes);
516 if (!myFeature2AttributeMap.empty()) {
517 FeaturePtr aFeature = myFeature2AttributeMap.begin().key();
518 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
519 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
521 myModule->editFeature(aSPFeature);
528 QStringList PartSet_SketcherMgr::sketchOperationIdList()
530 static QStringList aIds;
531 if (aIds.size() == 0) {
532 aIds << SketchPlugin_Line::ID().c_str();
533 aIds << SketchPlugin_Point::ID().c_str();
534 aIds << SketchPlugin_Arc::ID().c_str();
535 aIds << SketchPlugin_Circle::ID().c_str();
536 aIds << SketchPlugin_ConstraintLength::ID().c_str();
537 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
538 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
539 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
540 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
541 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
546 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
548 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
551 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
553 return theOperation &&
554 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
557 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
559 std::string aId = theOperation ? theOperation->id().toStdString() : "";
561 return (aId == SketchPlugin_ConstraintLength::ID()) ||
562 (aId == SketchPlugin_ConstraintDistance::ID()) ||
563 (aId == SketchPlugin_ConstraintRadius::ID());
566 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
568 // Display all sketcher sub-Objects
569 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
570 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
571 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
573 // Hide sketcher result
574 std::list<ResultPtr> aResults = myCurrentSketch->results();
575 std::list<ResultPtr>::const_iterator aIt;
576 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
577 aDisplayer->erase((*aIt), false);
579 aDisplayer->erase(myCurrentSketch, false);
581 // Display sketcher objects
582 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
583 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
584 std::list<ResultPtr> aResults = aFeature->results();
585 std::list<ResultPtr>::const_iterator aIt;
586 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
587 aDisplayer->display((*aIt), false);
589 aDisplayer->display(aFeature, false);
592 if (myPlaneFilter.IsNull())
593 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
595 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
596 if (theOperation->isEditOperation()) {
597 // If it is editing of sketch then it means that plane is already defined
598 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
599 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
601 aDisplayer->updateViewer();
604 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
606 myIsMouseOverWindow = false;
608 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
609 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
611 DataPtr aData = myCurrentSketch->data();
612 if ((!aData) || (!aData->isValid())) {
613 // The sketch was aborted
614 myCurrentSketch = CompositeFeaturePtr();
615 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
617 // Erase all sketcher objects
618 QStringList aSketchIds = sketchOperationIdList();
619 QObjectPtrList aObjects = aDisplayer->displayedObjects();
620 foreach (ObjectPtr aObj, aObjects) {
621 DataPtr aObjData = aObj->data();
622 if ((!aObjData) || (!aObjData->isValid()))
623 aDisplayer->erase(aObj);
627 // Hide all sketcher sub-Objects
628 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
629 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
630 std::list<ResultPtr> aResults = aFeature->results();
631 std::list<ResultPtr>::const_iterator aIt;
632 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
633 aDisplayer->erase((*aIt), false);
635 aDisplayer->erase(aFeature, false);
637 // Display sketcher result
638 std::list<ResultPtr> aResults = myCurrentSketch->results();
639 std::list<ResultPtr>::const_iterator aIt;
640 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
641 aDisplayer->display((*aIt), false);
643 aDisplayer->display(myCurrentSketch);
645 myCurrentSketch = CompositeFeaturePtr();
646 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
647 aDisplayer->updateViewer();
650 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
652 connectToPropertyPanel(true);
655 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
657 connectToPropertyPanel(false);
658 myIsPropertyPanelValueChanged = false;
659 myIsMouseOverViewProcessed = true;
662 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
664 if (isNestedCreateOperation(theOperation))
665 visualizeFeature(theOperation, true);
668 bool PartSet_SketcherMgr::canUndo() const
670 return isNestedCreateOperation(getCurrentOperation());
673 bool PartSet_SketcherMgr::canRedo() const
675 return isNestedCreateOperation(getCurrentOperation());
678 bool PartSet_SketcherMgr::canDisplayObject() const
680 bool aCanDisplay = true;
681 if (!isNestedCreateOperation(getCurrentOperation()))
684 // during a nested create operation, the feature is redisplayed only if the mouse over view
685 // of there was a value modified in the property panel after the mouse left the view
686 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
690 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
692 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
695 void PartSet_SketcherMgr::getCurrentSelection(const FeatureToAttributesMap& theFeatureToAttributes,
696 const FeaturePtr& theSketch,
697 ModuleBase_IWorkshop* theWorkshop,
698 FeatureToSelectionMap& theSelection)
700 FeatureToAttributesMap::const_iterator anIt = theFeatureToAttributes.begin(),
701 aLast = theFeatureToAttributes.end();
702 for (; anIt != aLast; anIt++) {
703 FeaturePtr aFeature = anIt.key();
704 getCurrentSelection(aFeature, theSketch, theWorkshop, theSelection);
708 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
709 const FeaturePtr& theSketch,
710 ModuleBase_IWorkshop* theWorkshop,
711 FeatureToSelectionMap& theSelection)
713 if (theFeature.get() == NULL)
716 std::set<AttributePtr> aSelectedAttributes;
717 std::set<ResultPtr> aSelectedResults;
719 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
720 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
721 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
722 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
724 std::list<ResultPtr> aResults = theFeature->results();
725 std::list<ResultPtr>::const_iterator aIt;
726 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
728 ResultPtr aResult = *aIt;
729 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
730 if (aAISObj.get() == NULL)
732 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
733 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
735 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
736 aContext->SelectedOwner());
737 if (aBRepOwner.IsNull())
739 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
740 aBRepOwner->Selectable());
744 if (aBRepOwner->HasShape()) {
745 const TopoDS_Shape& aShape = aBRepOwner->Shape();
746 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
747 if (aShapeType == TopAbs_VERTEX) {
748 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
750 if (aPntAttr.get() != NULL)
751 aSelectedAttributes.insert(aPntAttr);
753 else if (aShapeType == TopAbs_EDGE &&
754 aSelectedResults.find(aResult) == aSelectedResults.end()) {
755 aSelectedResults.insert(aResult);
760 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
763 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
764 const FeaturePtr& theSketch,
765 ModuleBase_IWorkshop* theWorkshop,
766 const FeatureToSelectionMap& theSelection,
767 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
769 if (theFeature.get() == NULL)
772 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
773 std::set<AttributePtr> aSelectedAttributes = anIt->second.first;
774 std::set<ResultPtr> aSelectedResults = anIt->second.second;
776 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
777 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
778 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
779 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
781 std::list<ResultPtr> aResults = theFeature->results();
782 std::list<ResultPtr>::const_iterator aIt;
783 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
785 ResultPtr aResult = *aIt;
786 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
787 if (aAISObj.get() == NULL)
789 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
791 SelectMgr_IndexedMapOfOwner aSelectedOwners;
792 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
793 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
794 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
795 if ( anOwner.IsNull() || !anOwner->HasShape() )
797 const TopoDS_Shape& aShape = anOwner->Shape();
798 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
799 if (aShapeType == TopAbs_VERTEX) {
800 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
801 if (aPntAttr.get() != NULL &&
802 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
803 anOwnersToSelect.Add(anOwner);
806 else if (aShapeType == TopAbs_EDGE) {
807 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
808 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
809 anOwnersToSelect.FindIndex(anOwner) <= 0)
810 anOwnersToSelect.Add(anOwner);
816 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
818 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
819 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
820 XGUI_Workshop* aWorkshop = aConnector->workshop();
821 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
822 if (aPropertyPanel) {
823 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
824 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
826 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
828 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
833 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
835 return myModule->workshop()->currentOperation();
838 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation) const
840 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
843 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
844 const bool isToDisplay)
846 if (!theOperation || theOperation->isEditOperation())
849 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
850 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
851 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
853 // 1. change visibility of the object itself, here the presentable object is processed,
854 // e.g. constraints features
855 FeaturePtr aFeature = theOperation->feature();
856 std::list<ResultPtr> aResults = aFeature->results();
858 aDisplayer->display(aFeature, false);
860 aDisplayer->erase(aFeature, false);
862 // change visibility of the object results, e.g. non-constraint features
863 std::list<ResultPtr>::const_iterator aIt;
864 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
866 aDisplayer->display(*aIt, false);
869 aDisplayer->erase(*aIt, false);
872 aDisplayer->updateViewer();