@ifc-lite/codegen 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/cli.js +23 -5
  2. package/dist/cli.js.map +1 -1
  3. package/dist/crc32.d.ts +16 -0
  4. package/dist/crc32.d.ts.map +1 -0
  5. package/dist/crc32.js +69 -0
  6. package/dist/crc32.js.map +1 -0
  7. package/dist/generator.d.ts +20 -4
  8. package/dist/generator.d.ts.map +1 -1
  9. package/dist/generator.js +175 -19
  10. package/dist/generator.js.map +1 -1
  11. package/dist/index.d.ts +13 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +19 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/rust-generator.d.ts +20 -0
  16. package/dist/rust-generator.d.ts.map +1 -0
  17. package/dist/rust-generator.js +640 -0
  18. package/dist/rust-generator.js.map +1 -0
  19. package/dist/serialization-generator.d.ts +11 -0
  20. package/dist/serialization-generator.d.ts.map +1 -0
  21. package/dist/serialization-generator.js +333 -0
  22. package/dist/serialization-generator.js.map +1 -0
  23. package/dist/type-ids-generator.d.ts +11 -0
  24. package/dist/type-ids-generator.d.ts.map +1 -0
  25. package/dist/type-ids-generator.js +153 -0
  26. package/dist/type-ids-generator.js.map +1 -0
  27. package/generated/ifc4x3/entities.ts +9 -13
  28. package/generated/ifc4x3/enums.ts +0 -4
  29. package/generated/ifc4x3/index.ts +4 -2
  30. package/generated/ifc4x3/schema-registry.ts +326 -2718
  31. package/generated/ifc4x3/selects.ts +0 -4
  32. package/generated/ifc4x3/serializers.ts +322 -0
  33. package/generated/ifc4x3/test-compile.ts +49 -0
  34. package/generated/ifc4x3/type-ids.ts +1882 -0
  35. package/generated/ifc4x3/types.ts +0 -4
  36. package/package.json +1 -1
  37. package/src/cli.ts +49 -18
  38. package/src/crc32.ts +77 -0
  39. package/src/generator.ts +213 -21
  40. package/src/index.ts +28 -2
  41. package/src/rust-generator.ts +715 -0
  42. package/src/serialization-generator.ts +343 -0
  43. package/src/type-ids-generator.ts +166 -0
  44. package/tsconfig.json +1 -0
