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>
52 #include <SelectMgr_IndexedMapOfOwner.hxx>
53 #include <StdSelect_BRepOwner.hxx>
55 //#include <AIS_DimensionSelectionMode.hxx>
56 //#include <AIS_Shape.hxx>
58 #include <ModelAPI_Events.h>
59 #include <ModelAPI_Session.h>
61 #include <QMouseEvent>
62 #include <QApplication>
65 /// Returns list of unique objects by sum of objects from List1 and List2
66 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
67 const QList<ModuleBase_ViewerPrs>& theList2)
69 QList<ModuleBase_ViewerPrs> aRes;
70 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
71 if (!aRes.contains(aPrs))
74 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
75 if (!aRes.contains(aPrs))
81 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
82 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
83 const FeaturePtr theSketch)
85 QList<ModuleBase_ViewerPrs> aRes;
87 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
88 aLast = theList.end();
89 for (; anIt != aLast; anIt++)
91 ModuleBase_ViewerPrs aPrs = *anIt;
92 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
93 if (aFeature.get() == NULL)
96 QList<AttributePtr> anAttributes;
97 if (theFeature2AttributeMap.contains(aFeature)) {
98 anAttributes = theFeature2AttributeMap[aFeature];
101 TopoDS_Shape aShape = aPrs.shape();
102 if (!aShape.IsNull()) {
103 if (aShape.ShapeType() == TopAbs_VERTEX) {
104 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
105 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
106 anAttributes.push_back(anAttr);
109 theFeature2AttributeMap[aFeature] = anAttributes;
116 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
117 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
118 myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
119 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
120 myIsPopupMenuActive(false)
122 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
123 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
125 myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
127 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
128 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
130 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
131 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
133 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
134 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
136 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
137 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
139 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
140 XGUI_Workshop* aWorkshop = aConnector->workshop();
141 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
144 PartSet_SketcherMgr::~PartSet_SketcherMgr()
146 if (!myPlaneFilter.IsNull())
147 myPlaneFilter.Nullify();
150 void PartSet_SketcherMgr::onEnterViewPort()
152 if (!isNestedCreateOperation(getCurrentOperation()))
154 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
155 // created feature because it should be done in onMouseMove(). Some widgets watch
156 // the mouse move and use the cursor position to update own values. If the presentaion is
157 // redisplayed before this update, the feature presentation jumps from reset value to current.
158 myIsMouseOverWindow = true;
159 myIsPropertyPanelValueChanged = false;
162 void PartSet_SketcherMgr::onLeaveViewPort()
164 if (!isNestedCreateOperation(getCurrentOperation()))
167 myIsMouseOverViewProcessed = false;
168 myIsMouseOverWindow = false;
170 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
171 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
172 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
173 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
174 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
175 // obtained after reset value
176 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
177 ModuleBase_Operation* aOperation = getCurrentOperation();
178 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
179 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
183 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
185 // hides the presentation of the current operation feature
186 myIsPropertyPanelValueChanged = false;
187 // the feature is to be erased here, but it is correct to call canDisplayObject because
188 // there can be additional check (e.g. editor widget in distance constraint)
189 visualizeFeature(aOperation, canDisplayObject());
192 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
194 if (isNestedCreateOperation(getCurrentOperation()))
196 // it is necessary to save current selection in order to restore it after the values are modifed
199 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
200 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
201 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
202 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
205 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
207 if (isNestedCreateOperation(getCurrentOperation()))
209 // it is necessary to restore current selection in order to restore it after the values are modified
211 myCurrentSelection.clear();
213 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
214 // viewer happens by deselect/select the modified objects. The flag should be restored after
215 // the selection processing. The update viewer should be also called.
216 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
217 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
218 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
219 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
220 aDisplayer->updateViewer();
223 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
225 if (!isNestedCreateOperation(getCurrentOperation()))
228 // visualize the current operation feature
229 myIsPropertyPanelValueChanged = true;
230 ModuleBase_Operation* aOperation = getCurrentOperation();
231 // the feature is to be erased here, but it is correct to call canDisplayObject because
232 // there can be additional check (e.g. editor widget in distance constraint)
233 visualizeFeature(aOperation, canDisplayObject());
236 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
238 get2dPoint(theWnd, theEvent, myClickedPoint);
240 myIsPopupMenuActive = theEvent->buttons() & Qt::RightButton;
242 if (!(theEvent->buttons() & Qt::LeftButton))
245 // Clear dragging mode
246 myIsDragging = false;
248 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
249 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
250 if (!aViewer->canDragByMouse())
253 ModuleBase_Operation* aOperation = getCurrentOperation();
254 if (aOperation && aOperation->isEditOperation()) {
255 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
256 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
257 // If the current widget is a selector, do do nothing, it processes the mouse press
258 if(aActiveWgt && aActiveWgt->isViewerSelector()) {
263 // Use only for sketch operations
264 if (aOperation && myCurrentSketch) {
265 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
268 bool isSketcher = isSketchOperation(aOperation);
269 bool isSketchOpe = isNestedSketchOperation(aOperation);
271 // Avoid non-sketch operations
272 if ((!isSketchOpe) && (!isSketcher))
275 bool isEditing = aOperation->isEditOperation();
277 // Ignore creation sketch operation
278 if ((!isSketcher) && (!isEditing))
281 // MoveTo in order to highlight current object
282 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
284 // Remember highlighted objects for editing
285 ModuleBase_ISelection* aSelect = aWorkshop->selection();
287 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
288 storeSelection(!aHasShift);
290 if (myCurrentSelection.empty()) {
291 if (isSketchOpe && (!isSketcher))
292 // commit previous operation
293 if (!aOperation->commit())
300 get2dPoint(theWnd, theEvent, myCurrentPoint);
304 } else if (isSketchOpe && isEditing) {
305 // If selected another object commit current result
306 aOperation->commit();
309 get2dPoint(theWnd, theEvent, myCurrentPoint);
312 // This is necessary in order to finalize previous operation
313 QApplication::processEvents();
319 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
321 if (myIsPopupMenuActive)
322 myIsPopupMenuActive = false;
324 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
325 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
326 if (!aViewer->canDragByMouse())
328 ModuleBase_Operation* aOp = getCurrentOperation();
330 if (isNestedSketchOperation(aOp)) {
331 get2dPoint(theWnd, theEvent, myClickedPoint);
333 // Only for sketcher operations
337 myCurrentSelection.clear();
338 // Reselect edited object
339 /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
340 if (theEvent->modifiers() & Qt::ShiftModifier)
341 aViewer->AISContext()->ShiftSelect();
343 aViewer->AISContext()->Select();
349 aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
350 myIsDragging = false;
353 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
355 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
356 myIsMouseOverViewProcessed = true;
357 // 1. perform the widget mouse move functionality and display the presentation
358 ModuleBase_Operation* aOperation = getCurrentOperation();
359 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
360 ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
361 // the mouse move should be processed in the widget, if it can in order to visualize correct
362 // presentation. These widgets correct the feature attribute according to the mouse position
363 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
365 aPoint2DWdg->onMouseMove(theWnd, theEvent);
367 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
370 aDistanceWdg->onMouseMove(theWnd, theEvent);
372 // the feature is to be erased here, but it is correct to call canDisplayObject because
373 // there can be additional check (e.g. editor widget in distance constraint)
374 visualizeFeature(aOperation, canDisplayObject());
377 myClickedPoint.clear();
380 // 1. the current selection is saved in the mouse press method in order to restore it after moving
381 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
382 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
383 // deselected). This flag should be restored in the slot, processed the mouse release signal.
384 ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
385 aViewer->enableSelection(false);
387 ModuleBase_Operation* aOperation = getCurrentOperation();
390 if (isSketchOperation(aOperation))
391 return; // No edit operation activated
393 Handle(V3d_View) aView = theWnd->v3dView();
394 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
396 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
397 double dX = aX - myCurrentPoint.myCurX;
398 double dY = aY - myCurrentPoint.myCurY;
400 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
401 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
402 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
403 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
404 // viewer happens by deselect/select the modified objects. The flag should be restored after
405 // the selection processing. The update viewer should be also called.
406 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
408 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
409 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
410 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
411 aLast = myCurrentSelection.end();
412 // 4. the features and attributes modification(move)
413 for (; anIt != aLast; anIt++) {
414 FeaturePtr aFeature = anIt.key();
416 std::set<AttributePtr> anAttributes = anIt.value().first;
417 // Process selection by attribute: the priority to the attribute
418 if (!anAttributes.empty()) {
419 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
420 anAttLast = anAttributes.end();
421 for (; anAttIt != anAttLast; anAttIt++) {
422 AttributePtr anAttr = *anAttIt;
423 if (anAttr.get() == NULL)
425 std::string aAttrId = anAttr->id();
426 DataPtr aData = aFeature->data();
427 if (aData.get() != NULL) {
428 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
429 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
430 if (aPoint.get() != NULL) {
431 bool isImmutable = aPoint->setImmutable(true);
432 aPoint->move(dX, dY);
433 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
434 aPoint->setImmutable(isImmutable);
439 // Process selection by feature
440 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
441 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
442 if (aSketchFeature) {
443 aSketchFeature->move(dX, dY);
444 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
448 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
449 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
451 // 5. it is necessary to save current selection in order to restore it after the features moving
453 // 6. restore the update viewer flag and call this update
454 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
455 aDisplayer->updateViewer();
458 myCurrentPoint.setValue(aX, aY);
462 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
464 ModuleBase_Operation* aOperation = getCurrentOperation();
465 if (aOperation && aOperation->isEditOperation()) {
466 std::string aId = aOperation->id().toStdString();
467 if (isDistanceOperation(aOperation))
469 // Activate dimension value editing on double click
470 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
471 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
472 // Find corresponded widget to activate value editing
473 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
474 if (aWgt->attributeID() == "ConstraintValue") {
483 void PartSet_SketcherMgr::onApplicationStarted()
485 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
486 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
487 XGUI_Workshop* aWorkshop = aConnector->workshop();
488 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
489 if (aPropertyPanel) {
490 connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
491 this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
494 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
495 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
496 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
499 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
501 if (!myClickedPoint.myIsInitialized)
504 ModuleBase_Operation* aOperation = getCurrentOperation();
505 // the distance constraint feature should not use the clickedd point
506 // this is workaround in order to don't throw down the flyout point value,
507 // set by execute() method of these type of features
508 if (isDistanceOperation(aOperation))
511 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
513 aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
517 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
520 Handle(V3d_View) aView = theWnd->v3dView();
521 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
523 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
524 thePoint.setValue(aX, anY);
527 void PartSet_SketcherMgr::launchEditing()
529 // there should be activate the vertex selection mode because the edit can happens by the selected
532 aModes << TopAbs_VERTEX << TopAbs_EDGE;
533 // TODO: #391 - to be uncommented
534 /*aModes.append(AIS_DSM_Text);
535 aModes.append(AIS_DSM_Line);
536 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
537 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
539 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
540 aConnector->activateSubShapesSelection(aModes);
542 if (!myCurrentSelection.empty()) {
543 FeaturePtr aFeature = myCurrentSelection.begin().key();
544 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
545 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
547 myModule->editFeature(aSPFeature);
554 QStringList PartSet_SketcherMgr::sketchOperationIdList()
556 static QStringList aIds;
557 if (aIds.size() == 0) {
558 aIds << SketchPlugin_Line::ID().c_str();
559 aIds << SketchPlugin_Point::ID().c_str();
560 aIds << SketchPlugin_Arc::ID().c_str();
561 aIds << SketchPlugin_Circle::ID().c_str();
562 aIds << SketchPlugin_ConstraintLength::ID().c_str();
563 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
564 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
565 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
566 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
567 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
572 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
574 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
577 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
579 return theOperation &&
580 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
583 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
585 return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
588 bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
590 std::string aId = theOperation ? theOperation->id().toStdString() : "";
592 return (aId == SketchPlugin_Line::ID()) ||
593 (aId == SketchPlugin_Point::ID()) ||
594 (aId == SketchPlugin_Arc::ID()) ||
595 (aId == SketchPlugin_Circle::ID());
598 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
600 std::string aId = theOperation ? theOperation->id().toStdString() : "";
602 return (aId == SketchPlugin_ConstraintLength::ID()) ||
603 (aId == SketchPlugin_ConstraintDistance::ID()) ||
604 (aId == SketchPlugin_ConstraintRadius::ID());
607 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
609 // Display all sketcher sub-Objects
610 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
611 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
612 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
614 // Hide sketcher result
615 std::list<ResultPtr> aResults = myCurrentSketch->results();
616 std::list<ResultPtr>::const_iterator aIt;
617 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
618 aDisplayer->erase((*aIt), false);
620 aDisplayer->erase(myCurrentSketch, false);
622 // Display sketcher objects
623 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
624 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
625 std::list<ResultPtr> aResults = aFeature->results();
626 std::list<ResultPtr>::const_iterator aIt;
627 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
628 aDisplayer->display((*aIt), false);
630 aDisplayer->display(aFeature, false);
633 if (myPlaneFilter.IsNull())
634 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
636 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
637 if (theOperation->isEditOperation()) {
638 // If it is editing of sketch then it means that plane is already defined
639 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
640 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
642 aDisplayer->updateViewer();
645 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
647 myIsMouseOverWindow = false;
649 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
650 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
652 DataPtr aData = myCurrentSketch->data();
653 if ((!aData) || (!aData->isValid())) {
654 // The sketch was aborted
655 myCurrentSketch = CompositeFeaturePtr();
656 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
658 // Erase all sketcher objects
659 QStringList aSketchIds = sketchOperationIdList();
660 QObjectPtrList aObjects = aDisplayer->displayedObjects();
661 foreach (ObjectPtr aObj, aObjects) {
662 DataPtr aObjData = aObj->data();
663 if ((!aObjData) || (!aObjData->isValid()))
664 aDisplayer->erase(aObj);
668 // Hide all sketcher sub-Objects
669 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
670 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
671 std::list<ResultPtr> aResults = aFeature->results();
672 std::list<ResultPtr>::const_iterator aIt;
673 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
674 aDisplayer->erase((*aIt), false);
676 aDisplayer->erase(aFeature, false);
678 // Display sketcher result
679 std::list<ResultPtr> aResults = myCurrentSketch->results();
680 std::list<ResultPtr>::const_iterator aIt;
681 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
682 aDisplayer->display((*aIt), false);
684 aDisplayer->display(myCurrentSketch);
686 myCurrentSketch = CompositeFeaturePtr();
687 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
688 aDisplayer->updateViewer();
691 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
693 connectToPropertyPanel(true);
696 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
698 connectToPropertyPanel(false);
699 myIsPropertyPanelValueChanged = false;
700 myIsMouseOverViewProcessed = true;
703 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
705 if (isNestedCreateOperation(theOperation))
706 visualizeFeature(theOperation, true);
709 bool PartSet_SketcherMgr::canUndo() const
711 return isNestedCreateOperation(getCurrentOperation());
714 bool PartSet_SketcherMgr::canRedo() const
716 return isNestedCreateOperation(getCurrentOperation());
719 bool PartSet_SketcherMgr::canDisplayObject() const
721 bool aCanDisplay = true;
722 if (!isNestedCreateOperation(getCurrentOperation()))
725 ModuleBase_Operation* aOperation = getCurrentOperation();
726 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
727 ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
728 // the active widget editor should not influence here. The presentation should be visible always
729 // when this widget is active.
731 ModuleBase_WidgetEditor* anEditorWdg = dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg);
736 if (myIsPopupMenuActive)
739 // during a nested create operation, the feature is redisplayed only if the mouse over view
740 // of there was a value modified in the property panel after the mouse left the view
741 aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
745 bool PartSet_SketcherMgr::canChangeConstruction(bool& isConstruction) const
747 bool anEnabled = false;
748 ModuleBase_Operation* anOperation = getCurrentOperation();
750 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
751 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
755 QObjectPtrList anObjects;
756 // 1. change construction type of a created feature
757 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
758 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
759 anObjects.append(anOperation->feature());
762 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
763 anOperation->abort();
764 // 2. change construction type of selected sketch entities
765 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
766 anObjects = aSelection->selectedPresentations();
768 anEnabled = anObjects.size() > 0;
770 bool isNotConstructedFound = false;
771 if (anObjects.size() > 0) {
772 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
773 for (; anIt != aLast && !isNotConstructedFound; anIt++) {
774 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
775 if (aFeature.get() != NULL) {
776 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
777 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
778 if (aSketchFeature.get() != NULL) {
779 std::string anAttribute = SketchPlugin_SketchEntity::CONSTRUCTION_ID();
781 std::shared_ptr<ModelAPI_AttributeBoolean> aConstructionAttr =
782 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
783 isNotConstructedFound = !aConstructionAttr->value();
788 isConstruction = anObjects.size() && !isNotConstructedFound;
792 void PartSet_SketcherMgr::setConstruction(const bool isChecked)
794 ModuleBase_Operation* anOperation = getCurrentOperation();
796 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
797 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
801 QObjectPtrList anObjects;
802 bool isUseTransaction = false;
803 // 1. change construction type of a created feature
804 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
805 PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
806 anObjects.append(anOperation->feature());
809 isUseTransaction = true;
810 // 2. change construction type of selected sketch entities
811 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
812 anObjects = aSelection->selectedPresentations();
815 QAction* anAction = myModule->action("CONSTRUCTION_CMD");
816 SessionPtr aMgr = ModelAPI_Session::get();
817 if (isUseTransaction) {
818 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
819 anOperation->abort();
820 aMgr->startOperation(anAction->text().toStdString());
824 if (anObjects.size() > 0) {
825 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
826 for (; anIt != aLast; anIt++) {
827 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
828 if (aFeature.get() != NULL) {
829 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
830 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
831 if (aSketchFeature.get() != NULL) {
832 std::string anAttribute = SketchPlugin_SketchEntity::CONSTRUCTION_ID();
834 std::shared_ptr<ModelAPI_AttributeBoolean> aConstructionAttr =
835 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
836 aConstructionAttr->setValue(isChecked);
841 if (isUseTransaction) {
842 aMgr->finishOperation();
844 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
848 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
850 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
853 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
854 const FeaturePtr& theSketch,
855 ModuleBase_IWorkshop* theWorkshop,
856 FeatureToSelectionMap& theSelection)
858 if (theFeature.get() == NULL)
861 std::set<AttributePtr> aSelectedAttributes;
862 std::set<ResultPtr> aSelectedResults;
864 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
865 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
866 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
867 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
869 std::list<ResultPtr> aResults = theFeature->results();
870 std::list<ResultPtr>::const_iterator aIt;
871 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
873 ResultPtr aResult = *aIt;
874 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
875 if (aAISObj.get() == NULL)
877 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
878 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
880 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
881 aContext->SelectedOwner());
882 if (aBRepOwner.IsNull())
884 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
885 aBRepOwner->Selectable());
889 if (aBRepOwner->HasShape()) {
890 const TopoDS_Shape& aShape = aBRepOwner->Shape();
891 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
892 if (aShapeType == TopAbs_VERTEX) {
893 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
895 if (aPntAttr.get() != NULL)
896 aSelectedAttributes.insert(aPntAttr);
898 else if (aShapeType == TopAbs_EDGE &&
899 aSelectedResults.find(aResult) == aSelectedResults.end()) {
900 aSelectedResults.insert(aResult);
905 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
908 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
909 const FeaturePtr& theSketch,
910 ModuleBase_IWorkshop* theWorkshop,
911 const FeatureToSelectionMap& theSelection,
912 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
914 if (theFeature.get() == NULL)
917 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
918 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
919 std::set<ResultPtr> aSelectedResults = anIt.value().second;
921 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
922 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
923 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
924 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
926 // 1. found the feature's owners. Check the AIS objects of the constructions
927 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
928 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
929 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
931 SelectMgr_IndexedMapOfOwner aSelectedOwners;
932 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
933 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
934 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
935 if (!anOwner.IsNull())
936 anOwnersToSelect.Add(anOwner);
940 // 2. found the feature results's owners
941 std::list<ResultPtr> aResults = theFeature->results();
942 std::list<ResultPtr>::const_iterator aIt;
943 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
945 ResultPtr aResult = *aIt;
946 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
947 if (aAISObj.get() == NULL)
949 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
951 SelectMgr_IndexedMapOfOwner aSelectedOwners;
952 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
953 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
954 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
955 if ( anOwner.IsNull() || !anOwner->HasShape() )
957 const TopoDS_Shape& aShape = anOwner->Shape();
958 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
959 if (aShapeType == TopAbs_VERTEX) {
960 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
961 if (aPntAttr.get() != NULL &&
962 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
963 anOwnersToSelect.Add(anOwner);
966 else if (aShapeType == TopAbs_EDGE) {
967 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
968 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
969 anOwnersToSelect.FindIndex(anOwner) <= 0)
970 anOwnersToSelect.Add(anOwner);
976 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
978 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
979 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
980 XGUI_Workshop* aWorkshop = aConnector->workshop();
981 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
982 if (aPropertyPanel) {
983 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
984 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
986 connect(aWidget, SIGNAL(beforeValuesChanged()),
987 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
988 connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
989 connect(aWidget, SIGNAL(afterValuesChanged()),
990 this, SLOT(onAfterValuesChangedInPropertyPanel()));
993 disconnect(aWidget, SIGNAL(beforeValuesChanged()),
994 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
995 disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
996 disconnect(aWidget, SIGNAL(afterValuesChanged()),
997 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1003 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1005 return myModule->workshop()->currentOperation();
1008 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1009 const bool isToDisplay)
1011 if (!theOperation || theOperation->isEditOperation())
1014 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1015 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1016 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1018 // 1. change visibility of the object itself, here the presentable object is processed,
1019 // e.g. constraints features
1020 FeaturePtr aFeature = theOperation->feature();
1021 std::list<ResultPtr> aResults = aFeature->results();
1023 aDisplayer->display(aFeature, false);
1025 aDisplayer->erase(aFeature, false);
1027 // change visibility of the object results, e.g. non-constraint features
1028 std::list<ResultPtr>::const_iterator aIt;
1029 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1031 aDisplayer->display(*aIt, false);
1034 aDisplayer->erase(*aIt, false);
1037 aDisplayer->updateViewer();
1040 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1042 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1043 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1044 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1046 QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1047 if (theHighlightedOnly) {
1048 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1051 fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1053 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1054 fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1057 // 1. it is necessary to save current selection in order to restore it after the features moving
1058 myCurrentSelection.clear();
1059 QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1060 aLast = aFeature2AttributeMap.end();
1061 for (; anIt != aLast; anIt++) {
1062 FeaturePtr aFeature = anIt.key();
1063 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1065 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1068 void PartSet_SketcherMgr::restoreSelection()
1070 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1071 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1072 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1073 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1074 aSLast = myCurrentSelection.end();
1075 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1076 for (; aSIt != aSLast; aSIt++) {
1077 anOwnersToSelect.Clear();
1078 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1080 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);