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;
83 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
84 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
85 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
87 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
88 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
89 myActions["EDIT_CMD"] = aAction;
91 aAction = new QAction(QIcon(), tr("Select parent feature"), this);
92 aAction->setCheckable(false);
93 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
94 myActions["SELECT_PARENT_CMD"] = aAction;
98 void PartSet_MenuMgr::onAction(bool isChecked)
100 QAction* aAction = static_cast<QAction*>(sender());
101 QString anId = aAction->data().toString();
103 if (anId == "AUXILIARY_CMD") {
104 setAuxiliary(isChecked);
108 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
110 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
111 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
112 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
115 myCoinsideLines.clear();
116 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
118 bool aIsDetach = false;
119 bool hasAttribute = false;
120 bool hasFeature = false;
122 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
126 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
127 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
128 if (aResult.get() != NULL) {
129 aShape = aPrs.shape();
130 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
135 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
136 hasFeature = (aFeature.get() != NULL);
140 if (aPrsList.size() == 1) {
141 TopoDS_Shape aShape = aPrsList.first().shape();
142 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
143 // Find 2d coordinates
144 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
145 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
146 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
147 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
148 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
150 // Find coincident in these coordinates
151 ObjectPtr aObj = aPrsList.first().object();
152 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
153 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
154 std::set<AttributePtr>::const_iterator aIt;
155 FeaturePtr aCoincident;
156 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
157 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
158 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
159 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
160 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
161 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
162 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
163 aCoincident = aConstrFeature;
166 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
167 SketchPlugin_ConstraintCoincidence::ENTITY_B());
168 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
169 aCoincident = aConstrFeature;
175 // If we have coincidence then add Detach menu
176 if (aCoincident.get() != NULL) {
177 mySelectedFeature = aCoincident;
178 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
179 SketchPlugin_ConstraintCoincidence::ENTITY_A());
180 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
181 SketchPlugin_ConstraintCoincidence::ENTITY_B());
182 if (myCoinsideLines.size() > 0) {
184 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
187 foreach (FeaturePtr aCoins, myCoinsideLines) {
188 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
189 aAction->setData(QVariant(i));
192 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
193 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
194 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
200 if ((!aIsDetach) && hasFeature) {
201 theMenu->addAction(theStdActions["DELETE_CMD"]);
206 if (canSetAuxiliary(isAuxiliary)) {
207 QAction* anAction = action("AUXILIARY_CMD");
208 theMenu->addAction(anAction);
209 anAction->setChecked(isAuxiliary);
214 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
216 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
218 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
219 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
220 if (isActiveSketch) {
221 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
222 theStdActions["SHADING_CMD"]->setEnabled(false);
223 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
224 theStdActions["SHOW_CMD"]->setEnabled(false);
225 theStdActions["HIDE_CMD"]->setEnabled(false);
226 theStdActions["HIDEALL_CMD"]->setEnabled(false);
231 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
233 if (myPrevId != -1) {
234 // Restore color for previous object
235 setLineColor(myPrevId, myColor, false);
237 myPrevId = theAction->data().toInt();
238 myColor = setLineColor(myPrevId, Qt::white, true);
241 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
243 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
244 XGUI_Workshop* aWorkshop = aConnector->workshop();
245 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
247 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
248 std::list<ResultPtr>::const_iterator aIt;
249 const std::list<ResultPtr>& aResults = aLine->results();
251 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
252 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
255 aDisplayer->updateViewer();
260 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
262 int aId = theAction->data().toInt();
263 FeaturePtr aLine = myCoinsideLines.at(aId);
264 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
265 SketchPlugin_ConstraintCoincidence::ENTITY_A());
266 if (aOrig.get() == NULL)
267 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
268 SketchPlugin_ConstraintCoincidence::ENTITY_B());
270 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
271 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
273 QObjectPtrList aToDelFeatures;
274 std::set<AttributePtr>::const_iterator aIt;
275 // Find all coincedences corresponded to the selected line in the selected point
276 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
277 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
278 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
279 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
280 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
281 SketchPlugin_ConstraintCoincidence::ENTITY_A());
282 if (aPnt.get() == NULL)
283 aPnt = PartSet_Tools::getPoint(aConstrFeature,
284 SketchPlugin_ConstraintCoincidence::ENTITY_B());
285 if (aPnt.get() == NULL)
287 gp_Pnt aP = aPnt->impl<gp_Pnt>();
288 if (aOrig->isEqual(aPnt)) {
289 aToDelFeatures.append(aConstrFeature);
291 aPnt = PartSet_Tools::getPoint(aConstrFeature,
292 SketchPlugin_ConstraintCoincidence::ENTITY_B());
293 if (aPnt.get() == NULL)
295 aP = aPnt->impl<gp_Pnt>();
296 if (aOrig->isEqual(aPnt)) {
297 aToDelFeatures.append(aConstrFeature);
303 if (aToDelFeatures.size() > 0) {
304 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
305 XGUI_Workshop* aWorkshop = aConnector->workshop();
306 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
308 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
309 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
310 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
311 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
312 // the active nested sketch operation should be aborted unconditionally
313 // the Delete action should be additionally granted for the Sketch operation
314 // in order to do not abort/commit it
315 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
316 return; // the objects are processed but can not be deleted
318 anOpMgr->startOperation(anOpAction);
319 aWorkshop->deleteFeatures(aToDelFeatures);
321 anOpMgr->commitOperation();
323 myCoinsideLines.clear();
327 void PartSet_MenuMgr::onDetachMenuHide()
329 if (myPrevId != -1) {
330 // Restore color for previous object
331 setLineColor(myPrevId, myColor, false);
333 // Clear previous definitions
338 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
340 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
342 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
343 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
347 QObjectPtrList anObjects;
348 bool isUseTransaction = false;
349 // 1. change auxiliary type of a created feature
350 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
351 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
352 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
355 anObjects.append(aFOperation->feature());
358 isUseTransaction = true;
359 // 2. change auxiliary type of selected sketch entities
360 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
361 anObjects = aSelection->selectedPresentations();
364 QAction* anAction = action("AUXILIARY_CMD");
365 //SessionPtr aMgr = ModelAPI_Session::get();
366 ModuleBase_OperationAction* anOpAction = 0;
367 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
368 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
369 if (isUseTransaction) {
370 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
371 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
373 if (!anOpMgr->canStartOperation(anOpAction->id(), isSketchOp/*granted*/))
374 return; // the objects are processed but can not be deleted
376 anOpMgr->startOperation(anOpAction);
378 myModule->sketchMgr()->storeSelection();
380 if (anObjects.size() > 0) {
381 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
382 for (; anIt != aLast; anIt++) {
383 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
384 if (aFeature.get() != NULL) {
385 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
386 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
387 if (aSketchFeature.get() != NULL) {
388 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
390 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
391 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
393 anAuxiliaryAttr->setValue(isChecked);
398 if (isUseTransaction)
399 anOpMgr->commitOperation();
401 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
402 myModule->sketchMgr()->restoreSelection();
405 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
407 bool anEnabled = false;
408 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
410 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
411 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
415 QObjectPtrList anObjects;
416 // 1. change auxiliary type of a created feature
417 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
418 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
419 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
421 anObjects.append(aFOperation->feature());
424 /// The operation should not be aborted here, because the method does not changed
425 /// the auxilliary state, but checks the possibility to perform this
426 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
427 /// anOperation->abort();
428 // 2. change auxiliary type of selected sketch entities
429 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
430 anObjects = aSelection->selectedPresentations();
433 bool isNotAuxiliaryFound = false;
434 if (anObjects.size() > 0) {
435 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
436 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
437 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
438 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
440 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
441 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
442 if (aSketchFeature.get() != NULL) {
443 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
445 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
446 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
448 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
453 theValue = anObjects.size() && !isNotAuxiliaryFound;
457 void PartSet_MenuMgr::onActivatePart(bool)
459 if (myModule->workshop()->currentOperation())
461 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
462 if (aObjects.size() > 0) {
463 ObjectPtr aObj = aObjects.first();
464 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
466 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
467 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
468 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
476 void PartSet_MenuMgr::onActivatePartSet(bool)
478 if (myModule->workshop()->currentOperation())
480 SessionPtr aMgr = ModelAPI_Session::get();
481 bool isNewTransaction = !aMgr->isOperation();
482 // activation may cause changes in current features in document, so it must be in transaction
483 if (isNewTransaction) {
484 aMgr->startOperation("Activation");
486 aMgr->setActiveDocument(aMgr->moduleDocument());
487 if (isNewTransaction) {
488 aMgr->finishOperation();
492 void PartSet_MenuMgr::onEdit(bool)
494 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
495 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
496 if (aFeature == NULL) {
497 ResultParameterPtr aParam =
498 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
499 if (aParam.get() != NULL) {
500 aFeature = ModelAPI_Feature::feature(aParam);
503 if (aFeature.get() != NULL)
504 myModule->editFeature(aFeature);
507 void PartSet_MenuMgr::onSelectParentFeature()
509 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
510 if (aObjects.size() != 1)
513 SessionPtr aMgr = ModelAPI_Session::get();
514 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
518 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
519 QObjectPtrList aSelection;
520 aSelection.append( aParentFeature );
521 myModule->workshop()->selection()->setSelectedObjects( aSelection );