Skip to content

Commit

Permalink
[BMSPT-306] multiple viewpoint and snapshot export to file
Browse files Browse the repository at this point in the history
  • Loading branch information
BalintBende committed Jul 25, 2024
1 parent 06bcb94 commit 54be747
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/bcf-toolkit/Builder/Bcf30/DocumentReferenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public DocumentReferenceBuilder SetGuid(string guid) {
return this;
}

public DocumentReferenceBuilder SetDescription(string description) {
public DocumentReferenceBuilder SetDescription(string? description) {
_documentReference.Description = description;
return this;
}
Expand Down
83 changes: 59 additions & 24 deletions src/bcf-toolkit/Converter/Bcf21/FileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,31 @@ public static void SerializeAndWriteBcfToStream(
var topicFolder = $"{guid}";

zip.CreateEntryFromObject($"{topicFolder}/markup.bcf", markup);

var visInfo =
(VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
zip.CreateEntryFromObject($"{topicFolder}/viewpoint.bcfv", visInfo);

var snapshotFileName = markup.GetFirstViewPoint()?.Snapshot;
var base64String = markup.GetFirstViewPoint()?.SnapshotData?.Data;
if (snapshotFileName == null || base64String == null) continue;
var bytes = Convert.FromBase64String(base64String);
zip.CreateEntryFromBytes($"{topicFolder}/{snapshotFileName}", bytes);

foreach (var viewpoint in markup.Viewpoints) {
zip.CreateEntryFromObject($"{topicFolder}/{viewpoint.Viewpoint}", viewpoint.VisualizationInfo);

var snapshotFileName = viewpoint.Snapshot;
var snapshotBase64String = viewpoint.SnapshotData?.Data;
if (string.IsNullOrEmpty(snapshotFileName) || snapshotBase64String == null)
continue;
var snapshotBytes = Convert.FromBase64String(snapshotBase64String);
zip.CreateEntryFromBytes($"{topicFolder}/{snapshotFileName}", snapshotBytes);
}

//Additional files can be referenced by other files via their relative
//paths. It is recommended to put them in a folder called Documents in the
//root folder of the zip archive.
var internalDocuments = markup
.Topic.DocumentReference
.Where(d => !d.IsExternal);
foreach (var document in internalDocuments) {
var documentFileName = Path.GetFileName(document.ReferencedDocument);
var documentBase64String = document.DocumentData.Data;
if (string.IsNullOrEmpty(documentFileName)) continue;
var documentBytes = Convert.FromBase64String(documentBase64String);
zip.CreateEntryFromBytes($"documents/{documentFileName}", documentBytes);
}
}

zip.CreateEntryFromObject("project.bcfp", bcfObject.Project);
Expand Down Expand Up @@ -179,21 +194,41 @@ public static async Task<string> SerializeAndWriteBcfToFolder(
topicFolder,
"markup.bcf",
markup));

var visInfo =
(VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
writeTasks.Add(
BcfExtensions.SerializeAndWriteXmlFile(

foreach (var viewpoint in markup.Viewpoints) {
writeTasks.Add( BcfExtensions.SerializeAndWriteXmlFile(
topicFolder,
"viewpoint.bcfv",
visInfo));

var snapshotFileName = markup.GetFirstViewPoint()?.Snapshot;
var base64String = markup.GetFirstViewPoint()?.SnapshotData?.Data;
if (snapshotFileName == null || base64String == null) continue;
writeTasks.Add(File.WriteAllBytesAsync(
$"{topicFolder}/{snapshotFileName}",
Convert.FromBase64String(base64String)));
viewpoint.Viewpoint,
viewpoint.VisualizationInfo));

var snapshotFileName = viewpoint.Snapshot;
var snapshotBase64String = viewpoint.SnapshotData?.Data;
if (string.IsNullOrEmpty(snapshotFileName) || snapshotBase64String == null)
continue;
writeTasks.Add(File.WriteAllBytesAsync(
$"{topicFolder}/{snapshotFileName}",
Convert.FromBase64String(snapshotBase64String)));
}

//Additional files can be referenced by other files via their relative
//paths. It is recommended to put them in a folder called Documents in the
//root folder of the zip archive.
var documentFolder = $"{tmpFolder}/documents";
var internalDocuments = markup
.Topic.DocumentReference
.Where(d => !d.IsExternal);
foreach (var document in internalDocuments) {
var documentFileName = Path.GetFileName(document.ReferencedDocument);
var documentBase64String = document.DocumentData.Data;
if (string.IsNullOrEmpty(documentFileName)) continue;

if (Directory.Exists(documentFolder) is not true)
Directory.CreateDirectory(documentFolder);

writeTasks.Add(File.WriteAllBytesAsync(
$"{documentFolder}/{documentFileName}",
Convert.FromBase64String(documentBase64String)));
}
}

writeTasks.Add(
Expand Down
52 changes: 27 additions & 25 deletions src/bcf-toolkit/Converter/Bcf30/FileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ public static void SerializeAndWriteBcfToStream(IBcf bcf, ZipArchive zip,
var topicFolder = $"{guid}";

zip.CreateEntryFromObject($"{topicFolder}/markup.bcf", markup);

var visInfo =
(VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
zip.CreateEntryFromObject($"{topicFolder}/viewpoint.bcfv", visInfo);

// Write snapshot
var snapshotFileName = markup.GetFirstViewPoint()?.Snapshot;
var base64String = markup.GetFirstViewPoint()?.SnapshotData?.Data;
if (snapshotFileName == null || base64String == null) continue;
var bytes = Convert.FromBase64String(base64String);
zip.CreateEntryFromBytes($"{topicFolder}/{snapshotFileName}", bytes);
foreach (var viewpoint in markup.Topic.Viewpoints) {
zip.CreateEntryFromObject($"{topicFolder}/{viewpoint.Viewpoint}", viewpoint.VisualizationInfo);
var snapshotFileName = viewpoint.Snapshot;
var snapshotBase64String = viewpoint.SnapshotData?.Data;
if (string.IsNullOrEmpty(snapshotFileName) || snapshotBase64String == null)
continue;
var snapshotBytes = Convert.FromBase64String(snapshotBase64String);
zip.CreateEntryFromBytes($"{topicFolder}/{snapshotFileName}", snapshotBytes);
}
}

zip.CreateEntryFromObject("extensions.xml", bcfObject.Extensions);
Expand Down Expand Up @@ -207,20 +207,22 @@ public static async Task<string> SerializeAndWriteBcfToFolder(
writeTasks.Add(
BcfExtensions.SerializeAndWriteXmlFile(topicFolder, "markup.bcf",
markup));

var visInfo =
(VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
writeTasks.Add(BcfExtensions.SerializeAndWriteXmlFile(
topicFolder,
"viewpoint.bcfv",
visInfo));

var snapshotFileName = markup.GetFirstViewPoint()?.Snapshot;
var base64String = markup.GetFirstViewPoint()?.SnapshotData?.Data;
if (snapshotFileName == null || base64String == null) continue;
writeTasks.Add(File.WriteAllBytesAsync(
$"{topicFolder}/{snapshotFileName}",
Convert.FromBase64String(base64String)));

foreach (var viewpoint in markup.Topic.Viewpoints) {
writeTasks.Add(BcfExtensions.SerializeAndWriteXmlFile(
topicFolder,
viewpoint.Viewpoint,
viewpoint.VisualizationInfo));

var snapshotFileName = viewpoint.Snapshot;
var snapshotBase64String = viewpoint.SnapshotData?.Data;
if (string.IsNullOrEmpty(snapshotFileName) ||
snapshotBase64String == null)
continue;
writeTasks.Add(File.WriteAllBytesAsync(
$"{topicFolder}/{snapshotFileName}",
Convert.FromBase64String(snapshotBase64String)));
}
}

writeTasks.Add(BcfExtensions.SerializeAndWriteXmlFile(tmpFolder,
Expand Down
15 changes: 15 additions & 0 deletions src/tests/Converter/Bcf21/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,19 @@ public async Task BuildV30BcfFromStreamWithInternalDocumentTest() {
Assert.That(document?.DocumentData.Mime, Is.EqualTo("data:application/xml;base64"));
Assert.That(document?.DocumentData.Data.Length, Is.EqualTo(10644));
}

/// <summary>
/// It should generate a bcf skipping the markup file.
/// </summary>
[Test]
[Category("BCF v2.1")]
public async Task WriteBcfToFolderTest() {
await using var stream =
new FileStream(
"Resources/Bcf/v2.1/MaximumInformation.bcfzip",
FileMode.Open,
FileAccess.Read);
var bcf = await _converter.BcfFromStream<BcfToolkit.Model.Bcf21.Bcf>(stream);
await _converter.ToBcf(bcf, "Resources/output/Bcf/v2.1/MaximumInformation.bcfzip");
}
}

0 comments on commit 54be747

Please sign in to comment.