Skip to content

Commit

Permalink
+ fixes #2029: VRML inline ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Mar 29, 2015
1 parent 88ef0b5 commit 995de94
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/App/PropertyFile.cpp
Expand Up @@ -113,12 +113,20 @@ std::string PropertyFileIncluded::getExchangeTempFile(void) const
(getValue()).fileName().c_str(), getDocTransientPath().c_str());
}

std::string PropertyFileIncluded::getOriginalFileName(void) const
{
return _OriginalName;
}

void PropertyFileIncluded::setValue(const char* sFile, const char* sName)
{
if (sFile && sFile[0] != '\0') {
if (_cValue == sFile)
throw Base::Exception("Not possible to set the same file!");

// keep the path to the original file
_OriginalName = sFile;

std::string pathTrans = getDocTransientPath();
Base::FileInfo file(sFile);
std::string path = file.dirPath();
Expand Down
2 changes: 2 additions & 0 deletions src/App/PropertyFile.h
Expand Up @@ -103,6 +103,7 @@ class AppExport PropertyFileIncluded : public Property
* the File.
*/
std::string getExchangeTempFile(void) const;
std::string getOriginalFileName(void) const;

protected:
// get the transient path if the property is in a DocumentObject
Expand All @@ -113,6 +114,7 @@ class AppExport PropertyFileIncluded : public Property
protected:
mutable std::string _cValue;
mutable std::string _BaseFileName;
mutable std::string _OriginalName;
};


Expand Down
111 changes: 75 additions & 36 deletions src/App/VRMLObject.cpp
Expand Up @@ -42,8 +42,12 @@ PROPERTY_SOURCE(App::VRMLObject, App::GeoFeature)
VRMLObject::VRMLObject()
{
ADD_PROPERTY_TYPE(VrmlFile,(0),"",Prop_None,"Included file with the VRML definition");
ADD_PROPERTY_TYPE(Urls,(""),"",static_cast<PropertyType>(Prop_ReadOnly|Prop_Output),"Included file with the VRML definition");
ADD_PROPERTY_TYPE(Urls,(""),"",static_cast<PropertyType>(Prop_ReadOnly|Prop_Output|Prop_Transient),
"Resource files loaded by the VRML file");
ADD_PROPERTY_TYPE(Resources,(""),"",static_cast<PropertyType>(Prop_ReadOnly|Prop_Output),
"Resource files loaded by the VRML file");
Urls.setSize(0);
Resources.setSize(0);
}

VRMLObject::~VRMLObject()
Expand All @@ -55,6 +59,29 @@ short VRMLObject::mustExecute(void) const
return 0;
}

void VRMLObject::onChanged(const App::Property* prop)
{
if (prop == &VrmlFile) {
std::string orig = VrmlFile.getOriginalFileName();
if (!orig.empty()) {
// store the path name of the VRML file
Base::FileInfo fi(orig);
this->vrmlPath = fi.dirPath();
}
}
else if (prop == &Urls) {
// save the relative paths to the resource files in the project file
Resources.setSize(Urls.getSize());
const std::vector<std::string>& urls = Urls.getValues();
int index=0;
for (std::vector<std::string>::const_iterator it = urls.begin(); it != urls.end(); ++it, ++index) {
std::string output = getOutputFile(this->vrmlPath, *it);
Resources.set1Value(index, output);
}
}
GeoFeature::onChanged(prop);
}

