Skip to content

Commit 21f6c6a

Browse files
authored
Merge pull request #14 from SmartPluginsCommunity/feature/get-properties
Feature/Properties extractor
2 parents 9f4b4bc + fb25b80 commit 21f6c6a

File tree

11 files changed

+270
-17
lines changed

11 files changed

+270
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections;
2+
3+
namespace RedButton.Common.Core.CollectionExtensions
4+
{
5+
public static class EnumerableExtensions
6+
{
7+
/// <summary>
8+
/// IEnumerable to ArrayList
9+
/// </summary>
10+
/// <param name="enumerable">IEnumerable</param>
11+
/// <returns></returns>
12+
public static ArrayList ToArrayList(this IEnumerable enumerable)
13+
{
14+
var array = new ArrayList();
15+
16+
foreach (var item in enumerable)
17+
{
18+
if (item != null)
19+
array.Add(item);
20+
}
21+
22+
return array;
23+
}
24+
}
25+
}

src/Common/RedButton.Common.Core/CollectionExtensions/EnumeratorExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class EnumeratorExtensions
99
/// <summary>
1010
/// IEnumerator to IEnumerable.
1111
/// </summary>
12-
/// <param name="enumerator"></param>
12+
/// <param name="enumerator">IEnumerator</param>
1313
/// <returns></returns>
1414
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator enumerator)
1515
{
@@ -23,7 +23,7 @@ public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator enumerator)
2323
/// <summary>
2424
/// IEnumerator to IList.
2525
/// </summary>
26-
/// <param name="enumerator"></param>
26+
/// <param name="enumerator">IEnumerator</param>
2727
/// <returns></returns>
2828
public static IReadOnlyList<T> ToList<T>(this IEnumerator enumerator)
2929
{
@@ -41,7 +41,7 @@ public static IReadOnlyList<T> ToList<T>(this IEnumerator enumerator)
4141
/// <summary>
4242
/// IEnumerator to ConcurrentBag.
4343
/// </summary>
44-
/// <param name="enumerator"></param>
44+
/// <param name="enumerator">IEnumerator</param>
4545
/// <returns></returns>
4646
public static ConcurrentBag<T> ToConcurrentBag<T>(this IEnumerator enumerator)
4747
{

src/Common/RedButton.Common.Core/RedButton.Common.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Reference Include="System.Xml" />
4040
</ItemGroup>
4141
<ItemGroup>
42+
<Compile Include="CollectionExtensions\EnumerableExtensions.cs" />
4243
<Compile Include="CollectionExtensions\EnumeratorExtensions.cs" />
4344
<Compile Include="Geometry\Extensions\PointExtension.cs" />
4445
<Compile Include="Geometry\Extensions\SphereExtension.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using RedButton.Common.Core.CollectionExtensions;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Tekla.Structures.Model;
6+
7+
namespace RedButton.Common.TeklaStructures.Model.Properties
8+
{
9+
/// <summary>
10+
/// Extract properties for Tekla objects.
11+
/// </summary>
12+
public class PropertiesExtractor
13+
{
14+
private ModelObject _modelObject;
15+
private Hashtable _values;
16+
17+
private List<TeklaPropertyString> _stringAttributes;
18+
private List<TeklaPropertyDouble> _doubleAttributes;
19+
private List<TeklaPropertyInt> _integerAttributes;
20+
21+
/// <summary>
22+
/// .ctor
23+
/// </summary>
24+
/// <param name="modelObject"></param>
25+
public PropertiesExtractor(ModelObject modelObject)
26+
{
27+
_modelObject = modelObject;
28+
_stringAttributes = new List<TeklaPropertyString>();
29+
_doubleAttributes = new List<TeklaPropertyDouble>();
30+
_integerAttributes = new List<TeklaPropertyInt>();
31+
}
32+
33+
/// <summary>
34+
/// Add string attribute
35+
/// </summary>
36+
/// <param name="attribute">Attribute name</param>
37+
public TeklaPropertyString AddStringAttribute(string attribute)
38+
{
39+
var property = new TeklaPropertyString(attribute, null);
40+
_stringAttributes.Add(property);
41+
return property;
42+
}
43+
44+
/// <summary>
45+
/// Add double attribute
46+
/// </summary>
47+
/// <param name="attribute">Attribute name</param>
48+
public TeklaPropertyDouble AddDoubleAttribute(string attribute)
49+
{
50+
var property = new TeklaPropertyDouble(attribute, null);
51+
_doubleAttributes.Add(property);
52+
return property;
53+
}
54+
55+
/// <summary>
56+
/// Add integer attribute
57+
/// </summary>
58+
/// <param name="attribute">Attribute name</param>
59+
public TeklaPropertyInt AddIntegerAttribute(string attribute)
60+
{
61+
var property = new TeklaPropertyInt(attribute, null);
62+
_integerAttributes.Add(property);
63+
return property;
64+
}
65+
66+
/// <summary>
67+
/// Refresh report properties
68+
/// </summary>
69+
public void ExtractProperties()
70+
{
71+
if (_modelObject == null)
72+
{
73+
_values = new Hashtable();
74+
return;
75+
}
76+
77+
var valuesCount = _stringAttributes.Count + _doubleAttributes.Count + _integerAttributes.Count;
78+
79+
_values = new Hashtable(valuesCount);
80+
81+
_modelObject.GetAllReportProperties(_stringAttributes.Select(a => a.Name).ToArrayList(),
82+
_doubleAttributes.Select(a => a.Name).ToArrayList(),
83+
_integerAttributes.Select(a => a.Name).ToArrayList(),
84+
ref _values);
85+
86+
foreach (var property in _stringAttributes)
87+
property.Value = GetExtractValue<string>(property.Name);
88+
89+
foreach (var property in _doubleAttributes)
90+
property.Value = GetExtractValue<double>(property.Name);
91+
92+
foreach (var property in _integerAttributes)
93+
property.Value = GetExtractValue<int>(property.Name);
94+
}
95+
96+
/// <summary>
97+
/// Get value
98+
/// </summary>
99+
/// <typeparam name="T">Type</typeparam>
100+
/// <param name="propertyName">Property name</param>
101+
/// <returns></returns>
102+
private T GetExtractValue<T>(string propertyName)
103+
{
104+
var val = _values[propertyName];
105+
return (T)val;
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace RedButton.Common.TeklaStructures.Model.Properties
4+
{
5+
public sealed class TeklaPropertyDouble
6+
{
7+
/// <summary>
8+
/// Property name
9+
/// </summary>
10+
public string Name { get; }
11+
12+
/// <summary>
13+
/// Value
14+
/// </summary>
15+
public double? Value { get; set; }
16+
17+
/// <summary>
18+
/// .ctor
19+
/// </summary>
20+
/// <param name="name">Property name</param>
21+
/// <param name="value">Property value</param>
22+
public TeklaPropertyDouble(string name, double? value)
23+
{
24+
Name = name;
25+
Value = value;
26+
}
27+
28+
public override string ToString()
29+
{
30+
if (Value == null)
31+
return string.Empty;
32+
33+
var v = Math.Round(Value.Value, 3);
34+
return v.ToString();
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace RedButton.Common.TeklaStructures.Model.Properties
2+
{
3+
public sealed class TeklaPropertyInt
4+
{
5+
/// <summary>
6+
/// Property name
7+
/// </summary>
8+
public string Name { get; }
9+
10+
/// <summary>
11+
/// Value
12+
/// </summary>
13+
public int? Value { get; set; }
14+
15+
/// <summary>
16+
/// .ctor
17+
/// </summary>
18+
/// <param name="name">Property name</param>
19+
/// <param name="value">Property value</param>
20+
public TeklaPropertyInt(string name, int? value)
21+
{
22+
Name = name;
23+
Value = value;
24+
}
25+
26+
public override string ToString()
27+
{
28+
if (Value == null)
29+
return string.Empty;
30+
31+
return Value.Value.ToString();
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace RedButton.Common.TeklaStructures.Model.Properties
2+
{
3+
public sealed class TeklaPropertyString
4+
{
5+
/// <summary>
6+
/// Property name
7+
/// </summary>
8+
public string Name { get; }
9+
10+
/// <summary>
11+
/// Value
12+
/// </summary>
13+
public string Value { get; set; }
14+
15+
/// <summary>
16+
/// .ctor
17+
/// </summary>
18+
/// <param name="name">Property name</param>
19+
/// <param name="value">Property value</param>
20+
public TeklaPropertyString(string name, string value)
21+
{
22+
Name = name;
23+
Value = value;
24+
}
25+
}
26+
}

src/Common/RedButton.Common.TeklaStructures/RedButton.Common.TeklaStructures.csproj

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="..\..\packages\TeklaOpenAPI.2020.0.3\build\TeklaOpenAPI.props" Condition="Exists('..\..\packages\TeklaOpenAPI.2020.0.3\build\TeklaOpenAPI.props')" />
43
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
54
<PropertyGroup>
65
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -95,16 +94,17 @@
9594
<Compile Include="Extensions\AssemblyExtension.cs" />
9695
<Compile Include="Model\Intersections\IntersectionAllFacesSolid.cs" />
9796
<Compile Include="Model\Geometry\Polygon.cs" />
97+
<Compile Include="Model\Properties\PropertiesExtractor.cs" />
98+
<Compile Include="Model\Properties\TeklaPropertyDouble.cs" />
99+
<Compile Include="Model\Properties\TeklaPropertyInt.cs" />
100+
<Compile Include="Model\Properties\TeklaPropertyString.cs" />
98101
<Compile Include="Properties\AssemblyInfo.cs" />
99102
<Compile Include="Properties\Resources.Designer.cs">
100103
<AutoGen>True</AutoGen>
101104
<DesignTime>True</DesignTime>
102105
<DependentUpon>Resources.resx</DependentUpon>
103106
</Compile>
104107
</ItemGroup>
105-
<ItemGroup>
106-
<None Include="packages.config" />
107-
</ItemGroup>
108108
<ItemGroup>
109109
<Content Include="DrawingTable\Example.png" />
110110
</ItemGroup>
@@ -121,10 +121,4 @@
121121
</ProjectReference>
122122
</ItemGroup>
123123
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
124-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
125-
<PropertyGroup>
126-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
127-
</PropertyGroup>
128-
<Error Condition="!Exists('..\..\packages\TeklaOpenAPI.2020.0.3\build\TeklaOpenAPI.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\TeklaOpenAPI.2020.0.3\build\TeklaOpenAPI.props'))" />
129-
</Target>
130124
</Project>

src/Common/RedButton.Common.TeklaStructures/packages.config

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using RedButton.Common.TeklaStructures.Model.Properties;
3+
4+
namespace RedButton.Tests.UnitTests.PropertiesTests
5+
{
6+
[TestClass]
7+
public class PropertiesExtractorTests : BaseTest
8+
{
9+
[TestMethod("Get several properties")]
10+
public void GetSeveralPropertiesTest()
11+
{
12+
var beam1 = TestObjectCreator.GetBeam();
13+
14+
AddTemporaryObject(beam1);
15+
16+
var properties = new PropertiesExtractor(beam1);
17+
18+
var width = properties.AddDoubleAttribute("WIDTH");
19+
var lenght = properties.AddDoubleAttribute("LENGTH");
20+
var name = properties.AddStringAttribute("NAME");
21+
var total = properties.AddIntegerAttribute("MODEL_TOTAL");
22+
23+
properties.ExtractProperties();
24+
25+
Assert.IsNotNull(width);
26+
Assert.IsNotNull(lenght);
27+
Assert.IsNotNull(name);
28+
Assert.IsNotNull(total);
29+
}
30+
}
31+
}

src/Tests/RedButton.Tests.UnitTests/RedButton.Tests.UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
</ItemGroup>
4545
<ItemGroup>
4646
<Compile Include="BaseTest.cs" />
47+
<Compile Include="PropertiesTests\PropertiesExtractorTests.cs" />
4748
<Compile Include="RedButton.Common.Core.Geometry\SphereTest.cs" />
4849
<Compile Include="ExtensionsTests\SolidExtensionTests.cs" />
4950
<Compile Include="ExtensionsTests\AssemblyExtensionsTests.cs" />

0 commit comments

Comments
 (0)