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>
29 #include <XGUI_DataModel.h>
30 #include <XGUI_ObjectsBrowser.h>
32 #include <Events_Loop.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_ResultParameter.h>
42 #include <BRep_Tool.hxx>
44 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
45 : QObject(theModule), myModule(theModule), myPrevId(-1)
51 QAction* PartSet_MenuMgr::action(const QString& theId) const
53 if (myActions.contains(theId))
54 return myActions[theId];
58 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
60 if (myActions.contains(theId))
61 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
62 theAction->setData(theId);
63 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
64 myActions[theId] = theAction;
67 void PartSet_MenuMgr::createActions()
71 aAction = new QAction(tr("Auxiliary"), this);
72 aAction->setCheckable(true);
73 addAction("AUXILIARY_CMD", aAction);
75 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
76 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
77 myActions["ACTIVATE_PART_CMD"] = aAction;
79 aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
80 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
81 myActions["DEACTIVATE_PART_CMD"] = aAction;
84 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
85 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
86 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
88 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
89 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
90 myActions["EDIT_CMD"] = aAction;
92 aAction = new QAction(QIcon(), tr("Select parent feature"), this);
93 aAction->setCheckable(false);
94 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
95 myActions["SELECT_PARENT_CMD"] = aAction;
99 void PartSet_MenuMgr::onAction(bool isChecked)
101 QAction* aAction = static_cast<QAction*>(sender());
102 QString anId = aAction->data().toString();
104 if (anId == "AUXILIARY_CMD") {
105 setAuxiliary(isChecked);
109 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
111 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
112 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
113 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
116 myCoinsideLines.clear();
117 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
119 bool aIsDetach = false;
120 bool hasAttribute = false;
121 bool hasFeature = false;
123 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
127 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
128 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
129 if (aResult.get() != NULL) {
130 aShape = aPrs.shape();
131 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
136 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
137 hasFeature = (aFeature.get() != NULL);
141 if (aPrsList.size() == 1) {
142 TopoDS_Shape aShape = aPrsList.first().shape();
143 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
144 // Find 2d coordinates
145 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
146 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
147 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
148 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
149 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
151 // Find coincident in these coordinates
152 ObjectPtr aObj = aPrsList.first().object();
153 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
154 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
155 std::set<AttributePtr>::const_iterator aIt;
156 FeaturePtr aCoincident;
157 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
158 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
159 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
160 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
161 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
162 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
163 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
164 aCoincident = aConstrFeature;
167 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
168 SketchPlugin_ConstraintCoincidence::ENTITY_B());
169 if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) {
170 aCoincident = aConstrFeature;
176 // If we have coincidence then add Detach menu
177 if (aCoincident.get() != NULL) {
178 mySelectedFeature = aCoincident;
179 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
180 SketchPlugin_ConstraintCoincidence::ENTITY_A());
181 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
182 SketchPlugin_ConstraintCoincidence::ENTITY_B());
183 if (myCoinsideLines.size() > 0) {
185 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
188 foreach (FeaturePtr aCoins, myCoinsideLines) {
189 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
190 aAction->setData(QVariant(i));
193 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
194 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
195 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
201 if ((!aIsDetach) && hasFeature) {
202 theMenu->addAction(theStdActions["DELETE_CMD"]);
207 if (canSetAuxiliary(isAuxiliary)) {
208 QAction* anAction = action("AUXILIARY_CMD");
209 theMenu->addAction(anAction);
210 anAction->setChecked(isAuxiliary);
215 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
217 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
219 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
220 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
221 if (isActiveSketch) {
222 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
223 theStdActions["SHADING_CMD"]->setEnabled(false);
224 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
225 theStdActions["SHOW_CMD"]->setEnabled(false);
226 theStdActions["HIDE_CMD"]->setEnabled(false);
227 theStdActions["HIDEALL_CMD"]->setEnabled(false);
232 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
234 if (myPrevId != -1) {
235 // Restore color for previous object
236 setLineColor(myPrevId, myColor, false);
238 myPrevId = theAction->data().toInt();
239 myColor = setLineColor(myPrevId, Qt::white, true);
242 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
244 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
245 XGUI_Workshop* aWorkshop = aConnector->workshop();
246 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
248 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
249 std::list<ResultPtr>::const_iterator aIt;
250 const std::list<ResultPtr>& aResults = aLine->results();
252 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
253 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
256 aDisplayer->updateViewer();
261 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
263 int aId = theAction->data().toInt();
264 FeaturePtr aLine = myCoinsideLines.at(aId);
265 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
266 SketchPlugin_ConstraintCoincidence::ENTITY_A());
267 if (aOrig.get() == NULL)
268 aOrig = PartSet_Tools::getPoint(mySelectedFeature,
269 SketchPlugin_ConstraintCoincidence::ENTITY_B());
271 gp_Pnt aOr = aOrig->impl<gp_Pnt>();
272 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
274 QObjectPtrList aToDelFeatures;
275 std::set<AttributePtr>::const_iterator aIt;
276 // Find all coincedences corresponded to the selected line in the selected point
277 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
278 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
279 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
280 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
281 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
282 SketchPlugin_ConstraintCoincidence::ENTITY_A());
283 if (aPnt.get() == NULL)
284 aPnt = PartSet_Tools::getPoint(aConstrFeature,
285 SketchPlugin_ConstraintCoincidence::ENTITY_B());
286 if (aPnt.get() == NULL)
288 gp_Pnt aP = aPnt->impl<gp_Pnt>();
289 if (aOrig->isEqual(aPnt)) {
290 aToDelFeatures.append(aConstrFeature);
292 aPnt = PartSet_Tools::getPoint(aConstrFeature,
293 SketchPlugin_ConstraintCoincidence::ENTITY_B());
294 aP = aPnt->impl<gp_Pnt>();
295 if (aOrig->isEqual(aPnt)) {
296 aToDelFeatures.append(aConstrFeature);
302 if (aToDelFeatures.size() > 0) {
303 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
304 XGUI_Workshop* aWorkshop = aConnector->workshop();
305 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
306 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
307 anOperation->abort();
309 SessionPtr aMgr = ModelAPI_Session::get();
311 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
312 aMgr->startOperation(aName.toStdString());
313 aWorkshop->deleteFeatures(aToDelFeatures);
314 aMgr->finishOperation();
316 myCoinsideLines.clear();
320 void PartSet_MenuMgr::onDetachMenuHide()
322 if (myPrevId != -1) {
323 // Restore color for previous object
324 setLineColor(myPrevId, myColor, false);
326 // Clear previous definitions
331 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
333 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
335 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
336 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
340 QObjectPtrList anObjects;
341 bool isUseTransaction = false;
342 // 1. change auxiliary type of a created feature
343 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
344 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
345 anObjects.append(anOperation->feature());
348 isUseTransaction = true;
349 // 2. change auxiliary type of selected sketch entities
350 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
351 anObjects = aSelection->selectedPresentations();
354 QAction* anAction = action("AUXILIARY_CMD");
355 SessionPtr aMgr = ModelAPI_Session::get();
356 if (isUseTransaction) {
357 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
358 anOperation->abort();
359 aMgr->startOperation(anAction->text().toStdString());
361 myModule->sketchMgr()->storeSelection();
363 if (anObjects.size() > 0) {
364 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
365 for (; anIt != aLast; anIt++) {
366 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
367 if (aFeature.get() != NULL) {
368 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
369 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
370 if (aSketchFeature.get() != NULL) {
371 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
373 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
374 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
376 anAuxiliaryAttr->setValue(isChecked);
381 if (isUseTransaction) {
382 aMgr->finishOperation();
383 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
384 XGUI_Workshop* aWorkshop = aConnector->workshop();
385 aWorkshop->updateCommandStatus();
388 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
389 myModule->sketchMgr()->restoreSelection();
392 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
394 bool anEnabled = false;
395 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
397 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
398 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
402 QObjectPtrList anObjects;
403 // 1. change auxiliary type of a created feature
404 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
405 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
406 anObjects.append(anOperation->feature());
409 /// The operation should not be aborted here, because the method does not changed
410 /// the auxilliary state, but checks the possibility to perform this
411 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
412 /// anOperation->abort();
413 // 2. change auxiliary type of selected sketch entities
414 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
415 anObjects = aSelection->selectedPresentations();
418 bool isNotAuxiliaryFound = false;
419 if (anObjects.size() > 0) {
420 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
421 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
422 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
423 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
425 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
426 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
427 if (aSketchFeature.get() != NULL) {
428 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
430 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
431 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
433 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
438 theValue = anObjects.size() && !isNotAuxiliaryFound;
442 void PartSet_MenuMgr::onActivatePart(bool)
444 if (myModule->workshop()->currentOperation())
446 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
447 if (aObjects.size() > 0) {
448 ObjectPtr aObj = aObjects.first();
449 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
451 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
452 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
453 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
461 void PartSet_MenuMgr::onActivatePartSet(bool)
463 if (myModule->workshop()->currentOperation())
465 SessionPtr aMgr = ModelAPI_Session::get();
466 bool isNewTransaction = !aMgr->isOperation();
467 // activation may cause changes in current features in document, so it must be in transaction
468 if (isNewTransaction) {
469 aMgr->startOperation("Activation");
471 aMgr->setActiveDocument(aMgr->moduleDocument());
472 if (isNewTransaction) {
473 aMgr->finishOperation();
477 void PartSet_MenuMgr::onEdit(bool)
479 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
480 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
481 if (aFeature == NULL) {
482 ResultParameterPtr aParam =
483 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
484 if (aParam.get() != NULL) {
485 aFeature = ModelAPI_Feature::feature(aParam);
488 if (aFeature.get() != NULL)
489 myModule->editFeature(aFeature);
492 void PartSet_MenuMgr::onSelectParentFeature()
494 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
495 if (aObjects.size() != 1)
498 SessionPtr aMgr = ModelAPI_Session::get();
499 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
503 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
504 QObjectPtrList aSelection;
505 aSelection.append( aParentFeature );
506 myModule->workshop()->selection()->setSelectedObjects( aSelection );