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 <GeomAPI_Pnt2d.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <SketchPlugin_ConstraintCoincidence.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Circle.h>
18 #include <SketchPlugin_Point.h>
19 #include <SketchPlugin_Sketch.h>
21 #include <ModuleBase_ISelection.h>
22 #include <ModuleBase_Operation.h>
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_Workshop.h>
26 #include <XGUI_Displayer.h>
28 #include <Events_Loop.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Session.h>
36 #include <BRep_Tool.hxx>
38 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
39 : QObject(theModule), myModule(theModule), myPrevId(-1)
45 QAction* PartSet_MenuMgr::action(const QString& theId) const
47 if (myActions.contains(theId))
48 return myActions[theId];
52 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
54 if (myActions.contains(theId))
55 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
56 theAction->setData(theId);
57 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
58 myActions[theId] = theAction;
61 void PartSet_MenuMgr::createActions()
65 anAction = new QAction(tr("Auxiliary"), this);
66 anAction->setCheckable(true);
67 addAction("AUXILIARY_CMD", anAction);
71 void PartSet_MenuMgr::onAction(bool isChecked)
73 QAction* aAction = static_cast<QAction*>(sender());
74 QString anId = aAction->data().toString();
76 if (anId == "AUXILIARY_CMD") {
77 setAuxiliary(isChecked);
81 /// Returns point of coincidence feature
82 /// \param theFeature the coincidence feature
83 /// \param theAttribute the attribute name
84 std::shared_ptr<GeomAPI_Pnt2d> getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
85 const std::string& theAttribute)
87 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
89 if (!theFeature->data())
90 return std::shared_ptr<GeomAPI_Pnt2d>();
93 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
94 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
96 aFeature = ModelAPI_Feature::feature(anAttr->object());
98 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
99 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
102 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
103 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
106 else if (anAttr->attr()) {
107 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
109 if (aPointAttr.get() != NULL)
110 return aPointAttr->pnt();
111 return std::shared_ptr<GeomAPI_Pnt2d>();
114 /// Returns list of features connected in a councedence feature point
115 /// \param theStartCoin the coincidence feature
116 /// \param theList a list which collects lines features
117 /// \param theAttr the attribute name
118 void findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList, std::string theAttr)
120 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
121 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
122 if (!theList.contains(aObj)) {
123 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(theStartCoin, theAttr);
124 theList.append(aObj);
125 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
126 std::set<AttributePtr>::const_iterator aIt;
127 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
128 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
129 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
130 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
131 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getPoint(aConstrFeature, theAttr);
132 if (aOrig->isEqual(aPnt)) {
133 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
134 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
142 bool PartSet_MenuMgr::addViewerItems(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
144 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
145 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
146 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
149 myCoinsideLines.clear();
150 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
152 bool aIsDetach = false;
153 bool hasAttribute = false;
154 bool hasFeature = false;
156 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected();
160 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
161 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
162 if (aResult.get() != NULL) {
163 aShape = aPrs.shape();
164 if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
169 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
170 hasFeature = (aFeature.get() != NULL);
174 if (aPrsList.size() == 1) {
175 TopoDS_Shape aShape = aPrsList.first().shape();
176 if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
177 // Find 2d coordinates
178 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
179 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
180 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
181 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
182 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
184 // Find coincident in these coordinates
185 ObjectPtr aObj = aPrsList.first().object();
186 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
187 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
188 std::set<AttributePtr>::const_iterator aIt;
189 FeaturePtr aCoincident;
190 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
191 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
192 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
193 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
194 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
195 if (aSelPnt->isEqual(a2dPnt)) {
196 aCoincident = aConstrFeature;
201 // If we have coincidence then add Detach menu
202 if (aCoincident.get() != NULL) {
203 mySelectedFeature = aCoincident;
204 findCoincidences(mySelectedFeature, myCoinsideLines, SketchPlugin_ConstraintCoincidence::ENTITY_A());
205 findCoincidences(mySelectedFeature, myCoinsideLines, SketchPlugin_ConstraintCoincidence::ENTITY_B());
206 if (myCoinsideLines.size() > 0) {
208 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
211 foreach (FeaturePtr aCoins, myCoinsideLines) {
212 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
213 aAction->setData(QVariant(i));
216 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
217 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
218 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
224 if ((!aIsDetach) && hasFeature) {
225 theMenu->addAction(theStdActions["DELETE_CMD"]);
230 if (canSetAuxiliary(isAuxiliary)) {
231 QAction* anAction = action("AUXILIARY_CMD");
232 theMenu->addAction(anAction);
233 anAction->setChecked(isAuxiliary);
238 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
240 if (myPrevId != -1) {
241 // Restore color for previous object
242 setLineColor(myPrevId, myColor, false);
244 myPrevId = theAction->data().toInt();
245 myColor = setLineColor(myPrevId, Qt::white, true);
248 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
250 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
251 XGUI_Workshop* aWorkshop = aConnector->workshop();
252 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
254 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
255 std::list<ResultPtr>::const_iterator aIt;
256 const std::list<ResultPtr>& aResults = aLine->results();
258 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
259 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
262 aDisplayer->updateViewer();
267 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
269 int aId = theAction->data().toInt();
270 FeaturePtr aLine = myCoinsideLines.at(aId);
271 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(mySelectedFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
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 = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
283 gp_Pnt aP = aPnt->impl<gp_Pnt>();
284 if (aOrig->isEqual(aPnt)) {
285 aToDelFeatures.append(aConstrFeature);
287 aPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B());
288 aP = aPnt->impl<gp_Pnt>();
289 if (aOrig->isEqual(aPnt)) {
290 aToDelFeatures.append(aConstrFeature);
296 if (aToDelFeatures.size() > 0) {
297 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
298 XGUI_Workshop* aWorkshop = aConnector->workshop();
299 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
300 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
301 anOperation->abort();
303 SessionPtr aMgr = ModelAPI_Session::get();
304 std::set<FeaturePtr> anIgnoredFeatures;
305 anIgnoredFeatures.insert(myModule->sketchMgr()->activeSketch());
307 QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
308 aMgr->startOperation(aName.toStdString());
309 aWorkshop->deleteFeatures(aToDelFeatures, anIgnoredFeatures);
310 aMgr->finishOperation();
312 myCoinsideLines.clear();
316 void PartSet_MenuMgr::onDetachMenuHide()
318 if (myPrevId != -1) {
319 // Restore color for previous object
320 setLineColor(myPrevId, myColor, false);
322 // Clear previous definitions
327 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
329 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
331 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
332 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
336 QObjectPtrList anObjects;
337 bool isUseTransaction = false;
338 // 1. change auxiliary type of a created feature
339 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
340 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
341 anObjects.append(anOperation->feature());
344 isUseTransaction = true;
345 // 2. change auxiliary type of selected sketch entities
346 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
347 anObjects = aSelection->selectedPresentations();
350 QAction* anAction = action("AUXILIARY_CMD");
351 SessionPtr aMgr = ModelAPI_Session::get();
352 if (isUseTransaction) {
353 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
354 anOperation->abort();
355 aMgr->startOperation(anAction->text().toStdString());
357 myModule->sketchMgr()->storeSelection();
359 if (anObjects.size() > 0) {
360 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
361 for (; anIt != aLast; anIt++) {
362 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
363 if (aFeature.get() != NULL) {
364 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
365 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
366 if (aSketchFeature.get() != NULL) {
367 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
369 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
370 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
372 anAuxiliaryAttr->setValue(isChecked);
377 if (isUseTransaction) {
378 aMgr->finishOperation();
379 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
380 XGUI_Workshop* aWorkshop = aConnector->workshop();
381 aWorkshop->updateCommandStatus();
384 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
385 myModule->sketchMgr()->restoreSelection();
388 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
390 bool anEnabled = false;
391 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
393 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
394 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
398 QObjectPtrList anObjects;
399 // 1. change auxiliary type of a created feature
400 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
401 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
402 anObjects.append(anOperation->feature());
405 /// The operation should not be aborted here, because the method does not changed
406 /// the auxilliary state, but checks the possibility to perform this
407 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
408 /// anOperation->abort();
409 // 2. change auxiliary type of selected sketch entities
410 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
411 anObjects = aSelection->selectedPresentations();
414 bool isNotAuxiliaryFound = false;
415 if (anObjects.size() > 0) {
416 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
417 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
418 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
419 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
421 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
422 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
423 if (aSketchFeature.get() != NULL) {
424 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
426 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
427 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
429 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
434 theValue = anObjects.size() && !isNotAuxiliaryFound;