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 <GeomDataAPI_Point2D.h>
28 #include <Events_Loop.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Sketch.h>
32 #include <SketchPlugin_Point.h>
33 #include <SketchPlugin_Arc.h>
34 #include <SketchPlugin_Circle.h>
35 #include <SketchPlugin_ConstraintLength.h>
36 #include <SketchPlugin_ConstraintDistance.h>
37 #include <SketchPlugin_ConstraintParallel.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
39 #include <SketchPlugin_ConstraintRadius.h>
40 #include <SketchPlugin_ConstraintRigid.h>
42 #include <SelectMgr_IndexedMapOfOwner.hxx>
43 #include <StdSelect_BRepOwner.hxx>
45 #include <ModelAPI_Events.h>
47 #include <QMouseEvent>
48 #include <QApplication>
51 /// Returns list of unique objects by sum of objects from List1 and List2
52 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
53 const QList<ModuleBase_ViewerPrs>& theList2)
55 QList<ModuleBase_ViewerPrs> aRes;
56 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
57 if (!aRes.contains(aPrs))
60 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
61 if (!aRes.contains(aPrs))
67 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
68 QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
69 const FeaturePtr theSketch)
71 QList<ModuleBase_ViewerPrs> aRes;
73 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
74 aLast = theList.end();
75 for (; anIt != aLast; anIt++)
77 ModuleBase_ViewerPrs aPrs = *anIt;
78 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
79 if (aFeature.get() == NULL)
82 QList<AttributePtr> anAttributes;
83 if (theFeature2AttributeMap.contains(aFeature)) {
84 anAttributes = theFeature2AttributeMap[aFeature];
87 TopoDS_Shape aShape = aPrs.shape();
88 if (!aShape.IsNull()) {
89 if (aShape.ShapeType() == TopAbs_VERTEX) {
90 anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
91 if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
92 anAttributes.push_back(anAttr);
95 theFeature2AttributeMap[aFeature] = anAttributes;
102 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
103 : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false)
105 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
106 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
108 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
109 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
111 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
112 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
114 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
115 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
117 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
118 this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
121 PartSet_SketcherMgr::~PartSet_SketcherMgr()
123 if (!myPlaneFilter.IsNull())
124 myPlaneFilter.Nullify();
127 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
130 if (!(theEvent->buttons() & Qt::LeftButton))
133 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
134 ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
135 // Use only for sketch operations
136 if (aOperation && myCurrentSketch) {
137 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
140 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
141 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
143 // Avoid non-sketch operations
144 if ((!isSketchOpe) && (!isSketcher))
147 bool isEditing = aOperation->isEditOperation();
149 // Ignore creation sketch operation
150 if ((!isSketcher) && (!isEditing))
153 // MoveTo in order to highlight current object
154 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
155 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
157 // Remember highlighted objects for editing
158 ModuleBase_ISelection* aSelect = aWorkshop->selection();
159 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
160 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
161 myFeature2AttributeMap.clear();
163 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
165 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
166 fillFeature2Attribute(aSelected, myFeature2AttributeMap, myCurrentSketch);
169 fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
172 if (myFeature2AttributeMap.empty()) {
173 if (isSketchOpe && (!isSketcher))
174 // commit previous operation
175 if (!aOperation->commit())
182 get2dPoint(theWnd, theEvent, myCurX, myCurY);
184 aWorkshop->viewer()->enableMultiselection(false);
187 } else if (isSketchOpe && isEditing) {
188 // If selected another object commit current result
189 aOperation->commit();
192 get2dPoint(theWnd, theEvent, myCurX, myCurY);
194 aWorkshop->viewer()->enableMultiselection(false);
196 // This is necessary in order to finalize previous operation
197 QApplication::processEvents();
203 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
205 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
206 ModuleBase_Operation* aOp = aWorkshop->currentOperation();
209 if (!sketchOperationIdList().contains(aOp->id()))
212 // Only for sketcher operations
213 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
215 myIsDragging = false;
217 aViewer->enableMultiselection(true);
219 myFeature2AttributeMap.clear();
221 // Reselect edited object
222 aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
223 if (theEvent->modifiers() & Qt::ShiftModifier)
224 aViewer->AISContext()->ShiftSelect();
226 aViewer->AISContext()->Select();
230 if (!aViewer->isMultiSelectionEnabled()) {
231 aViewer->enableMultiselection(true);
235 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
238 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
239 if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
240 return; // No edit operation activated
242 Handle(V3d_View) aView = theWnd->v3dView();
243 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
245 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
246 double dX = aX - myCurX;
247 double dY = aY - myCurY;
249 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
250 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
251 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
252 bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
254 static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
255 //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
257 FeatureToAttributesMap::const_iterator anIt = myFeature2AttributeMap.begin(),
258 aLast = myFeature2AttributeMap.end();
259 FeatureToSelectionMap aCurrentSelection;
260 for (; anIt != aLast; anIt++) {
261 FeaturePtr aFeature = anIt.key();
264 AttributeList anAttributes = anIt.value();
265 if (!anAttributes.empty()) {
266 anAttr = anAttributes.first();
269 // save the previous selection
270 getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, aCurrentSelection);
271 // save the previous selection: end
273 // Process selection by attribute: the priority to the attribute
274 if (anAttr.get() != NULL) {
275 std::string aAttrId = anAttr->id();
276 DataPtr aData = aFeature->data();
277 if (aData.get() != NULL) {
278 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
279 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
280 if (aPoint.get() != NULL) {
281 bool isImmutable = aPoint->setImmutable(true);
282 aPoint->move(dX, dY);
283 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
284 aPoint->setImmutable(isImmutable);
288 // Process selection by feature
289 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
290 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
291 if (aSketchFeature) {
292 aSketchFeature->move(dX, dY);
293 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
297 Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
298 //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
300 // restore the previous selection
301 FeatureToSelectionMap::const_iterator aSIt = aCurrentSelection.begin(),
302 aSLast = aCurrentSelection.end();
303 SelectMgr_IndexedMapOfOwner anOwnersToSelect;
304 for (; aSIt != aSLast; aSIt++) {
305 anOwnersToSelect.Clear();
306 getSelectionOwners(aSIt->first, myCurrentSketch, aWorkshop, aCurrentSelection,
308 aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
310 // restore the previous selection: end
312 aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
313 aDisplayer->updateViewer();
320 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
322 ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
323 if (aOperation && aOperation->isEditOperation()) {
324 std::string aId = aOperation->id().toStdString();
325 if ((aId == SketchPlugin_ConstraintLength::ID()) ||
326 (aId == SketchPlugin_ConstraintDistance::ID()) ||
327 (aId == SketchPlugin_ConstraintRadius::ID()))
329 // Activate dimension value editing on double click
330 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
331 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
332 // Find corresponded widget to activate value editing
333 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
334 if (aWgt->attributeID() == "ConstraintValue") {
343 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
344 double& theX, double& theY)
346 Handle(V3d_View) aView = theWnd->v3dView();
347 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
348 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
351 void PartSet_SketcherMgr::launchEditing()
353 // there should be activate the vertex selection mode because the edit can happens by the selected
356 aModes << TopAbs_VERTEX << TopAbs_EDGE;
357 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
358 aConnector->activateSubShapesSelection(aModes);
360 if (!myFeature2AttributeMap.empty()) {
361 FeaturePtr aFeature = myFeature2AttributeMap.begin().key();
362 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
363 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
365 myModule->editFeature(aSPFeature);
372 QStringList PartSet_SketcherMgr::sketchOperationIdList()
374 static QStringList aIds;
375 if (aIds.size() == 0) {
376 aIds << SketchPlugin_Line::ID().c_str();
377 aIds << SketchPlugin_Point::ID().c_str();
378 aIds << SketchPlugin_Arc::ID().c_str();
379 aIds << SketchPlugin_Circle::ID().c_str();
380 aIds << SketchPlugin_ConstraintLength::ID().c_str();
381 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
382 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
383 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
384 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
385 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
390 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
392 // Display all sketcher sub-Objects
393 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
394 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
395 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
397 // Hide sketcher result
398 std::list<ResultPtr> aResults = myCurrentSketch->results();
399 std::list<ResultPtr>::const_iterator aIt;
400 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
401 aDisplayer->erase((*aIt), false);
403 aDisplayer->erase(myCurrentSketch, false);
405 // Display sketcher objects
406 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
407 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
408 std::list<ResultPtr> aResults = aFeature->results();
409 std::list<ResultPtr>::const_iterator aIt;
410 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
411 aDisplayer->display((*aIt), false);
413 aDisplayer->display(aFeature, false);
416 if (myPlaneFilter.IsNull())
417 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
419 myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
420 if (theOperation->isEditOperation()) {
421 // If it is editing of sketch then it means that plane is already defined
422 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
423 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
425 aDisplayer->updateViewer();
428 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
430 DataPtr aData = myCurrentSketch->data();
431 if ((!aData) || (!aData->isValid())) {
432 // The sketch was aborted
433 myCurrentSketch = CompositeFeaturePtr();
434 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
437 // Hide all sketcher sub-Objects
438 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
439 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
440 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
441 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
442 std::list<ResultPtr> aResults = aFeature->results();
443 std::list<ResultPtr>::const_iterator aIt;
444 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
445 aDisplayer->erase((*aIt), false);
447 aDisplayer->erase(aFeature, false);
449 // Display sketcher result
450 std::list<ResultPtr> aResults = myCurrentSketch->results();
451 std::list<ResultPtr>::const_iterator aIt;
452 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
453 aDisplayer->display((*aIt), false);
455 aDisplayer->display(myCurrentSketch);
457 myCurrentSketch = CompositeFeaturePtr();
458 myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
459 aDisplayer->updateViewer();
463 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
465 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
468 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
469 const FeaturePtr& theSketch,
470 ModuleBase_IWorkshop* theWorkshop,
471 FeatureToSelectionMap& theSelection)
472 //std::set<AttributePtr>& theSelectedAttributes,
473 //std::set<ResultPtr>& theSelectedResults)
475 if (theFeature.get() == NULL)
478 std::set<AttributePtr> aSelectedAttributes;
479 std::set<ResultPtr> aSelectedResults;
481 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
482 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
483 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
484 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
486 std::list<ResultPtr> aResults = theFeature->results();
487 std::list<ResultPtr>::const_iterator aIt;
488 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
490 ResultPtr aResult = *aIt;
491 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
492 if (aAISObj.get() == NULL)
494 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
495 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
497 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
498 aContext->SelectedOwner());
499 if (aBRepOwner.IsNull())
501 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
502 aBRepOwner->Selectable());
503 if (aBRepOwner->HasShape()) {
504 const TopoDS_Shape& aShape = aBRepOwner->Shape();
505 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
506 if (aShapeType == TopAbs_VERTEX) {
507 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
509 if (aPntAttr.get() != NULL)
510 aSelectedAttributes.insert(aPntAttr);
512 else if (aShapeType == TopAbs_EDGE &&
513 aSelectedResults.find(aResult) == aSelectedResults.end()) {
514 aSelectedResults.insert(aResult);
519 theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
522 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
523 const FeaturePtr& theSketch,
524 ModuleBase_IWorkshop* theWorkshop,
525 //const std::set<AttributePtr>& theSelectedAttributes,
526 //const std::set<ResultPtr>& theSelectedResults,
527 const FeatureToSelectionMap& theSelection,
528 SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
530 if (theFeature.get() == NULL)
533 FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
534 std::set<AttributePtr> aSelectedAttributes = anIt->second.first;
535 std::set<ResultPtr> aSelectedResults = anIt->second.second;
537 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
538 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
539 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
540 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
542 std::list<ResultPtr> aResults = theFeature->results();
543 std::list<ResultPtr>::const_iterator aIt;
544 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
546 ResultPtr aResult = *aIt;
547 AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
548 if (aAISObj.get() == NULL)
550 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
552 SelectMgr_IndexedMapOfOwner aSelectedOwners;
553 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
554 for ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
555 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
556 if ( anOwner.IsNull() || !anOwner->HasShape() )
558 const TopoDS_Shape& aShape = anOwner->Shape();
559 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
560 if (aShapeType == TopAbs_VERTEX) {
561 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
562 if (aPntAttr.get() != NULL &&
563 aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
564 anOwnersToSelect.Add(anOwner);
567 else if (aShapeType == TopAbs_EDGE) {
568 bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
569 if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
570 anOwnersToSelect.FindIndex(anOwner) <= 0)
571 anOwnersToSelect.Add(anOwner);