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();
287 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
288 aMgr->startOperation(aName.toStdString());
289 aWorkshop->deleteFeatures(aToDelFeatures);
290 aMgr->finishOperation();
292 myCoinsideLines.clear();
296 void PartSet_MenuMgr::onDetachMenuHide()
298 if (myPrevId != -1) {
299 // Restore color for previous object
300 setLineColor(myPrevId, myColor, false);
302 // Clear previous definitions
307 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
309 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
311 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
312 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
316 QObjectPtrList anObjects;
317 bool isUseTransaction = false;
318 // 1. change auxiliary type of a created feature
319 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
320 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
321 anObjects.append(anOperation->feature());
324 isUseTransaction = true;
325 // 2. change auxiliary type of selected sketch entities
326 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
327 anObjects = aSelection->selectedPresentations();
330 QAction* anAction = action("AUXILIARY_CMD");
331 SessionPtr aMgr = ModelAPI_Session::get();
332 if (isUseTransaction) {
333 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
334 anOperation->abort();
335 aMgr->startOperation(anAction->text().toStdString());
337 myModule->sketchMgr()->storeSelection();
339 if (anObjects.size() > 0) {
340 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
341 for (; anIt != aLast; anIt++) {
342 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
343 if (aFeature.get() != NULL) {
344 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
345 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
346 if (aSketchFeature.get() != NULL) {
347 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
349 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
350 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
352 anAuxiliaryAttr->setValue(isChecked);
357 if (isUseTransaction) {
358 aMgr->finishOperation();
359 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
360 XGUI_Workshop* aWorkshop = aConnector->workshop();
361 aWorkshop->updateCommandStatus();
364 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
365 myModule->sketchMgr()->restoreSelection();
368 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
370 bool anEnabled = false;
371 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
373 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
374 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
378 QObjectPtrList anObjects;
379 // 1. change auxiliary type of a created feature
380 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
381 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
382 anObjects.append(anOperation->feature());
385 /// The operation should not be aborted here, because the method does not changed
386 /// the auxilliary state, but checks the possibility to perform this
387 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
388 /// anOperation->abort();
389 // 2. change auxiliary type of selected sketch entities
390 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
391 anObjects = aSelection->selectedPresentations();
394 bool isNotAuxiliaryFound = false;
395 if (anObjects.size() > 0) {
396 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
397 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
398 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
399 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
401 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
402 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
403 if (aSketchFeature.get() != NULL) {
404 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
406 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
407 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
409 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
414 theValue = anObjects.size() && !isNotAuxiliaryFound;
418 void PartSet_MenuMgr::onActivatePart(bool)
420 if (myModule->workshop()->currentOperation())
422 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
423 if (aObjects.size() > 0) {
424 ObjectPtr aObj = aObjects.first();
425 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
427 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
428 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
429 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
437 void PartSet_MenuMgr::onActivatePartSet(bool)
439 if (myModule->workshop()->currentOperation())
441 SessionPtr aMgr = ModelAPI_Session::get();
442 bool isNewTransaction = !aMgr->isOperation();
443 // activation may cause changes in current features in document, so it must be in transaction
444 if (isNewTransaction) {
445 aMgr->startOperation("Activation");
447 aMgr->setActiveDocument(aMgr->moduleDocument());
448 if (isNewTransaction) {
449 aMgr->finishOperation();
453 void PartSet_MenuMgr::onEdit(bool)
455 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
456 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
457 if (aFeature == NULL) {
458 ResultParameterPtr aParam =
459 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
460 if (aParam.get() != NULL) {
461 aFeature = ModelAPI_Feature::feature(aParam);
464 if (aFeature.get() != NULL)
465 myModule->editFeature(aFeature);