PD: fix ProfileBased::Restore to not affect sub-classes that need to handle changed property types/names

This commit is contained in:
wmayer
2021-09-15 15:45:24 +02:00
parent 2af9c5038e
commit fff27ee78b
2 changed files with 29 additions and 53 deletions

View File

@@ -1196,57 +1196,32 @@ Base::Vector3d ProfileBased::getProfileNormal() const {
return SketchVector;
}
void ProfileBased::Restore(Base::XMLReader& reader) {
reader.readElement("Properties");
int Cnt = reader.getAttributeAsInteger("Count");
for (int i=0 ;i<Cnt ;i++) {
reader.readElement("Property");
const char* PropName = reader.getAttribute("name");
const char* TypeName = reader.getAttribute("type");
App::Property* prop = getPropertyByName(PropName);
// NOTE: We must also check the type of the current property because a
// subclass of PropertyContainer might change the type of a property but
// not its name. In this case we would force to read-in a wrong property
// type and the behaviour would be undefined.
try {
//check if we load the old sketch property
if(!prop && (strcmp("Sketch", PropName) == 0) && (strcmp("App::PropertyLink", TypeName) == 0)) {
std::vector<std::string> vec;
// read my element
reader.readElement("Link");
// get the value of my attribute
std::string name = reader.getAttribute("value");
if (name != "") {
App::Document* document = getDocument();
DocumentObject* object = document ? document->getObject(name.c_str()) : 0;
Profile.setValue(object, vec);
}
else {
Profile.setValue(0, vec);
}
}
else if (prop && strcmp(prop->getTypeId().getName(), TypeName) == 0)
prop->Restore(reader);
}
catch (const Base::XMLParseException&) {
throw; // re-throw
}
catch (const Base::Exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const std::exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const char* e) {
Base::Console().Error("%s\n", e);
}
reader.readEndElement("Property");
}
reader.readEndElement("Properties");
void ProfileBased::Restore(Base::XMLReader& reader)
{
PartDesign::FeatureAddSub::Restore(reader);
}
void ProfileBased::handleChangedPropertyName(Base::XMLReader &reader, const char * TypeName, const char *PropName)
{
//check if we load the old sketch property
if ((strcmp("Sketch", PropName) == 0) && (strcmp("App::PropertyLink", TypeName) == 0)) {
std::vector<std::string> vec;
// read my element
reader.readElement("Link");
// get the value of my attribute
std::string name = reader.getAttribute("value");
if (name != "") {
App::Document* document = getDocument();
DocumentObject* object = document ? document->getObject(name.c_str()) : 0;
Profile.setValue(object, vec);
}
else {
Profile.setValue(0, vec);
}
}
else {
PartDesign::FeatureAddSub::handleChangedPropertyName(reader, TypeName, PropName);
}
}

View File

@@ -112,6 +112,7 @@ public:
//backwards compatibility: profile property was renamed and has different type now
virtual void Restore(Base::XMLReader& reader);
virtual void handleChangedPropertyName(Base::XMLReader &reader, const char * TypeName, const char *PropName);
// calculate the through all length
double getThroughAllLength() const;