+bool PartSet_Module::isSketchOperationActive() const
+{
+ ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
+
+ bool isSketchOp = aOperation && aOperation->id().toStdString() == SketchPlugin_Sketch::ID();
+ return isSketchOp;
+}
+
+bool PartSet_Module::isSketchFeatureOperationActive() const
+{
+ bool isCurrentSketchOp = false;
+ ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
+ if (aOperation) {
+ FeaturePtr aFeature = aOperation->feature();
+ std::shared_ptr<SketchPlugin_Feature> aSPFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+ isCurrentSketchOp = aSPFeature.get() != NULL;
+ }
+ return isCurrentSketchOp;
+}
+
+void PartSet_Module::createActions()
+{
+ QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
+ addAction("DELETE_PARTSET_CMD", aAction);
+}
+
+QAction* PartSet_Module::action(const QString& theId) const
+{
+ if (myActions.contains(theId))
+ return myActions[theId];
+ return 0;
+}
+
+void PartSet_Module::addAction(const QString& theId, QAction* theAction)
+{
+ if (myActions.contains(theId))
+ qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
+ theAction->setData(theId);
+ connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
+ myActions[theId] = theAction;
+}
+
+void PartSet_Module::onAction(bool isChecked)
+{
+ QAction* aAction = static_cast<QAction*>(sender());
+ QString anId = aAction->data().toString();
+
+ if (anId == "DELETE_PARTSET_CMD") {
+ deleteObjects();
+ }
+}
+
+void PartSet_Module::deleteObjects()
+{
+ bool isSketchOp = isSketchOperationActive();
+ if (!isSketchOp && !isSketchFeatureOperationActive())
+ return;
+
+ XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
+ XGUI_Workshop* aWorkshop = aConnector->workshop();
+
+ XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
+ if (!isSketchOp && anOpMgr->canStopOperation()) {
+ ModuleBase_Operation* aCurrentOp = anOpMgr->currentOperation();
+ if (aCurrentOp) {
+ aCurrentOp->abort();
+ }
+ }
+ // sketch feature should be skipped, only sub-features can be removed
+ // when sketch operation is active
+ CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
+
+ ModuleBase_ISelection* aSel = aConnector->selection();
+ QObjectPtrList aSelectedObj = aSel->selectedPresentations();
+
+ std::set<FeaturePtr> aRefFeatures;
+ foreach (ObjectPtr aObj, aSelectedObj)
+ {
+ //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+ //if (aPart) {
+ // TODO: check for what there is this condition. It is placed here historicaly because
+ // ther is this condition during remove features.
+ //} else {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
+ if (aFeature.get() != NULL) {
+ aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
+ }
+ //}
+ }
+
+ if (!aRefFeatures.empty()) {
+ QStringList aRefNames;
+ std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
+ aLast = aRefFeatures.end();
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = (*anIt);
+ if (aFeature == aSketch)
+ continue;
+ aRefNames.append((*anIt)->name().c_str());
+ }
+ if (!aRefNames.empty()) {
+ QString aNames = aRefNames.join(", ");
+
+ QMainWindow* aDesktop = aWorkshop->desktop();
+ QMessageBox::StandardButton aRes = QMessageBox::warning(
+ aDesktop, tr("Delete features"),
+ QString(tr("Selected features are used in the following features: %1.\
+ These features will be deleted also. Would you like to continue?")).arg(aNames),
+ QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
+ if (aRes != QMessageBox::Yes)
+ return;
+ }
+ }
+
+ SessionPtr aMgr = ModelAPI_Session::get();
+ aMgr->startOperation();
+ std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
+ aLast = aRefFeatures.end();
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aRefFeature = (*anIt);
+ if (aRefFeature == aSketch)
+ continue;
+ aRefFeature->document()->removeFeature(aRefFeature);
+ }
+
+ foreach (ObjectPtr aObj, aSelectedObj)
+ {
+ DocumentPtr aDoc = aObj->document();
+ //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+ //if (aPart) {
+ // if (aDoc == aMgr->activeDocument()) {
+ // aDoc->close();
+ // }
+ //} else {
+ //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
+ if (aFeature.get() != NULL) {
+ aDoc->removeFeature(aFeature);
+ }
+ //}
+ }
+ aWorkshop->displayer()->updateViewer();
+ //myDisplayer->updateViewer();
+ aMgr->finishOperation();
+}