private void toolStripMenuItemToolsConvert_Click(object sender, EventArgs e)
{
using(OpenFileDialog dlgImport = new OpenFileDialog())
{
dlgImport.Title = "Convert [Step 1 of 2]: Choose the input file";
dlgImport.Filter = "IFC-SPF (*.ifc)|*.ifc";
if(dlgImport.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
using(SaveFileDialog dlgExport = new SaveFileDialog())
{
dlgExport.Filter =
"IFC-JSN (*.ifcjsn)|*.ifcjsn|"+
"IFC-RDF (*.ttl)|*.ttl|" +
"IFC-XML (*.ifcxml)|*.ifcxml";
dlgExport.Title = "Convert [Step 2 of 2]: Specify the output file and format";
dlgExport.FileName = System.IO.Path.GetFileNameWithoutExtension(dlgImport.FileName);
if(dlgExport.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
//todo: run in background, show progress
Dictionary<string, Type> typemap = new Dictionary<string, Type>();
Compiler compiler = new Compiler(this.m_project, this.m_filterviews, this.m_filterexchange);
System.Reflection.Emit.AssemblyBuilder assembly = compiler.Assembly;
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
typemap.Add(t.Name.ToUpper(), t);
}
Dictionary<long, SEntity> instances = new Dictionary<long, SEntity>();
try
{
m_loading = true;
using (FormatSPF format = new FormatSPF(dlgImport.FileName, typemap, instances))
{
format.Load();
}
}
catch(Exception xx)
{
MessageBox.Show(xx.Message);
return;
}
finally
{
this.m_loading = false;
}
// build dictionary to map IFC type name to entity and schema
Dictionary<string, DocObject> mapEntity = new Dictionary<string, DocObject>();
// build dictionary to map IFC type name to schema
Dictionary<string, string> mapSchema = new Dictionary<string, string>();
this.BuildMaps(mapEntity, mapSchema);
// find the IfcProject
SEntity rootproject = null;
foreach (SEntity ent in instances.Values)
{
if (ent.GetType().Name.Equals("IfcProject"))
{
rootproject = ent;
break;
}
}
//TODO: use schema according to source file, look up publication...
List<DocXsdFormat> xsdFormatBase = this.m_project.BuildXsdFormatList();
string xmlns = "http://www.buildingsmart-tech.org/ifcXML/IFC4/final";
string code = "IFC4";//...
string ifcowlns = "http://ifcowl.openbimstandards.org/IFC4_ADD1";
try
{
IFormatData formatter = null;
switch (dlgExport.FilterIndex)
{
case 1:
formatter = new FormatJSN(xsdFormatBase, xmlns, code);
break;
case 2:
formatter = new FormatTTL_Stream(new System.IO.MemoryStream(), ifcowlns);
break;
case 3:
formatter = new FormatSML(new System.IO.MemoryStream(), xsdFormatBase, xmlns, code);
break;
}
if (formatter != null)
{
string content = formatter.FormatData(this.m_project, null, null, mapEntity, instances, rootproject, false);
using (System.IO.FileStream filestream = System.IO.File.OpenWrite(dlgExport.FileName))
{
System.IO.TextWriter writer = new System.IO.StreamWriter(filestream);
writer.Write(content);
writer.Flush();
filestream.Close();
}
}
}
catch (Exception yy)
{
MessageBox.Show(yy.Message);
return;
}
}
}
}
}
//...
}