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>
26 #include <XGUI_ModuleConnector.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_Displayer.h>
30 #include <Events_Loop.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_ResultParameter.h>
40 #include <BRep_Tool.hxx>
42 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
43 : QObject(theModule), myModule(theModule), myPrevId(-1)
49 QAction* PartSet_MenuMgr::action(const QString& theId) const
51 if (myActions.contains(theId))
52 return myActions[theId];
56 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
58 if (myActions.contains(theId))
59 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
60 theAction->setData(theId);
61 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
62 myActions[theId] = theAction;
65 void PartSet_MenuMgr::createActions()
69 aAction = new QAction(tr("Auxiliary"), this);
70 aAction->setCheckable(true);
71 addAction("AUXILIARY_CMD", aAction);
73 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
74 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
75 myActions["ACTIVATE_PART_CMD"] = aAction;
77 aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
78 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
79 myActions["DEACTIVATE_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;
92 void PartSet_MenuMgr::onAction(bool isChecked)
94 QAction* aAction = static_cast<QAction*>(sender());
95 QString anId = aAction->data().toString();
97 if (anId == "AUXILIARY_CMD") {
98 setAuxiliary(isChecked);
102 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
104 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
105 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
106 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
109 myCoinsideLines.clear();
110 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
112 bool aIsDetach = false;
113 bool hasAttribute = false;
114 bool hasFeature = false;
116 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::AllControls);
120 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
121 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
122 if (aResult.get() != NULL) {
123 aShape = aPrs.shape();
124 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
129 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
130 hasFeature = (aFeature.get() != NULL);
134 if (aPrsList.size() == 1) {
135 TopoDS_Shape aShape = aPrsList.first().shape();
136 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
137 // Find 2d coordinates
138 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
139 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
140 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
141 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
142 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
144 // Find coincident in these coordinates
145 ObjectPtr aObj = aPrsList.first().object();
146 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
147 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
148 std::set<AttributePtr>::const_iterator aIt;
149 FeaturePtr aCoincident;
150 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
151 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
152 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
153 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
154 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
155 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
156 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
157 aCoincident = aConstrFeature;
160 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
161 SketchPlugin_ConstraintCoincidence::ENTITY_B());
162 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
163 aCoincident = aConstrFeature;
169 // If we have coincidence then add Detach menu
170 if (aCoincident.get() != NULL) {
171 mySelectedFeature = aCoincident;
172 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
173 SketchPlugin_ConstraintCoincidence::ENTITY_A());
174 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
175 SketchPlugin_ConstraintCoincidence::ENTITY_B());
176 if (myCoinsideLines.size() > 0) {
178 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
181 foreach (FeaturePtr aCoins, myCoinsideLines) {
182 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
183 aAction->setData(QVariant(i));
186 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
187 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
188 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
194 if ((!aIsDetach) && hasFeature) {
195 theMenu->addAction(theStdActions["DELETE_CMD"]);
200 if (canSetAuxiliary(isAuxiliary)) {
201 QAction* anAction = action("AUXILIARY_CMD");
202 theMenu->addAction(anAction);
203 anAction->setChecked(isAuxiliary);
208 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
210 if (myPrevId != -1) {
211 // Restore color for previous object
212 setLineColor(myPrevId, myColor, false);
214 myPrevId = theAction->data().toInt();
215 myColor = setLineColor(myPrevId, Qt::white, true);
218 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
220 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
221 XGUI_Workshop* aWorkshop = aConnector->workshop();
222 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
224 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
225 std::list<ResultPtr>::const_iterator aIt;
226 const std::list<ResultPtr>& aResults = aLine->results();
228 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
229 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
232 aDisplayer->updateViewer();
237 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
239 int aId = theAction->data().toInt();
240 FeaturePtr aLine = myCoinsideLines.at(aId);
241 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
242 SketchPlugin_ConstraintCoincidence::ENTITY_A());
243 if (aOrig.get() == NULL)
244 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
245 SketchPlugin_ConstraintCoincidence::ENTITY_B());
247 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
248 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
250 QObjectPtrList aToDelFeatures;
251 std::set<AttributePtr>::const_iterator aIt;
252 // Find all coincedences corresponded to the selected line in the selected point
253 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
254 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
255 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
256 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
257 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
258 SketchPlugin_ConstraintCoincidence::ENTITY_A());
259 if (aPnt.get() == NULL)
260 aPnt = PartSet_Tools::getPoint(aConstrFeature,
261 SketchPlugin_ConstraintCoincidence::ENTITY_B());
262 if (aPnt.get() == NULL)
264 gp_Pnt aP = aPnt->impl<gp_Pnt>();
265 if (aOrig->isEqual(aPnt)) {
266 aToDelFeatures.append(aConstrFeature);
268 aPnt = PartSet_Tools::getPoint(aConstrFeature,
269 SketchPlugin_ConstraintCoincidence::ENTITY_B());
270 aP = aPnt->impl<gp_Pnt>();
271 if (aOrig->isEqual(aPnt)) {
272 aToDelFeatures.append(aConstrFeature);
278 if (aToDelFeatures.size() > 0) {
279 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
280 XGUI_Workshop* aWorkshop = aConnector->workshop();
281 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
282 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
283 anOperation->abort();
285 SessionPtr aMgr = ModelAPI_Session::get();
286 std::set<FeaturePtr> anIgnoredFeatures;
287 anIgnoredFeatures.insert(myModule->sketchMgr()->activeSketch());
289 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
290 aMgr->startOperation(aName.toStdString());
291 aWorkshop->deleteFeatures(aToDelFeatures, anIgnoredFeatures);
292 aMgr->finishOperation();
294 myCoinsideLines.clear();
298 void PartSet_MenuMgr::onDetachMenuHide()
300 if (myPrevId != -1) {
301 // Restore color for previous object
302 setLineColor(myPrevId, myColor, false);
304 // Clear previous definitions
309 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
311 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
313 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
314 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
318 QObjectPtrList anObjects;
319 bool isUseTransaction = false;
320 // 1. change auxiliary type of a created feature
321 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
322 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
323 anObjects.append(anOperation->feature());
326 isUseTransaction = true;
327 // 2. change auxiliary type of selected sketch entities
328 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
329 anObjects = aSelection->selectedPresentations();
332 QAction* anAction = action("AUXILIARY_CMD");
333 SessionPtr aMgr = ModelAPI_Session::get();
334 if (isUseTransaction) {
335 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
336 anOperation->abort();
337 aMgr->startOperation(anAction->text().toStdString());
339 myModule->sketchMgr()->storeSelection();
341 if (anObjects.size() > 0) {
342 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
343 for (; anIt != aLast; anIt++) {
344 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
345 if (aFeature.get() != NULL) {
346 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
347 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
348 if (aSketchFeature.get() != NULL) {
349 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
351 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
352 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
354 anAuxiliaryAttr->setValue(isChecked);
359 if (isUseTransaction) {
360 aMgr->finishOperation();
361 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
362 XGUI_Workshop* aWorkshop = aConnector->workshop();
363 aWorkshop->updateCommandStatus();
366 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
367 myModule->sketchMgr()->restoreSelection();
370 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
372 bool anEnabled = false;
373 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
375 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
376 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
380 QObjectPtrList anObjects;
381 // 1. change auxiliary type of a created feature
382 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
383 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
384 anObjects.append(anOperation->feature());
387 /// The operation should not be aborted here, because the method does not changed
388 /// the auxilliary state, but checks the possibility to perform this
389 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
390 /// anOperation->abort();
391 // 2. change auxiliary type of selected sketch entities
392 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
393 anObjects = aSelection->selectedPresentations();
396 bool isNotAuxiliaryFound = false;
397 if (anObjects.size() > 0) {
398 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
399 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
400 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
401 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
403 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
404 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
405 if (aSketchFeature.get() != NULL) {
406 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
408 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
409 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
411 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
416 theValue = anObjects.size() && !isNotAuxiliaryFound;
420 void PartSet_MenuMgr::onActivatePart(bool)
422 if (myModule->workshop()->currentOperation())
424 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
425 if (aObjects.size() > 0) {
426 ObjectPtr aObj = aObjects.first();
427 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
429 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
430 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
431 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
439 void PartSet_MenuMgr::onActivatePartSet(bool)
441 if (myModule->workshop()->currentOperation())
443 SessionPtr aMgr = ModelAPI_Session::get();
444 bool isNewTransaction = !aMgr->isOperation();
445 // activation may cause changes in current features in document, so it must be in transaction
446 if (isNewTransaction) {
447 aMgr->startOperation("Activation");
449 aMgr->setActiveDocument(aMgr->moduleDocument());
450 if (isNewTransaction) {
451 aMgr->finishOperation();
455 void PartSet_MenuMgr::onEdit(bool)
457 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
458 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
459 if (aFeature == NULL) {
460 ResultParameterPtr aParam =
461 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
462 if (aParam.get() != NULL) {
463 aFeature = ModelAPI_Feature::feature(aParam);
466 if (aFeature.get() != NULL)
467 myModule->editFeature(aFeature);