Tutorial: copy from FEM to SIN to SIF
A tutorial which demonstrates copying all data from a FEM file to a SIN file, and then exporting the file to SIF format.
First create a folder in which temporary data for the reader will be stored
var dirName = Guid.NewGuid().ToString();
System.IO.Directory.CreateDirectory(dirName);
Create a reader instance with a random filename. We use a using statement so that Dispose is called when the instance goes out of scope.
using (var reader = SesamDataFactory.CreateReader(dirName, "T1.FEM"))
{
Invoke CreateModel in order to establish the indexation data necessary to access the file content.
int diagnostic = reader.CreateModel();
Create a writer instance. We use a using statement so that Dispose is called when the instance goes out of scope.
var randomFileNameSIN = Guid.NewGuid().ToString() + ".SIN";
using (var writer = SesamDataFactory.CreateWriter(randomFileNameSIN))
{
Use the Toc to loop over all datatypes present on the FEM-file. We establish all corresponding pointer tables on the SIN-file.
foreach (var datatype in reader.GetToc())
{
writer.CreateTab(datatype.Key, datatype.Value.AllIndices.Select(item => (long)item).ToArray(), datatype.Value.Count);
}
Use the Toc to loop over all datatypes present on the FEM-file. We read all data in arrays of 10, and write the same data to the SIN-file.
int blockSize = 10;
foreach (var datatype in reader.GetToc())
{
reader.SetFirstTimeReadAll(datatype.Key);
while (true)
{
List<ISifData> data = reader.ReadAll(datatype.Key, blockSize, out var noMoreData);
writer.WriteData(datatype.Key, data);
if (noMoreData)
{
break;
}
}
}
Export the SIN-file to a SIF-file
var randomFileNameSIF = Guid.NewGuid().ToString() + ".SIF";
writer.WriteSifFile(randomFileNameSIF);
When the writer and reader instances goe out of scope the file and directory are unlocked and may be deleted if desired.
}
System.IO.File.Delete(randomFileNameSIN);
}
System.IO.Directory.Delete(dirName, true);
The complete example (in the form of a nUnit unit test) as follows.
using System;
using System.Collections.Generic;
using System.Linq;
using DNV.Sesam.SifApi.Core;
using NUnit.Framework;
namespace DNV.Sesam.SifApi.IO.Doc
{
public partial class Tutorials
{
[Test]
public void CopyFEMtoSINtoSIF()
{
var dirName = Guid.NewGuid().ToString();
System.IO.Directory.CreateDirectory(dirName);
using (var reader = SesamDataFactory.CreateReader(dirName, "T1.FEM"))
{
int diagnostic = reader.CreateModel();
Assert.AreEqual(0, diagnostic);
var randomFileNameSIN = Guid.NewGuid().ToString() + ".SIN";
using (var writer = SesamDataFactory.CreateWriter(randomFileNameSIN))
{
foreach (var datatype in reader.GetToc())
{
writer.CreateTab(datatype.Key, datatype.Value.AllIndices.Select(item => (long)item).ToArray(), datatype.Value.Count);
}
int blockSize = 10;
foreach (var datatype in reader.GetToc())
{
reader.SetFirstTimeReadAll(datatype.Key);
while (true)
{
List<ISifData> data = reader.ReadAll(datatype.Key, blockSize, out var noMoreData);
writer.WriteData(datatype.Key, data);
if (noMoreData)
{
break;
}
}
}
var randomFileNameSIF = Guid.NewGuid().ToString() + ".SIF";
writer.WriteSifFile(randomFileNameSIF);
Assert.IsTrue(System.IO.File.Exists(randomFileNameSIF));
System.IO.File.Delete(randomFileNameSIF);
}
System.IO.File.Delete(randomFileNameSIN);
}
System.IO.Directory.Delete(dirName, true);
}
}
}