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())
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())
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 = myModule->workshop()->currentOperation();
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 updateVisibilityOfCreatedFeature();
185 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
187 if (!isNestedCreateOperation())
190 // visualize the current operation feature
191 myIsPropertyPanelValueChanged = true;
192 updateVisibilityOfCreatedFeature();
195 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
197 get2dPoint(theWnd, theEvent, myClickedPoint);
199 if (!(theEvent->buttons() & Qt::LeftButton))
202 // Clear dragging mode
203 myIsDragging = false;
205 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
206 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
207 if (!aViewer->canDragByMouse())
210 ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
211 if (aOperation && aOperation->isEditOperation()) {
212 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
213 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
214 // If the current widget is a selector, do do nothing, it processes the mouse press
215 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
220 // Use only for sketch operations
221 if (aOperation && myCurrentSketch) {
222 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
225 bool isSketcher = isSketchOperation(aOperation);
226 bool isSketchOpe = isNestedSketchOperation(aOperation);
228 // Avoid non-sketch operations
229 if ((!isSketchOpe) && (!isSketcher))
232 bool isEditing = aOperation->isEditOperation();
234 // Ignore creation sketch operation
235 if ((!isSketcher) && (!isEditing))
238 // MoveTo in order to highlight current object
239 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
241 // Remember highlighted objects for editing
242 ModuleBase_ISelection* aSelect = aWorkshop->selection();
243 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
244 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
245 myFeature2AttributeMap.clear();
247 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
249 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
250 fillFeature2Attribute(aSelected, myFeature2AttributeMap, myCurrentSketch);
253 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
256 if (myFeature2AttributeMap.empty()) {
257 if (isSketchOpe && (!isSketcher))
258 // commit previous operation
259 if (!aOperation->commit())
266 get2dPoint(theWnd, theEvent, myCurrentPoint);
270 } else if (isSketchOpe && isEditing) {
271 // If selected another object commit current result
272 aOperation->commit();
275 get2dPoint(theWnd, theEvent, myCurrentPoint);
278 // This is necessary in order to finalize previous operation
279 QApplication::processEvents();
285 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
287 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
288 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
289 if (!aViewer->canDragByMouse())
291 ModuleBase_Operation* aOp = aWorkshop->currentOperation();
293 if (isNestedSketchOperation(aOp)) {
294 get2dPoint(theWnd, theEvent, myClickedPoint);
296 // Only for sketcher operations
300 myFeature2AttributeMap.clear();
302 // Reselect edited object
303 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
304 if (theEvent->modifiers() & Qt::ShiftModifier)
305 aViewer->AISContext()->ShiftSelect();
307 aViewer->AISContext()->Select();
313 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
314 myIsDragging = false;
317 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
319 if (isNestedCreateOperation() && !myIsMouseOverViewProcessed) {
320 myIsMouseOverViewProcessed = true;
321 // 1. perform the widget mouse move functionality and display the presentation
322 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
323 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
324 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
325 // the mouse move should be processed in the widget, if it can in order to visualize correct
326 // presentation. These widgets correct the feature attribute according to the mouse position
327 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
329 aPoint2DWdg->onMouseMove(theWnd, theEvent);
331 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
334 aDistanceWdg->onMouseMove(theWnd, theEvent);
336 updateVisibilityOfCreatedFeature();
339 myClickedPoint.clear();
342 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
343 // 1. it is necessary to save current selection in order to restore it after the features moving
344 FeatureToSelectionMap aCurrentSelection;
345 getCurrentSelection(myFeature2AttributeMap, myCurrentSketch, aWorkshop, aCurrentSelection);
347 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
348 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
349 // deselected). This flag should be restored in the slot, processed the mouse release signal.
350 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
351 aViewer->enableSelection(false);
353 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
356 if (isSketchOperation(aOperation))
357 return; // No edit operation activated
359 Handle(V3d_View) aView = theWnd->v3dView();
360 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
362 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
363 double dX = aX - myCurrentPoint.myCurX;
364 double dY = aY - myCurrentPoint.myCurY;
366 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
367 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
368 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
369 // viewer happens by deselect/select the modified objects. The flag should be restored after
370 // the selection processing. The update viewer should be also called.
371 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
373 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
374 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
376 FeatureToAttributesMap::const_iterator anIt = myFeature2AttributeMap.begin(),
377 aLast = myFeature2AttributeMap.end();
378 // 4. the features and attributes modification(move)
379 for (; anIt != aLast; anIt++) {
380 FeaturePtr aFeature = anIt.key();
382 AttributeList anAttributes = anIt.value();
383 // Process selection by attribute: the priority to the attribute
384 if (!anAttributes.empty()) {
385 AttributeList::const_iterator anAttIt = anAttributes.begin(), anAttLast = anAttributes.end();
386 for (; anAttIt != anAttLast; anAttIt++) {
387 AttributePtr anAttr = *anAttIt;
388 if (anAttr.get() == NULL)
390 std::string aAttrId = anAttr->id();
391 DataPtr aData = aFeature->data();
392 if (aData.get() != NULL) {
393 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
394 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
395 if (aPoint.get() != NULL) {
396 bool isImmutable = aPoint->setImmutable(true);
397 aPoint->move(dX, dY);
398 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
399 aPoint->setImmutable(isImmutable);
404 // Process selection by feature
405 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
406 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
407 if (aSketchFeature) {
408 aSketchFeature->move(dX, dY);
409 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
413 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
414 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
416 // 5. it is necessary to save current selection in order to restore it after the features moving
417 FeatureToSelectionMap::const_iterator aSIt = aCurrentSelection.begin(),
418 aSLast = aCurrentSelection.end();
419 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
420 for (; aSIt != aSLast; aSIt++) {
421 anOwnersToSelect.Clear();
422 getSelectionOwners(aSIt->first, myCurrentSketch, aWorkshop, aCurrentSelection,
424 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
427 // 6. restore the update viewer flag and call this update
428 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
429 aDisplayer->updateViewer();
431 myCurrentPoint.setValue(aX, aY);
435 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
437 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
438 if (aOperation && aOperation->isEditOperation()) {
439 std::string aId = aOperation->id().toStdString();
440 if (isDistanceOperation(aOperation))
442 // Activate dimension value editing on double click
443 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
444 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
445 // Find corresponded widget to activate value editing
446 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
447 if (aWgt->attributeID() == "ConstraintValue") {
456 void PartSet_SketcherMgr::onApplicationStarted()
458 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
459 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
460 XGUI_Workshop* aWorkshop = aConnector->workshop();
461 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
462 if (aPropertyPanel) {
463 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
464 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
467 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
468 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
469 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
472 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
474 if (!myClickedPoint.myIsInitialized)
477 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
478 // the distance constraint feature should not use the clickedd point
479 // this is workaround in order to don't throw down the flyout point value,
480 // set by execute() method of these type of features
481 if (isDistanceOperation(aOperation))
484 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
486 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
490 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
493 Handle(V3d_View) aView = theWnd->v3dView();
494 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
496 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
497 thePoint.setValue(aX, anY);
500 void PartSet_SketcherMgr::launchEditing()
502 // there should be activate the vertex selection mode because the edit can happens by the selected
505 aModes << TopAbs_VERTEX << TopAbs_EDGE;
506 // TODO: #391 - to be uncommented
507 /*aModes.append(AIS_DSM_Text);
508 aModes.append(AIS_DSM_Line);
509 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
510 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
512 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
513 aConnector->activateSubShapesSelection(aModes);
515 if (!myFeature2AttributeMap.empty()) {
516 FeaturePtr aFeature = myFeature2AttributeMap.begin().key();
517 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
518 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
520 myModule->editFeature(aSPFeature);
527 QStringList PartSet_SketcherMgr::sketchOperationIdList()
529 static QStringList aIds;
530 if (aIds.size() == 0) {
531 aIds << SketchPlugin_Line::ID().c_str();
532 aIds << SketchPlugin_Point::ID().c_str();
533 aIds << SketchPlugin_Arc::ID().c_str();
534 aIds << SketchPlugin_Circle::ID().c_str();
535 aIds << SketchPlugin_ConstraintLength::ID().c_str();
536 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
537 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
538 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
539 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
540 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
545 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
547 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
550 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
552 return theOperation &&
553 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
556 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
558 std::string aId = theOperation ? theOperation->id().toStdString() : "";
560 return (aId == SketchPlugin_ConstraintLength::ID()) ||
561 (aId == SketchPlugin_ConstraintDistance::ID()) ||
562 (aId == SketchPlugin_ConstraintRadius::ID());
565 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
567 // Display all sketcher sub-Objects
568 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
569 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
570 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
572 // Hide sketcher result
573 std::list<ResultPtr> aResults = myCurrentSketch->results();
574 std::list<ResultPtr>::const_iterator aIt;
575 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
576 aDisplayer->erase((*aIt), false);
578 aDisplayer->erase(myCurrentSketch, false);
580 // Display sketcher objects
581 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
582 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
583 std::list<ResultPtr> aResults = aFeature->results();
584 std::list<ResultPtr>::const_iterator aIt;
585 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
586 aDisplayer->display((*aIt), false);
588 aDisplayer->display(aFeature, false);
591 if (myPlaneFilter.IsNull())
592 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
594 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
595 if (theOperation->isEditOperation()) {
596 // If it is editing of sketch then it means that plane is already defined
597 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
598 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
600 aDisplayer->updateViewer();
603 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
605 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
606 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
608 DataPtr aData = myCurrentSketch->data();
609 if ((!aData) || (!aData->isValid())) {
610 // The sketch was aborted
611 myCurrentSketch = CompositeFeaturePtr();
612 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
614 // Erase all sketcher objects
615 QStringList aSketchIds = sketchOperationIdList();
616 QObjectPtrList aObjects = aDisplayer->displayedObjects();
617 foreach (ObjectPtr aObj, aObjects) {
618 DataPtr aObjData = aObj->data();
619 if ((!aObjData) || (!aObjData->isValid()))
620 aDisplayer->erase(aObj);
624 // Hide all sketcher sub-Objects
625 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
626 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
627 std::list<ResultPtr> aResults = aFeature->results();
628 std::list<ResultPtr>::const_iterator aIt;
629 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
630 aDisplayer->erase((*aIt), false);
632 aDisplayer->erase(aFeature, false);
634 // Display sketcher result
635 std::list<ResultPtr> aResults = myCurrentSketch->results();
636 std::list<ResultPtr>::const_iterator aIt;
637 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
638 aDisplayer->display((*aIt), false);
640 aDisplayer->display(myCurrentSketch);
642 myCurrentSketch = CompositeFeaturePtr();
643 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
644 aDisplayer->updateViewer();
647 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
649 connectToPropertyPanel(true);
652 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* )
654 connectToPropertyPanel(false);
655 myIsPropertyPanelValueChanged = false;
656 myIsMouseOverWindow = false;
657 myIsMouseOverViewProcessed = true;
660 bool PartSet_SketcherMgr::canUndo() const
662 return isNestedCreateOperation();
665 bool PartSet_SketcherMgr::canRedo() const
667 return isNestedCreateOperation();
670 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
672 bool aCanDisplay = true;
673 if (!isNestedCreateOperation())
676 // during a nested create operation, the feature is redisplayed only if the mouse over view
677 // of there was a value modified in the property panel after the mouse left the view
678 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
679 /*if (!aCanDisplay) {
680 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
681 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
682 XGUI_Workshop* aWorkshop = aConnector->workshop();
683 AppElements_MainWindow* aMainWindow = aWorkshop->mainWindow();
685 //aCanDisplay = aMainWindow->isMouseOverWindow();
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 bool PartSet_SketcherMgr::isNestedCreateOperation() const
835 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
836 return aOperation && !aOperation->isEditOperation() && isNestedSketchOperation(aOperation);
839 void PartSet_SketcherMgr::updateVisibilityOfCreatedFeature()
841 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
842 ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
843 if (!aOperation || aOperation->isEditOperation())
845 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
846 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
848 FeaturePtr aFeature = aOperation->feature();
849 std::list<ResultPtr> aResults = aFeature->results();
850 std::list<ResultPtr>::const_iterator aIt;
851 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
852 if (canDisplayObject(aFeature)) {
853 aDisplayer->display(*aIt, false);
856 aDisplayer->erase(*aIt, false);
859 aDisplayer->updateViewer();