-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLoadModel.cs
155 lines (144 loc) · 5.94 KB
/
LoadModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// BIMRL (BIM Rule Language) Simplified Schema ETL (Extract, Transform, Load) library: this library transforms IFC data into BIMRL Simplified Schema for RDBMS.
// This work is part of the original author's Ph.D. thesis work on the automated rule checking in Georgia Institute of Technology
// Copyright (C) 2013 Wawan Solihin ([email protected])
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; If not, see <http://www.gnu.org/licenses/>.
//
// This file incorporates work covered by the following copyright and permission notice:
// Xbim License (Common Development and Distribution License (CDDL)
// the license details can be found at: https://github.com/xBimTeam/XbimWindowsUI/blob/master/LICENCE.md
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Xbim.Ifc;
using Xbim.Common;
using Xbim.Common.Step21;
using Xbim.IO.Esent;
using Xbim.ModelGeometry.Scene;
using Xbim.Presentation.FederatedModel;
using Newtonsoft.Json;
using BIMRL;
using BIMRL.Common;
namespace BIMRLDiffModelsCmd
{
class LoadModel
{
static public int LoadModelToBIMRL(IfcStore model)
{
BIMRLProcessModel bimrlPM = new BIMRLProcessModel(model, false);
if (bimrlPM != null)
return DBOperation.currFedModel.FederatedID;
return -1;
}
static public IfcStore OpenModel(string fileName)
{
IfcStore model = null;
try
{
string extName = Path.GetExtension(fileName);
if (extName.Equals(".xbimf"))
{
model = LoadFederatedModelDef(fileName);
}
else
{
model = IfcStore.Open(fileName, null, null, null, XbimDBAccess.Read);
if (model.GeometryStore.IsEmpty)
{
var context = new Xbim3DModelContext(model);
//upgrade to new geometry representation, uses the default 3D model
//context.CreateContext(progDelegate: _worker.ReportProgress);
context.CreateContext();
}
foreach (var modelReference in model.ReferencedModels)
{
// creates federation geometry contexts if needed
if (modelReference.Model == null)
continue;
if (!modelReference.Model.GeometryStore.IsEmpty)
continue;
var context = new Xbim3DModelContext(modelReference.Model);
//upgrade to new geometry representation, uses the default 3D model
context.CreateContext();
}
}
return model;
}
catch (Exception ex)
{
var sb = new StringBuilder();
sb.AppendLine($"Error opening '{fileName}' {ex.StackTrace}.");
throw new Exception(sb.ToString(), ex);
}
}
static private IfcStore LoadFederatedModelDef(string fileName)
{
XBimFederation xFedModel = JsonConvert.DeserializeObject<XBimFederation>(File.ReadAllText(fileName));
xFedModel.fedFilePath = fileName;
IfcStore fedModel = ProcessModels(xFedModel);
return fedModel;
}
static private IfcStore ProcessModels(XBimFederation xFedModel)
{
IfcStore fedModel = IfcStore.Create(null, IfcSchemaVersion.Ifc4, XbimStoreType.InMemoryModel);
using (var txn = fedModel.BeginTransaction())
{
var project = fedModel.Instances.New<Xbim.Ifc4.Kernel.IfcProject>();
//project.Name = "Default Project Name";
project.Name = xFedModel.FederationName;
project.LongName = xFedModel.FederationName + " - Federation";
project.Description = xFedModel.FederationDescription;
project.Initialize(ProjectUnits.SIUnitsUK);
txn.Commit();
}
var informUser = true;
for (var i = 0; i < xFedModel.GetMemberModelList().Count; i++)
{
var fileName = xFedModel.GetMemberModelList()[i].ModelFileName;
if (!addReferencedModel(fileName, fedModel, i, informUser, out informUser))
break; // The process is being cancelled by user
}
return fedModel;
}
static bool addReferencedModel(string fileName, IfcStore fedModel, int counter, bool informUser, out bool userRespOnInfo)
{
userRespOnInfo = true;
var temporaryReference = new XbimReferencedModelViewModel
{
Name = fileName,
OrganisationName = "OrganisationName " + counter,
OrganisationRole = "Undefined"
};
var buildRes = false;
Exception exception = null;
try
{
buildRes = temporaryReference.TryBuildAndAddTo(fedModel);
}
catch (Exception ex)
{
//usually an EsentDatabaseSharingViolationException, user needs to close db first
exception = ex;
Console.WriteLine("%Error. Unable to open the .xbim cache file (" + fileName + "). Check source file exists or the cache file is being used or corrupted!");
return false;
}
return true;
}
}
}