@@ -1,7 +1,3 @@
1
- /* This Source Code Form is subject to the terms of the Mozilla Public
2
- * License, v. 2.0. If a copy of the MPL was not distributed with this
3
- * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
-
5
1
  /**
6
2
  * IFC SELECT Types (Unions)
7
3
  * Generated from EXPRESS schema: IFC4X3_DEV_923b0514
@@ -0,0 +1,322 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ /**
6
+ * IFC Serialization Support
7
+ * Generated from EXPRESS schema: IFC4X3_DEV_923b0514
8
+ *
9
+ * Utilities for serializing IFC entities to STEP format.
10
+ *
11
+ * DO NOT EDIT - This file is auto-generated
12
+ */
13
+
14
+ import { SCHEMA_REGISTRY } from './schema-registry.js';
15
+
16
+ /**
17
+ * STEP value types
18
+ */
19
+ export type StepValue =
20
+ | string
21
+ | number
22
+ | boolean
23
+ | null
24
+ | undefined
25
+ | StepValue[]
26
+ | EntityRef
27
+ | EnumValue;
28
+
29
+ /**
30
+ * Entity reference (#123)
31
+ */
32
+ export interface EntityRef {
33
+ ref: number;
34
+ }
35
+
36
+ /**
37
+ * Enum value (.VALUE.)
38
+ */
39
+ export interface EnumValue {
40
+ enum: string;
41
+ }
42
+
43
+ /**
44
+ * Check if value is an entity reference
45
+ */
46
+ export function isEntityRef(value: unknown): value is EntityRef {
47
+ return typeof value === 'object' && value !== null && 'ref' in value;
48
+ }
49
+
50
+ /**
51
+ * Check if value is an enum value
52
+ */
53
+ export function isEnumValue(value: unknown): value is EnumValue {
54
+ return typeof value === 'object' && value !== null && 'enum' in value;
55
+ }
56
+
57
+ /**
58
+ * Create an entity reference
59
+ */
60
+ export function ref(id: number): EntityRef {
61
+ return { ref: id };
62
+ }
63
+
64
+ /**
65
+ * Create an enum value
66
+ */
67
+ export function enumVal(value: string): EnumValue {
68
+ return { enum: value };
69
+ }
70
+
71
+ /**
72
+ * Base interface for serializable entities
73
+ */
74
+ export interface StepEntity {
75
+ /** Express ID (#123) */
76
+ expressId: number;
77
+ /** IFC type name */
78
+ type: string;
79
+ /** Attribute values */
80
+ [key: string]: unknown;
81
+ }
82
+
83
+ /**
84
+ * Serialize a single value to STEP format
85
+ */
86
+ export function serializeValue(value: StepValue): string {
87
+ // Null/undefined -> $
88
+ if (value === null || value === undefined) {
89
+ return '$';
90
+ }
91
+
92
+ // Derived value -> *
93
+ if (value === '*') {
94
+ return '*';
95
+ }
96
+
97
+ // Boolean
98
+ if (typeof value === 'boolean') {
99
+ return value ? '.T.' : '.F.';
100
+ }
101
+
102
+ // Number
103
+ if (typeof value === 'number') {
104
+ if (!Number.isFinite(value)) {
105
+ return '$';
106
+ }
107
+ // Use exponential notation for large/small numbers
108
+ if (Math.abs(value) > 1e10 || (Math.abs(value) < 1e-10 && value !== 0)) {
109
+ return value.toExponential(10).toUpperCase().replace('E+', 'E');
110
+ }
111
+ // Otherwise use fixed notation
112
+ const str = value.toString();
113
+ // Ensure there's a decimal point for REAL values
114
+ return str.includes('.') ? str : str + '.';
115
+ }
116
+
117
+ // String
118
+ if (typeof value === 'string') {
119
+ return "'" + escapeStepString(value) + "'";
120
+ }
121
+
122
+ // Entity reference
123
+ if (isEntityRef(value)) {
124
+ return '#' + value.ref;
125
+ }
126
+
127
+ // Enum value
128
+ if (isEnumValue(value)) {
129
+ return '.' + value.enum + '.';
130
+ }
131
+
132
+ // Array/List
133
+ if (Array.isArray(value)) {
134
+ if (value.length === 0) {
135
+ return '()';
136
+ }
137
+ return '(' + value.map(v => serializeValue(v as StepValue)).join(',') + ')';
138
+ }
139
+
140
+ // Object (shouldn't happen for valid STEP values)
141
+ return '$';
142
+ }
143
+
144
+ /**
145
+ * Escape a string for STEP format
146
+ */
147
+ function escapeStepString(str: string): string {
148
+ return str
149
+ .replace(/\\/g, '\\\\') // Backslash
150
+ .replace(/'/g, "''"); // Single quote
151
+ }
152
+
153
+ /**
154
+ * Serialize an entity to a STEP line
155
+ */
156
+ export function toStepLine(entity: StepEntity): string {
157
+ const schema = SCHEMA_REGISTRY.entities[entity.type];
158
+ if (!schema) {
159
+ throw new Error(`Unknown entity type: ${entity.type}`);
160
+ }
161
+
162
+ // Get all attributes in order
163
+ const values: string[] = [];
164
+ for (const attr of schema.allAttributes) {
165
+ const value = entity[attr.name];
166
+ values.push(serializeValue(value as StepValue));
167
+ }
168
+
169
+ return `#${entity.expressId}=${entity.type.toUpperCase()}(${values.join(',')});`;
170
+ }
171
+
172
+ /**
173
+ * Generate STEP file header
174
+ */
175
+ export function generateHeader(options: {
176
+ description?: string;
177
+ author?: string;
178
+ organization?: string;
179
+ application?: string;
180
+ schema: 'IFC2X3' | 'IFC4' | 'IFC4X3';
181
+ filename?: string;
182
+ }): string {
183
+ const now = new Date().toISOString().replace(/[-:]/g, '').split('.')[0];
184
+ const desc = options.description || 'ViewDefinition [CoordinationView]';
185
+ const author = options.author || '';
186
+ const org = options.organization || '';
187
+ const app = options.application || 'ifc-lite';
188
+ const filename = options.filename || 'output.ifc';
189
+
190
+ return `ISO-10303-21;
191
+ HEADER;
192
+ FILE_DESCRIPTION('${desc}','2;1');
193
+ FILE_NAME('${filename}','${now}',('${author}'),('${org}'),'${app}','${app}','');
194
+ FILE_SCHEMA(('${options.schema}'));
195
+ ENDSEC;
196
+ `;
197
+ }
198
+
199
+ /**
200
+ * Generate complete STEP file content
201
+ */
202
+ export function generateStepFile(
203
+ entities: StepEntity[],
204
+ options: Parameters<typeof generateHeader>[0]
205
+ ): string {
206
+ const header = generateHeader(options);
207
+
208
+ // Sort entities by ID for deterministic output
209
+ const sorted = [...entities].sort((a, b) => a.expressId - b.expressId);
210
+
211
+ const data = sorted.map(e => toStepLine(e)).join('\n');
212
+
213
+ return `${header}DATA;
214
+ ${data}
215
+ ENDSEC;
216
+ END-ISO-10303-21;
217
+ `;
218
+ }
219
+
220
+ /**
221
+ * Parse a STEP value from string
222
+ */
223
+ export function parseStepValue(str: string): StepValue {
224
+ str = str.trim();
225
+
226
+ // Null
227
+ if (str === '$') {
228
+ return null;
229
+ }
230
+
231
+ // Derived
232
+ if (str === '*') {
233
+ return '*' as unknown as StepValue;
234
+ }
235
+
236
+ // Boolean
237
+ if (str === '.T.') {
238
+ return true;
239
+ }
240
+ if (str === '.F.') {
241
+ return false;
242
+ }
243
+ if (str === '.U.') {
244
+ return null; // Unknown/indeterminate
245
+ }
246
+
247
+ // Entity reference
248
+ if (str.startsWith('#')) {
249
+ return { ref: parseInt(str.substring(1), 10) };
250
+ }
251
+
252
+ // Enum
253
+ if (str.startsWith('.') && str.endsWith('.')) {
254
+ return { enum: str.substring(1, str.length - 1) };
255
+ }
256
+
257
+ // String
258
+ if (str.startsWith("'") && str.endsWith("'")) {
259
+ return unescapeStepString(str.substring(1, str.length - 1));
260
+ }
261
+
262
+ // List
263
+ if (str.startsWith('(') && str.endsWith(')')) {
264
+ return parseStepList(str);
265
+ }
266
+
267
+ // Number
268
+ const num = parseFloat(str);
269
+ if (!isNaN(num)) {
270
+ return num;
271
+ }
272
+
273
+ // Unknown
274
+ return str;
275
+ }
276
+
277
+ /**
278
+ * Parse a STEP list
279
+ */
280
+ function parseStepList(str: string): StepValue[] {
281
+ // Remove outer parentheses
282
+ const inner = str.substring(1, str.length - 1).trim();
283
+ if (inner === '') {
284
+ return [];
285
+ }
286
+
287
+ const values: StepValue[] = [];
288
+ let depth = 0;
289
+ let current = '';
290
+
291
+ for (let i = 0; i < inner.length; i++) {
292
+ const char = inner[i];
293
+
294
+ if (char === '(' || char === '[') {
295
+ depth++;
296
+ current += char;
297
+ } else if (char === ')' || char === ']') {
298
+ depth--;
299
+ current += char;
300
+ } else if (char === ',' && depth === 0) {
301
+ values.push(parseStepValue(current));
302
+ current = '';
303
+ } else {
304
+ current += char;
305
+ }
306
+ }
307
+
308
+ if (current.trim()) {
309
+ values.push(parseStepValue(current));
310
+ }
311
+
312
+ return values;
313
+ }
314
+
315
+ /**
316
+ * Unescape a STEP string
317
+ */
318
+ function unescapeStepString(str: string): string {
319
+ return str
320
+ .replace(/''/g, "'")
321
+ .replace(/\\\\/g, '\\');
322
+ }
@@ -0,0 +1,49 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ /**
6
+ * Type-check test file
7
+ * This file is used to verify the generated types compile correctly.
8
+ *
9
+ * DO NOT EDIT - This file is auto-generated
10
+ */
11
+
12
+ import type { IfcWall, IfcProject, IfcExtrudedAreaSolid } from './entities.js';
13
+ import { TYPE_IDS, getTypeId, getTypeName } from './type-ids.js';
14
+ import { SCHEMA_REGISTRY, getEntityMetadata } from './schema-registry.js';
15
+ import { toStepLine, serializeValue, ref, enumVal, type StepEntity } from './serializers.js';
16
+
17
+ // Test type IDs
18
+ const wallId: number = TYPE_IDS.IfcWall;
19
+ const projectId: number = TYPE_IDS.IfcProject;
20
+
21
+ // Test ID lookup
22
+ const wallIdFromName = getTypeId('IfcWall');
23
+ const nameFromId = getTypeName(wallId);
24
+
25
+ // Test schema registry
26
+ const wallMeta = getEntityMetadata('IfcWall');
27
+ const wallAttrs = wallMeta?.allAttributes;
28
+
29
+ // Test serialization
30
+ const testEntity: StepEntity = {
31
+ expressId: 1,
32
+ type: 'IfcProject',
33
+ GlobalId: '0YvctVUKr0kugbFTf53O9L',
34
+ OwnerHistory: ref(2),
35
+ Name: 'Test Project',
36
+ Description: null,
37
+ ObjectType: null,
38
+ LongName: null,
39
+ Phase: null,
40
+ RepresentationContexts: [ref(3)],
41
+ UnitsInContext: ref(4),
42
+ };
43
+
44
+ const stepLine = toStepLine(testEntity);
45
+
46
+ console.log('✓ All types compile correctly');
47
+ console.log(' Wall ID:', wallId);
48
+ console.log(' Project ID:', projectId);
49
+ console.log(' STEP line:', stepLine);