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 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
153 // If we have coincidence then add Detach menu
154 if (aCoincident.get() != NULL) {
155 mySelectedFeature = aCoincident;
156 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
157 SketchPlugin_ConstraintCoincidence::ENTITY_A());
158 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
159 SketchPlugin_ConstraintCoincidence::ENTITY_B());
160 if (myCoinsideLines.size() > 0) {
162 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
165 foreach (FeaturePtr aCoins, myCoinsideLines) {
166 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
167 aAction->setData(QVariant(i));
170 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
171 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
172 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
178 if ((!aIsDetach) && hasFeature) {
179 theMenu->addAction(theStdActions["DELETE_CMD"]);
184 if (canSetAuxiliary(isAuxiliary)) {
185 QAction* anAction = action("AUXILIARY_CMD");
186 theMenu->addAction(anAction);
187 anAction->setChecked(isAuxiliary);
192 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
194 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
196 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
197 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
198 if (isActiveSketch) {
199 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
200 theStdActions["SHADING_CMD"]->setEnabled(false);
201 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
202 theStdActions["SHOW_CMD"]->setEnabled(false);
203 theStdActions["HIDE_CMD"]->setEnabled(false);
204 theStdActions["HIDEALL_CMD"]->setEnabled(false);
209 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
211 if (myPrevId != -1) {
212 // Restore color for previous object
213 setLineColor(myPrevId, myColor, false);
215 myPrevId = theAction->data().toInt();
216 myColor = setLineColor(myPrevId, Qt::white, true);
219 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
221 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
222 XGUI_Workshop* aWorkshop = aConnector->workshop();
223 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
225 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
226 std::list<ResultPtr>::const_iterator aIt;
227 const std::list<ResultPtr>& aResults = aLine->results();
229 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
230 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
233 aDisplayer->updateViewer();
238 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
239 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
240 QObjectPtrList& theOutList)
242 std::set<AttributePtr>::const_iterator aIt;
243 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
244 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
245 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
246 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
247 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
248 if (aPnt.get() == NULL)
250 gp_Pnt aP = aPnt->impl<gp_Pnt>();
251 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
252 theOutList.append(aConstrFeature);
258 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
260 int aId = theAction->data().toInt();
261 FeaturePtr aLine = myCoinsideLines.at(aId);
262 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
266 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
268 QObjectPtrList aToDelFeatures;
270 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
272 const std::list<ResultPtr>& aResults = aLine->results();
273 std::list<ResultPtr>::const_iterator aResIt;
274 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
275 ResultPtr aResult = (*aResIt);
276 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
277 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
279 if (aToDelFeatures.size() > 0) {
280 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
281 XGUI_Workshop* aWorkshop = aConnector->workshop();
282 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
284 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
285 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
286 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
287 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
288 // the active nested sketch operation should be aborted unconditionally
289 // the Delete action should be additionally granted for the Sketch operation
290 // in order to do not abort/commit it
291 if (!anOpMgr->canStartOperation(tr("Detach")))
292 return; // the objects are processed but can not be deleted
294 anOpMgr->startOperation(anOpAction);
295 aWorkshop->deleteFeatures(aToDelFeatures);
297 anOpMgr->commitOperation();
299 myCoinsideLines.clear();
303 void PartSet_MenuMgr::onDetachMenuHide()
305 if (myPrevId != -1) {
306 // Restore color for previous object
307 setLineColor(myPrevId, myColor, false);
309 // Clear previous definitions
314 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
316 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
318 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
319 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
323 QObjectPtrList anObjects;
324 bool isUseTransaction = false;
325 // 1. change auxiliary type of a created feature
326 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
327 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
328 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
331 anObjects.append(aFOperation->feature());
334 isUseTransaction = true;
335 // 2. change auxiliary type of selected sketch entities
336 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
337 anObjects = aSelection->selectedPresentations();
340 QAction* anAction = action("AUXILIARY_CMD");
341 //SessionPtr aMgr = ModelAPI_Session::get();
342 ModuleBase_OperationAction* anOpAction = 0;
343 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
344 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
345 if (isUseTransaction) {
346 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
347 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
349 if (!anOpMgr->canStartOperation(anOpAction->id()))
350 return; // the objects are processed but can not be deleted
352 anOpMgr->startOperation(anOpAction);
354 myModule->sketchMgr()->storeSelection();
356 if (anObjects.size() > 0) {
357 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
358 for (; anIt != aLast; anIt++) {
359 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
360 if (aFeature.get() != NULL) {
361 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
362 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
363 if (aSketchFeature.get() != NULL) {
364 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
366 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
367 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
369 anAuxiliaryAttr->setValue(isChecked);
374 if (isUseTransaction)
375 anOpMgr->commitOperation();
377 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
378 myModule->sketchMgr()->restoreSelection();
381 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
383 bool anEnabled = false;
384 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
386 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
387 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
391 QObjectPtrList anObjects;
392 // 1. change auxiliary type of a created feature
393 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
394 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
395 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
397 anObjects.append(aFOperation->feature());
400 /// The operation should not be aborted here, because the method does not changed
401 /// the auxilliary state, but checks the possibility to perform this
402 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
403 /// anOperation->abort();
404 // 2. change auxiliary type of selected sketch entities
405 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
406 anObjects = aSelection->selectedPresentations();
409 bool isNotAuxiliaryFound = false;
410 if (anObjects.size() > 0) {
411 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
412 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
413 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
414 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
416 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
417 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
418 if (aSketchFeature.get() != NULL) {
419 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
421 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
422 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
424 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
429 theValue = anObjects.size() && !isNotAuxiliaryFound;
433 void PartSet_MenuMgr::onActivatePart(bool)
435 if (myModule->workshop()->currentOperation())
437 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
438 if (aObjects.size() > 0) {
439 ObjectPtr aObj = aObjects.first();
440 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
442 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
443 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
444 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
452 void PartSet_MenuMgr::onActivatePartSet(bool)
454 if (myModule->workshop()->currentOperation())
459 void PartSet_MenuMgr::activatePartSet() const
461 SessionPtr aMgr = ModelAPI_Session::get();
462 bool isNewTransaction = !aMgr->isOperation();
463 // activation may cause changes in current features in document, so it must be in transaction
464 if (isNewTransaction) aMgr->startOperation("Activation");
465 aMgr->setActiveDocument(aMgr->moduleDocument());
466 if (isNewTransaction) aMgr->finishOperation();
469 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
470 QStringList& theIds) const
472 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
473 theIds.append(tr("Detach"));
474 theIds.append(tr("Auxiliary"));
478 void PartSet_MenuMgr::onEdit(bool)
480 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
481 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
482 if (aFeature == NULL) {
483 ResultParameterPtr aParam =
484 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
485 if (aParam.get() != NULL) {
486 aFeature = ModelAPI_Feature::feature(aParam);
489 if (aFeature.get() != NULL)
490 myModule->editFeature(aFeature);
493 void PartSet_MenuMgr::onSelectParentFeature()
495 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
496 if (aObjects.size() != 1)
499 SessionPtr aMgr = ModelAPI_Session::get();
500 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
504 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
505 QObjectPtrList aSelection;
506 aSelection.append( aParentFeature );
507 myModule->workshop()->selection()->setSelectedObjects( aSelection );