PyObject *VRMLObject::getPyObject()
{
if (PythonObject.is(Py::_None())){
Expand All @@ -64,27 +91,46 @@ PyObject *VRMLObject::getPyObject()
return Py::new_reference_to(PythonObject);
}

std::string VRMLObject::getOutputFile(const std::string& prefix, const std::string& resource) const
{
std::string str;
std::string intname = this->getNameInDocument();
if (!prefix.empty()) {
if (resource.substr(0, prefix.size()) == prefix) {
std::string suffix = resource.substr(prefix.size());
str = intname + suffix;
}
}

if (str.empty()) {
Base::FileInfo fi(resource);
str = intname + "/" + fi.fileName();
}

return str;
}

void VRMLObject::makeDirectories(const std::string& path, const std::string& subdir)
{
std::string::size_type pos = subdir.find('/');
while (pos != std::string::npos) {
std::string sub = subdir.substr(0, pos);
std::string dir = path + "/" + sub;
Base::FileInfo fi(dir);
if (!fi.createDirectory())
break;
pos = subdir.find('/', pos+1);
}
}

void VRMLObject::Save (Base::Writer &writer) const
{
App::GeoFeature::Save(writer);

// if the VRML file has some inline files then store them inside the project file
if (Urls.getSize() > 0) {
const std::vector<std::string>& urls = Urls.getValues();
writer.incInd();
writer.Stream() << writer.ind() << "<UrlList count=\"" << urls.size() <<"\">" << std::endl;
writer.incInd();
std::string intname = this->getNameInDocument();
for (std::vector<std::string>::const_iterator it = urls.begin(); it != urls.end(); ++it) {
Base::FileInfo fi(*it);
// make sure to put the VRML files into a sub-directory
std::string output = intname + "/" + fi.fileName();
output = writer.addFile(output.c_str(), this);
writer.Stream() << writer.ind() << "<Url value=\"" << output <<"\"/>" << std::endl;
}
writer.decInd();
writer.Stream() << writer.ind() << "</UrlList>" << std::endl;
writer.decInd();
// save also the inline files if there
const std::vector<std::string>& urls = Resources.getValues();
for (std::vector<std::string>::const_iterator it = urls.begin(); it != urls.end(); ++it) {
writer.addFile(it->c_str(), this);
}

this->index = 0;
Expand All @@ -93,18 +139,12 @@ void VRMLObject::Save (Base::Writer &writer) const
void VRMLObject::Restore(Base::XMLReader &reader)
{
App::GeoFeature::Restore(reader);
Urls.setSize(Resources.getSize());

// are there inline files
if (Urls.getSize() > 0) {
reader.readElement("UrlList");
int count = reader.getAttributeAsInteger("count");
for(int i = 0; i < count; i++) {
reader.readElement("Url");
std::string value = reader.getAttribute("value");
reader.addFile(value.c_str(), this);
}

reader.readEndElement("UrlList");
// restore also the inline files if there
const std::vector<std::string>& urls = Resources.getValues();
for(std::vector<std::string>::const_iterator it = urls.begin(); it != urls.end(); ++it) {
reader.addFile(it->c_str(), this);
}

this->index = 0;
Expand All @@ -127,16 +167,13 @@ void VRMLObject::SaveDocFile (Base::Writer &writer) const

void VRMLObject::RestoreDocFile(Base::Reader &reader)
{
if (this->index < Urls.getSize()) {
if (this->index < Resources.getSize()) {
std::string path = getDocument()->TransientDir.getValue();
std::string url = Urls[this->index];
std::string intname = this->getNameInDocument();
std::string url = Resources[this->index];
makeDirectories(path, url);

url = path + "/" + url;
Base::FileInfo fi(url);
std::string subdir = path + "/" + intname;
Base::FileInfo(subdir).createDirectory();
url = subdir + "/" + fi.fileName();
fi.setFile(url);
Urls.set1Value(this->index, url);
this->index++;

Expand All @@ -149,6 +186,8 @@ void VRMLObject::RestoreDocFile(Base::Reader &reader)
// after restoring all inline files reload the VRML file
if (this->index == Urls.getSize()) {
VrmlFile.touch();
Base::FileInfo fi(VrmlFile.getValue());
this->vrmlPath = fi.dirPath();
}
}
}
7 changes: 7 additions & 0 deletions src/App/VRMLObject.h
Expand Up @@ -56,8 +56,15 @@ class AppExport VRMLObject : public GeoFeature

PropertyFileIncluded VrmlFile;
PropertyStringList Urls;
PropertyStringList Resources;

protected:
void onChanged(const App::Property*);
std::string getOutputFile(const std::string&, const std::string&) const;
void makeDirectories(const std::string&, const std::string&);

private:
mutable std::string vrmlPath;
mutable int index;
};

Expand Down

0 comments on commit 995de94

Please sign in to comment.