1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_MenuMgr.cpp
4 // Created: 03 April 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_MenuMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_SketcherMgr.h"
10 #include "PartSet_Tools.h"
12 #include <PartSetPlugin_Part.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
17 #include <SketchPlugin_ConstraintCoincidence.h>
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Circle.h>
20 #include <SketchPlugin_Point.h>
21 #include <SketchPlugin_Sketch.h>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_OperationAction.h>
26 #include <ModuleBase_OperationFeature.h>
28 #include <XGUI_ModuleConnector.h>
29 #include <XGUI_Workshop.h>
30 #include <XGUI_Displayer.h>
31 #include <XGUI_DataModel.h>
32 #include <XGUI_OperationMgr.h>
34 #include <Events_Loop.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_ResultPart.h>
38 #include <ModelAPI_ResultParameter.h>
44 #include <BRep_Tool.hxx>
46 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
47 : QObject(theModule), myModule(theModule), myPrevId(-1)
53 QAction* PartSet_MenuMgr::action(const QString& theId) const
55 if (myActions.contains(theId))
56 return myActions[theId];
60 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
62 if (myActions.contains(theId))
63 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
64 theAction->setData(theId);
65 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
66 myActions[theId] = theAction;
69 void PartSet_MenuMgr::createActions()
73 aAction = new QAction(tr("Auxiliary"), this);
74 aAction->setCheckable(true);
75 addAction("AUXILIARY_CMD", aAction);
77 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
78 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
79 myActions["ACTIVATE_PART_CMD"] = aAction;
82 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
83 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
84 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
86 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
87 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
88 myActions["EDIT_CMD"] = aAction;
90 aAction = new QAction(QIcon(), tr("Select parent feature"), this);
91 aAction->setCheckable(false);
92 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
93 myActions["SELECT_PARENT_CMD"] = aAction;
97 void PartSet_MenuMgr::onAction(bool isChecked)
99 QAction* aAction = static_cast<QAction*>(sender());
100 QString anId = aAction->data().toString();
102 if (anId == "AUXILIARY_CMD") {
103 setAuxiliary(isChecked);
107 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
109 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
110 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
111 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
114 myCoinsideLines.clear();
115 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
117 bool aIsDetach = false;
118 bool hasAttribute = false;
119 bool hasFeature = false;
121 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
125 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
126 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
127 if (aResult.get() != NULL) {
128 aShape = aPrs.shape();
129 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
134 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
135 hasFeature = (aFeature.get() != NULL);
139 if (aPrsList.size() == 1) {
140 TopoDS_Shape aShape = aPrsList.first().shape();
141 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
142 // Find 2d coordinates
143 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
144 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
145 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
146 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
147 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
149 // Find coincident in these coordinates
150 ObjectPtr aObj = aPrsList.first().object();
151 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
152 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
153 std::set<AttributePtr>::const_iterator aIt;
154 FeaturePtr aCoincident;
155 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
156 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
157 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
158 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
159 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
160 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
161 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
162 aCoincident = aConstrFeature;
165 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
166 SketchPlugin_ConstraintCoincidence::ENTITY_B());
167 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
168 aCoincident = aConstrFeature;
174 // If we have coincidence then add Detach menu
175 if (aCoincident.get() != NULL) {
176 mySelectedFeature = aCoincident;
177 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
178 SketchPlugin_ConstraintCoincidence::ENTITY_A());
179 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
180 SketchPlugin_ConstraintCoincidence::ENTITY_B());
181 if (myCoinsideLines.size() > 0) {
183 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
186 foreach (FeaturePtr aCoins, myCoinsideLines) {
187 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
188 aAction->setData(QVariant(i));
191 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
192 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
193 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
199 if ((!aIsDetach) && hasFeature) {
200 theMenu->addAction(theStdActions["DELETE_CMD"]);
205 if (canSetAuxiliary(isAuxiliary)) {
206 QAction* anAction = action("AUXILIARY_CMD");
207 theMenu->addAction(anAction);
208 anAction->setChecked(isAuxiliary);
213 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
215 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
217 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
218 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
219 if (isActiveSketch) {
220 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
221 theStdActions["SHADING_CMD"]->setEnabled(false);
222 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
223 theStdActions["SHOW_CMD"]->setEnabled(false);
224 theStdActions["HIDE_CMD"]->setEnabled(false);
225 theStdActions["HIDEALL_CMD"]->setEnabled(false);
230 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
232 if (myPrevId != -1) {
233 // Restore color for previous object
234 setLineColor(myPrevId, myColor, false);
236 myPrevId = theAction->data().toInt();
237 myColor = setLineColor(myPrevId, Qt::white, true);
240 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
242 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
243 XGUI_Workshop* aWorkshop = aConnector->workshop();
244 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
246 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
247 std::list<ResultPtr>::const_iterator aIt;
248 const std::list<ResultPtr>& aResults = aLine->results();
250 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
251 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
254 aDisplayer->updateViewer();
259 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
260 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
261 QObjectPtrList& theOutList)
263 std::set<AttributePtr>::const_iterator aIt;
264 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
265 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
266 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
267 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
268 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
269 if (aPnt.get() == NULL)
271 gp_Pnt aP = aPnt->impl<gp_Pnt>();
272 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
273 theOutList.append(aConstrFeature);
279 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
281 int aId = theAction->data().toInt();
282 FeaturePtr aLine = myCoinsideLines.at(aId);
283 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
287 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
289 QObjectPtrList aToDelFeatures;
291 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
293 const std::list<ResultPtr>& aResults = aLine->results();
294 std::list<ResultPtr>::const_iterator aResIt;
295 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
296 ResultPtr aResult = (*aResIt);
297 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
298 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
300 if (aToDelFeatures.size() > 0) {
301 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
302 XGUI_Workshop* aWorkshop = aConnector->workshop();
303 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
305 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
306 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
307 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
308 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
309 // the active nested sketch operation should be aborted unconditionally
310 // the Delete action should be additionally granted for the Sketch operation
311 // in order to do not abort/commit it
312 if (!anOpMgr->canStartOperation(tr("Detach")))
313 return; // the objects are processed but can not be deleted
315 anOpMgr->startOperation(anOpAction);
316 aWorkshop->deleteFeatures(aToDelFeatures);
318 anOpMgr->commitOperation();
320 myCoinsideLines.clear();
324 void PartSet_MenuMgr::onDetachMenuHide()
326 if (myPrevId != -1) {
327 // Restore color for previous object
328 setLineColor(myPrevId, myColor, false);
330 // Clear previous definitions
335 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
337 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
339 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
340 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
344 QObjectPtrList anObjects;
345 bool isUseTransaction = false;
346 // 1. change auxiliary type of a created feature
347 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
348 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
349 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
352 anObjects.append(aFOperation->feature());
355 isUseTransaction = true;
356 // 2. change auxiliary type of selected sketch entities
357 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
358 anObjects = aSelection->selectedPresentations();
361 QAction* anAction = action("AUXILIARY_CMD");
362 //SessionPtr aMgr = ModelAPI_Session::get();
363 ModuleBase_OperationAction* anOpAction = 0;
364 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
365 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
366 if (isUseTransaction) {
367 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
368 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
370 if (!anOpMgr->canStartOperation(anOpAction->id()))
371 return; // the objects are processed but can not be deleted
373 anOpMgr->startOperation(anOpAction);
375 myModule->sketchMgr()->storeSelection();
377 if (anObjects.size() > 0) {
378 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
379 for (; anIt != aLast; anIt++) {
380 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
381 if (aFeature.get() != NULL) {
382 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
383 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
384 if (aSketchFeature.get() != NULL) {
385 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
387 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
388 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
390 anAuxiliaryAttr->setValue(isChecked);
395 if (isUseTransaction)
396 anOpMgr->commitOperation();
398 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
399 myModule->sketchMgr()->restoreSelection();
402 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
404 bool anEnabled = false;
405 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
407 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
408 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
412 QObjectPtrList anObjects;
413 // 1. change auxiliary type of a created feature
414 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
415 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
416 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
418 anObjects.append(aFOperation->feature());
421 /// The operation should not be aborted here, because the method does not changed
422 /// the auxilliary state, but checks the possibility to perform this
423 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
424 /// anOperation->abort();
425 // 2. change auxiliary type of selected sketch entities
426 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
427 anObjects = aSelection->selectedPresentations();
430 bool isNotAuxiliaryFound = false;
431 if (anObjects.size() > 0) {
432 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
433 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
434 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
435 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
437 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
438 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
439 if (aSketchFeature.get() != NULL) {
440 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
442 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
443 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
445 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
450 theValue = anObjects.size() && !isNotAuxiliaryFound;
454 void PartSet_MenuMgr::onActivatePart(bool)
456 if (myModule->workshop()->currentOperation())
458 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
459 if (aObjects.size() > 0) {
460 ObjectPtr aObj = aObjects.first();
461 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
463 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
464 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
465 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
473 void PartSet_MenuMgr::onActivatePartSet(bool)
475 if (myModule->workshop()->currentOperation())
480 void PartSet_MenuMgr::activatePartSet() const
482 SessionPtr aMgr = ModelAPI_Session::get();
483 bool isNewTransaction = !aMgr->isOperation();
484 // activation may cause changes in current features in document, so it must be in transaction
485 if (isNewTransaction) aMgr->startOperation("Activation");
486 aMgr->setActiveDocument(aMgr->moduleDocument());
487 if (isNewTransaction) aMgr->finishOperation();
490 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
491 QStringList& theIds) const
493 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
494 theIds.append(tr("Detach"));
495 theIds.append(tr("Auxiliary"));
499 void PartSet_MenuMgr::onEdit(bool)
501 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
502 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
503 if (aFeature == NULL) {
504 ResultParameterPtr aParam =
505 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
506 if (aParam.get() != NULL) {
507 aFeature = ModelAPI_Feature::feature(aParam);
510 if (aFeature.get() != NULL)
511 myModule->editFeature(aFeature);
514 void PartSet_MenuMgr::onSelectParentFeature()
516 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
517 if (aObjects.size() != 1)
520 SessionPtr aMgr = ModelAPI_Session::get();
521 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
525 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
526 QObjectPtrList aSelection;
527 aSelection.append( aParentFeature );
528 myModule->workshop()->selection()->setSelectedObjects( aSelection );