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_OperationFeature.h>
27 #include <XGUI_ModuleConnector.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_Displayer.h>
30 #include <XGUI_DataModel.h>
31 #include <XGUI_ObjectsBrowser.h>
33 #include <Events_Loop.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_ResultPart.h>
37 #include <ModelAPI_ResultParameter.h>
43 #include <BRep_Tool.hxx>
45 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
46 : QObject(theModule), myModule(theModule), myPrevId(-1)
52 QAction* PartSet_MenuMgr::action(const QString& theId) const
54 if (myActions.contains(theId))
55 return myActions[theId];
59 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
61 if (myActions.contains(theId))
62 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
63 theAction->setData(theId);
64 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
65 myActions[theId] = theAction;
68 void PartSet_MenuMgr::createActions()
72 aAction = new QAction(tr("Auxiliary"), this);
73 aAction->setCheckable(true);
74 addAction("AUXILIARY_CMD", aAction);
76 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
77 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
78 myActions["ACTIVATE_PART_CMD"] = aAction;
80 aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
81 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
82 myActions["DEACTIVATE_PART_CMD"] = aAction;
85 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
86 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
87 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
89 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
90 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
91 myActions["EDIT_CMD"] = aAction;
93 aAction = new QAction(QIcon(), tr("Select parent feature"), this);
94 aAction->setCheckable(false);
95 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
96 myActions["SELECT_PARENT_CMD"] = aAction;
100 void PartSet_MenuMgr::onAction(bool isChecked)
102 QAction* aAction = static_cast<QAction*>(sender());
103 QString anId = aAction->data().toString();
105 if (anId == "AUXILIARY_CMD") {
106 setAuxiliary(isChecked);
110 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
112 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
113 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
114 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
117 myCoinsideLines.clear();
118 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
120 bool aIsDetach = false;
121 bool hasAttribute = false;
122 bool hasFeature = false;
124 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
128 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
129 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
130 if (aResult.get() != NULL) {
131 aShape = aPrs.shape();
132 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
137 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
138 hasFeature = (aFeature.get() != NULL);
142 if (aPrsList.size() == 1) {
143 TopoDS_Shape aShape = aPrsList.first().shape();
144 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
145 // Find 2d coordinates
146 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
147 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
148 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
149 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
150 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
152 // Find coincident in these coordinates
153 ObjectPtr aObj = aPrsList.first().object();
154 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
155 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
156 std::set<AttributePtr>::const_iterator aIt;
157 FeaturePtr aCoincident;
158 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
159 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
160 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
161 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
162 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
163 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
164 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
165 aCoincident = aConstrFeature;
168 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
169 SketchPlugin_ConstraintCoincidence::ENTITY_B());
170 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
171 aCoincident = aConstrFeature;
177 // If we have coincidence then add Detach menu
178 if (aCoincident.get() != NULL) {
179 mySelectedFeature = aCoincident;
180 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
181 SketchPlugin_ConstraintCoincidence::ENTITY_A());
182 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
183 SketchPlugin_ConstraintCoincidence::ENTITY_B());
184 if (myCoinsideLines.size() > 0) {
186 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
189 foreach (FeaturePtr aCoins, myCoinsideLines) {
190 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
191 aAction->setData(QVariant(i));
194 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
195 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
196 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
202 if ((!aIsDetach) && hasFeature) {
203 theMenu->addAction(theStdActions["DELETE_CMD"]);
208 if (canSetAuxiliary(isAuxiliary)) {
209 QAction* anAction = action("AUXILIARY_CMD");
210 theMenu->addAction(anAction);
211 anAction->setChecked(isAuxiliary);
216 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
218 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
220 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
221 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
222 if (isActiveSketch) {
223 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
224 theStdActions["SHADING_CMD"]->setEnabled(false);
225 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
226 theStdActions["SHOW_CMD"]->setEnabled(false);
227 theStdActions["HIDE_CMD"]->setEnabled(false);
228 theStdActions["HIDEALL_CMD"]->setEnabled(false);
233 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
235 if (myPrevId != -1) {
236 // Restore color for previous object
237 setLineColor(myPrevId, myColor, false);
239 myPrevId = theAction->data().toInt();
240 myColor = setLineColor(myPrevId, Qt::white, true);
243 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
245 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
246 XGUI_Workshop* aWorkshop = aConnector->workshop();
247 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
249 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
250 std::list<ResultPtr>::const_iterator aIt;
251 const std::list<ResultPtr>& aResults = aLine->results();
253 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
254 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
257 aDisplayer->updateViewer();
262 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
264 int aId = theAction->data().toInt();
265 FeaturePtr aLine = myCoinsideLines.at(aId);
266 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
267 SketchPlugin_ConstraintCoincidence::ENTITY_A());
268 if (aOrig.get() == NULL)
269 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
270 SketchPlugin_ConstraintCoincidence::ENTITY_B());
272 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
273 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
275 QObjectPtrList aToDelFeatures;
276 std::set<AttributePtr>::const_iterator aIt;
277 // Find all coincedences corresponded to the selected line in the selected point
278 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
279 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
280 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
281 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
282 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
283 SketchPlugin_ConstraintCoincidence::ENTITY_A());
284 if (aPnt.get() == NULL)
285 aPnt = PartSet_Tools::getPoint(aConstrFeature,
286 SketchPlugin_ConstraintCoincidence::ENTITY_B());
287 if (aPnt.get() == NULL)
289 gp_Pnt aP = aPnt->impl<gp_Pnt>();
290 if (aOrig->isEqual(aPnt)) {
291 aToDelFeatures.append(aConstrFeature);
293 aPnt = PartSet_Tools::getPoint(aConstrFeature,
294 SketchPlugin_ConstraintCoincidence::ENTITY_B());
295 if (aPnt.get() == NULL)
297 aP = aPnt->impl<gp_Pnt>();
298 if (aOrig->isEqual(aPnt)) {
299 aToDelFeatures.append(aConstrFeature);
305 if (aToDelFeatures.size() > 0) {
306 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
307 XGUI_Workshop* aWorkshop = aConnector->workshop();
308 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
309 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
310 anOperation->abort();
312 SessionPtr aMgr = ModelAPI_Session::get();
314 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
315 aMgr->startOperation(aName.toStdString());
316 aWorkshop->deleteFeatures(aToDelFeatures);
317 aMgr->finishOperation();
319 myCoinsideLines.clear();
323 void PartSet_MenuMgr::onDetachMenuHide()
325 if (myPrevId != -1) {
326 // Restore color for previous object
327 setLineColor(myPrevId, myColor, false);
329 // Clear previous definitions
334 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
336 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
338 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
339 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
343 QObjectPtrList anObjects;
344 bool isUseTransaction = false;
345 // 1. change auxiliary type of a created feature
346 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
347 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
348 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
351 anObjects.append(aFOperation->feature());
354 isUseTransaction = true;
355 // 2. change auxiliary type of selected sketch entities
356 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
357 anObjects = aSelection->selectedPresentations();
360 QAction* anAction = action("AUXILIARY_CMD");
361 SessionPtr aMgr = ModelAPI_Session::get();
362 if (isUseTransaction) {
363 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
364 anOperation->abort();
365 aMgr->startOperation(anAction->text().toStdString());
367 myModule->sketchMgr()->storeSelection();
369 if (anObjects.size() > 0) {
370 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
371 for (; anIt != aLast; anIt++) {
372 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
373 if (aFeature.get() != NULL) {
374 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
375 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
376 if (aSketchFeature.get() != NULL) {
377 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
379 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
380 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
382 anAuxiliaryAttr->setValue(isChecked);
387 if (isUseTransaction) {
388 aMgr->finishOperation();
389 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
390 XGUI_Workshop* aWorkshop = aConnector->workshop();
391 aWorkshop->updateCommandStatus();
394 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
395 myModule->sketchMgr()->restoreSelection();
398 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
400 bool anEnabled = false;
401 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
403 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
404 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
408 QObjectPtrList anObjects;
409 // 1. change auxiliary type of a created feature
410 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
411 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
412 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
414 anObjects.append(aFOperation->feature());
417 /// The operation should not be aborted here, because the method does not changed
418 /// the auxilliary state, but checks the possibility to perform this
419 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
420 /// anOperation->abort();
421 // 2. change auxiliary type of selected sketch entities
422 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
423 anObjects = aSelection->selectedPresentations();
426 bool isNotAuxiliaryFound = false;
427 if (anObjects.size() > 0) {
428 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
429 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
430 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
431 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
433 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
434 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
435 if (aSketchFeature.get() != NULL) {
436 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
438 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
439 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
441 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
446 theValue = anObjects.size() && !isNotAuxiliaryFound;
450 void PartSet_MenuMgr::onActivatePart(bool)
452 if (myModule->workshop()->currentOperation())
454 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
455 if (aObjects.size() > 0) {
456 ObjectPtr aObj = aObjects.first();
457 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
459 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
460 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
461 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
469 void PartSet_MenuMgr::onActivatePartSet(bool)
471 if (myModule->workshop()->currentOperation())
473 SessionPtr aMgr = ModelAPI_Session::get();
474 bool isNewTransaction = !aMgr->isOperation();
475 // activation may cause changes in current features in document, so it must be in transaction
476 if (isNewTransaction) {
477 aMgr->startOperation("Activation");
479 aMgr->setActiveDocument(aMgr->moduleDocument());
480 if (isNewTransaction) {
481 aMgr->finishOperation();
485 void PartSet_MenuMgr::onEdit(bool)
487 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
488 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
489 if (aFeature == NULL) {
490 ResultParameterPtr aParam =
491 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
492 if (aParam.get() != NULL) {
493 aFeature = ModelAPI_Feature::feature(aParam);
496 if (aFeature.get() != NULL)
497 myModule->editFeature(aFeature);
500 void PartSet_MenuMgr::onSelectParentFeature()
502 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
503 if (aObjects.size() != 1)
506 SessionPtr aMgr = ModelAPI_Session::get();
507 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
511 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
512 QObjectPtrList aSelection;
513 aSelection.append( aParentFeature );
514 myModule->workshop()->selection()->setSelectedObjects( aSelection );