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_SketcherReetntrantMgr.h"
9 #include "PartSet_Module.h"
10 #include "PartSet_WidgetPoint2d.h"
11 #include "PartSet_WidgetPoint2dDistance.h"
12 #include "PartSet_Tools.h"
13 #include "PartSet_WidgetSketchLabel.h"
14 #include "PartSet_WidgetEditor.h"
16 #include <XGUI_ModuleConnector.h>
17 #include <XGUI_Displayer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_ContextMenuMgr.h>
20 #include <XGUI_Selection.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <XGUI_ModuleConnector.h>
23 #include <XGUI_PropertyPanel.h>
24 #include <XGUI_ViewerProxy.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_Tools.h>
28 #include <ModuleBase_IPropertyPanel.h>
29 #include <ModuleBase_ISelection.h>
30 #include <ModuleBase_IViewer.h>
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_IViewWindow.h>
33 #include <ModuleBase_ModelWidget.h>
34 #include <ModuleBase_Operation.h>
35 #include <ModuleBase_OperationFeature.h>
36 #include <ModuleBase_Operation.h>
37 #include <ModuleBase_WidgetEditor.h>
39 #include <GeomDataAPI_Point2D.h>
41 #include <Events_Loop.h>
43 #include <SketchPlugin_Line.h>
44 #include <SketchPlugin_Sketch.h>
45 #include <SketchPlugin_Point.h>
46 #include <SketchPlugin_Arc.h>
47 #include <SketchPlugin_Circle.h>
48 #include <SketchPlugin_ConstraintLength.h>
49 #include <SketchPlugin_ConstraintDistance.h>
50 #include <SketchPlugin_ConstraintParallel.h>
51 #include <SketchPlugin_ConstraintPerpendicular.h>
52 #include <SketchPlugin_ConstraintRadius.h>
53 #include <SketchPlugin_ConstraintRigid.h>
54 #include <SketchPlugin_ConstraintHorizontal.h>
55 #include <SketchPlugin_ConstraintVertical.h>
56 #include <SketchPlugin_ConstraintEqual.h>
57 #include <SketchPlugin_ConstraintTangent.h>
58 #include <SketchPlugin_ConstraintCoincidence.h>
59 #include <SketchPlugin_ConstraintFillet.h>
60 #include <SketchPlugin_ConstraintMirror.h>
61 #include <SketchPlugin_ConstraintAngle.h>
62 #include <SketchPlugin_ConstraintCollinear.h>
63 #include <SketchPlugin_ConstraintMiddle.h>
64 #include <SketchPlugin_MultiRotation.h>
65 #include <SketchPlugin_MultiTranslation.h>
66 #include <SketchPlugin_IntersectionPoint.h>
68 #include <SketcherPrs_Tools.h>
70 #include <SelectMgr_IndexedMapOfOwner.hxx>
71 #include <StdSelect_BRepOwner.hxx>
73 //#include <AIS_DimensionSelectionMode.hxx>
74 #include <AIS_Shape.hxx>
75 #include <AIS_Dimension.hxx>
77 #include <ModelAPI_Events.h>
78 #include <ModelAPI_Session.h>
79 #include <ModelAPI_AttributeString.h>
81 #include <QMouseEvent>
82 #include <QApplication>
84 #include <QMessageBox>
86 //#define DEBUG_DO_NOT_BY_ENTER
88 //#define DEBUG_CURSOR
90 /// Returns list of unique objects by sum of objects from List1 and List2
91 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
92 const QList<ModuleBase_ViewerPrs>& theList2)
94 QList<ModuleBase_ViewerPrs> aRes;
95 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
96 if (!aRes.contains(aPrs))
99 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
100 if (!aRes.contains(aPrs))
106 // Fills the list of features the list of selected presentations.
107 // \param theList a list of selected presentations
108 // \param theSketch a sketch to project a vertex shape of a presentation to the plane
109 // and find the corresponded attribute
110 // \param theFeatureList an output list of features
111 void fillFeatureList(const QList<ModuleBase_ViewerPrs>& theList,
112 const FeaturePtr theSketch,
113 QList<FeaturePtr>& theFeatureList)
115 QList<ModuleBase_ViewerPrs> aRes;
117 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
118 aLast = theList.end();
119 for (; anIt != aLast; anIt++)
121 ModuleBase_ViewerPrs aPrs = *anIt;
122 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
123 if (aFeature.get() && !theFeatureList.contains(aFeature))
124 theFeatureList.append(aFeature);
128 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
129 /// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge, put the function
130 /// result as is to the list of results.
131 /// \param theOwner a viewer selected owner
132 /// \param theFeature a feature, where the attribute is searched
133 /// \param theSketch a current sketch
134 /// \param theSelectedAttribute an output list of attributes
135 /// \param theSelectedResults an output list of edge results
136 void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
137 const FeaturePtr& theFeature, const FeaturePtr& theSketch,
138 const ResultPtr& theResult,
139 std::set<AttributePtr>& aSelectedAttributes,
140 std::set<ResultPtr>& aSelectedResults)
142 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
143 if (aBRepOwner.IsNull())
145 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
146 aBRepOwner->Selectable());
147 if (aBRepOwner->HasShape()) {
148 const TopoDS_Shape& aShape = aBRepOwner->Shape();
149 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
150 if (aShapeType == TopAbs_VERTEX) {
151 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
153 if (aPntAttr.get() != NULL)
154 aSelectedAttributes.insert(aPntAttr);
156 else if (aShapeType == TopAbs_EDGE &&
157 aSelectedResults.find(theResult) == aSelectedResults.end()) {
158 aSelectedResults.insert(theResult);
163 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
164 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
165 myIsMouseOverWindow(false),
166 myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
167 myIsPopupMenuActive(false)
169 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
170 ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
172 myPreviousDrawModeEnabled = true;//aViewer->isSelectionEnabled();
174 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
175 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
177 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
178 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
180 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
181 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
183 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
184 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
186 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
187 XGUI_Workshop* aWorkshop = aConnector->workshop();
188 connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
190 myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
191 myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
194 PartSet_SketcherMgr::~PartSet_SketcherMgr()
196 if (!myPlaneFilter.IsNull())
197 myPlaneFilter.Nullify();
200 void PartSet_SketcherMgr::onEnterViewPort()
202 // 1. if the mouse over window, update the next flag. Do not perform update visibility of
203 // created feature because it should be done in onMouseMove(). Some widgets watch
204 // the mouse move and use the cursor position to update own values. If the presentaion is
205 // redisplayed before this update, the feature presentation jumps from reset value to current.
206 myIsMouseOverWindow = true;
208 #ifdef DEBUG_DO_NOT_BY_ENTER
212 if (canChangeCursor(getCurrentOperation())) {
213 QCursor* aCurrentCursor = QApplication::overrideCursor();
214 if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
215 QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
217 qDebug("onEnterViewPort() : Qt::CrossCursor");
222 if (!isNestedCreateOperation(getCurrentOperation()))
225 operationMgr()->onValidateOperation();
227 // we need change displayed state of the current operation feature
228 // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
229 // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
230 // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
231 // presentation becomes valid and redisplay happens
232 //ModuleBase_Operation* aOperation = getCurrentOperation();
233 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
234 (getCurrentOperation());
236 FeaturePtr aFeature = aFOperation->feature();
237 if (aFeature.get() && aFeature->data()->isValid()) {
238 visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature), false);
243 void PartSet_SketcherMgr::onLeaveViewPort()
245 myIsMouseOverViewProcessed = false;
246 myIsMouseOverWindow = false;
247 // it is important to validate operation here only if sketch entity create operation is active
248 // because at this operation we reacts to the mouse leave/enter view port
249 //operationMgr()->onValidateOperation();
251 #ifdef DEBUG_DO_NOT_BY_ENTER
255 if (canChangeCursor(getCurrentOperation())) {
256 QApplication::restoreOverrideCursor();
258 qDebug("onLeaveViewPort() : None");
262 if (!isNestedCreateOperation(getCurrentOperation()))
265 // the method should be performed if the popup menu is called,
266 // the reset of the current widget should not happen
267 if (myIsPopupMenuActive)
270 operationMgr()->onValidateOperation();
272 // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
273 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
274 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
275 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
276 // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
277 // obtained after reset value
278 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
279 ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
281 anActiveWidget->reset();
283 // hides the presentation of the current operation feature
284 // the feature is to be erased here, but it is correct to call canDisplayObject because
285 // there can be additional check (e.g. editor widget in distance constraint)
286 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
287 (getCurrentOperation());
289 FeaturePtr aFeature = aFOperation->feature();
290 visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
292 // we should update viewer after the presentation are hidden in the viewer
293 // otherwise the reset presentation(line) appears in the viewer(by quick move from viewer to PP)
294 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
297 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
299 if (!isNestedEditOperation(getCurrentOperation()) ||
300 myModule->sketchReentranceMgr()->isInternalEditActive())
302 // it is necessary to save current selection in order to restore it after the values are modifed
305 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
306 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
307 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
308 myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
311 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
313 if (!isNestedEditOperation(getCurrentOperation()) ||
314 myModule->sketchReentranceMgr()->isInternalEditActive()) {
315 myModule->sketchReentranceMgr()->updateInternalEditActiveState();
318 // it is necessary to restore current selection in order to restore it after the values are modified
320 myCurrentSelection.clear();
322 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
323 // viewer happens by deselect/select the modified objects. The flag should be restored after
324 // the selection processing. The update viewer should be also called.
325 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
326 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
327 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
328 aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
329 aDisplayer->updateViewer();
334 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
336 if (myModule->sketchReentranceMgr()->processMousePressed(theWnd, theEvent))
339 //get2dPoint(theWnd, theEvent, myClickedPoint);
341 if (!(theEvent->buttons() & Qt::LeftButton))
344 // Clear dragging mode
345 myIsDragging = false;
347 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
348 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
349 if (!aViewer->canDragByMouse())
352 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
353 (getCurrentOperation());
357 if (aFOperation->isEditOperation()) {
358 // If the current widget is a selector, do nothing, it processes the mouse press
359 ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
360 if(anActiveWidget && anActiveWidget->isViewerSelector()) {
365 // Use only for sketch operations
366 if (myCurrentSketch) {
367 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
370 bool isSketcher = isSketchOperation(aFOperation);
371 bool isSketchOpe = isNestedSketchOperation(aFOperation);
373 // Avoid non-sketch operations
374 if ((!isSketchOpe) && (!isSketcher))
377 bool isEditing = aFOperation->isEditOperation();
379 // Ignore creation sketch operation
380 if ((!isSketcher) && (!isEditing))
383 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
384 if (!aContext.IsNull()) {
385 // MoveTo in order to highlight current object
386 aContext->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
388 // Remember highlighted objects for editing
389 ModuleBase_ISelection* aSelect = aWorkshop->selection();
391 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
392 storeSelection(!aHasShift);
394 if (myCurrentSelection.empty()) {
395 if (isSketchOpe && (!isSketcher))
396 // commit previous operation
397 if (!aFOperation->commit())
398 aFOperation->abort();
401 // Init flyout point for radius rotation
402 FeaturePtr aFeature = myCurrentSelection.begin().key();
406 get2dPoint(theWnd, theEvent, myCurrentPoint);
409 myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
411 if (aFeature.get() != NULL) {
412 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
413 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
414 if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
415 DataPtr aData = aSPFeature->data();
416 AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
417 std::shared_ptr<GeomDataAPI_Point2D> aFPAttr =
418 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
419 aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
422 } else if (isSketchOpe && isEditing) {
423 // If selected another object commit current result
424 aFOperation->commit();
427 get2dPoint(theWnd, theEvent, myCurrentPoint);
430 myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
432 // this is temporary commented in order to avoid the following wrong case:
433 // Distance constraint is under edition, double click on the digit -> nothing happens
434 // because QApplication::processEvents() calls onMouseDoubleClick, which try to show editor
435 // but as the prev edit is commited an new one is not started, editor is not shown.
436 // This is necessary in order to finalize previous operation
437 //QApplication::processEvents();
443 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
445 if (myModule->sketchReentranceMgr()->processMouseReleased(theWnd, theEvent))
448 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
449 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
450 if (!aViewer->canDragByMouse())
452 ModuleBase_Operation* aOp = getCurrentOperation();
454 if (isNestedSketchOperation(aOp)) {
455 //get2dPoint(theWnd, theEvent, myClickedPoint);
457 // Only for sketcher operations
461 myCurrentSelection.clear();
462 /*Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
463 if (!aContext.IsNull()) {
464 // Reselect edited object
465 aContext->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
466 if (theEvent->modifiers() & Qt::ShiftModifier)
467 aContext->ShiftSelect();
476 aWorkshop->viewer()->enableDrawMode(myPreviousDrawModeEnabled);
477 myIsDragging = false;
480 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
482 if (myModule->sketchReentranceMgr()->processMouseMoved(theWnd, theEvent))
485 if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
486 myIsMouseOverViewProcessed = true;
487 // 1. perform the widget mouse move functionality and display the presentation
488 // the mouse move should be processed in the widget, if it can in order to visualize correct
489 // presentation. These widgets correct the feature attribute according to the mouse position
490 ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
491 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
493 aPoint2DWdg->onMouseMove(theWnd, theEvent);
495 PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
498 aDistanceWdg->onMouseMove(theWnd, theEvent);
500 // the feature is to be erased here, but it is correct to call canDisplayObject because
501 // there can be additional check (e.g. editor widget in distance constraint)
502 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
503 (getCurrentOperation());
505 FeaturePtr aFeature = aFOperation->feature();
506 visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
510 //myClickedPoint.clear();
513 // 1. the current selection is saved in the mouse press method in order to restore it after moving
514 // 2. the enable selection in the viewer should be temporary switched off in order to ignore
515 // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
516 // deselected). This flag should be restored in the slot, processed the mouse release signal.
518 ModuleBase_Operation* aCurrentOperation = getCurrentOperation();
519 if (!aCurrentOperation)
521 if (isSketchOperation(aCurrentOperation))
522 return; // No edit operation activated
524 Handle(V3d_View) aView = theWnd->v3dView();
525 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
527 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
528 double dX = aX - myCurrentPoint.myCurX;
529 double dY = aY - myCurrentPoint.myCurY;
531 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
532 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
533 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
534 // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
535 // viewer happens by deselect/select the modified objects. The flag should be restored after
536 // the selection processing. The update viewer should be also called.
537 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
539 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
540 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
541 FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
542 aLast = myCurrentSelection.end();
543 // 4. the features and attributes modification(move)
544 bool isModified = false;
545 for (; anIt != aLast; anIt++) {
546 FeaturePtr aFeature = anIt.key();
548 std::set<AttributePtr> anAttributes = anIt.value().first;
549 // Process selection by attribute: the priority to the attribute
550 if (!anAttributes.empty()) {
551 std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
552 anAttLast = anAttributes.end();
553 for (; anAttIt != anAttLast; anAttIt++) {
554 AttributePtr anAttr = *anAttIt;
555 if (anAttr.get() == NULL)
557 std::string aAttrId = anAttr->id();
558 DataPtr aData = aFeature->data();
559 if (aData->isValid()) {
560 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
561 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
562 if (aPoint.get() != NULL) {
563 bool isImmutable = aPoint->setImmutable(true);
564 aPoint->move(dX, dY);
566 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
567 aPoint->setImmutable(isImmutable);
572 // Process selection by feature
573 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
574 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
575 if (aSketchFeature) {
576 aSketchFeature->move(dX, dY);
578 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
582 // the modified state of the current operation should be updated if there are features, which
585 aCurrentOperation->onValuesChanged();
587 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
588 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
590 // 5. it is necessary to save current selection in order to restore it after the features moving
592 // 6. restore the update viewer flag and call this update
593 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
594 aDisplayer->updateViewer();
597 myCurrentPoint.setValue(aX, aY);
601 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
603 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
604 (getCurrentOperation());
605 if (aFOperation && aFOperation->isEditOperation()) {
606 std::string aId = aFOperation->id().toStdString();
607 if (isDistanceOperation(aFOperation))
609 // Activate dimension value editing on double click
610 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
611 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
612 // Find corresponded widget to activate value editing
613 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
614 if (aWgt->attributeID() == "ConstraintValue" ||
615 aWgt->attributeID() == "AngleValue") {
616 PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
618 anEditor->showPopupEditor();
626 void PartSet_SketcherMgr::onApplicationStarted()
628 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
629 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
630 XGUI_Workshop* aWorkshop = aConnector->workshop();
631 PartSet_SketcherReetntrantMgr* aReentranceMgr = myModule->sketchReentranceMgr();
633 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
634 if (aPropertyPanel) {
635 //connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
636 // this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
638 connect(aPropertyPanel, SIGNAL(noMoreWidgets(const std::string&)),
639 aReentranceMgr, SLOT(onNoMoreWidgets(const std::string&)));
640 connect(aPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
641 aReentranceMgr, SLOT(onWidgetActivated()));
644 XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
645 connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
646 connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
648 XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
649 connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
650 connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
653 //void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
655 //if (!myClickedPoint.myIsInitialized)
658 //ModuleBase_Operation* aOperation = getCurrentOperation();
659 // the distance constraint feature should not use the clickedd point
660 // this is workaround in order to don't throw down the flyout point value,
661 // set by execute() method of these type of features
662 //if (isDistanceOperation(aOperation))
665 //PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
667 // aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
671 void PartSet_SketcherMgr::onBeforeContextMenu()
673 myIsPopupMenuActive = true;
676 void PartSet_SketcherMgr::onAfterContextMenu()
678 myIsPopupMenuActive = false;
681 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
684 Handle(V3d_View) aView = theWnd->v3dView();
685 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
687 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
688 thePoint.setValue(aX, anY);
691 void PartSet_SketcherMgr::launchEditing()
693 if (!myCurrentSelection.empty()) {
694 FeaturePtr aFeature = myCurrentSelection.begin().key();
695 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
696 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
698 myModule->editFeature(aSPFeature);
703 bool PartSet_SketcherMgr::sketchSolverError()
705 bool anError = false;
706 CompositeFeaturePtr aSketch = activeSketch();
708 AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
709 anError = !aAttributeString->value().empty();
714 QString PartSet_SketcherMgr::getFeatureError(const FeaturePtr& theFeature)
716 QString anError = "";
717 if (!theFeature.get() || !theFeature->data()->isValid())
720 CompositeFeaturePtr aSketch = activeSketch();
721 if (aSketch.get() && aSketch == theFeature) {
722 AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
723 anError = aAttributeString->value().c_str();
728 void PartSet_SketcherMgr::clearClickedFlags()
730 //myClickedPoint.clear();
731 myCurrentPoint.clear();
734 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
736 static QStringList aIds;
737 if (aIds.size() == 0) {
738 aIds << SketchPlugin_Line::ID().c_str();
739 aIds << SketchPlugin_Point::ID().c_str();
740 aIds << SketchPlugin_Arc::ID().c_str();
741 aIds << SketchPlugin_Circle::ID().c_str();
742 aIds << SketchPlugin_ConstraintFillet::ID().c_str();
743 aIds << SketchPlugin_IntersectionPoint::ID().c_str();
745 // SketchRectangle is a python feature, so its ID is passed just as a string
746 aIds << "SketchRectangle";
747 aIds.append(constraintsIdList());
752 const QStringList& PartSet_SketcherMgr::constraintsIdList()
754 static QStringList aIds;
755 if (aIds.size() == 0) {
756 aIds << SketchPlugin_ConstraintLength::ID().c_str();
757 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
758 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
759 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
760 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
761 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
762 aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
763 aIds << SketchPlugin_ConstraintVertical::ID().c_str();
764 aIds << SketchPlugin_ConstraintEqual::ID().c_str();
765 aIds << SketchPlugin_ConstraintTangent::ID().c_str();
766 aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
767 aIds << SketchPlugin_ConstraintMirror::ID().c_str();
768 aIds << SketchPlugin_ConstraintAngle::ID().c_str();
769 aIds << SketchPlugin_MultiRotation::ID().c_str();
770 aIds << SketchPlugin_MultiTranslation::ID().c_str();
771 aIds << SketchPlugin_ConstraintCollinear::ID().c_str();
772 aIds << SketchPlugin_ConstraintMiddle::ID().c_str();
777 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
781 theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
782 theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
783 theModes.append(SketcherPrs_Tools::Sel_Constraint);
784 theModes.append(TopAbs_VERTEX);
785 theModes.append(TopAbs_EDGE);
788 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
790 return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
793 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
795 return theOperation &&
796 PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
799 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
801 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
803 return aFOperation && !aFOperation->isEditOperation() && isNestedSketchOperation(aFOperation);
806 bool PartSet_SketcherMgr::isNestedEditOperation(ModuleBase_Operation* theOperation)
808 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
810 return aFOperation && aFOperation->isEditOperation() && isNestedSketchOperation(aFOperation);
813 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
815 return (theId == SketchPlugin_Line::ID()) ||
816 (theId == SketchPlugin_Point::ID()) ||
817 (theId == SketchPlugin_Arc::ID()) ||
818 (theId == SketchPlugin_Circle::ID());
821 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
823 std::string anId = theOperation ? theOperation->id().toStdString() : "";
825 return isDistanceKind(anId);
828 bool PartSet_SketcherMgr::isDistanceKind(std::string& theKind)
830 return (theKind == SketchPlugin_ConstraintLength::ID()) ||
831 (theKind == SketchPlugin_ConstraintDistance::ID()) ||
832 (theKind == SketchPlugin_ConstraintRadius::ID()) ||
833 (theKind == SketchPlugin_ConstraintAngle::ID());
836 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
838 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
839 (getCurrentOperation());
843 myModule->onViewTransformed();
845 // Display all sketcher sub-Objects
846 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFOperation->feature());
847 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
848 aConnector->workshop()->displayer()->activateTrihedron(true);
850 // Hide sketcher result
851 std::list<ResultPtr> aResults = myCurrentSketch->results();
852 std::list<ResultPtr>::const_iterator aIt;
853 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
854 (*aIt)->setDisplayed(false);
856 myCurrentSketch->setDisplayed(false);
858 // Display sketcher objects
859 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
860 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
861 std::list<ResultPtr> aResults = aFeature->results();
862 std::list<ResultPtr>::const_iterator aIt;
863 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
864 (*aIt)->setDisplayed(true);
866 aFeature->setDisplayed(true);
869 if(myCirclePointFilter.IsNull()) {
870 myCirclePointFilter = new PartSet_CirclePointFilter(myModule->workshop());
873 myModule->workshop()->viewer()->addSelectionFilter(myCirclePointFilter);
875 if (myPlaneFilter.IsNull())
876 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
878 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
879 bool aHasPlane = false;
880 std::shared_ptr<GeomAPI_Pln> aPln;
881 if (aFOperation->isEditOperation()) {
882 // If it is editing of sketch then it means that plane is already defined
883 aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
887 myPlaneFilter->setPlane(aPln);
889 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
890 // all sketch objects should be activated in the sketch selection modes by edit operation start
891 // in case of creation operation, there is an active widget, which activates own selection mode
892 if (aFOperation->isEditOperation() && aHasPlane)
893 aConnector->activateModuleSelectionModes();
896 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
898 myIsMouseOverWindow = false;
899 myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
900 myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
902 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
904 DataPtr aData = myCurrentSketch->data();
905 if (!aData->isValid()) {
906 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
907 // The sketch was aborted
908 myCurrentSketch = CompositeFeaturePtr();
909 // TODO: move this outside of if-else
910 myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
911 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
913 // Erase all sketcher objects
914 QStringList aSketchIds = sketchOperationIdList();
915 QObjectPtrList aObjects = aDisplayer->displayedObjects();
916 foreach (ObjectPtr aObj, aObjects) {
917 DataPtr aObjData = aObj->data();
918 if (!aObjData->isValid())
919 aObj->setDisplayed(false);
923 // Hide all sketcher sub-Objects
924 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
925 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
926 std::list<ResultPtr> aResults = aFeature->results();
927 std::list<ResultPtr>::const_iterator aIt;
928 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
929 (*aIt)->setDisplayed(false);
931 aFeature->setDisplayed(false);
933 // Display sketcher result
934 std::list<ResultPtr> aResults = myCurrentSketch->results();
935 std::list<ResultPtr>::const_iterator aIt;
936 Events_Loop* aLoop = Events_Loop::loop();
937 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
939 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
941 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
942 if (!aFOperation->isDisplayedOnStart(*aIt)) {
943 (*aIt)->setDisplayed(true);
944 // this display event is needed because sketch already may have "displayed" state,
945 // but not displayed while it is still active (issue 613, abort of existing sketch)
946 ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
949 if (!aFOperation->isDisplayedOnStart(myCurrentSketch))
950 myCurrentSketch->setDisplayed(true);
952 myCurrentSketch = CompositeFeaturePtr();
954 myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
955 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
957 Events_Loop::loop()->flush(aDispEvent);
959 // restore the module selection modes, which were changed on startSketch
960 aConnector->activateModuleSelectionModes();
961 aConnector->workshop()->displayer()->activateTrihedron(false);
964 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
966 if (canChangeCursor(theOperation) && myIsMouseOverWindow) {
967 QCursor* aCurrentCursor = QApplication::overrideCursor();
968 if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
969 QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
971 qDebug("startNestedSketch() : Qt::CrossCursor");
977 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
979 myIsMouseOverViewProcessed = true;
980 operationMgr()->onValidateOperation();
981 if (canChangeCursor(theOperation)) {
982 QApplication::restoreOverrideCursor();
984 qDebug("stopNestedSketch() : None");
989 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
991 if (isNestedCreateOperation(theOperation)) {
992 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
995 FeaturePtr aFeature = aFOperation->feature();
996 // it is necessary to check the the feature data validity because
997 // some kind of features are removed by an operation commit(the macro state of a feature)
998 if (aFeature.get() && aFeature->data()->isValid()) {
999 visualizeFeature(aFeature, aFOperation->isEditOperation(), true);
1005 void PartSet_SketcherMgr::activatePlaneFilter(const bool& toActivate)
1008 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
1010 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
1013 bool PartSet_SketcherMgr::operationActivatedByPreselection()
1015 bool isOperationStopped = false;
1016 ModuleBase_Operation* anOperation = getCurrentOperation();
1017 if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
1018 // Set final definitions if they are necessary
1019 //propertyPanelDefined(aOperation);
1020 /// Commit sketcher operations automatically
1021 /// distance operation are able to show popup editor to modify the distance value
1022 /// after entering the value, the operation should be committed/aborted(by Esc key)
1023 bool aCanCommitOperation = true;
1024 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1026 if (aFOperation && PartSet_SketcherMgr::isDistanceOperation(aFOperation))
1027 aCanCommitOperation = setDistanceValueByPreselection(anOperation, myModule->workshop());
1029 if (aCanCommitOperation)
1030 isOperationStopped = anOperation->commit();
1032 anOperation->abort();
1033 isOperationStopped = true;
1036 return isOperationStopped;
1039 bool PartSet_SketcherMgr::canUndo() const
1041 return isNestedCreateOperation(getCurrentOperation());
1044 bool PartSet_SketcherMgr::canRedo() const
1046 return isNestedCreateOperation(getCurrentOperation());
1049 bool PartSet_SketcherMgr::canEraseObject(const ObjectPtr& theObject) const
1051 bool aCanErase = true;
1052 // when the sketch operation is active, results of sketch sub-feature can not be hidden
1053 if (myCurrentSketch.get()) {
1054 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1055 if (aResult.get()) {
1056 // Display sketcher objects
1057 for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1059 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1060 std::list<ResultPtr> aResults = aFeature->results();
1061 std::list<ResultPtr>::const_iterator anIt;
1062 for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1063 aCanErase = *anIt != aResult;
1071 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
1073 bool aCanDisplay = true;
1075 bool aHasActiveSketch = activeSketch().get() != NULL;
1076 if (aHasActiveSketch) {
1077 // 1. the sketch feature should not be displayed during the sketch active operation
1078 // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
1079 // nested features can be visualized
1080 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1081 if (aFeature.get() != NULL && aFeature == activeSketch()) {
1082 aCanDisplay = false;
1085 else { // there are no an active sketch
1086 // 2. sketch sub-features should not be visualized if the sketch operation is not active
1087 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1088 if (aFeature.get() != NULL) {
1089 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1090 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1091 if (aSketchFeature.get()) {
1092 aCanDisplay = false;
1097 // 3. the method should not filter the objects, which are not related to the current operation.
1098 // The object is filtered just if it is a current operation feature or this feature result
1099 bool isObjectFound = false;
1100 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1101 (getCurrentOperation());
1103 FeaturePtr aFeature = aFOperation->feature();
1104 if (aFeature.get()) {
1105 std::list<ResultPtr> aResults = aFeature->results();
1106 if (theObject == aFeature)
1107 isObjectFound = true;
1109 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1110 for (; anIt != aLast && !isObjectFound; anIt++) {
1111 isObjectFound = *anIt == theObject;
1116 if (isObjectFound) {
1117 // 4. For created nested feature operation do not display the created feature if
1118 // the mouse curstor leaves the OCC window.
1119 // The correction cases, which ignores this condition:
1120 // a. the property panel values modification
1121 // b. the popup menu activated
1122 // c. widget editor control
1123 #ifndef DEBUG_DO_NOT_BY_ENTER
1124 if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
1125 ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1126 ModuleBase_WidgetEditor* anEditorWdg = anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
1127 // the active widget editor should not influence here. The presentation should be visible always
1128 // when this widget is active.
1129 if (!anEditorWdg && !myIsPopupMenuActive) {
1130 // during a nested create operation, the feature is redisplayed only if the mouse over view
1131 // of there was a value modified in the property panel after the mouse left the view
1132 aCanDisplay = canDisplayCurrentCreatedFeature();
1138 // checks the sketcher constraints visibility according to active sketch check box states
1140 bool aProcessed = false;
1141 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1142 if (aFeature.get()) {
1143 bool aConstraintDisplayed = canDisplayConstraint(aFeature, PartSet_Tools::Any, aProcessed);
1145 aCanDisplay = aConstraintDisplayed;
1152 bool PartSet_SketcherMgr::canDisplayConstraint(const FeaturePtr& theFeature,
1153 const PartSet_Tools::ConstraintVisibleState& theState,
1154 bool& isProcessed) const
1156 bool aSwitchedOn = true;
1158 const QStringList& aConstrIds = constraintsIdList();
1160 std::string aKind = theFeature->getKind();
1161 if (aConstrIds.contains(QString(aKind.c_str()))) {
1162 bool isTypedConstraint = false;
1165 case PartSet_Tools::Dimensional: {
1166 bool isDistance = isDistanceKind(aKind);
1169 aSwitchedOn = myIsConstraintsShown[theState];
1173 case PartSet_Tools::Geometrical: {
1174 bool isGeometrical = !isDistanceKind(aKind);
1175 if (isGeometrical) {
1177 aSwitchedOn = myIsConstraintsShown[theState];
1181 case PartSet_Tools::Any: {
1183 bool isDistance = isDistanceKind(aKind);
1185 aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Dimensional];
1187 aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Geometrical];
1197 void PartSet_SketcherMgr::processHiddenObject(const std::list<ObjectPtr>& theObjects)
1199 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1200 (getCurrentOperation());
1201 if (aFOperation && myCurrentSketch.get()) {
1202 // find results of the current operation
1203 // these results should not be proposed to be deleted
1204 FeaturePtr anOperationFeature = aFOperation->feature();
1205 std::list<ResultPtr> anOperationResultList = anOperationFeature->results();
1206 std::set<ResultPtr> anOperationResults;
1207 std::list<ResultPtr>::const_iterator aRIt = anOperationResultList.begin(),
1208 aRLast = anOperationResultList.end();
1209 for (; aRIt != aRLast; aRIt++)
1210 anOperationResults.insert(*aRIt);
1212 std::set<FeaturePtr> anObjectsToBeDeleted;
1213 QStringList anObjectsToBeDeletedNames;
1214 std::list<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1215 for (; anIt != aLast; anIt++) {
1216 ObjectPtr anObject = *anIt;
1217 bool aCanErase = true;
1218 // when the sketch operation is active, results of sketch sub-feature can not be hidden
1219 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1220 // the result is found between current feature results
1221 if (anOperationResults.find(aResult) != anOperationResults.end())
1224 if (aResult.get()) {
1225 // Display sketcher objects
1226 for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1227 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1228 std::list<ResultPtr> aResults = aFeature->results();
1229 std::list<ResultPtr>::const_iterator anIt;
1230 for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1231 aCanErase = *anIt != aResult;
1236 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1237 if (aFeature.get() && anObjectsToBeDeleted.find(aFeature) == anObjectsToBeDeleted.end()) {
1238 anObjectsToBeDeleted.insert(aFeature);
1239 anObjectsToBeDeletedNames.append(aFeature->name().c_str());
1243 if (!anObjectsToBeDeleted.empty()) {
1244 QString aFeatureNames = anObjectsToBeDeletedNames.join(", ");
1245 QString aMessage = tr("The following features have incorrect presentation and \
1246 will be hidden: %1. Would you like to delete them?")
1247 .arg(aFeatureNames);
1248 int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Features hide"),
1249 aMessage, QMessageBox::Ok | QMessageBox::Cancel,
1250 QMessageBox::Cancel);
1251 if (anAnswer == QMessageBox::Ok) {
1252 QObjectPtrList anObjects;
1253 std::set<FeaturePtr>::const_iterator anIt = anObjectsToBeDeleted.begin(),
1254 aLast = anObjectsToBeDeleted.end();
1255 for (; anIt != aLast; anIt++)
1256 anObjects.append(*anIt);
1257 SessionPtr aMgr = ModelAPI_Session::get();
1258 DocumentPtr aDoc = aMgr->activeDocument();
1259 bool aIsOp = aMgr->isOperation();
1261 aMgr->startOperation();
1262 workshop()->deleteFeatures(anObjects);
1263 //static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1264 //static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
1265 //Events_Loop::loop()->flush(aDeletedEvent);
1266 //Events_Loop::loop()->flush(aRedispEvent);
1269 aMgr->finishOperation();
1275 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
1277 bool aCanDisplay = myIsMouseOverWindow;
1279 ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1281 aCanDisplay = anActiveWidget->getValueState() == ModuleBase_ModelWidget::Stored;
1286 bool PartSet_SketcherMgr::canChangeCursor(ModuleBase_Operation* theOperation) const
1288 return isNestedCreateOperation(theOperation) ||
1289 myModule->sketchReentranceMgr()->isInternalEditActive();
1292 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& PartSet_SketcherMgr::showConstraintStates()
1294 return myIsConstraintsShown;
1297 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
1299 bool isFoundObject = false;
1301 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
1302 if (anObjectFeature.get()) {
1303 int aSize = myCurrentSketch->numberOfSubs();
1304 for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
1305 FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
1306 isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
1309 return isFoundObject;
1312 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
1314 if (myPlaneFilter.IsNull())
1315 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
1317 myPlaneFilter->setPlane(thePln);
1320 bool PartSet_SketcherMgr::setDistanceValueByPreselection(ModuleBase_Operation* theOperation,
1321 ModuleBase_IWorkshop* theWorkshop)
1323 bool isValueAccepted = false;
1325 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1327 FeaturePtr aFeature = aFOperation->feature();
1328 // editor is shown only if all attribute references are filled by preseletion
1329 bool anAllRefAttrInitialized = true;
1331 std::list<AttributePtr> aRefAttrs = aFeature->data()->attributes(
1332 ModelAPI_AttributeRefAttr::typeId());
1333 std::list<AttributePtr>::const_iterator anIt = aRefAttrs.begin(), aLast = aRefAttrs.end();
1334 for (; anIt != aLast && anAllRefAttrInitialized; anIt++) {
1335 anAllRefAttrInitialized = (*anIt)->isInitialized();
1337 if (anAllRefAttrInitialized) {
1338 // Activate dimension value editing on double click
1339 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
1340 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
1341 // Find corresponded widget to activate value editing
1342 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
1343 if (aWgt->attributeID() == "ConstraintValue") {
1344 // the featue should be displayed in order to find the AIS text position,
1345 // the place where the editor will be shown
1346 aFeature->setDisplayed(true);
1347 /// the execute is necessary to perform in the feature compute for flyout position
1348 aFeature->execute();
1350 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1351 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1353 PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
1355 int aX = 0, anY = 0;
1357 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(theWorkshop);
1358 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1359 AISObjectPtr anAIS = aDisplayer->getAISObject(aFeature);
1360 Handle(AIS_InteractiveObject) anAISIO;
1361 if (anAIS.get() != NULL) {
1362 anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1364 if (anAIS.get() != NULL) {
1365 Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1367 if (!anAISIO.IsNull()) {
1368 Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAISIO);
1369 if (!aDim.IsNull()) {
1370 gp_Pnt aPosition = aDim->GetTextPosition();
1372 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
1373 Handle(V3d_View) aView = aViewer->activeView();
1375 aView->Convert(aPosition.X(), aPosition.Y(), aPosition.Z(), aCX, aCY);
1377 QWidget* aViewPort = aViewer->activeViewPort();
1378 QPoint aGlPoint = aViewPort->mapToGlobal(QPoint(aCX, aCY));
1383 anEditor->setCursorPosition(aX, anY);
1384 isValueAccepted = anEditor->showPopupEditor(false);
1390 return isValueAccepted;
1393 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
1394 const FeaturePtr& theSketch,
1395 ModuleBase_IWorkshop* theWorkshop,
1396 FeatureToSelectionMap& theSelection)
1398 if (theFeature.get() == NULL)
1401 std::set<AttributePtr> aSelectedAttributes;
1402 std::set<ResultPtr> aSelectedResults;
1404 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1405 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1406 if (!aContext.IsNull()) {
1407 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1408 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1410 std::list<ResultPtr> aResults = theFeature->results();
1411 std::list<ResultPtr>::const_iterator aIt;
1412 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1414 ResultPtr aResult = *aIt;
1415 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1416 if (aAISObj.get() == NULL)
1418 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1419 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
1421 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1422 if (anOwner->Selectable() != anAISIO)
1424 getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1425 aSelectedAttributes, aSelectedResults);
1427 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
1428 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
1429 if (anOwner.IsNull())
1431 if (anOwner->Selectable() != anAISIO)
1433 getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1434 aSelectedAttributes, aSelectedResults);
1438 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
1441 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1442 const FeaturePtr& theSketch,
1443 ModuleBase_IWorkshop* theWorkshop,
1444 const FeatureToSelectionMap& theSelection,
1445 SelectMgr_IndexedMapOfOwner& theOwnersToSelect)
1447 if (theFeature.get() == NULL)
1450 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1451 std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1452 std::set<ResultPtr> aSelectedResults = anIt.value().second;
1454 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1456 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1457 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1459 // 1. found the feature's owners. Check the AIS objects of the constructions
1460 AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1461 if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1462 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1464 SelectMgr_IndexedMapOfOwner aSelectedOwners;
1465 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1466 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1467 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1468 if (!anOwner.IsNull())
1469 theOwnersToSelect.Add(anOwner);
1473 // 2. found the feature results's owners
1474 std::list<ResultPtr> aResults = theFeature->results();
1475 std::list<ResultPtr>::const_iterator aIt;
1476 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1478 ResultPtr aResult = *aIt;
1479 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1480 if (aAISObj.get() == NULL)
1482 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1484 SelectMgr_IndexedMapOfOwner aSelectedOwners;
1485 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1486 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1487 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1488 if ( anOwner.IsNull() || !anOwner->HasShape() )
1490 const TopoDS_Shape& aShape = anOwner->Shape();
1491 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1492 if (aShapeType == TopAbs_VERTEX) {
1493 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1494 if (aPntAttr.get() != NULL &&
1495 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1496 theOwnersToSelect.Add(anOwner);
1499 else if (aShapeType == TopAbs_EDGE) {
1500 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1501 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1502 theOwnersToSelect.FindIndex(anOwner) <= 0)
1503 theOwnersToSelect.Add(anOwner);
1509 void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
1512 connect(theWidget, SIGNAL(beforeValuesChanged()),
1513 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1514 connect(theWidget, SIGNAL(afterValuesChanged()),
1515 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1518 disconnect(theWidget, SIGNAL(beforeValuesChanged()),
1519 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1520 disconnect(theWidget, SIGNAL(afterValuesChanged()),
1521 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1525 void PartSet_SketcherMgr::widgetStateChanged(int thePreviousState)
1527 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1528 (getCurrentOperation());
1530 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) ||
1531 PartSet_SketcherMgr::isNestedSketchOperation(aFOperation) &&
1532 thePreviousState == ModuleBase_ModelWidget::ModifiedInPP) {
1533 FeaturePtr aFeature = aFOperation->feature();
1534 visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
1539 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1541 return myModule->workshop()->currentOperation();
1544 //**************************************************************
1545 ModuleBase_ModelWidget* PartSet_SketcherMgr::getActiveWidget() const
1547 ModuleBase_ModelWidget* aWidget = 0;
1548 ModuleBase_Operation* anOperation = getCurrentOperation();
1550 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
1552 aWidget = aPanel->activeWidget();
1557 void PartSet_SketcherMgr::visualizeFeature(const FeaturePtr& theFeature,
1558 const bool isEditOperation,
1559 const bool isToDisplay,
1560 const bool isFlushRedisplay)
1562 #ifdef DEBUG_DO_NOT_BY_ENTER
1566 if (isEditOperation || !theFeature.get())
1569 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1570 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1572 // 1. change visibility of the object itself, here the presentable object is processed,
1573 // e.g. constraints features
1574 //FeaturePtr aFeature = aFOperation->feature();
1575 std::list<ResultPtr> aResults = theFeature->results();
1577 theFeature->setDisplayed(true);
1579 theFeature->setDisplayed(false);
1581 // change visibility of the object results, e.g. non-constraint features
1582 std::list<ResultPtr>::const_iterator aIt;
1583 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1585 (*aIt)->setDisplayed(true);
1588 (*aIt)->setDisplayed(false);
1591 if (isFlushRedisplay)
1592 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1595 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1597 if (!myCurrentSketch.get())
1600 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1601 ModuleBase_ISelection* aSelect = aWorkshop->selection();
1602 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1604 QList<FeaturePtr> aFeatureList;
1605 if (theHighlightedOnly) {
1606 fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1609 fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1611 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected(ModuleBase_ISelection::AllControls);
1612 fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1615 // 1. it is necessary to save current selection in order to restore it after the features moving
1616 myCurrentSelection.clear();
1617 QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1618 for (; anIt != aLast; anIt++) {
1619 getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1621 //qDebug(QString(" storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1624 void PartSet_SketcherMgr::restoreSelection()
1626 if (!myCurrentSketch.get())
1629 //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1630 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1631 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1632 FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1633 aSLast = myCurrentSelection.end();
1634 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1635 for (; aSIt != aSLast; aSIt++) {
1636 anOwnersToSelect.Clear();
1637 getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1639 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1643 void PartSet_SketcherMgr::onShowConstraintsToggle(int theType, bool theState)
1645 PartSet_Tools::ConstraintVisibleState aType = (PartSet_Tools::ConstraintVisibleState)theType;
1646 if (myIsConstraintsShown.contains(aType) && myIsConstraintsShown[aType] == theState)
1648 if (myCurrentSketch.get() == NULL)
1651 myIsConstraintsShown[aType] = theState;
1653 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1654 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1656 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1657 FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1658 bool aProcessed = false;
1659 bool aConstraintDisplayed = canDisplayConstraint(aSubFeature, aType, aProcessed);
1661 aSubFeature->setDisplayed(aConstraintDisplayed);
1663 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1666 XGUI_Workshop* PartSet_SketcherMgr::workshop() const
1668 ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1669 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1670 return aConnector->workshop();
1673 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
1675 return workshop()->operationMgr();