This repository has been archived by the owner on May 16, 2022. It is now read-only.
/
AdnStructurePanel.js
303 lines (242 loc) · 8.77 KB
/
AdnStructurePanel.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
//
// Copyright (c) Autodesk, Inc. All rights reserved
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Forge AU Sample
// by Eason Kang - Autodesk Developer Network (ADN)
//
'use strict';
(function() {
/////////////////////////////////////////////////////////////////
// Generates random guid to use as DOM id
//
/////////////////////////////////////////////////////////////////
function guid() {
var d = new Date().getTime();
var guid = 'xxxx-xxxx-xxxx-xxxx'.replace(
/[xy]/g,
function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
});
return guid;
}
class AdnModelStructurePanel extends Autodesk.Viewing.UI.DockingPanel {
constructor( viewer, title, options ) {
options = options || {};
// Height adjustment for scroll container, offset to height of the title bar and footer by default.
if( !options.heightAdjustment )
options.heightAdjustment = 70;
if( !options.marginTop )
options.marginTop = 0;
super( viewer.container, viewer.container.id + 'AdnModelStructurePanel', title, options );
this.container.classList.add( 'adn-docking-panel' );
this.container.classList.add( 'adn-model-structure-panel' );
this.createScrollContainer( options );
this.viewer = viewer;
this.options = options;
this.uiCreated = false;
this.addVisibilityListener(( show ) => {
if( !show ) return;
if( !this.uiCreated )
this.createUI();
this.resizeToContent();
});
}
hasPropertyTask( model, dbId, propName, matches ) {
return new Promise(function( resolve, reject ) {
const instanceTree = model.getData().instanceTree;
model.getProperties( dbId, function( result ) {
const nodeName = instanceTree.getNodeName( dbId );
const hasChildren = instanceTree.getChildCount( dbId ) > 0 ;
if( !result.properties || hasChildren )
return resolve();
for( let i = 0; i < result.properties.length; ++i ) {
const prop = result.properties[i];
//check if we have a match
if( !prop.displayName.contains( propName ) || !prop.displayValue )
continue;
let match = matches.find( node => node.text == prop.displayValue );
if( !match ) {
match = {
id: 'mat-' + guid(),
text: prop.displayValue,
children: [
{
id: dbId,
text: nodeName
}
]
};
matches.push( match );
} else {
match.children.push({
id: dbId,
text: nodeName
});
}
}
return resolve();
}, function() {
return reject();
});
});
}
executeTaskOnModelTree( model, task ) {
const taskResults = [];
function _executeTaskOnModelTreeRec( dbId ){
instanceTree.enumNodeChildren( dbId,
function( childId ) {
taskResults.push( task( model, childId) );
_executeTaskOnModelTreeRec( childId );
});
}
//get model instance tree and root component
const instanceTree = model.getData().instanceTree;
const rootId = instanceTree.getRootId();
_executeTaskOnModelTreeRec( rootId );
return taskResults;
}
buildTree() {
const viewer = this.viewer;
viewer.getObjectTree( () => {
const matches = [];
// Creates a thunk for our task
// We look for all components which have a
// property named 'Material' and returns a list
// of matches containing dbId and the prop value
const taskThunk = ( model, dbId ) => {
return this.hasPropertyTask(
model, dbId, 'Material', matches);
};
const taskResults = this.executeTaskOnModelTree(
viewer.model,
taskThunk
);
Promise.all( taskResults )
.then(() => {
console.log( 'Found ' + matches.length + ' matches' );
console.log( matches );
$( this.treeContainer )
.on('select_node.jstree', function( e, data ) {
console.log( e, data );
if( !data ) return;
let dbIds = [];
viewer.clearSelection();
if( data.node.id.contains( 'mat-' ) ) {
dbIds = data.node.children.map( child => parseInt( child ) );
} else {
const dbId = parseInt( data.node.id );
dbIds = [dbId];
}
viewer.select( dbIds );
viewer.fitToView( dbIds );
})
// .on( 'open_node.jstree', ( e, data ) => {
// this.resizeToContent();
// })
// .on( 'close_node.jstree', ( e, data ) => {
// this.resizeToContent();
// })
.jstree({
core: {
data: matches,
themes: {
icons: false
}
}
});
});
},
function( code, msg ) {
console.error( code, msg );
});
}
createUI() {
this.uiCreated = true;
const div = document.createElement( 'div' );
const treeDiv = document.createElement( 'div' );
div.appendChild( treeDiv );
this.treeContainer = treeDiv;
this.scrollContainer.appendChild( div );
this.buildTree();
}
}
class AdnModelStructurePanelExtension extends Autodesk.Viewing.Extension {
constructor( viewer, options ) {
super( viewer, options );
this.panel = null;
this.createUI = this.createUI.bind( this );
this.onToolbarCreated = this.onToolbarCreated.bind( this );
}
onToolbarCreated() {
this.viewer.removeEventListener(
Autodesk.Viewing.TOOLBAR_CREATED_EVENT,
this.onToolbarCreated
);
this.createUI();
}
createUI() {
const viewer = this.viewer;
const modelStructurePanel = new AdnModelStructurePanel( viewer, 'Material Browser' );
viewer.addPanel( modelStructurePanel );
this.panel = modelStructurePanel;
const structureButton = new Autodesk.Viewing.UI.Button( 'toolbar-adnModelStructureTool' );
structureButton.setToolTip( 'ADN Model browser' );
structureButton.setIcon( 'adsk-icon-structure' );
structureButton.onClick = function() {
modelStructurePanel.setVisible( !modelStructurePanel.isVisible() );
};
const subToolbar = new Autodesk.Viewing.UI.ControlGroup( 'toolbar-adn-tools' );
subToolbar.addControl( structureButton );
subToolbar.adnstructurebutton = structureButton;
this.subToolbar = subToolbar;
viewer.toolbar.addControl( this.subToolbar );
modelStructurePanel.addVisibilityListener(function( visible ) {
if( visible )
viewer.onPanelVisible( modelStructurePanel, viewer );
structureButton.setState( visible ? Autodesk.Viewing.UI.Button.State.ACTIVE : Autodesk.Viewing.UI.Button.State.INACTIVE );
});
}
load() {
if( this.viewer.toolbar ) {
// Toolbar is already available, create the UI
this.createUI();
} else {
// Toolbar hasn't been created yet, wait until we get notification of its creation
this.viewer.addEventListener(
Autoesek.Viewing.TOOLBAR_CREATED_EVENT,
this.onToolbarCreated
);
}
return true;
}
unload() {
if( this.panel ) {
this.panel.uninitialize();
delete this.panel;
this.panel = null;
}
if( this.subToolbar ) {
this.viewer.toolbar.removeControl( this.subToolbar );
delete this.subToolbar;
this.subToolbar = null;
}
return true;
}
}
Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.ModelStructurePanel', AdnModelStructurePanelExtension );
})();