-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerateClassIdHasType.html
More file actions
230 lines (207 loc) · 10.5 KB
/
generateClassIdHasType.html
File metadata and controls
230 lines (207 loc) · 10.5 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IFC EXPRESS Schema to C++ Code Generator</title>
</head>
<body>
<h1>Generate classIDhasType() function for web-ifc</h1>
<p>classIDhasType() lets you easily check if a type code like webifc::schema::IfcElement has a certain type, for example webifc::schema::IfcObjectDefinition</p>
<p>Enter the URL to the raw IFC EXPRESS schema file (e.g., for IFC4):</p>
<input type="text" id="schemaUrl" value="https://raw.githubusercontent.com/stepcode/stepcode/master/data/ifc4/IFC4.exp" size="80">
<button onclick="generateCode()">Generate Code</button>
<p>Generated C++ Code:</p>
<textarea id="output" rows="30" cols="100"></textarea>
<script>
async function generateCode() {
const url = document.getElementById('schemaUrl').value;
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Failed to fetch schema');
let text = await response.text();
// Remove multi-line comments (/* */)
text = text.replace(/\/\*[\s\S]*?\*\//g, '');
// Remove single-line comments (--)
text = text.replace(/--.*?\n/g, '\n');
const entities = {};
let currentEntity = null;
let headerLines = [];
let inEntity = false;
let inAttributes = false;
const lines = text.split('\n');
for (let originalLine of lines) {
let line = originalLine.trim();
if (line === '' || line.startsWith('--')) continue;
if (!inEntity) {
if (line.match(/^(ABSTRACT\s+)?(SUPERTYPE\s+OF\s*\(.*\)\s+)?ENTITY\b/i)) {
inEntity = true;
headerLines = [line];
// Extract name
let namePart = line.replace(/^(ABSTRACT\s+)?(SUPERTYPE\s+OF\s*\([^\)]+\)\s+)?ENTITY\s+/i, '').trim();
namePart = namePart.split(/\s/)[0].replace(/;?$/, '');
currentEntity = namePart;
entities[currentEntity] = { supers: [] };
inAttributes = false;
}
} else {
if (line.match(/^END_ENTITY/i)) {
// Process header for SUBTYPE
let fullHeader = headerLines.join(' ');
let superMatch = fullHeader.match(/SUBTYPE\s+OF\s*\(([^\)]+)\)/i);
if (superMatch) {
let superStr = superMatch[1].trim();
const supers = superStr.split(',').map(s => s.trim()).filter(s => s);
entities[currentEntity].supers = supers;
}
currentEntity = null;
headerLines = [];
inEntity = false;
inAttributes = false;
} else if (inAttributes || line.match(/WHERE/i) || line.match(/UNIQUE/i) || line.match(/DERIVE/i) || line.match(/INVERSE/i)) {
inAttributes = true;
// Skip attributes and constraints
} else {
// Continuation of header
headerLines.push(line);
}
}
}
// Build all transitive supertypes for each entity
const allSupers = {};
function getAllSupers(name, visited = new Set()) {
if (allSupers[name]) return allSupers[name];
if (visited.has(name)) return []; // Cycle prevention
visited.add(name);
let sups = [];
const entity = entities[name];
if (entity && entity.supers.length > 0) {
for (let direct of entity.supers) {
sups.push(direct);
sups = sups.concat(getAllSupers(direct, visited));
}
}
allSupers[name] = sups;
return sups;
}
// Compute for all
const entityNames = Object.keys(entities).sort();
for (let name of entityNames) {
getAllSupers(name);
}
// Generate C++ code
let code = '#pragma once\n';
code += '#include <web-ifc/schema/IfcSchemaManager.h>\n';
code += '// generated with www.ifcquery.com/generateClassIdHasType.html\n';
code += 'using namespace webifc::schema;\n';
code += 'constexpr bool classIDhasType(uint32_t classID, uint32_t check)\n';
code += '{\n';
code += ' if (classID == check) { return true; }\n';
code += ' switch (classID)\n';
code += ' {\n';
for (let name of entityNames) {
const supers = allSupers[name] || [];
const uniqueSupers = [...new Set(supers)];
const constName = name.toUpperCase();
code += ` case ${constName}: return `;
if (uniqueSupers.length === 0) {
code += 'false;\n';
} else {
code += uniqueSupers.map(s => `check == ${s.toUpperCase()}`).join(' || ') + ';\n';
}
}
code += ' }\n';
code += ' return false;\n';
code += '}\n';
document.getElementById('output').value = code;
} catch (error) {
document.getElementById('output').value = 'Error: ' + error.message;
}
}
async function generateCode2() {
const url = document.getElementById('schemaUrl').value;
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Failed to fetch schema');
let text = await response.text();
// Remove comments (--) and multi-line comments (/* */)
text = text.replace(/\/\*[\s\S]*?\*\//g, '');
text = text.replace(/--.*?\n/g, '\n');
// Split by ';' to tokenize
const tokens = text.split(';').map(t => t.trim()).filter(t => t.length > 0);
const entities = {};
let currentEntity = null;
for (let token of tokens) {
if (token.startsWith('ENTITY ')) {
// Extract entity name: after 'ENTITY ' until whitespace or end
const nameMatch = token.match(/^ENTITY\s+([a-zA-Z0-9_]+)/);
if (nameMatch) {
const name = nameMatch[1];
currentEntity = name;
entities[name] = { supers: [] };
}
} else if (currentEntity && token.startsWith('SUBTYPE OF (') && token.endsWith(')')) {
// Extract supertypes: between 'SUBTYPE OF (' and ')'
let superStr = token.substring(12, token.length - 1).trim();
// Remove any nested parens if present, but IFC is usually simple
superStr = superStr.replace(/[\(\)]/g, '').trim();
const supers = superStr.split(',').map(s => s.trim()).filter(s => s.length > 0);
entities[currentEntity].supers = supers;
} else if (token === 'END_ENTITY') {
currentEntity = null;
}
}
// Build all transitive supertypes for each entity
const allSupers = {};
function getAllSupers(name, visited = new Set()) {
if (allSupers[name]) return allSupers[name];
if (visited.has(name)) return []; // Cycle prevention, though unlikely in IFC
visited.add(name);
let sups = [];
const entity = entities[name];
if (entity && entity.supers.length > 0) {
for (let direct of entity.supers) {
sups.push(direct);
sups = sups.concat(getAllSupers(direct, visited));
}
}
allSupers[name] = sups;
return sups;
}
// Compute for all
const entityNames = Object.keys(entities).sort();
for (let name of entityNames) {
getAllSupers(name);
}
// Generate C++ code
let code = '#pragma once\n';
code += '#include <web-ifc/schema/IfcSchemaManager.h>\n';
code += '// generated with www.ifcquery.com/generateClassIdHasType.html\n';
code += 'using namespace webifc::schema;\n';
code += 'constexpr bool classIDhasType(uint32_t classID, uint32_t check)\n';
code += '{\n';
code += ' if (classID == check) { return true; }\n';
code += ' switch (classID)\n';
code += ' {\n';
for (let name of entityNames) {
const supers = allSupers[name] || [];
// Remove duplicates if any from multiple inheritance
const uniqueSupers = [...new Set(supers)];
const constName = 'IFC' + name.toUpperCase();
code += ` case ${constName}: return `;
if (uniqueSupers.length === 0) {
code += 'false;\n';
} else {
code += uniqueSupers.map(s => `check == IFC${s.toUpperCase()}`).join(' || ') + ';\n';
}
}
code += ' }\n';
code += ' return false;\n';
code += '}\n';
document.getElementById('output').value = code;
} catch (error) {
document.getElementById('output').value = 'Error: ' + error.message;
}
}
</script>
</body>
</html>