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_ObjectsBrowser.h>
33 #include <XGUI_OperationMgr.h>
35 #include <Events_Loop.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_ResultPart.h>
39 #include <ModelAPI_ResultParameter.h>
45 #include <BRep_Tool.hxx>
47 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
48 : QObject(theModule), myModule(theModule), myPrevId(-1)
54 QAction* PartSet_MenuMgr::action(const QString& theId) const
56 if (myActions.contains(theId))
57 return myActions[theId];
61 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
63 if (myActions.contains(theId))
64 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
65 theAction->setData(theId);
66 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
67 myActions[theId] = theAction;
70 void PartSet_MenuMgr::createActions()
74 aAction = new QAction(tr("Auxiliary"), this);
75 aAction->setCheckable(true);
76 addAction("AUXILIARY_CMD", aAction);
78 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
79 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
80 myActions["ACTIVATE_PART_CMD"] = aAction;
82 aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
83 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
84 myActions["DEACTIVATE_PART_CMD"] = aAction;
87 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
88 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
89 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
91 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
92 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
93 myActions["EDIT_CMD"] = aAction;
95 aAction = new QAction(QIcon(), tr("Select parent feature"), this);
96 aAction->setCheckable(false);
97 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
98 myActions["SELECT_PARENT_CMD"] = aAction;
102 void PartSet_MenuMgr::onAction(bool isChecked)
104 QAction* aAction = static_cast<QAction*>(sender());
105 QString anId = aAction->data().toString();
107 if (anId == "AUXILIARY_CMD") {
108 setAuxiliary(isChecked);
112 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
114 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
115 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
116 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
119 myCoinsideLines.clear();
120 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
122 bool aIsDetach = false;
123 bool hasAttribute = false;
124 bool hasFeature = false;
126 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
130 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
131 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
132 if (aResult.get() != NULL) {
133 aShape = aPrs.shape();
134 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
139 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
140 hasFeature = (aFeature.get() != NULL);
144 if (aPrsList.size() == 1) {
145 TopoDS_Shape aShape = aPrsList.first().shape();
146 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
147 // Find 2d coordinates
148 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
149 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
150 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
151 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
152 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
154 // Find coincident in these coordinates
155 ObjectPtr aObj = aPrsList.first().object();
156 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
157 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
158 std::set<AttributePtr>::const_iterator aIt;
159 FeaturePtr aCoincident;
160 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
161 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
162 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
163 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
164 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
165 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
166 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
167 aCoincident = aConstrFeature;
170 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
171 SketchPlugin_ConstraintCoincidence::ENTITY_B());
172 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
173 aCoincident = aConstrFeature;
179 // If we have coincidence then add Detach menu
180 if (aCoincident.get() != NULL) {
181 mySelectedFeature = aCoincident;
182 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
183 SketchPlugin_ConstraintCoincidence::ENTITY_A());
184 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
185 SketchPlugin_ConstraintCoincidence::ENTITY_B());
186 if (myCoinsideLines.size() > 0) {
188 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
191 foreach (FeaturePtr aCoins, myCoinsideLines) {
192 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
193 aAction->setData(QVariant(i));
196 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
197 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
198 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
204 if ((!aIsDetach) && hasFeature) {
205 theMenu->addAction(theStdActions["DELETE_CMD"]);
210 if (canSetAuxiliary(isAuxiliary)) {
211 QAction* anAction = action("AUXILIARY_CMD");
212 theMenu->addAction(anAction);
213 anAction->setChecked(isAuxiliary);
218 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
220 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
222 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
223 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
224 if (isActiveSketch) {
225 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
226 theStdActions["SHADING_CMD"]->setEnabled(false);
227 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
228 theStdActions["SHOW_CMD"]->setEnabled(false);
229 theStdActions["HIDE_CMD"]->setEnabled(false);
230 theStdActions["HIDEALL_CMD"]->setEnabled(false);
235 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
237 if (myPrevId != -1) {
238 // Restore color for previous object
239 setLineColor(myPrevId, myColor, false);
241 myPrevId = theAction->data().toInt();
242 myColor = setLineColor(myPrevId, Qt::white, true);
245 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
247 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
248 XGUI_Workshop* aWorkshop = aConnector->workshop();
249 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
251 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
252 std::list<ResultPtr>::const_iterator aIt;
253 const std::list<ResultPtr>& aResults = aLine->results();
255 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
256 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
259 aDisplayer->updateViewer();
264 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
266 int aId = theAction->data().toInt();
267 FeaturePtr aLine = myCoinsideLines.at(aId);
268 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
269 SketchPlugin_ConstraintCoincidence::ENTITY_A());
270 if (aOrig.get() == NULL)
271 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
272 SketchPlugin_ConstraintCoincidence::ENTITY_B());
274 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
275 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
277 QObjectPtrList aToDelFeatures;
278 std::set<AttributePtr>::const_iterator aIt;
279 // Find all coincedences corresponded to the selected line in the selected point
280 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
281 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
282 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
283 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
284 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
285 SketchPlugin_ConstraintCoincidence::ENTITY_A());
286 if (aPnt.get() == NULL)
287 aPnt = PartSet_Tools::getPoint(aConstrFeature,
288 SketchPlugin_ConstraintCoincidence::ENTITY_B());
289 if (aPnt.get() == NULL)
291 gp_Pnt aP = aPnt->impl<gp_Pnt>();
292 if (aOrig->isEqual(aPnt)) {
293 aToDelFeatures.append(aConstrFeature);
295 aPnt = PartSet_Tools::getPoint(aConstrFeature,
296 SketchPlugin_ConstraintCoincidence::ENTITY_B());
297 if (aPnt.get() == NULL)
299 aP = aPnt->impl<gp_Pnt>();
300 if (aOrig->isEqual(aPnt)) {
301 aToDelFeatures.append(aConstrFeature);
307 if (aToDelFeatures.size() > 0) {
308 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
309 XGUI_Workshop* aWorkshop = aConnector->workshop();
310 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
312 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
313 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
314 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
315 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
316 // the active nested sketch operation should be aborted unconditionally
317 // the Delete action should be additionally granted for the Sketch operation
318 // in order to do not abort/commit it
319 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
320 return; // the objects are processed but can not be deleted
322 anOpMgr->startOperation(anOpAction);
323 aWorkshop->deleteFeatures(aToDelFeatures);
325 anOpMgr->commitOperation();
327 myCoinsideLines.clear();
331 void PartSet_MenuMgr::onDetachMenuHide()
333 if (myPrevId != -1) {
334 // Restore color for previous object
335 setLineColor(myPrevId, myColor, false);
337 // Clear previous definitions
342 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
344 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
346 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
347 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
351 QObjectPtrList anObjects;
352 bool isUseTransaction = false;
353 // 1. change auxiliary type of a created feature
354 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
355 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
356 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
359 anObjects.append(aFOperation->feature());
362 isUseTransaction = true;
363 // 2. change auxiliary type of selected sketch entities
364 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
365 anObjects = aSelection->selectedPresentations();
368 QAction* anAction = action("AUXILIARY_CMD");
369 //SessionPtr aMgr = ModelAPI_Session::get();
370 ModuleBase_OperationAction* anOpAction = 0;
371 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
372 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
373 if (isUseTransaction) {
374 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
375 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
377 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
378 return; // the objects are processed but can not be deleted
380 anOpMgr->startOperation(anOpAction);
382 myModule->sketchMgr()->storeSelection();
384 if (anObjects.size() > 0) {
385 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
386 for (; anIt != aLast; anIt++) {
387 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
388 if (aFeature.get() != NULL) {
389 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
390 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
391 if (aSketchFeature.get() != NULL) {
392 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
394 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
395 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
397 anAuxiliaryAttr->setValue(isChecked);
402 if (isUseTransaction)
403 anOpMgr->commitOperation();
405 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
406 myModule->sketchMgr()->restoreSelection();
409 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
411 bool anEnabled = false;
412 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
414 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
415 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
419 QObjectPtrList anObjects;
420 // 1. change auxiliary type of a created feature
421 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
422 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
423 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
425 anObjects.append(aFOperation->feature());
428 /// The operation should not be aborted here, because the method does not changed
429 /// the auxilliary state, but checks the possibility to perform this
430 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
431 /// anOperation->abort();
432 // 2. change auxiliary type of selected sketch entities
433 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
434 anObjects = aSelection->selectedPresentations();
437 bool isNotAuxiliaryFound = false;
438 if (anObjects.size() > 0) {
439 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
440 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
441 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
442 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
444 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
445 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
446 if (aSketchFeature.get() != NULL) {
447 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
449 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
450 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
452 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
457 theValue = anObjects.size() && !isNotAuxiliaryFound;
461 void PartSet_MenuMgr::onActivatePart(bool)
463 if (myModule->workshop()->currentOperation())
465 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
466 if (aObjects.size() > 0) {
467 ObjectPtr aObj = aObjects.first();
468 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
470 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
471 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
472 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
480 void PartSet_MenuMgr::onActivatePartSet(bool)
482 if (myModule->workshop()->currentOperation())
484 SessionPtr aMgr = ModelAPI_Session::get();
485 bool isNewTransaction = !aMgr->isOperation();
486 // activation may cause changes in current features in document, so it must be in transaction
487 if (isNewTransaction) {
488 aMgr->startOperation("Activation");
490 aMgr->setActiveDocument(aMgr->moduleDocument());
491 if (isNewTransaction) {
492 aMgr->finishOperation();
496 void PartSet_MenuMgr::onEdit(bool)
498 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
499 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
500 if (aFeature == NULL) {
501 ResultParameterPtr aParam =
502 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
503 if (aParam.get() != NULL) {
504 aFeature = ModelAPI_Feature::feature(aParam);
507 if (aFeature.get() != NULL)
508 myModule->editFeature(aFeature);
511 void PartSet_MenuMgr::onSelectParentFeature()
513 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
514 if (aObjects.size() != 1)
517 SessionPtr aMgr = ModelAPI_Session::get();
518 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
522 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
523 QObjectPtrList aSelection;
524 aSelection.append( aParentFeature );
525 myModule->workshop()->selection()->setSelectedObjects( aSelection );