Skip to content

Commit

Permalink
Implement merge BCF feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgDangl committed Apr 9, 2024
1 parent 5e1090e commit 478516d
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/IPA.Bcfier.App/Controllers/BcfConversionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,63 @@ public async Task<IActionResult> ExportBcfFileAsync([FromBody] BcfFile bcfFile)
await bcfFileResult.CopyToAsync(fs);
return NoContent();
}

[HttpPost("merge")]
public async Task<IActionResult> MergeBcfFilesAsync()
{
var electronWindow = _electronWindowProvider.BrowserWindow;
if (electronWindow == null)
{
return BadRequest();
}

var fileSelectionResult = await Electron.Dialog.ShowOpenDialogAsync(electronWindow, new OpenDialogOptions
{
Properties = new OpenDialogProperty[]
{
OpenDialogProperty.multiSelections
},
Filters = new[]
{
new FileFilter
{
Name = "BCF Files",
Extensions = new string[] { "bcf", "bcfzip" }
}
}
});

if (fileSelectionResult == null)
{
return NoContent();
}

try
{
var memStreams = new List<Stream>();
foreach (var file in fileSelectionResult)
{
using var bcfFileStream = System.IO.File.OpenRead(file);
var memStream = new MemoryStream();
await bcfFileStream.CopyToAsync(memStream);
memStream.Position = 0;
memStreams.Add(memStream);
}

var mergeService = new BcfMergeService();
var bcfResult = await mergeService.MergeBcfFilesAsync(memStreams);

if (bcfResult == null)
{
return BadRequest();
}

return Ok(bcfResult);
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
}
}
2 changes: 1 addition & 1 deletion src/IPA.Bcfier/Services/BcfImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ private enum BcfFileType
V21,
V3
}
}
}
}
70 changes: 70 additions & 0 deletions src/IPA.Bcfier/Services/BcfMergeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using IPA.Bcfier.Models.Bcf;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace IPA.Bcfier.Services
{
public class BcfMergeService
{
public async Task<BcfFile?> MergeBcfFilesAsync(List<Stream> bcfFileStreams)
{
try
{
BcfFile newBcf = null;
foreach (var stream in bcfFileStreams)
{
var bcfResult = await new BcfImportService().ImportBcfFileAsync(stream, "issue.bcf");
if (newBcf == null)
{
newBcf = bcfResult;
continue;
}

foreach (var mergedIssue in bcfResult.Topics)
{
//it's a new issue
if (!newBcf.Topics.Any(x => x.Id == mergedIssue.Id))
{
newBcf.Topics.Add(mergedIssue);
// TODO VIEWPOINTS?
}
//it exists, let's loop comments and views
else
{
var issue = newBcf.Topics.First(x => x.Id == mergedIssue.Id);
var newComments = mergedIssue.Comments.Where(x => issue.Comments.All(y => y.Id != x.Id)).ToList();
if (newComments.Any())
{
foreach (var newComment in newComments)
{
issue.Comments.Add(newComment);
}
}

//sort comments
issue.Comments = issue.Comments.OrderByDescending(x => x.CreationDate).ToList();

var newViews = mergedIssue.Viewpoints.Where(x => issue.Viewpoints.All(y => y.Id != x.Id)).ToList();
if (newViews.Any())
{
foreach (var newView in newViews)
{
issue.Viewpoints.Add(newView);
// TODO SNAPSHOTS?
}
}
}
}
}

return newBcf;
}
catch
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<mat-icon>folder_open</mat-icon>
Open BCF
</button>
<button mat-raised-button>
<button mat-raised-button (click)="openMergeSelection()">
<mat-icon>file_copy</mat-icon>
Merge...
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export class TopMenuComponent {
});
}

openMergeSelection(): void {
this.backendService.mergeBcfFile().subscribe({
next: (bcfFile: BcfFile) => {
this.bcfFilesMessengerService.openBcfFile(bcfFile);
},
error: () => {
this.notificationsService.error('Error during BCF merge.');
},
});
}

newBcfFile(): void {
this.bcfFilesMessengerService.createNewBcfFile();
}
Expand Down
4 changes: 4 additions & 0 deletions src/ipa-bcfier-ui/src/app/services/BackendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class BackendService {
return this.http.post<BcfFile>('/api/bcf-conversion/import', null);
}

mergeBcfFile(): Observable<BcfFile> {
return this.http.post<BcfFile>('/api/bcf-conversion/merge', null);
}

exportBcfFile(bcfFile: BcfFile): Observable<void> {
return this.http.post<void>('/api/bcf-conversion/export', bcfFile);
}
Expand Down
4 changes: 4 additions & 0 deletions src/ipa-bcfier-ui/src/app/services/RevitBackendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class RevitBackendService {
return this.sendCommand<BcfFile>('importBcfFile', null);
}

mergeBcfFile(): Observable<BcfFile> {
throw new Error('Method not implemented.');
}

exportBcfFile(bcfFile: BcfFile): Observable<void> {
return this.sendCommand<void>('exportBcfFile', bcfFile);
}
Expand Down

0 comments on commit 478516d

Please sign in to comment.