void CmdSketcherMapSketch::activated(int iMsg) { QString msg_str; try{ Attacher::eMapMode suggMapMode; std::vector<Attacher::eMapMode> validModes; //check that selection is valid for at least some mapping mode. Attacher::SuggestResult::eSuggestResult msgid = Attacher::SuggestResult::srOK; suggMapMode = SuggestAutoMapMode(&msgid, &msg_str, &validModes); App::Document* doc = App::GetApplication().getActiveDocument(); std::vector<App::DocumentObject*> sketches = doc->getObjectsOfType(Part::Part2DObject::getClassTypeId()); if (sketches.empty()) { QMessageBox::warning(Gui::getMainWindow(), qApp->translate(className(), "No sketch found"), qApp->translate(className(), "The document doesn't have a sketch")); return; } bool ok; QStringList items; for (std::vector<App::DocumentObject*>::iterator it = sketches.begin(); it != sketches.end(); ++it) items.push_back(QString::fromUtf8((*it)->Label.getValue())); QString text = QInputDialog::getItem(Gui::getMainWindow(), qApp->translate(className(), "Select sketch"), qApp->translate(className(), "Select a sketch from the list"), items, 0, false, &ok); if (!ok) return; int index = items.indexOf(text); Part2DObject &sketch = *(static_cast<Part2DObject*>(sketches[index])); // check circular dependency std::vector<Gui::SelectionObject> selobjs = Gui::Selection().getSelectionEx(); for (size_t i = 0 ; i < selobjs.size() ; ++i){ App::DocumentObject* part = static_cast<Part::Feature*>(selobjs[i].getObject()); if (!part) { assert(0); throw Base::Exception("Unexpected null pointer in CmdSketcherMapSketch::activated"); } std::vector<App::DocumentObject*> input = part->getOutList(); if (std::find(input.begin(), input.end(), &sketch) != input.end()) { throw ExceptionWrongInput(QT_TR_NOOP("Some of the selected objects depend on the sketch to be mapped. Circular dependencies are not allowed!")); } } //Ask for a new mode. //outline: // * find out the modes that are compatible with selection. // * Test if current mode is OK. // * fill in the dialog // * execute the dialog // * collect dialog result // * action bool bAttach = true; bool bCurIncompatible = false; // * find out the modes that are compatible with selection. eMapMode curMapMode = eMapMode(sketch.MapMode.getValue()); // * Test if current mode is OK. if (std::find(validModes.begin(), validModes.end(), curMapMode) == validModes.end()) bCurIncompatible = true; // * fill in the dialog validModes.insert(validModes.begin(), Attacher::mmDeactivated); if(bCurIncompatible) validModes.push_back(curMapMode); //bool ok; //already defined //QStringList items; //already defined items.clear(); items.push_back(QObject::tr("Don't attach")); int iSugg = 0;//index of the auto-suggested mode in the list of valid modes int iCurr = 0;//index of current mode in the list of valid modes for (size_t i = 0 ; i < validModes.size() ; ++i){ items.push_back(QString::fromLatin1(AttachEngine::getModeName(validModes[i]).c_str())); if (validModes[i] == curMapMode) { iCurr = items.size() - 1; items.back().append(bCurIncompatible? qApp->translate(className()," (incompatible with selection)") : qApp->translate(className()," (current)") ); } if (validModes[i] == suggMapMode){ iSugg = items.size() - 1; if(iSugg == 1){ iSugg = 0;//redirect deactivate to detach } else { items.back().append(qApp->translate(className()," (suggested)")); } } } // * execute the dialog text = QInputDialog::getItem(Gui::getMainWindow() ,qApp->translate(className(), "Sketch attachment") ,bCurIncompatible? qApp->translate(className(), "Current attachment mode is incompatible with the new selection. Select the method to attach this sketch to selected objects.") : qApp->translate(className(), "Select the method to attach this sketch to selected objects.") ,items , bCurIncompatible ? iSugg : iCurr , false , &ok); // * collect dialog result if (!ok) return; index = items.indexOf(text); if (index == 0){ bAttach = false; suggMapMode = Attacher::mmDeactivated; } else { bAttach = true; suggMapMode = validModes[index-1]; } // * action std::string featName = sketch.getNameInDocument(); if (bAttach) { App::PropertyLinkSubList support; Gui::Selection().getAsPropertyLinkSubList(support); std::string supportString = support.getPyReprString(); openCommand("Attach Sketch"); doCommand(Gui,"App.activeDocument().%s.MapMode = \"%s\"",featName.c_str(),AttachEngine::getModeName(suggMapMode).c_str()); doCommand(Gui,"App.activeDocument().%s.Support = %s",featName.c_str(),supportString.c_str()); commitCommand(); } else { openCommand("Detach Sketch"); doCommand(Gui,"App.activeDocument().%s.MapMode = \"%s\"",featName.c_str(),AttachEngine::getModeName(suggMapMode).c_str()); doCommand(Gui,"App.activeDocument().%s.Support = None",featName.c_str()); commitCommand(); } } catch (ExceptionWrongInput &e) { QMessageBox::warning(Gui::getMainWindow(), qApp->translate(className(), "Map sketch"), qApp->translate(className(), "Can't map a sketch to support:\n" "%1").arg(e.ErrMsg.length() ? e.ErrMsg : msg_str)); } }