Example #1
0
void AutoSaveProperty::slotNewObject(const App::DocumentObject& obj)
{
    std::vector<App::Property*> props;
    obj.getPropertyList(props);

    // if an object was deleted and then restored by an undo then add all properties
    // because this might be the data files which we may want to re-write
    for (std::vector<App::Property*>::iterator it = props.begin(); it != props.end(); ++it) {
        slotChangePropertyData(*(*it));
    }
}
App::DocumentObject* TaskFeaturePick::makeCopy(App::DocumentObject* obj, std::string sub, bool independent) {
    
    App::DocumentObject* copy = nullptr;
    // Check for null to avoid segfault
    if (!obj)
        return copy;
    if( independent &&
        (obj->isDerivedFrom(Sketcher::SketchObject::getClassTypeId()) ||
        obj->isDerivedFrom(PartDesign::FeaturePrimitive::getClassTypeId()))) {

        //we do know that the created instance is a document object, as obj is one. But we do not know which
        //exact type
        auto name =  std::string("Copy") + std::string(obj->getNameInDocument());
        copy = App::GetApplication().getActiveDocument()->addObject(obj->getTypeId().getName(), name.c_str());

        //copy over all properties
        std::vector<App::Property*> props;
        std::vector<App::Property*> cprops;
        obj->getPropertyList(props);
        copy->getPropertyList(cprops);

        auto it = cprops.begin();
        for( App::Property* prop : props ) {

            //independent copies don't have links and are not attached
            if(independent && (
                prop->getTypeId().isDerivedFrom(App::PropertyLink::getClassTypeId()) ||
                prop->getTypeId().isDerivedFrom(App::PropertyLinkList::getClassTypeId()) ||
                prop->getTypeId().isDerivedFrom(App::PropertyLinkSub::getClassTypeId()) ||
                prop->getTypeId().isDerivedFrom(App::PropertyLinkSubList::getClassTypeId())||
                ( prop->getGroup() && strcmp(prop->getGroup(),"Attachment")==0) ))    {

                ++it;
                continue;
            }

            App::Property* cprop = *it++;

            if( strcmp(prop->getName(), "Label") == 0 ) {
                static_cast<App::PropertyString*>(cprop)->setValue(name.c_str());
                continue;
            }

            cprop->Paste(*prop);

            //we are a independent copy, therefore no external geometry was copied. WE therefore can delete all
            //constraints
            if(obj->isDerivedFrom(Sketcher::SketchObject::getClassTypeId()))
                static_cast<Sketcher::SketchObject*>(copy)->delConstraintsToExternal();
        }
    }
    else {

        std::string name;
        if(!independent)
            name = std::string("Reference");
        else
            name = std::string("Copy");
        name += std::string(obj->getNameInDocument());

        std::string entity;
        if(!sub.empty())
            entity = sub;

        Part::PropertyPartShape* shapeProp = nullptr;

        // TODO Replace it with commands (2015-09-11, Fat-Zer)
        if(obj->isDerivedFrom(Part::Datum::getClassTypeId())) {
            copy = App::GetApplication().getActiveDocument()->addObject(
                    obj->getClassTypeId().getName(), name.c_str() );

            //we need to reference the individual datums and make again datums. This is important as
            //datum adjust their size dependent on the part size, hence simply copying the shape is
            //not enough
            long int mode = mmDeactivated;
            Part::Datum *datumCopy = static_cast<Part::Datum*>(copy);

            if(obj->getTypeId() == PartDesign::Point::getClassTypeId()) {
                mode = mm0Vertex;
            }
            else if(obj->getTypeId() == PartDesign::Line::getClassTypeId()) {
                mode = mm1TwoPoints;
            }
            else if(obj->getTypeId() == PartDesign::Plane::getClassTypeId()) {
                mode = mmFlatFace;
            }
            else
                return copy;

            // TODO Recheck this. This looks strange in case of independent copy (2015-10-31, Fat-Zer)
            if(!independent) {
                datumCopy->Support.setValue(obj, entity.c_str());
                datumCopy->MapMode.setValue(mode);
            }
            else if(!entity.empty()) {
                datumCopy->Shape.setValue(static_cast<Part::Datum*>(obj)->Shape.getShape().getSubShape(entity.c_str()));
            } else {
                datumCopy->Shape.setValue(static_cast<Part::Datum*>(obj)->Shape.getValue());
            }
        }
        else if(obj->getTypeId() == PartDesign::ShapeBinder::getClassTypeId() ||
                obj->isDerivedFrom(Part::Feature::getClassTypeId())) {

            copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder", name.c_str());

            if(!independent)
                static_cast<PartDesign::ShapeBinder*>(copy)->Support.setValue(obj, entity.c_str());
            else
                shapeProp = &static_cast<PartDesign::ShapeBinder*>(copy)->Shape;
        }

        if(independent && shapeProp) {
            if(entity.empty())
                shapeProp->setValue(static_cast<Part::Feature*>(obj)->Shape.getValue());
            else
                shapeProp->setValue(static_cast<Part::Feature*>(obj)->Shape.getShape().getSubShape(entity.c_str()));
        }
    }

    return copy;
}