QString GLC_WorldTo3dxml::representationFileName(const GLC_3DRep* pRep)
{
	Q_ASSERT(m_ReferenceRepToIdHash.contains(pRep));
	QString repName= pRep->name();
	QString fileName;
	if (m_ExportType == StructureOnly)
	{
		QString newFileName= pRep->fileName();
		// Test if the file name is encoded by GLC_Lib (Structure only loaded)
		if (glc::isFileString(newFileName))
		{
			newFileName= glc::archiveEntryFileName(newFileName);
		}
		if (newFileName.isEmpty() || (glc::isArchiveString(newFileName)))
		{
			fileName= "urn:3DXML:NoFile_0.3DRep";
		}
		else
		{
			// Compute the relative fileName from the structure
			QDir structureDir(m_AbsolutePath);
			QString relativeFilePath= structureDir.relativeFilePath(newFileName);
			fileName= "urn:3DXML:" + relativeFilePath;
		}
	}
	else if (repName.isEmpty())
	{
		fileName= "urn:3DXML:Representation_0.3DRep";
	}
	else
	{
		fileName= "urn:3DXML:" + repName + ".3DRep";
	}
	return xmlFileName(fileName);
}
FFmpegPresets::FFmpegPresets()
{
   mPresets = new FFmpegPresetList();
   XMLFileReader xmlfile;
   wxFileName xmlFileName(FileNames::DataDir(), wxT("ffmpeg_presets.xml"));
   xmlfile.Parse(this,xmlFileName.GetFullPath());
}
FFmpegPresets::~FFmpegPresets()
{
   XMLFileWriter writer;
   wxFileName xmlFileName(FileNames::DataDir(), wxT("ffmpeg_presets.xml"));
   writer.Open(xmlFileName.GetFullPath(),wxT("wb"));
   WriteXMLHeader(writer);
   WriteXML(writer);
   delete mPresets;
}
void GLC_WorldTo3dxml::writeCATRepresentationImage(const GLC_Material* pMat, unsigned int id)
{
	Q_ASSERT(pMat->hasTexture());
	m_MaterialIdTo3dxmlImageId.insert(pMat->id(), id);

	const QString imageName= pMat->name();
	QString imageFileName;
	QString imageFormat;
	const QString textureFileName= pMat->textureHandle()->fileName();
	if (textureFileName.isEmpty())
	{
		imageFormat= "jpg";
		if (imageName.isEmpty())
		{
			imageFileName= xmlFileName("textureImage." + imageFormat);
		}
		else
		{
			imageFileName= xmlFileName(imageName + '.' + imageFormat);
		}
	}
	else
	{
		imageFormat= QFileInfo(textureFileName).suffix();
		imageFileName= xmlFileName(QFileInfo(textureFileName).fileName());
	}
	m_MaterialIdToTexture3dxmlName.insert(pMat->id(), imageFileName);

	m_pOutStream->writeStartElement("CATRepresentationImage");
		m_pOutStream->writeAttribute("xsi:type", "CATRepresentationImageType");
		m_pOutStream->writeAttribute("id", QString::number(id));
		m_pOutStream->writeAttribute("name", imageName);
		m_pOutStream->writeAttribute("format", imageFormat);
		m_pOutStream->writeAttribute("associatedFile", QString("urn:3DXML:" + imageFileName));

		m_pOutStream->writeTextElement("PLM_ExternalID", imageName);
		m_pOutStream->writeTextElement("V_PrimaryMimeType", imageFormat);
		m_pOutStream->writeTextElement("V_PrimaryFileName", imageFileName);
	m_pOutStream->writeEndElement(); // CATRepresentationImage
}
void ConvertFiles()
{
    Safir::Dob::Typesystem::BinarySerialization bin;
    boost::filesystem::path storagePath = GetStorageDirectory();

    for (boost::filesystem::directory_iterator it (storagePath);
         it != boost::filesystem::directory_iterator(); ++it)
    {
        const boost::filesystem::path path = it->path();
        const EntityIdAndHandlerId id = Filename2EntityIdAndHandlerId(*it);

        if (path.extension() == ".bin")
        {
            const size_t fileSize = static_cast<size_t>(boost::filesystem::file_size(path));
            if (fileSize == 0)
            {
                continue;
            }
            bin.resize(fileSize);

            size_t numBytesRead = 0;
            boost::filesystem::ifstream file(path, std::ios::in | std::ios::binary);
            while (file.good())
            {
                file.read(&bin[0] + numBytesRead,4096);
                numBytesRead += static_cast<size_t>(file.gcount());
            }

            if(fileSize != numBytesRead)

            {
                throw Safir::Dob::Typesystem::SoftwareViolationException(L"Stupid error in file reading, probably", __WFILE__, __LINE__);
            }
            file.close();

            const Safir::Dob::Typesystem::ObjectPtr object = Safir::Dob::Typesystem::Serialization::ToObject(bin);

            const std::wstring xml = Safir::Dob::Typesystem::Serialization::ToXml(object);
            boost::filesystem::path xmlFileName(path);
            xmlFileName.replace_extension(".xml");
            boost::filesystem::wofstream xmlFile(xmlFileName);
            xmlFile << xml;
        }
    }
}
void GLC_WorldTo3dxml::writeMaterial(const GLC_Material* pMaterial)
{
	const GLC_uint materialId= pMaterial->id();
	QString materialName;
	if (pMaterial->name().isEmpty())
	{
		materialName= "Material_0" + QString::number(materialId);
	}
	else
	{
		materialName= symplifyName(pMaterial->name());

		// If the materialName is already uses append material id to the name
		QSet<QString> materialsName= QSet<QString>::fromList(m_MaterialIdToMaterialName.values());
		while (materialsName.contains(materialName))
		{
			materialName= materialName + '_' + QString::number(materialId);
		}
	}

	m_MaterialIdToMaterialName.insert(materialId, materialName);

	const QString fileName= xmlFileName(materialName + "_Rendering.3DRep");
	setStreamWriterToFile(fileName);

	// Begin to write the material file
	m_pOutStream->writeStartDocument();
	m_pOutStream->writeStartElement("Osm");
		m_pOutStream->writeAttribute("xmlns", "http://www.3ds.com/xsd/osm.xsd");
		m_pOutStream->writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
		m_pOutStream->writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
		m_pOutStream->writeAttribute("xsi:schemaLocation", "http://www.3ds.com/xsd/3DXML ./osm.xsd");
		m_pOutStream->writeStartElement("Feature");
			m_pOutStream->writeAttribute("Alias", "RenderingRootFeature");
			m_pOutStream->writeAttribute("Id", "1");
			m_pOutStream->writeAttribute("StartUp", "RenderingRootFeature");
		m_pOutStream->writeEndElement(); // Feature
		m_pOutStream->writeStartElement("Feature");
			m_pOutStream->writeAttribute("Alias", "RenderingFeature");
			m_pOutStream->writeAttribute("Id", "2");
			m_pOutStream->writeAttribute("StartUp", "RenderingFeature");
			m_pOutStream->writeAttribute("Aggregating", "1");

			writeMaterialAttributtes("Filtering", "int", "1");
			writeMaterialAttributtes("Rotation", "double", "0");
			writeMaterialAttributtes("PreviewType", "int", "1");
			writeMaterialAttributtes("AmbientCoef", "double", "1");
			writeMaterialAttributtes("DiffuseCoef", "double", "1");
			writeMaterialAttributtes("SpecularCoef", "double", "1");
			writeMaterialAttributtes("EmissiveCoef", "double", "0");
			writeMaterialAttributtes("AlphaTest", "boolean", "false");
			writeMaterialAttributtes("TextureFunction", "int", "0");
			writeMaterialAttributtes("MappingType", "int", "2");
			writeMaterialAttributtes("Refraction", "double", "1");
			writeMaterialAttributtes("TextureDimension", "int", "2");
			writeMaterialAttributtes("TranslationU", "double", "0");
			writeMaterialAttributtes("TranslationV", "double", "0");
			writeMaterialAttributtes("FlipU", "boolean", "false");
			writeMaterialAttributtes("FlipV", "boolean", "false");
			writeMaterialAttributtes("WrappingModeU", "int", "1");
			writeMaterialAttributtes("WrappingModeV", "int", "1");
			writeMaterialAttributtes("ScaleU", "double", "1");
			writeMaterialAttributtes("ScaleV", "double", "1");
			writeMaterialAttributtes("Reflectivity", "double", "0.1");
			writeMaterialAttributtes("BlendColor", "double", "[1,1,1]");

			writeMaterialAttributtes("Transparency", "double", QString::number(1.0 - pMaterial->opacity()));
			double specularExponent= pMaterial->shininess() / 128.0;
			writeMaterialAttributtes("SpecularExponent", "double", QString::number(specularExponent));
			writeMaterialAttributtes("DiffuseColor", "double", colorToString(pMaterial->diffuseColor()));
			writeMaterialAttributtes("SpecularColor", "double", colorToString(pMaterial->specularColor()));
			writeMaterialAttributtes("AmbientColor", "double", colorToString(pMaterial->ambientColor()));
			writeMaterialAttributtes("EmissiveColor", "double", colorToString(pMaterial->emissiveColor()));
			if (pMaterial->hasTexture())
			{
				Q_ASSERT(m_MaterialIdTo3dxmlImageId.contains(pMaterial->id()));
				const QString imageId(QString::number(m_MaterialIdTo3dxmlImageId.value(pMaterial->id())));

				writeMaterialAttributtes("TextureImage", "external", "urn:3DXML:CATRepImage.3dxml#" + imageId);
			}
		m_pOutStream->writeEndElement(); // Feature
	m_pOutStream->writeEndElement(); // Osm
	m_pOutStream->writeEndDocument();
}