├── .gitignore
├── .travis.yml
├── .vscode
├── launch.json
└── tasks.json
├── IFC-dotnet.sln
├── IFC.xml
├── LICENSE
├── README.md
├── build.sh
├── src
└── IFC-dotnet
│ ├── Exceptions.cs
│ ├── IFC-dotnet.csproj
│ ├── IFC.cs
│ ├── IfcSelect.cs
│ ├── Model.cs
│ ├── STEP.g4
│ ├── STEPListener.cs
│ ├── TypeConverters.cs
│ └── antlr
│ ├── STEP.tokens
│ ├── STEPBaseListener.cs
│ ├── STEPLexer.cs
│ ├── STEPLexer.tokens
│ ├── STEPListener.cs
│ └── STEPParser.cs
└── tests
└── IFC-dotnet-test
├── IFC-dotnet-test.csproj
├── SerializationTests.cs
└── models
├── 20160125WestRiverSide Hospital - IFC4-Autodesk_Hospital_Sprinkle.ifc
├── AC-20-Smiley-West-10-Bldg.ifc
└── example.ifc
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: csharp
2 | solution: ./src/IFC-dotnet.sln
3 | mono: none
4 | dotnet: 2.0.0
5 | dist: trusty
6 | script:
7 | - dotnet restore
8 | - dotnet build
9 | - cd tests/IFC-dotnet-test
10 | - dotnet xunit
11 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to find out which attributes exist for C# debugging
3 | // Use hover for the description of the existing attributes
4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "name": ".NET Core Launch (console)",
9 | "type": "coreclr",
10 | "request": "launch",
11 | "preLaunchTask": "build",
12 | // If you have changed target frameworks, make sure to update the program path.
13 | "program": "${workspaceRoot}/src/IFC-dotnet-test/bin/Debug/netcoreapp2.0/IFC-dotnet-test.dll",
14 | "args": [],
15 | "cwd": "${workspaceRoot}/src/IFC-dotnet-test",
16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17 | "console": "internalConsole",
18 | "stopAtEntry": false,
19 | "internalConsoleOptions": "openOnSessionStart"
20 | },
21 | {
22 | "name": ".NET Core Attach",
23 | "type": "coreclr",
24 | "request": "attach",
25 | "processId": "${command:pickProcess}"
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.1.0",
3 | "command": "dotnet",
4 | "isShellCommand": true,
5 | "args": [],
6 | "tasks": [
7 | {
8 | "taskName": "build",
9 | "args": [
10 | "${workspaceRoot}/src/IFC-dotnet-test/IFC-dotnet-test.csproj"
11 | ],
12 | "isBuildCommand": true,
13 | "problemMatcher": "$msCompile"
14 | }
15 | ]
16 | }
--------------------------------------------------------------------------------
/IFC-dotnet.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26124.0
5 | MinimumVisualStudioVersion = 15.0.26124.0
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IFC-dotnet", "src\IFC-dotnet\IFC-dotnet.csproj", "{301045AC-77F1-4B10-B668-0213CB53A2AB}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IFC-dotnet-test", "tests\IFC-dotnet-test\IFC-dotnet-test.csproj", "{3FA9536F-5D93-49B0-88E6-365400E8BB60}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Debug|x64 = Debug|x64
14 | Debug|x86 = Debug|x86
15 | Release|Any CPU = Release|Any CPU
16 | Release|x64 = Release|x64
17 | Release|x86 = Release|x86
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|x64.ActiveCfg = Debug|x64
26 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|x64.Build.0 = Debug|x64
27 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|x86.ActiveCfg = Debug|x86
28 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Debug|x86.Build.0 = Debug|x86
29 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|Any CPU.Build.0 = Release|Any CPU
31 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|x64.ActiveCfg = Release|x64
32 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|x64.Build.0 = Release|x64
33 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|x86.ActiveCfg = Release|x86
34 | {301045AC-77F1-4B10-B668-0213CB53A2AB}.Release|x86.Build.0 = Release|x86
35 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|Any CPU.Build.0 = Debug|Any CPU
37 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|x64.ActiveCfg = Debug|x64
38 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|x64.Build.0 = Debug|x64
39 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|x86.ActiveCfg = Debug|x86
40 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Debug|x86.Build.0 = Debug|x86
41 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|Any CPU.ActiveCfg = Release|Any CPU
42 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|Any CPU.Build.0 = Release|Any CPU
43 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|x64.ActiveCfg = Release|x64
44 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|x64.Build.0 = Release|x64
45 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|x86.ActiveCfg = Release|x86
46 | {3FA9536F-5D93-49B0-88E6-365400E8BB60}.Release|x86.Build.0 = Release|x86
47 | EndGlobalSection
48 | EndGlobal
49 |
--------------------------------------------------------------------------------
/IFC.xml:
--------------------------------------------------------------------------------
1 | 3ZdNj5swEIZ/DdeKYAjsdbPph9SqVXNo9+iFCXhrGOSYhfTX14RxCIVUlVaLUDgg+/V47Hn1TBQctsmbD4qX2RdMQDqemzQOe3A8L2K+ebfCsRPCIOyEVImkk1a9sBO/gUSX1EokcBgEakSpRTkUYywKiPVA40phPQzboxyeWvIURsIu5nKs/hCJzqiswO31jyDSzJ68cmnlice/UoVVQec5Htufnm455zYXxR8ynmB9IbGtwzYKUXejvNmAbK21tnX73l9ZPd9bQaH/awPtOOijrR0SYwVNUekMUyy43Pbq/ak+aDO4ZpbpXJrhygyhEfpnK78LaPZoVwqtjhdL7fSREjyD1kdigFcajdSf+xmxpBzdTdvrXS3WVoOViinKI3y4SoGiwrPRhl/AHMxlTIgCybV4GWbnRFJ6juvdNAMydNpcOvqFy4qSftrH3xQmlUH2b9uHptaZ0LAr+amK2vTY0GhKDEpD828zxmXSBhYRgtSi7G7dzese+JXFNLuA3TbBa5yxfX672LExdt5M2LEp7L4+PcMSqXP9+ai7u3Xo/DF0bCbo/OvQPcBeFEILLBaHn2FtPvyiW8cvGOPnz4RfMIXf9/Zv1NKQO+M1B3LrKVvuKyETUaRbCXlbxNIcCqIZHbI/ALfbleG4K9czdWU4hd9SsfOj6M2wM9P+m+60dvHdzLZ/AA==
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Ian Keough
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # THIS PROJECT HAS BEEN MERGED INTO [IFC-gen](https://github.com/ikeough/IFC-gen/blob/master/README.md) AND IS NO LONGER IN DEVELOPMENT HERE.
2 |
3 | # IFC-dotnet
4 |
5 | An [Industry Foundation Classes](http://www.buildingsmart-tech.org/specifications/ifc-overview/ifc-overview-summary) (IFC) library for .NET Core.
6 |
7 | # Pre-requisites
8 | - [.NET Core](https://www.microsoft.com/net/core)
9 |
10 | # To Build
11 | ```
12 | cd src
13 | dotnet build
14 | ```
15 |
16 | # To Test
17 | ```
18 | cd tests
19 | cd IFC-dotnet-test
20 | dotnet xunit
21 | ```
22 |
23 | # Design Considerations
24 | Most of the code in this repository is generated automatically using [IFC-gen](https://github.com/ikeough/IFC-gen/blob/master/README.md). The templates for code generation and certain base classes have been designed to generate an idiomatic C# library.
25 |
26 | - All EXPRESS `TYPE` have a corresponding class derived from `IfcType`, which is a wrapper class whose `Value` property holds the actual value.
27 | - All EXPRESS `ENUM` have a corresponding enum.
28 | - All EXPRESS collection types: 'LIST', 'SET', and 'ARRAY', currently generate `List` fields in C#. This solution does not conform to the EXPRESS specification for those types. So it will be updated in the future.
29 | - EXPRESS `SELECT` types are created as classes derived from the `Select` base class. The `Select` base class has generic parameters corresponding to the types which can be selected in the `SELECT`. Additionally, the code generator creates one constructor for each selection type to aid in discovery of selectable types. Other languages which support discriminated unions may prefer to use that mechanism for modeling `SELECT`.
30 | - All EXPRESS `ENTITY` have a corresponding class.
31 | - All IFC attributes are generated as public class properties with a getter and setter. Chances are these will become more restrictive in the future. For those attributes not marked as `OPTIONAL`, the entity's corresponding class constructor will have parameters corresponding to the property, and a corresponding assignment will be generated in the constructor body.
32 | - Attributes marked as `DERIVED` are not currently turned into properties. Although the grammar generates a parser which can understand `DERIVE` and `WHERE` statements, these are not currently converted to code in property or method bodies.
33 | - Types derived from `IfcRelationship` do not have corresponding parameters in the generated constructors. Many types have _many_ relationship properties. Adding all of these to the constructor made the constructors unwieldy. It's possible that these are required, but my present intuition is that a higher-level factory class for properly generating entities and establishing their relationships is a better way to do this. Whether these higher level libraries can also be auto-generated is a point on which I welcome all feedback.
34 | - EXPRESS `WHERE` statements do not currently generate code. One possible pattern for this would be to generate static constructors for classes, placing the data validation logic in the static constructor and throwing an exception if the data validation fails. But, I feel like data validation needs to be done whenever the state of the object changes, so these data validation methods should either be reactive to property value changes, or they should live in a separate class containing testing methods for validating objects of certain types.
35 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | dir=$(pwd)
3 | cd src/IFC-dotnet
4 | java -jar /usr/local/lib/antlr-4.7-complete.jar -Dlanguage=CSharp -package STEP -o ./antlr STEP.g4
5 | cd $dir
6 | dotnet build -c Release
7 | cd tests/IFC-dotnet-test
8 | dotnet xunit -c Release
9 | cd $dir
--------------------------------------------------------------------------------
/src/IFC-dotnet/Exceptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace IFC4
4 | {
5 |
6 | ///
7 | /// Exception thrown when an unknown type is encountered.
8 | ///
9 | public class STEPUnknownTypeException : Exception
10 | {
11 | private string desiredType;
12 |
13 | public override string Message
14 | {
15 | get
16 | {
17 | return $"A type corresponding to, {desiredType}, cannot be found in IFC-dotnet assembly.";
18 | }
19 | }
20 |
21 | public STEPUnknownTypeException(string desiredType)
22 | {
23 | this.desiredType = desiredType;
24 | }
25 | }
26 |
27 | ///
28 | /// Exception thrown when a type cannot be coerced into a target type.
29 | ///
30 | public class STEPParserException : Exception
31 | {
32 | private string parseValue;
33 | private Type destinationType;
34 | public override string Message
35 | {
36 | get{return $"The specified value, {parseValue}, could not be coerced into the type, {destinationType.Name}";}
37 | }
38 |
39 | public STEPParserException(Type destinationType, string parseValue)
40 | {
41 | this.destinationType = destinationType;
42 | this.parseValue = parseValue;
43 | }
44 | }
45 |
46 | ///
47 | /// Exception thrown when there is a mismatch in parameters between the type and what is provided in the STEP file.
48 | ///
49 | public class STEPParameterMismatchException : Exception
50 | {
51 | Type type;
52 | int providedCount;
53 | int expectedCount;
54 |
55 | public override string Message
56 | {
57 | get
58 | {
59 | return $"{type}'s constructor expects {expectedCount} parameters but {providedCount} parameters are provided.";
60 | }
61 | }
62 |
63 | public STEPParameterMismatchException(Type type, int providedCount, int expectedCount)
64 | {
65 | this.type = type;
66 | this.providedCount = providedCount;
67 | this.expectedCount = expectedCount;
68 | }
69 | }
70 |
71 | ///
72 | /// Exception thrown when an identifier is found for which there is not construction.
73 | ///
74 | public class STEPIdentifierNotFoundException : Exception
75 | {
76 | private int id;
77 | private int currLine;
78 |
79 | public override string Message
80 | {
81 | get
82 | {
83 | return $"The id, {id}, found at line, {currLine}, could not be found in the STEP file being opened.";
84 | }
85 | }
86 |
87 | public STEPIdentifierNotFoundException(int id, int currLine)
88 | {
89 | this.id = id;
90 | this.currLine = currLine;
91 | }
92 | }
93 |
94 | ///
95 | /// Exception thrown when the specified id does not correspond to an instance in the Model.
96 | ///
97 | public class InstanceNotFoundException : Exception
98 | {
99 | private Guid id;
100 | public override string Message
101 | {
102 | get
103 | {
104 | return $"An instance with Id, {id}, does not exist in the model.";
105 | }
106 | }
107 |
108 | public InstanceNotFoundException(Guid id)
109 | {
110 | this.id = id;
111 | }
112 |
113 | }
114 |
115 | ///
116 | /// Exception thrown when an instance with the same id already exists in the model.
117 | ///
118 | public class DuplicateInstanceException : Exception
119 | {
120 | private Guid id;
121 |
122 | public override string Message
123 | {
124 | get
125 | {
126 | return $"An instance with the specified Id, {id}, already exists in the Model.";
127 | }
128 | }
129 |
130 | public DuplicateInstanceException(Guid id)
131 | {
132 | this.id = id;
133 | }
134 | }
135 | }
--------------------------------------------------------------------------------
/src/IFC-dotnet/IFC-dotnet.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netstandard2.0
4 | 0.0.1-beta1
5 | Ian Keough
6 | IFC-dotnet
7 | IFC-dotnet
8 | 3021
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/IFC-dotnet/Model.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using Antlr4.Runtime.Tree;
3 | using Newtonsoft.Json;
4 | using System;
5 | using System.Collections;
6 | using System.ComponentModel;
7 | using System.Diagnostics;
8 | using System.IO;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Transactions;
12 | using System.Collections.Generic;
13 |
14 | namespace IFC4
15 | {
16 | public abstract class STEPError
17 | {
18 | protected int currId;
19 |
20 | public virtual string Message
21 | {
22 | get{return $"There was an error reading the STEP file at Id, {currId}.";}
23 | }
24 |
25 | public STEPError(int currId)
26 | {
27 | this.currId = currId;
28 | }
29 | }
30 |
31 | public class MissingIdError : STEPError
32 | {
33 | private int missingId;
34 | public override string Message
35 | {
36 | get{return $"Id {missingId}, referenced in constructor, {currId}, could not be found in the file.";}
37 | }
38 |
39 | public MissingIdError(int id, int missingId):base(id)
40 | {
41 | this.missingId = missingId;
42 | }
43 | }
44 |
45 | ///
46 | /// Model provides a container for instances of BaseIfc.
47 | ///
48 | public class Model
49 | {
50 | private Dictionary instances;
51 |
52 | [JsonProperty("instances")]
53 | public Dictionary Instances
54 | {
55 | get{ return instances;}
56 | }
57 |
58 | public Model()
59 | {
60 | instances = new Dictionary();
61 | }
62 |
63 | internal Model(Dictionary instances)
64 | {
65 | this.instances = instances;
66 | }
67 |
68 | ///
69 | /// Add an instance to the model.
70 | ///
71 | /// The instance to add to the Model.
72 | /// Another instance already exists in the model with the same id.
73 | public void AddInstance(BaseIfc instance)
74 | {
75 | if(instances.ContainsKey(instance.Id))
76 | {
77 | throw new DuplicateInstanceException(instance.Id);
78 | }
79 |
80 | instances.Add(instance.Id, instance);
81 | }
82 |
83 | ///
84 | /// Remove an instance from the model.
85 | ///
86 | ///
87 | /// The specified instance does not exist in the model.
88 | public void RemoveInstance(Guid id)
89 | {
90 | if(!instances.ContainsKey(id))
91 | {
92 | throw new InstanceNotFoundException(id);
93 | }
94 |
95 | instances.Remove(id);
96 | }
97 |
98 | ///
99 | /// Finds an instance in the model, given its unique identifier.
100 | ///
101 | /// The unique id of the instance to find.
102 | /// An BaseIfc instance or null if no instance can be found with the provided id.
103 | public BaseIfc InstanceById(Guid id)
104 | {
105 | if(instances.ContainsKey(id))
106 | {
107 | return instances[id];
108 | }
109 |
110 | return null;
111 | }
112 |
113 | ///
114 | /// Find all instances of type T in the model.
115 | ///
116 | /// A collection of objects whose type is T.
117 | public IEnumerable AllInstancesOfType()
118 | {
119 | return instances.Values.OfType();
120 | }
121 |
122 | ///
123 | /// Find all instances derived from type T in the model.
124 | ///
125 | /// A collection of objects whose types are derived from T.
126 | public IEnumerable AllInstancesDerivedFromType()
127 | {
128 | return instances.Where(i=>typeof(T).IsAssignableFrom(i.Value.GetType())).Select(e=>e.Value);
129 | }
130 |
131 | ///
132 | /// Serialize the model to JSON.
133 | ///
134 | /// A string representing the model serialized to JSON. The string will be indented and include type references.
135 | public string ToJSON()
136 | {
137 | var settings = new JsonSerializerSettings(){
138 | Formatting = Formatting.Indented,
139 | TypeNameHandling = TypeNameHandling.Objects
140 | };
141 | var json = JsonConvert.SerializeObject(this, settings);
142 | return json;
143 | }
144 |
145 | ///
146 | /// Serialize the model to a DOT graph notation.
147 | ///
148 | /// A string representing the model serialized in DOT notation.
149 | public string ToDOT()
150 | {
151 | var relationships = AllInstancesDerivedFromType();
152 | var visited = new List(); // Ids of visited nodes;
153 |
154 | var relations = new StringBuilder();
155 | foreach(var r in relationships)
156 | {
157 | var rType = r.GetType();
158 | foreach(var p in r.GetType().GetProperties().Where(p=>p.DeclaringType == r.GetType()))
159 | {
160 | var pVal = p.GetValue(r);
161 | if(pVal is IList && pVal.GetType().IsGenericType)
162 | {
163 | var vs = (IList)pVal;
164 | foreach(BaseIfc v in vs)
165 | {
166 | relations.AppendLine($"\t\"{r.Id} : {rType.Name}\" -- \"{v.Id} : {v.GetType().Name}\"");
167 | }
168 | }
169 | else if(pVal is BaseIfc)
170 | {
171 | var v = (BaseIfc)pVal;
172 | relations.AppendLine($"\t\"{r.Id} : {rType.Name}\" -- \"{v.Id} : {v.GetType().Name}\"");
173 | }
174 | else if(pVal == null)
175 | {
176 | relations.AppendLine($"\t\"{r.Id} : {rType.Name}\" -- \"null\"");
177 | }
178 | }
179 | }
180 | string graph =
181 | $@"graph model{{
182 | rankdir=LR
183 | node [shape=box];
184 | {relations.ToString()}
185 | }}";
186 | return graph;
187 | }
188 |
189 | ///
190 | /// Create a Model given a STEP file.
191 | ///
192 | /// The path to the STEP file.
193 | /// A Model.
194 | /// The specified file path does not exist.
195 | public static Model FromSTEP(string filePath, out IList errors)
196 | {
197 | if(!File.Exists(filePath))
198 | {
199 | throw new FileNotFoundException($"The specified IFC STEP file does not exist: {filePath}.");
200 | }
201 |
202 | Model model = new Model();
203 |
204 | using (FileStream fs = new FileStream(filePath, FileMode.Open))
205 | {
206 | var input = new AntlrInputStream(fs);
207 | var lexer = new STEP.STEPLexer(input);
208 | var tokens = new CommonTokenStream(lexer);
209 |
210 | var parser = new STEP.STEPParser(tokens);
211 | parser.BuildParseTree = true;
212 |
213 | var tree = parser.file();
214 | var walker = new ParseTreeWalker();
215 |
216 | var listener = new STEP.STEPListener();
217 | walker.Walk(listener, tree);
218 |
219 | var err = new List();
220 | foreach(var data in listener.InstanceData)
221 | {
222 | if(data.Value.ConstructedGuid != null && model.InstanceById(data.Value.ConstructedGuid) != null)
223 | {
224 | // Instance may have been previously constructed as the result
225 | // of another construction.
226 | continue;
227 | }
228 |
229 | var instance = ConstructRecursive(data.Value, listener.InstanceData, model, data.Key, err);
230 | model.AddInstance(instance);
231 | }
232 | errors = err;
233 | }
234 | return model;
235 | }
236 |
237 | ///
238 | /// Recursively construct instances provided instance data.
239 | /// Construction is recursive because the instance data my include other
240 | /// instance data or id references to instances which have not yet been
241 | /// constructed.
242 | ///
243 | /// The instance data from which to construct the instance.
244 | /// The dictionary containing instance data gathered from the parser.
245 | /// The Model in which constructed instances will be stored.
246 | ///
247 | private static BaseIfc ConstructRecursive(STEP.InstanceData data, Dictionary instanceDataMap, Model model, int currLine, IList errors)
248 | {
249 | Debug.WriteLine($"{currLine},{data.Id} : Constructing type {data.Type.Name} with parameters [{string.Join(",",data.Parameters)}]");
250 |
251 | for(var i=data.Parameters.Count()-1; i>=0; i--)
252 | {
253 | var instData = data.Parameters[i] as STEP.InstanceData;
254 | if(instData != null)
255 | {
256 | var subInstance = ConstructRecursive(instData, instanceDataMap, model, currLine, errors);
257 | data.Parameters[i] = subInstance;
258 | continue;
259 | }
260 |
261 | var stepId = data.Parameters[i] as STEP.STEPId;
262 | if(stepId != null)
263 | {
264 | if(instanceDataMap.ContainsKey(stepId.Value))
265 | {
266 | var guid = instanceDataMap[stepId.Value].ConstructedGuid;
267 | var existingInst = model.InstanceById(guid);
268 | if(existingInst != null)
269 | {
270 | data.Parameters[i] = existingInst;
271 | continue;
272 | }
273 | }
274 | else
275 | {
276 | // A missing identifier results in an error.
277 | // set the data to null;
278 | errors.Add(new MissingIdError(currLine, stepId.Value));
279 | data.Parameters[i] = null;
280 | continue;
281 | }
282 |
283 | var subInstance = ConstructRecursive(instanceDataMap[stepId.Value], instanceDataMap, model, currLine, errors);
284 | data.Parameters[i] = subInstance;
285 | continue;
286 | }
287 |
288 | var list = data.Parameters[i] as List