コード例 #1
0
void StdCmdDuplicateSelection::activated(int iMsg)
{
    App::Document* act = App::GetApplication().getActiveDocument();
    if (!act)
        return; // no active document found
    Gui::Document* doc = Gui::Application::Instance->getDocument(act);
    std::vector<Gui::SelectionSingleton::SelObj> sel = Gui::Selection().getCompleteSelection();
    for (std::vector<SelectionSingleton::SelObj>::iterator it = sel.begin(); it != sel.end(); ++it) {
        if (!it->pObject)
            continue; // should actually not happen
        // create a copy of the object
        App::DocumentObject* copy = act->copyObject(it->pObject, false);
        if (!copy) // continue if no copy could be created
            continue;
        // mark all properties of the copy as "touched" which are touched in the original object
        std::map<std::string,App::Property*> props;
        it->pObject->getPropertyMap(props);
        std::map<std::string,App::Property*> copy_props;
        copy->getPropertyMap(copy_props);
        for (std::map<std::string,App::Property*>::iterator jt = props.begin(); jt != props.end(); ++jt) {
            if (jt->second->isTouched()) {
                std::map<std::string,App::Property*>::iterator kt;
                kt = copy_props.find(jt->first);
                if (kt != copy_props.end()) {
                    kt->second->touch();
                }
            }
        }

        Gui::Document* parent = Gui::Application::Instance->getDocument(it->pObject->getDocument());
        if (!parent || !doc)
            continue; // should not happen
        // copy the properties of the associated view providers
        Gui::ViewProvider* view = parent->getViewProvider(it->pObject);
        Gui::ViewProvider* copy_view = doc->getViewProvider(copy);
        copy_view->addDynamicProperties(view);
        if (!view || !copy_view)
            continue; // should not happen

        // get the properties of the view provider
        props.clear();
        view->getPropertyMap(props);
        copy_props.clear();
        copy_view->getPropertyMap(copy_props);
        for (std::map<std::string,App::Property*>::iterator jt = props.begin(); jt != props.end(); ++jt) {
            std::map<std::string,App::Property*>::iterator kt;
            kt = copy_props.find(jt->first);
            if (kt != copy_props.end()) {
                std::auto_ptr<App::Property> data(jt->second->Copy());
                if (data.get()) {
                    kt->second->Paste(*data);
                }
            }
        }
    }
}