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::Viewer);
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::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
210 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
212 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
213 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
214 if (isActiveSketch) {
215 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
216 theStdActions["SHADING_CMD"]->setEnabled(false);
217 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
218 theStdActions["SHOW_CMD"]->setEnabled(false);
219 theStdActions["HIDE_CMD"]->setEnabled(false);
220 theStdActions["HIDEALL_CMD"]->setEnabled(false);
225 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
227 if (myPrevId != -1) {
228 // Restore color for previous object
229 setLineColor(myPrevId, myColor, false);
231 myPrevId = theAction->data().toInt();
232 myColor = setLineColor(myPrevId, Qt::white, true);
235 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
237 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
238 XGUI_Workshop* aWorkshop = aConnector->workshop();
239 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
241 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
242 std::list<ResultPtr>::const_iterator aIt;
243 const std::list<ResultPtr>& aResults = aLine->results();
245 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
246 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
249 aDisplayer->updateViewer();
254 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
256 int aId = theAction->data().toInt();
257 FeaturePtr aLine = myCoinsideLines.at(aId);
258 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
259 SketchPlugin_ConstraintCoincidence::ENTITY_A());
260 if (aOrig.get() == NULL)
261 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
262 SketchPlugin_ConstraintCoincidence::ENTITY_B());
264 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
265 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
267 QObjectPtrList aToDelFeatures;
268 std::set<AttributePtr>::const_iterator aIt;
269 // Find all coincedences corresponded to the selected line in the selected point
270 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
271 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
272 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
273 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
274 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
275 SketchPlugin_ConstraintCoincidence::ENTITY_A());
276 if (aPnt.get() == NULL)
277 aPnt = PartSet_Tools::getPoint(aConstrFeature,
278 SketchPlugin_ConstraintCoincidence::ENTITY_B());
279 if (aPnt.get() == NULL)
281 gp_Pnt aP = aPnt->impl<gp_Pnt>();
282 if (aOrig->isEqual(aPnt)) {
283 aToDelFeatures.append(aConstrFeature);
285 aPnt = PartSet_Tools::getPoint(aConstrFeature,
286 SketchPlugin_ConstraintCoincidence::ENTITY_B());
287 aP = aPnt->impl<gp_Pnt>();
288 if (aOrig->isEqual(aPnt)) {
289 aToDelFeatures.append(aConstrFeature);
295 if (aToDelFeatures.size() > 0) {
296 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
297 XGUI_Workshop* aWorkshop = aConnector->workshop();
298 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
299 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
300 anOperation->abort();
302 SessionPtr aMgr = ModelAPI_Session::get();
304 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
305 aMgr->startOperation(aName.toStdString());
306 aWorkshop->deleteFeatures(aToDelFeatures);
307 aMgr->finishOperation();
309 myCoinsideLines.clear();
313 void PartSet_MenuMgr::onDetachMenuHide()
315 if (myPrevId != -1) {
316 // Restore color for previous object
317 setLineColor(myPrevId, myColor, false);
319 // Clear previous definitions
324 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
326 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
328 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
329 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
333 QObjectPtrList anObjects;
334 bool isUseTransaction = false;
335 // 1. change auxiliary type of a created feature
336 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
337 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
338 anObjects.append(anOperation->feature());
341 isUseTransaction = true;
342 // 2. change auxiliary type of selected sketch entities
343 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
344 anObjects = aSelection->selectedPresentations();
347 QAction* anAction = action("AUXILIARY_CMD");
348 SessionPtr aMgr = ModelAPI_Session::get();
349 if (isUseTransaction) {
350 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
351 anOperation->abort();
352 aMgr->startOperation(anAction->text().toStdString());
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 aMgr->finishOperation();
376 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
377 XGUI_Workshop* aWorkshop = aConnector->workshop();
378 aWorkshop->updateCommandStatus();
381 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
382 myModule->sketchMgr()->restoreSelection();
385 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
387 bool anEnabled = false;
388 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
390 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
391 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
395 QObjectPtrList anObjects;
396 // 1. change auxiliary type of a created feature
397 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
398 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
399 anObjects.append(anOperation->feature());
402 /// The operation should not be aborted here, because the method does not changed
403 /// the auxilliary state, but checks the possibility to perform this
404 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
405 /// anOperation->abort();
406 // 2. change auxiliary type of selected sketch entities
407 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
408 anObjects = aSelection->selectedPresentations();
411 bool isNotAuxiliaryFound = false;
412 if (anObjects.size() > 0) {
413 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
414 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
415 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
416 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
418 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
419 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
420 if (aSketchFeature.get() != NULL) {
421 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
423 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
424 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
426 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
431 theValue = anObjects.size() && !isNotAuxiliaryFound;
435 void PartSet_MenuMgr::onActivatePart(bool)
437 if (myModule->workshop()->currentOperation())
439 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
440 if (aObjects.size() > 0) {
441 ObjectPtr aObj = aObjects.first();
442 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
444 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
445 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
446 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
454 void PartSet_MenuMgr::onActivatePartSet(bool)
456 if (myModule->workshop()->currentOperation())
458 SessionPtr aMgr = ModelAPI_Session::get();
459 bool isNewTransaction = !aMgr->isOperation();
460 // activation may cause changes in current features in document, so it must be in transaction
461 if (isNewTransaction) {
462 aMgr->startOperation("Activation");
464 aMgr->setActiveDocument(aMgr->moduleDocument());
465 if (isNewTransaction) {
466 aMgr->finishOperation();
470 void PartSet_MenuMgr::onEdit(bool)
472 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
473 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
474 if (aFeature == NULL) {
475 ResultParameterPtr aParam =
476 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
477 if (aParam.get() != NULL) {
478 aFeature = ModelAPI_Feature::feature(aParam);
481 if (aFeature.get() != NULL)
482 myModule->editFeature(aFeature);