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_Tools.h"
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_SelectionMgr.h>
18 #include <ModuleBase_IViewer.h>
19 #include <ModuleBase_IWorkshop.h>
20 #include <ModuleBase_IViewWindow.h>
21 #include <ModuleBase_Operation.h>
22 #include <ModuleBase_ISelection.h>
23 #include <ModuleBase_IPropertyPanel.h>
24 #include <ModuleBase_Operation.h>
26 #include <Events_Loop.h>
28 #include <SketchPlugin_Line.h>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Arc.h>
32 #include <SketchPlugin_Circle.h>
33 #include <SketchPlugin_ConstraintLength.h>
34 #include <SketchPlugin_ConstraintDistance.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
40 #include <SelectMgr_IndexedMapOfOwner.hxx>
41 #include <StdSelect_BRepOwner.hxx>
43 #include <ModelAPI_Events.h>
45 #include <QMouseEvent>
46 #include <QApplication>
51 /// Returns list of unique objects by sum of objects from List1 and List2
52 QList<ObjectPtr> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
53 const QList<ModuleBase_ViewerPrs>& theList2)
55 QList<ObjectPtr> aRes;
56 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
57 if (!aRes.contains(aPrs.object()))
58 aRes.append(aPrs.object());
60 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
61 if (!aRes.contains(aPrs.object()))
62 aRes.append(aPrs.object());
70 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
71 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false)
73 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
74 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
76 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
77 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
79 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
80 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
82 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
83 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
85 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
86 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
89 PartSet_SketcherMgr::~PartSet_SketcherMgr()
91 if (!myPlaneFilter.IsNull())
92 myPlaneFilter.Nullify();
95 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
98 if (!(theEvent->buttons() & Qt::LeftButton))
101 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
102 ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
103 // Use only for sketch operations
104 if (aOperation && myCurrentSketch) {
105 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
108 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
109 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
111 // Avoid non-sketch operations
112 if ((!isSketchOpe) && (!isSketcher))
115 bool isEditing = aOperation->isEditOperation();
117 // Ignore creation sketch operation
118 if ((!isSketcher) && (!isEditing))
121 if (theEvent->modifiers()) {
122 // If user performs multiselection
123 if (isSketchOpe /* && (!isSketcher)*/)
124 if (!aOperation->commit())
129 // MoveTo in order to highlight current object
130 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
131 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
133 // Remember highlighted objects for editing
134 ModuleBase_ISelection* aSelect = aWorkshop->selection();
135 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
136 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
137 myEditingFeatures.clear();
138 myEditingAttr.clear();
140 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
141 QObjectPtrList aSelObjects;
143 aSelObjects = getSumList(aHighlighted, aSelected);
145 foreach (ModuleBase_ViewerPrs aPrs, aHighlighted) {
146 aSelObjects.append(aPrs.object());
149 if ((aSelObjects.size() == 0)) {
150 if (isSketchOpe && (!isSketcher))
151 // commit previous operation
152 if (!aOperation->commit())
156 if (aSelObjects.size() == 1) {
157 // Move by selected shape (vertex). Can be used only for single selection
158 foreach(ModuleBase_ViewerPrs aPrs, aHighlighted) {
159 FeaturePtr aFeature = ModelAPI_Feature::feature(aHighlighted.first().object());
161 myEditingFeatures.append(aFeature);
162 TopoDS_Shape aShape = aPrs.shape();
163 if (!aShape.IsNull()) {
164 if (aShape.ShapeType() == TopAbs_VERTEX) {
165 AttributePtr aAttr = PartSet_Tools::findAttributeBy2dPoint(myEditingFeatures.first(),
166 aShape, myCurrentSketch);
168 myEditingAttr.append(aAttr);
174 // Provide multi-selection. Can be used only for features
175 foreach (ObjectPtr aObj, aSelObjects) {
176 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
177 if (aFeature && (!myEditingFeatures.contains(aFeature)))
178 myEditingFeatures.append(aFeature);
182 // If nothing highlighted - return
183 if (myEditingFeatures.size() == 0)
188 get2dPoint(theWnd, theEvent, myCurX, myCurY);
190 aWorkshop->viewer()->enableMultiselection(false);
193 } else if (isSketchOpe && isEditing) {
194 // If selected another object commit current result
195 aOperation->commit();
198 get2dPoint(theWnd, theEvent, myCurX, myCurY);
200 aWorkshop->viewer()->enableMultiselection(false);
202 // This is necessary in order to finalize previous operation
203 QApplication::processEvents();
209 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
211 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
212 ModuleBase_Operation* aOp = aWorkshop->currentOperation();
215 if (!sketchOperationIdList().contains(aOp->id()))
218 // Only for sketcher operations
219 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
221 myIsDragging = false;
223 aViewer->enableMultiselection(true);
225 myEditingFeatures.clear();
226 myEditingAttr.clear();
228 // Reselect edited object
229 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
230 if (theEvent->modifiers() & Qt::ShiftModifier)
231 aViewer->AISContext()->ShiftSelect();
233 aViewer->AISContext()->Select();
237 if (!aViewer->isMultiSelectionEnabled()) {
238 aViewer->enableMultiselection(true);
242 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
245 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
246 if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
247 return; // No edit operation activated
249 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
251 Handle(V3d_View) aView = theWnd->v3dView();
252 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
254 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
255 double dX = aX - myCurX;
256 double dY = aY - myCurY;
258 if ((myEditingAttr.size() == 1) && myEditingAttr.first()) {
259 // probably we have prehighlighted point
260 AttributePtr aAttr = myEditingAttr.first();
261 std::string aAttrId = aAttr->id();
262 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
263 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
264 // Find corresponded widget to provide dragging
265 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
266 if (aWgt->attributeID() == aAttrId) {
267 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
269 aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
275 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
276 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
277 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
278 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
280 foreach(FeaturePtr aFeature, myEditingFeatures) {
281 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
282 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
283 if (aSketchFeature) {
284 // save the previous selection
286 std::set<AttributePtr> aSelectedAttributes;
287 std::set<ResultPtr> aSelectedResults;
288 getCurrentSelection(aSketchFeature, myCurrentSketch, aWorkshop, aSelectedAttributes,
290 // save the previous selection: end
292 aSketchFeature->move(dX, dY);
293 // TODO: the selection restore should be after the AIS presentation is rebuilt
294 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
295 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
297 // restore the previous selection
298 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
299 getSelectionOwners(aSketchFeature, myCurrentSketch, aWorkshop, aSelectedAttributes,
300 aSelectedResults, anOwnersToSelect);
301 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
302 // restore the previous selection
304 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent, true);
305 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
308 //Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
309 //Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
311 //Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
312 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
313 aDisplayer->updateViewer();
321 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
323 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
324 if (aOperation && aOperation->isEditOperation()) {
325 std::string aId = aOperation->id().toStdString();
326 if ((aId == SketchPlugin_ConstraintLength::ID()) ||
327 (aId == SketchPlugin_ConstraintDistance::ID()) ||
328 (aId == SketchPlugin_ConstraintRadius::ID()))
330 // Activate dimension value editing on double click
331 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
332 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
333 // Find corresponded widget to activate value editing
334 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
335 if (aWgt->attributeID() == "ConstraintValue") {
344 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
345 double& theX, double& theY)
347 Handle(V3d_View) aView = theWnd->v3dView();
348 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
349 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
352 void PartSet_SketcherMgr::launchEditing()
354 if (myEditingFeatures.size() > 0) {
355 FeaturePtr aFeature = myEditingFeatures.first();
356 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
357 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
359 myModule->editFeature(aSPFeature);
365 QStringList PartSet_SketcherMgr::sketchOperationIdList()
367 static QStringList aIds;
368 if (aIds.size() == 0) {
369 aIds << SketchPlugin_Line::ID().c_str();
370 aIds << SketchPlugin_Point::ID().c_str();
371 aIds << SketchPlugin_Arc::ID().c_str();
372 aIds << SketchPlugin_Circle::ID().c_str();
373 aIds << SketchPlugin_ConstraintLength::ID().c_str();
374 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
375 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
376 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
377 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
378 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
383 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
385 // Display all sketcher sub-Objects
386 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
387 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
388 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
390 // Hide sketcher result
391 std::list<ResultPtr> aResults = myCurrentSketch->results();
392 std::list<ResultPtr>::const_iterator aIt;
393 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
394 aDisplayer->erase((*aIt), false);
396 aDisplayer->erase(myCurrentSketch, false);
398 // Display sketcher objects
399 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
400 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
401 std::list<ResultPtr> aResults = aFeature->results();
402 std::list<ResultPtr>::const_iterator aIt;
403 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
404 aDisplayer->display((*aIt), false);
406 aDisplayer->display(aFeature, false);
409 if (myPlaneFilter.IsNull())
410 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
412 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
413 if (theOperation->isEditOperation()) {
414 // If it is editing of sketch then it means that plane is already defined
415 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
416 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
418 aDisplayer->updateViewer();
421 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
423 DataPtr aData = myCurrentSketch->data();
424 if ((!aData) || (!aData->isValid())) {
425 // The sketch was aborted
426 myCurrentSketch = CompositeFeaturePtr();
427 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
430 // Hide all sketcher sub-Objects
431 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
432 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
433 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
434 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
435 std::list<ResultPtr> aResults = aFeature->results();
436 std::list<ResultPtr>::const_iterator aIt;
437 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
438 aDisplayer->erase((*aIt), false);
440 aDisplayer->erase(aFeature, false);
442 // Display sketcher result
443 std::list<ResultPtr> aResults = myCurrentSketch->results();
444 std::list<ResultPtr>::const_iterator aIt;
445 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
446 aDisplayer->display((*aIt), false);
448 aDisplayer->display(myCurrentSketch);
450 myCurrentSketch = CompositeFeaturePtr();
451 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
452 aDisplayer->updateViewer();
456 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
458 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
461 void PartSet_SketcherMgr::getCurrentSelection(const ObjectPtr& theObject,
462 const FeaturePtr& theSketch,
463 ModuleBase_IWorkshop* theWorkshop,
464 std::set<AttributePtr>& theSelectedAttributes,
465 std::set<ResultPtr>& theSelectedResults)
467 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
468 if (aFeature.get() == NULL)
471 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
472 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
473 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
474 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
476 std::list<ResultPtr> aResults = aFeature->results();
477 std::list<ResultPtr>::const_iterator aIt;
478 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
480 ResultPtr aResult = *aIt;
481 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
482 if (aAISObj.get() == NULL)
484 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
485 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
487 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
488 aContext->SelectedOwner());
489 if (aBRepOwner.IsNull())
491 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
492 aBRepOwner->Selectable());
493 if (aBRepOwner->HasShape()) {
494 const TopoDS_Shape& aShape = aBRepOwner->Shape();
495 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
496 if (aShapeType == TopAbs_VERTEX) {
497 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theObject,
499 if (aPntAttr.get() != NULL)
500 theSelectedAttributes.insert(aPntAttr);
502 else if (aShapeType == TopAbs_EDGE &&
503 theSelectedResults.find(aResult) == theSelectedResults.end()) {
504 theSelectedResults.insert(aResult);
511 void PartSet_SketcherMgr::getSelectionOwners(const ObjectPtr& theObject,
512 const FeaturePtr& theSketch,
513 ModuleBase_IWorkshop* theWorkshop,
514 const std::set<AttributePtr>& theSelectedAttributes,
515 const std::set<ResultPtr>& theSelectedResults,
516 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
518 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
519 if (aFeature.get() == NULL)
522 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
523 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
524 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
525 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
527 std::list<ResultPtr> aResults = aFeature->results();
528 std::list<ResultPtr>::const_iterator aIt;
529 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
531 ResultPtr aResult = *aIt;
532 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
533 if (aAISObj.get() == NULL)
535 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
537 SelectMgr_IndexedMapOfOwner aSelectedOwners;
538 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
539 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
540 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
541 if ( anOwner.IsNull() || !anOwner->HasShape() )
543 const TopoDS_Shape& aShape = anOwner->Shape();
544 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
545 if (aShapeType == TopAbs_VERTEX) {
546 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
547 if (aPntAttr.get() != NULL &&
548 theSelectedAttributes.find(aPntAttr) != theSelectedAttributes.end()) {
549 anOwnersToSelect.Add(anOwner);
552 else if (aShapeType == TopAbs_EDGE) {
553 bool aFound = theSelectedResults.find(aResult) != theSelectedResults.end();
554 int anIndex = anOwnersToSelect.FindIndex(anOwner);
555 if (theSelectedResults.find(aResult) != theSelectedResults.end()/* &&
556 anOwnersToSelect.FindIndex(anOwner) < 0*/)
557 anOwnersToSelect.Add(anOwner);