forked from opensourceBIM/BIMsurfer-before2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BimSurfer.js
418 lines (347 loc) · 12.7 KB
/
BimSurfer.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
window.BIMSERVER_VERSION = "1.5";
define(["./Notifier", "./BimServerModel", "./PreloadQuery", "./BimServerGeometryLoader", "./xeoViewer/xeoViewer", "./EventHandler"], function (Notifier, Model, PreloadQuery, GeometryLoader, xeoViewer, EventHandler, _BimServerApi) {
// Backwards compatibility
var BimServerApi;
if (_BimServerApi) {
BimServerApi = _BimServerApi;
} else {
BimServerApi = window.BimServerClient;
}
function BimSurfer(cfg) {
var self = this;
EventHandler.call(this);
cfg = cfg || {};
var viewer = this.viewer = new xeoViewer(cfg);
/**
* Fired whenever this BIMSurfer's camera changes.
* @event camera-changed
*/
viewer.on("camera-changed", function() {
self.fire("camera-changed", arguments);
});
/**
* Fired whenever this BIMSurfer's selection changes.
* @event selection-changed
*/
viewer.on("selection-changed", function() {
self.fire("selection-changed", arguments);
});
// This are arrays as multiple models might be loaded or unloaded.
this._idMapping = {
'toGuid': [],
'toId' : []
};
/**
* Loads a model into this BIMSurfer.
* @param params
*/
this.load = function (params) {
if (params.test) {
viewer.loadRandom(params);
return null;
} else if (params.bimserver) {
return this._loadFromServer(params);
} else if (params.api) {
return this._loadFromAPI(params);
} else if (params.src) {
return this._loadFrom_glTF(params);
}
};
this._loadFromServer = function (params) {
var notifier = new Notifier();
var bimServerApi = new BimServerApi(params.bimserver, notifier);
params.api = bimServerApi; // TODO: Make copy of params
return self._initApi(params)
.then(self._loginToServer)
.then(self._getRevisionFromServer)
.then(self._loadFromAPI);
};
this._initApi = function(params) {
return new Promise(function(resolve, reject) {
params.api.init(function () {
resolve(params);
});
});
};
this._loginToServer = function (params) {
return new Promise(function(resolve, reject) {
if (params.token) {
params.api.setToken(params.token, function() {
resolve(params)
}, reject);
} else {
params.api.login(params.username, params.password, function() {
resolve(params)
}, reject);
}
});
};
this._getRevisionFromServer = function (params) {
return new Promise(function(resolve, reject) {
if (params.roid) {
resolve(params);
} else {
params.api.call("ServiceInterface", "getAllRelatedProjects", {poid: params.poid}, function(data) {
var resolved = false;
data.forEach(function(projectData) {
if (projectData.oid == params.poid) {
params.roid = projectData.lastRevisionId;
params.schema = projectData.schema;
if (!self.models) {
self.models = [];
}
self.models.push(projectData);
resolved = true;
resolve(params);
}
});
if (!resolved) {
reject();
}
}, reject);
}
});
};
this._loadFrom_glTF = function (params) {
if (params.src) {
var maxActiveProcessesEncountered = 0;
var oldProgress = 0;
return new Promise(function (resolve, reject) {
var m = viewer.loadglTF(params.src);
m.on("loaded", function() {
viewer.scene.canvas.spinner.on('processes', function(n) {
if (n === 0) {
viewer.viewFit({});
resolve(m);
}
if (n > maxActiveProcessesEncountered) {
maxActiveProcessesEncountered = n;
}
var progress = parseInt((maxActiveProcessesEncountered - n) * 100 / maxActiveProcessesEncountered);
if (oldProgress != progress) {
self.fire("progress", [progress]);
}
oldProgress = progress;
});
});
});
}
};
this._loadFromAPI = function (params) {
return new Promise(function (resolve, reject) {
params.api.getModel(params.poid, params.roid, params.schema, false,
function (model) {
// TODO: Preload not necessary combined with the bruteforce tree
var fired = false;
model.query(PreloadQuery,
function () {
if (!fired) {
fired = true;
var vmodel = new Model(params.api, model);
self._loadModel(vmodel);
resolve(vmodel);
}
});
});
});
};
this._loadModel = function (model) {
model.getTree().then(function (tree) {
var oids = [];
var oidToGuid = {};
var guidToOid = {};
var visit = function (n) {
if (BIMSERVER_VERSION == "1.4") {
oids.push(n.id);
} else {
oids[n.gid] = n.id;
}
oidToGuid[n.id] = n.guid;
guidToOid[n.guid] = n.id;
for (var i = 0; i < (n.children || []).length; ++i) {
visit(n.children[i]);
}
};
visit(tree);
self._idMapping.toGuid.push(oidToGuid);
self._idMapping.toId.push(guidToOid);
var models = {};
// TODO: Ugh. Undecorate some of the newly created classes
models[model.model.roid] = model.model;
// Notify viewer that things are loading, so viewer can
// reduce rendering speed and show a spinner.
viewer.taskStarted();
viewer.createModel(model.model.roid);
var loader = new GeometryLoader(model.api, models, viewer);
loader.addProgressListener(function (progress, nrObjectsRead, totalNrObjects) {
if (progress == "start") {
console.log("Started loading geometries");
self.fire("loading-started");
} else if (progress == "done") {
console.log("Finished loading geometries (" + totalNrObjects + " objects received)");
self.fire("loading-finished");
viewer.taskFinished();
}
});
loader.setLoadOids([model.model.roid], oids);
// viewer.clear(); // For now, until we support multiple models through the API
viewer.on("tick", function () { // TODO: Fire "tick" event from xeoViewer
loader.process();
});
loader.start();
});
};
// Helper function to traverse over the mappings for individually loaded models
var _traverseMappings = function(mappings) {
return function(k) {
for (var i = 0; i < mappings.length; ++i) {
var v = mappings[i][k];
if (v) return v;
}
return null;
}
};
/**
* Returns a list of object ids (oid) for the list of guids (GlobalId)
*
* @param guids List of globally unique identifiers from the IFC model
*/
this.toId = function(guids) {
return guids.map(_traverseMappings(self._idMapping.toId));
};
/**
* Returns a list of guids (GlobalId) for the list of object ids (oid)
*
* @param ids List of internal object ids from the BIMserver / glTF file
*/
this.toGuid = function(ids) {
return ids.map(_traverseMappings(self._idMapping.toGuid));
};
/**
* Shows/hides objects specified by id or entity type, e.g IfcWall.
*
* When recursive is set to true, hides children (aggregates, spatial structures etc) or
* subtypes (IfcWallStandardCase ⊆ IfcWall).
*
* @param params
*/
this.setVisibility = function (params) {
viewer.setVisibility(params);
};
/**
* Selects/deselects objects specified by id.
**
* @param params
*/
this.setSelection = function (params) {
return viewer.setSelection(params);
};
/**
* Gets a list of selected elements.
*/
this.getSelection = function () {
return viewer.getSelection();
};
/**
* Sets color of objects specified by ids or entity type, e.g IfcWall.
**
* @param params
*/
this.setColor = function (params) {
viewer.setColor(params);
};
/**
* Sets opacity of objects specified by ids or entity type, e.g IfcWall.
**
* @param params
*/
this.setOpacity = function (params) {
viewer.setOpacity(params);
};
/**
* Fits the elements into view.
*
* Fits the entire model into view if ids is an empty array, null or undefined.
* Animate allows to specify a transition period in milliseconds in which the view is altered.
*
* @param params
*/
this.viewFit = function (params) {
viewer.viewFit(params);
};
/**
*
*/
this.getCamera = function () {
return viewer.getCamera();
};
/**
*
* @param params
*/
this.setCamera = function (params) {
viewer.setCamera(params);
};
/**
* Redefines light sources.
*
* @param params Array of lights {type: "ambient"|"dir"|"point", params: {[...]}}
* See http://xeoengine.org/docs/classes/Lights.html for possible params for each light type
*/
this.setLights = function (params) {
viewer.setLights(params);
};
/**
* Returns light sources.
*
* @returns Array of lights {type: "ambient"|"dir"|"point", params: {[...]}}
*/
this.getLights = function () {
return viewer.getLights;
};
/**
*
* @param params
*/
this.reset = function (params) {
viewer.reset(params);
}
/**
* Returns a list of loaded IFC entity types in the model.
*
* @method getTypes
* @returns {Array} List of loaded IFC entity types, with visibility flag
*/
this.getTypes = function() {
return viewer.getTypes();
};
/**
* Sets the default behaviour of mouse and touch drag input
*
* @method setDefaultDragAction
* @param {String} action ("pan" | "orbit")
*/
this.setDefaultDragAction = function (action) {
viewer.setDefaultDragAction(action);
};
/**
* Returns the world boundary of an object
*
* @method getWorldBoundary
* @param {String} objectId id of object
* @param {Object} result Existing boundary object
* @returns {Object} World boundary of object, containing {obb, aabb, center, sphere} properties. See xeogl.Boundary3D
*/
this.getWorldBoundary = function(objectId, result) {
return viewer.getWorldBoundary(objectId, result);
};
/**
* Destroys the BIMSurfer
*/
this.destroy = function() {
viewer.destroy();
}
}
BimSurfer.prototype = Object.create(EventHandler.prototype);
return BimSurfer;
});