-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(tekla): Read write model cards to/from appdata + fixes double UI (#…
…395) * Fix multiple problems * Connect windows each other * Add note
- Loading branch information
1 parent
a4f27b5
commit c74e92f
Showing
3 changed files
with
147 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 89 additions & 6 deletions
95
Connectors/Tekla/Speckle.Connector.TeklaShared/HostApp/TeklaDocumentModelStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,100 @@ | ||
using Speckle.Connectors.DUI.Models; | ||
using System.IO; | ||
using Microsoft.Extensions.Logging; | ||
using Speckle.Connectors.DUI.Models; | ||
using Speckle.Newtonsoft.Json; | ||
using Speckle.Sdk; | ||
using Speckle.Sdk.Helpers; | ||
using Speckle.Sdk.Logging; | ||
|
||
namespace Speckle.Connector.Tekla2024.HostApp; | ||
|
||
public class TeklaDocumentModelStore : DocumentModelStore | ||
{ | ||
private readonly ISpeckleApplication _speckleApplication; | ||
private readonly ILogger<TeklaDocumentModelStore> _logger; | ||
private readonly TSM.Model _model; | ||
private readonly TSM.Events _events; | ||
private string HostAppUserDataPath { get; set; } | ||
private string DocumentStateFile { get; set; } | ||
private string ModelPathHash { get; set; } | ||
|
||
public TeklaDocumentModelStore( | ||
JsonSerializerSettings jsonSerializerSettings | ||
// ITopLevelExceptionHandler topLevelExceptionHandler | ||
JsonSerializerSettings jsonSerializerSettings, | ||
ISpeckleApplication speckleApplication, | ||
ILogger<TeklaDocumentModelStore> logger | ||
) | ||
: base(jsonSerializerSettings, true) { } | ||
: base(jsonSerializerSettings, true) | ||
{ | ||
_speckleApplication = speckleApplication; | ||
_logger = logger; | ||
_model = new TSM.Model(); | ||
SetPaths(); | ||
_events = new TSM.Events(); | ||
_events.ModelLoad += () => | ||
{ | ||
SetPaths(); | ||
ReadFromFile(); | ||
OnDocumentChanged(); | ||
}; | ||
_events.Register(); | ||
if (SpeckleTeklaPanelHost.IsInitialized) | ||
{ | ||
ReadFromFile(); | ||
OnDocumentChanged(); | ||
} | ||
} | ||
|
||
private void SetPaths() | ||
{ | ||
ModelPathHash = Crypt.Md5(_model.GetInfo().ModelPath, length: 32); | ||
HostAppUserDataPath = Path.Combine( | ||
SpecklePathProvider.UserSpeckleFolderPath, | ||
"Connectors", | ||
_speckleApplication.Slug | ||
); | ||
DocumentStateFile = Path.Combine(HostAppUserDataPath, $"{ModelPathHash}.json"); | ||
} | ||
|
||
public override void WriteToFile() | ||
{ | ||
string serializedState = Serialize(); | ||
try | ||
{ | ||
if (!Directory.Exists(HostAppUserDataPath)) | ||
{ | ||
Directory.CreateDirectory(HostAppUserDataPath); | ||
} | ||
File.WriteAllText(DocumentStateFile, serializedState); | ||
} | ||
catch (Exception ex) when (!ex.IsFatal()) | ||
{ | ||
_logger.LogError(ex.Message); | ||
} | ||
} | ||
|
||
public override void ReadFromFile() | ||
{ | ||
try | ||
{ | ||
if (!Directory.Exists(HostAppUserDataPath)) | ||
{ | ||
Models = new(); | ||
return; | ||
} | ||
|
||
public override void WriteToFile() { } | ||
if (!File.Exists(DocumentStateFile)) | ||
{ | ||
Models = new(); | ||
return; | ||
} | ||
|
||
public override void ReadFromFile() { } | ||
string serializedState = File.ReadAllText(DocumentStateFile); | ||
Models = Deserialize(serializedState) ?? new(); | ||
} | ||
catch (Exception ex) when (!ex.IsFatal()) | ||
{ | ||
Models = new(); | ||
_logger.LogError(ex.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters