Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nic/snap connector #142

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion .github/workflows/installer-creator-snap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ jobs:
run: |
$tag = "${{ github.event.inputs.version }}.$($env:GITHUB_RUN_ID%65536)"
echo "::set-output name=tag::$tag"

- name: Update Version
shell: powershell
working-directory: ConnectorSNAP/ConnectorSNAP/Properties
run: |
(Get-content .\AssemblyInfo.cs) -replace '1\.0\.0\.0', '${{ steps.calculate-tag-version.outputs.tag }}' | Out-File -encoding ASCII .\AssemblyInfo.cs

# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore all the applications
shell: cmd
Expand Down Expand Up @@ -159,4 +161,4 @@ jobs:
upload_url: ${{ steps.create-release.outputs.upload_url }}
asset_path: ./Installer/${{ steps.installer.outputs.filename }}
asset_name: ${{ steps.installer.outputs.filename }}
asset_content_type: application/vnd.microsoft.portable-executable
asset_content_type: application/vnd.microsoft.portable-executable
9 changes: 3 additions & 6 deletions ConnectorGSA/ConnectorGSA.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31424.327
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorGSA", "ConnectorGSA\ConnectorGSA.csproj", "{1BA03BE5-F893-46A3-B569-4FAB87F0B60E}"
EndProject
Expand All @@ -19,7 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Local Dependencies", "Local
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core", "..\Core\Core\Core.csproj", "{9CF2E568-CAE4-4224-9458-720F83909FB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolygonMesher", "..\Objects\Converters\StructuralUtilities\PolygonMesher\PolygonMesher.csproj", "{6185B1A1-0A12-44B1-8AC5-0ED48147FB21}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolygonMesher", "..\Objects\Converters\StructuralUtilities\PolygonMesher\PolygonMesher.csproj", "{6185B1A1-0A12-44B1-8AC5-0ED48147FB21}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConverterGSA", "..\Objects\Converters\ConverterGSA\ConverterGSA\ConverterGSA.csproj", "{6E561E4F-A3B4-4277-A2F7-B934A4141394}"
EndProject
Expand All @@ -44,11 +44,9 @@ Global
{76CAC32D-3334-4F70-BBA7-B20B1FDDE298}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76CAC32D-3334-4F70-BBA7-B20B1FDDE298}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76CAC32D-3334-4F70-BBA7-B20B1FDDE298}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76CAC32D-3334-4F70-BBA7-B20B1FDDE298}.Release|Any CPU.Build.0 = Release|Any CPU
{A7D05402-E125-4F42-AC60-B6800C7802A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7D05402-E125-4F42-AC60-B6800C7802A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7D05402-E125-4F42-AC60-B6800C7802A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7D05402-E125-4F42-AC60-B6800C7802A7}.Release|Any CPU.Build.0 = Release|Any CPU
{11DB7D37-A6ED-4C8A-B354-D9FB3C82386C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11DB7D37-A6ED-4C8A-B354-D9FB3C82386C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11DB7D37-A6ED-4C8A-B354-D9FB3C82386C}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -68,7 +66,6 @@ Global
{F8521EEA-6C45-4B01-A002-542824B97062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8521EEA-6C45-4B01-A002-542824B97062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8521EEA-6C45-4B01-A002-542824B97062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8521EEA-6C45-4B01-A002-542824B97062}.Release|Any CPU.Build.0 = Release|Any CPU
{69087486-2CC6-4DDF-BFA2-F8B20C12BF02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69087486-2CC6-4DDF-BFA2-F8B20C12BF02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69087486-2CC6-4DDF-BFA2-F8B20C12BF02}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ private bool AddMat(List<string> items, out List<string> remainingItems)
remainingItems = items;
return false;
}

record.Mat = (GsaMat)gsaMatParser.Record;
record.Name = record.Mat.Name;

return true;
}
#endregion
Expand Down
6 changes: 2 additions & 4 deletions ConnectorSNAP/ConnectorSNAP.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Local Dependencies", "Local Dependencies", "{9D115BF9-4440-4E74-8716-2F35C935D73B}"
EndProject
Expand Down Expand Up @@ -36,7 +36,6 @@ Global
{CEF6DA53-D4F3-4B12-A6C9-703788FEA5CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEF6DA53-D4F3-4B12-A6C9-703788FEA5CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEF6DA53-D4F3-4B12-A6C9-703788FEA5CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEF6DA53-D4F3-4B12-A6C9-703788FEA5CD}.Release|Any CPU.Build.0 = Release|Any CPU
{C7DD30CC-F463-4C22-8AC2-E898E0AE837C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7DD30CC-F463-4C22-8AC2-E898E0AE837C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7DD30CC-F463-4C22-8AC2-E898E0AE837C}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -48,7 +47,6 @@ Global
{B5AEC88C-A424-4420-A5A4-04812E7F8F7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5AEC88C-A424-4420-A5A4-04812E7F8F7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5AEC88C-A424-4420-A5A4-04812E7F8F7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5AEC88C-A424-4420-A5A4-04812E7F8F7E}.Release|Any CPU.Build.0 = Release|Any CPU
{4BDAF4E2-45C0-49A1-BECB-7983F41C2AC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BDAF4E2-45C0-49A1-BECB-7983F41C2AC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BDAF4E2-45C0-49A1-BECB-7983F41C2AC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
62 changes: 57 additions & 5 deletions ConnectorSNAP/ConnectorSNAP/Cache/SnapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,76 @@ namespace ConnectorSNAP
{
public class SnapCache : ISNAPCache
{
private List<SnapCacheRecord> snapCacheRecords = new List<SnapCacheRecord>();
private List<SnapCacheRecord> records = new List<SnapCacheRecord>();
private Dictionary<Type, List<int>> recordIndicesByType = new Dictionary<Type, List<int>>();

public bool GetNatives(out List<object> snapRecords)
{
snapRecords = snapCacheRecords.Select(cr => cr.SnapRecord).ToList();
/*
var namedRecords = new List<object>();
var otherRecords = new List<object>();
var namedIntType = typeof(ISnapRecordNamed);
var namedTypes = recordIndicesByType.Keys.Where(k => k.InheritsOrImplements(namedIntType)).ToList();

foreach (var nt in namedTypes)
{
var orderedObjs = recordIndicesByType[nt].OrderBy(i => ((ISnapRecordNamed)records[i].SnapRecord)
}
*/
snapRecords = records.Select(cr => cr.SnapRecord).ToList();
return true;
}

public bool Upsert(List<object> nativeObjects)
public bool GetNatives<T>(out List<object> snapRecords)
{
snapCacheRecords.AddRange(nativeObjects.Select(o => new SnapCacheRecord() { SnapRecord = o }));
var t = typeof(T);
snapRecords = records.Where(cr => cr.SnapRecord.GetType() == t).Select(cr => cr.SnapRecord).ToList();
return true;
}

public bool Upsert(List<object> nativeObjects)
{
foreach (var no in nativeObjects)
{
var newIndex = records.Count;
var t = no.GetType();
if (!recordIndicesByType.ContainsKey(t))
{
recordIndicesByType.Add(t, new List<int>());
}
recordIndicesByType[t].Add(newIndex);
records.Add(new SnapCacheRecord() { SnapRecord = no });
}
return true;
}

public bool Upsert(object nativeObject)
{
snapCacheRecords.Add(new SnapCacheRecord() { SnapRecord = nativeObject });
records.Add(new SnapCacheRecord() { SnapRecord = nativeObject });
return true;
}

public bool Contains<T>(string name, out object snapRecord) where T : ISnapRecordNamed
{
var t = typeof(T);
if (recordIndicesByType.ContainsKey(t))
{
var typeRecords = recordIndicesByType[t].Select(i => records[i].SnapRecord).ToList();
var matchedByName = typeRecords.FirstOrDefault(r => ((ISnapRecordNamed)r).Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
if (matchedByName != null)
{
snapRecord = matchedByName;
return true;
}
}
snapRecord = null;
return false;
}

public void Clear()
{
records.Clear();
recordIndicesByType.Clear();
}
}
}
7 changes: 4 additions & 3 deletions ConnectorSNAP/ConnectorSNAP/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ public static async Task<bool> Receive(string commitId, ITransport transport, Li
return false;
}

public static bool ConvertToNative(List<Base> objects, ISpeckleConverter converter, IProgress<string> loggingProgress) //Includes writing to Cache
public static bool ConvertToNative(List<Base> objects, ISpeckleConverter converter) //Includes writing to Cache
{
Instance.SnapModel.Cache.Clear();
try
{
var nativeObjects = converter.ConvertToNative(objects).ToList();
((SnapCache)Instance.SnapModel.Cache).Upsert(nativeObjects);
//((SnapCache)Instance.SnapModel.Cache).Upsert(nativeObjects);
}
catch (Exception ex)
{
loggingProgress.Report("Unable to convert one or more received objects: " + ex.Message);
converter.Report.LogOperationError(new Exception("Unable to convert one or more received objects: " + ex.Message));
}

return true;
Expand Down
13 changes: 9 additions & 4 deletions ConnectorSNAP/ConnectorSNAP/ConnectorSNAP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Proxy\s81CsvMaps\BeamMap.cs" />
<Compile Include="Proxy\s81CsvMaps\Enums.cs" />
<Compile Include="Proxy\s81CsvMaps\GirderMap.cs" />
<Compile Include="Proxy\s81CsvMaps\MapConverters.cs" />
<Compile Include="Proxy\s81CsvMaps\MaterialSteelMap.cs" />
<Compile Include="Proxy\s81CsvMaps\NodalSupportMap.cs" />
<Compile Include="Proxy\s81CsvMaps\NodeMap.cs" />
<Compile Include="Proxy\s81CsvMaps\EndReleasesMap.cs" />
<Compile Include="Proxy\s81CsvMaps\SecondaryNodeMap.cs" />
<Compile Include="Proxy\s81CsvMaps\SectionMap.cs" />
<Compile Include="Proxy\SnapProxy.cs" />
<Compile Include="SNAPBindings.cs" />
<Compile Include="SnapModel.cs" />
Expand All @@ -71,16 +76,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia">
<Version>0.10.10</Version>
<Version>0.10.13</Version>
</PackageReference>
<PackageReference Include="Avalonia.Desktop">
<Version>0.10.10</Version>
<Version>0.10.13</Version>
</PackageReference>
<PackageReference Include="Avalonia.ReactiveUI">
<Version>0.10.10</Version>
<Version>0.10.13</Version>
</PackageReference>
<PackageReference Include="CsvHelper">
<Version>27.2.0</Version>
<Version>27.2.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
48 changes: 48 additions & 0 deletions ConnectorSNAP/ConnectorSNAP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,53 @@ public static IEnumerable<Type> GetBaseClasses(this Type type)
.Distinct();
}

//https://stackoverflow.com/questions/457676/check-if-a-class-is-derived-from-a-generic-class
public static bool InheritsOrImplements(this Type child, Type parent)
{
parent = ResolveGenericTypeDefinition(parent);

var currentChild = child.IsGenericType
? child.GetGenericTypeDefinition()
: child;

while (currentChild != typeof(object))
{
if (parent == currentChild || HasAnyInterfaces(parent, currentChild))
return true;

currentChild = currentChild.BaseType != null
&& currentChild.BaseType.IsGenericType
? currentChild.BaseType.GetGenericTypeDefinition()
: currentChild.BaseType;

if (currentChild == null)
return false;
}
return false;
}

private static bool HasAnyInterfaces(Type parent, Type child)
{
return child.GetInterfaces()
.Any(childInterface =>
{
var currentInterface = childInterface.IsGenericType
? childInterface.GetGenericTypeDefinition()
: childInterface;

return currentInterface == parent;
});
}

private static Type ResolveGenericTypeDefinition(Type parent)
{
var shouldUseGenericType = true;
if (parent.IsGenericType && parent.GetGenericTypeDefinition() != parent)
shouldUseGenericType = false;

if (parent.IsGenericType && shouldUseGenericType)
parent = parent.GetGenericTypeDefinition();
return parent;
}
}
}
11 changes: 1 addition & 10 deletions ConnectorSNAP/ConnectorSNAP/Headless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,7 @@ public bool RunCLI(params string[] args)

Console.WriteLine("Converting objects into SNAP records in .s8i format");

Commands.ConvertToNative(topLevelObjects, converter, loggingProgress);

if (converter.Report.ConversionErrors != null && converter.Report.ConversionErrors.Count > 0)
{
foreach (var ce in converter.Report.ConversionErrors)
{
loggingProgress.Report(ce.Message);
loggingProgress.Report(ce.Message);
}
}
Commands.ConvertToNative(topLevelObjects, converter);

//The cache is filled with natives
if (Instance.SnapModel.Cache.GetNatives(out var snapRecords) && snapRecords != null && snapRecords.Count > 0)
Expand Down
5 changes: 5 additions & 0 deletions ConnectorSNAP/ConnectorSNAP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,10 @@ protected override void OnClosing(CancelEventArgs e)
{
this.Hide();
}

private void SaveCommand()
{

}
}
}
8 changes: 7 additions & 1 deletion ConnectorSNAP/ConnectorSNAP/Proxy/SnapProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void NewFile(bool v)

public void WriteModel(List<object> snapRecords)
{
recordsToSave = snapRecords;
var recordsByType = snapRecords.GroupBy(r => r.GetType()).ToDictionary(r => r.Key, r => r.ToList());
recordsToSave = recordsByType.Keys.SelectMany(k => recordsByType[k]).ToList();
}

public void PrepareResults(object resultTypes)
Expand Down Expand Up @@ -83,9 +84,14 @@ private bool Writes8iCsv(string filePath, List<object> records, out List<string>

using (var csvWriter = new CsvWriter(writer, config))
{
csvWriter.Context.RegisterClassMap<MaterialSteelMap>();
csvWriter.Context.RegisterClassMap<BeamMap>();
csvWriter.Context.RegisterClassMap<NodeMap>();
csvWriter.Context.RegisterClassMap<SecondaryNodeMap>();
csvWriter.Context.RegisterClassMap<GirderMap>();
csvWriter.Context.RegisterClassMap<EndReleasesMap>();
csvWriter.Context.RegisterClassMap<NodalSupportMap>();
csvWriter.Context.RegisterClassMap<SectionMap>();

foreach (var record in records)
{
Expand Down
12 changes: 6 additions & 6 deletions ConnectorSNAP/ConnectorSNAP/Proxy/s81CsvMaps/BeamMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ internal sealed class BeamMap : ClassMap<Beam>
public BeamMap()
{
//Reading
Map(m => m.NodeIEdgeType).Index(0).Convert(r => (r.Row.GetField(0).Split('/').Last().Trim() == "0") ? NodeType.SecondaryNode : NodeType.Node);
Map(m => m.NodeIEdgeName).Index(1);
Map(m => m.NodeJEdgeType).Index(2).Convert(r => (r.Row.GetField(2) == "0") ? NodeType.SecondaryNode : NodeType.Node);
Map(m => m.NodeJEdgeName).Index(3);
Map(m => m.NodeIType).Index(0).Convert(r => (r.Row.GetField(0).Split('/').Last().Trim() == "0") ? NodeType.SecondaryNode : NodeType.Node);
Map(m => m.NodeI).Index(1);
Map(m => m.NodeJType).Index(2).Convert(r => (r.Row.GetField(2) == "0") ? NodeType.SecondaryNode : NodeType.Node);
Map(m => m.NodeJ).Index(3);
Map(m => m.StructureType).Index(4).TypeConverter<EnumIntConverter<StructureType>>();
Map(m => m.CrossSection).Index(5);
Map(m => m.FinishingCoating).Index(6);
Expand All @@ -25,8 +25,8 @@ public BeamMap()
Map(m => m.SectionProperties).Index(11);

//Writing
Map(m => m.NodeIEdgeType).Convert(o => Keyword.BM.GetStringValue() + " / " + (int)o.Value.NodeIEdgeType);
Map(m => m.NodeJEdgeType).Convert(o => ((int)o.Value.NodeJEdgeType).ToString());
Map(m => m.NodeIType).Convert(o => Keyword.BM.GetStringValue() + " / " + (int)o.Value.NodeIType);
Map(m => m.NodeJType).Convert(o => ((int)o.Value.NodeJType).ToString());
}
}
}
23 changes: 23 additions & 0 deletions ConnectorSNAP/ConnectorSNAP/Proxy/s81CsvMaps/EndReleasesMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CsvHelper.Configuration;
using Speckle.SNAP.API;
using Speckle.SNAP.API.s8iSchema;
using System;
using System.Linq;

namespace ConnectorSNAP.Proxy.s81CsvMaps
{
internal class EndReleasesMap : ClassMap<EndReleases>
{
public EndReleasesMap()
{
//Reading & writing
Map(m => m.Name).Index(0).Convert(r => r.Row.GetField(0).Split('/').Last().Trim());
Map(m => m.Restraints).Index(1).TypeConverter<BoolArrConverter>();

//Writing-only overrides
Map(m => m.Name).Convert(o => Keyword.BD.GetStringValue() + " / " + o.Value.Name);
Map(m => m.Restraints).TypeConverter<BoolArrConverter>();
Map(m => m.defaultRemaining).TypeConverter<ObjectArrConverter>();
}
}
}
Loading