@buerli.io/headless 0.7.1-beta.2 → 0.7.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.
@@ -138,8 +138,9 @@ const createRecursiveScene = async (object, drawingId, root, result, options) =>
138
138
  const entities = object.link ? tree[object.link].solids : object.solids;
139
139
  if (entities != undefined) {
140
140
  const part = new three_1.Group();
141
- part.matrix = object.coordinateSystem ? core_1.MathUtils.convertToMatrix4(object.coordinateSystem) : new three_1.Matrix4();
142
- part.matrixAutoUpdate = false;
141
+ const matrix = object.coordinateSystem ? core_1.MathUtils.convertToMatrix4(object.coordinateSystem) : new three_1.Matrix4();
142
+ matrix.decompose(part.position, part.quaternion, part.scale);
143
+ part.updateMatrix();
143
144
  part.userData = { id: object.id }; // product reference id
144
145
  part.name = object.name; // product reference name
145
146
  for (const entity of entities) {
@@ -154,55 +155,92 @@ const createRecursiveScene = async (object, drawingId, root, result, options) =>
154
155
  const solidObject = cDrawing.structure.tree[cachedEntity.container.ownerId];
155
156
  if (solidObject) {
156
157
  const _solid = new three_1.Group();
157
- _solid.name = solidObject.name;
158
+ _solid.name = solidObject.name + '_solid';
158
159
  _solid.userData = { id: solidObject.id }; // add solid id to the userData
159
160
  if (options === null || options === void 0 ? void 0 : options.meshPerGeometry) {
160
161
  // Create a mesh for each geometry element
161
- cachedEntity.meshes.forEach(m => {
162
+ cachedEntity.meshes.forEach((m, i) => {
162
163
  const mesh = new three_1.Mesh();
164
+ mesh.name = solidObject.name + '_' + i;
163
165
  mesh.userData = { id: m.graphicId }; // add graphic id to the userData
164
- mesh.geometry = m.geometry;
165
- mesh.material = new three_1.MeshStandardMaterial({
166
- color: m.color ? m.color : cachedEntity.color,
167
- transparent: m.color ? m.opacity < 1 : cachedEntity.opacity < 1,
168
- opacity: m.color ? m.opacity : cachedEntity.opacity,
169
- });
166
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
167
+ mesh.geometry = m.geometry;
168
+ mesh.material = new three_1.MeshStandardMaterial({
169
+ color: m.color ? m.color : cachedEntity.color,
170
+ transparent: m.color ? m.opacity < 1 : cachedEntity.opacity < 1,
171
+ opacity: m.color ? m.opacity : cachedEntity.opacity,
172
+ });
173
+ mesh.material.name = mesh.name + '_material';
174
+ result.materials[mesh.material.name] = mesh.material;
175
+ }
176
+ result.nodes[mesh.name] = mesh;
170
177
  _solid.add(mesh);
171
- result.meshes.push(mesh);
172
178
  });
179
+ part.add(_solid);
180
+ result.nodes[_solid.name] = _solid;
173
181
  }
174
182
  else {
175
183
  // Merge all geometries into one mesh, sort them by color
176
184
  const colorMeshesMap = (0, meshes_1.getDifferentColoredMeshesFromEntity)(cachedEntity);
177
- Object.keys(colorMeshesMap).forEach(key => {
178
- const mesh = new three_1.Mesh();
179
- mesh.geometry = BufferGeometryUtils.mergeBufferGeometries(colorMeshesMap[key].meshes.map(m => m.geometry));
180
- mesh.material = new three_1.MeshStandardMaterial({
181
- color: colorMeshesMap[key].color,
182
- transparent: colorMeshesMap[key].opacity < 1,
183
- opacity: colorMeshesMap[key].opacity,
185
+ const colorMeshes = Object.keys(colorMeshesMap);
186
+ if (colorMeshes.length > 1) {
187
+ // If the solid consists of multiple meshes we must keep the group
188
+ colorMeshes.forEach((key, i) => {
189
+ const mesh = new three_1.Mesh();
190
+ mesh.name = solidObject.name + '_' + i;
191
+ mesh.userData = { id: solidObject.id }; // add solid id to the userData
192
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
193
+ mesh.geometry = (BufferGeometryUtils.mergeBufferGeometries || BufferGeometryUtils.mergeGeometries)(colorMeshesMap[key].meshes.map(m => m.geometry));
194
+ mesh.material = new three_1.MeshStandardMaterial({
195
+ color: colorMeshesMap[key].color,
196
+ transparent: colorMeshesMap[key].opacity < 1,
197
+ opacity: colorMeshesMap[key].opacity,
198
+ });
199
+ mesh.material.name = mesh.name + '_material';
200
+ result.materials[mesh.material.name] = mesh.material;
201
+ }
202
+ result.nodes[mesh.name] = mesh; // Collect mesh to return result later
203
+ _solid.add(mesh); // Add mesh to solid of the scene
184
204
  });
185
- _solid.add(mesh); // Add mesh to solid of the scene
186
- result.meshes.push(mesh); // Collect mesh to return result later
187
- });
205
+ part.add(_solid);
206
+ result.nodes[_solid.name] = _solid;
207
+ }
208
+ else {
209
+ // Otherwise we can use the mesh itself
210
+ const key = colorMeshes[0];
211
+ const mesh = new three_1.Mesh();
212
+ mesh.name = solidObject.name;
213
+ mesh.userData = { id: solidObject.id }; // add solid id to the userData
214
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
215
+ mesh.geometry = (BufferGeometryUtils.mergeBufferGeometries || BufferGeometryUtils.mergeGeometries)(colorMeshesMap[key].meshes.map(m => m.geometry));
216
+ mesh.material = new three_1.MeshStandardMaterial({
217
+ color: colorMeshesMap[key].color,
218
+ transparent: colorMeshesMap[key].opacity < 1,
219
+ opacity: colorMeshesMap[key].opacity,
220
+ });
221
+ mesh.material.name = mesh.name + '_material';
222
+ result.materials[mesh.material.name] = mesh.material;
223
+ }
224
+ result.nodes[mesh.name] = mesh;
225
+ part.add(mesh);
226
+ }
188
227
  }
189
- part.add(_solid);
190
- result.solids.push(_solid);
191
228
  }
192
229
  }
193
230
  }
194
231
  root.add(part);
195
- result.nodes.push(part);
232
+ result.nodes[part.name] = part;
196
233
  }
197
234
  }
198
235
  else if (object.children) {
199
236
  // Assembly
200
237
  const assembly = new three_1.Group();
201
- assembly.matrix = object.coordinateSystem ? core_1.MathUtils.convertToMatrix4(object.coordinateSystem) : new three_1.Matrix4();
202
- assembly.matrixAutoUpdate = false;
238
+ const matrix = object.coordinateSystem ? core_1.MathUtils.convertToMatrix4(object.coordinateSystem) : new three_1.Matrix4();
239
+ matrix.decompose(assembly.position, assembly.quaternion, assembly.scale);
240
+ assembly.updateMatrix();
203
241
  assembly.userData = { id: object.id }; // assembly root or product reference id
204
242
  assembly.name = object.name; // assembly root or product reference name
205
- result.nodes.push(assembly);
243
+ result.nodes[assembly.name] = assembly;
206
244
  root.add(assembly);
207
245
  for (const child of object.children) {
208
246
  if (classcad_1.ccUtils.base.isA(tree[child].class, classcad_1.CCClasses.IProductReference)) {
@@ -1063,14 +1101,13 @@ class CAD {
1063
1101
  createScene: async (objectId, options) => {
1064
1102
  await this.queue;
1065
1103
  this.queue = Promise.resolve(0);
1066
- const rObjectId = await objectId;
1067
1104
  const cDrawing = (0, core_1.getDrawing)(drawingId);
1068
1105
  const structureTree = cDrawing.structure.tree;
1106
+ const rObjectId = objectId ? await objectId : cDrawing.structure.root;
1069
1107
  const scene = new three_1.Scene();
1070
1108
  const result = {
1071
- nodes: [],
1072
- solids: [],
1073
- meshes: [],
1109
+ nodes: {},
1110
+ materials: {},
1074
1111
  };
1075
1112
  if (rObjectId != null) {
1076
1113
  const rootObj = structureTree[rObjectId];
@@ -113,8 +113,9 @@ const createRecursiveScene = async (object, drawingId, root, result, options) =>
113
113
  const entities = object.link ? tree[object.link].solids : object.solids;
114
114
  if (entities != undefined) {
115
115
  const part = new Group();
116
- part.matrix = object.coordinateSystem ? MathUtils.convertToMatrix4(object.coordinateSystem) : new Matrix4();
117
- part.matrixAutoUpdate = false;
116
+ const matrix = object.coordinateSystem ? MathUtils.convertToMatrix4(object.coordinateSystem) : new Matrix4();
117
+ matrix.decompose(part.position, part.quaternion, part.scale);
118
+ part.updateMatrix();
118
119
  part.userData = { id: object.id }; // product reference id
119
120
  part.name = object.name; // product reference name
120
121
  for (const entity of entities) {
@@ -129,55 +130,92 @@ const createRecursiveScene = async (object, drawingId, root, result, options) =>
129
130
  const solidObject = cDrawing.structure.tree[cachedEntity.container.ownerId];
130
131
  if (solidObject) {
131
132
  const _solid = new Group();
132
- _solid.name = solidObject.name;
133
+ _solid.name = solidObject.name + '_solid';
133
134
  _solid.userData = { id: solidObject.id }; // add solid id to the userData
134
135
  if (options === null || options === void 0 ? void 0 : options.meshPerGeometry) {
135
136
  // Create a mesh for each geometry element
136
- cachedEntity.meshes.forEach(m => {
137
+ cachedEntity.meshes.forEach((m, i) => {
137
138
  const mesh = new Mesh();
139
+ mesh.name = solidObject.name + '_' + i;
138
140
  mesh.userData = { id: m.graphicId }; // add graphic id to the userData
139
- mesh.geometry = m.geometry;
140
- mesh.material = new MeshStandardMaterial({
141
- color: m.color ? m.color : cachedEntity.color,
142
- transparent: m.color ? m.opacity < 1 : cachedEntity.opacity < 1,
143
- opacity: m.color ? m.opacity : cachedEntity.opacity,
144
- });
141
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
142
+ mesh.geometry = m.geometry;
143
+ mesh.material = new MeshStandardMaterial({
144
+ color: m.color ? m.color : cachedEntity.color,
145
+ transparent: m.color ? m.opacity < 1 : cachedEntity.opacity < 1,
146
+ opacity: m.color ? m.opacity : cachedEntity.opacity,
147
+ });
148
+ mesh.material.name = mesh.name + '_material';
149
+ result.materials[mesh.material.name] = mesh.material;
150
+ }
151
+ result.nodes[mesh.name] = mesh;
145
152
  _solid.add(mesh);
146
- result.meshes.push(mesh);
147
153
  });
154
+ part.add(_solid);
155
+ result.nodes[_solid.name] = _solid;
148
156
  }
149
157
  else {
150
158
  // Merge all geometries into one mesh, sort them by color
151
159
  const colorMeshesMap = getDifferentColoredMeshesFromEntity(cachedEntity);
152
- Object.keys(colorMeshesMap).forEach(key => {
153
- const mesh = new Mesh();
154
- mesh.geometry = BufferGeometryUtils.mergeBufferGeometries(colorMeshesMap[key].meshes.map(m => m.geometry));
155
- mesh.material = new MeshStandardMaterial({
156
- color: colorMeshesMap[key].color,
157
- transparent: colorMeshesMap[key].opacity < 1,
158
- opacity: colorMeshesMap[key].opacity,
160
+ const colorMeshes = Object.keys(colorMeshesMap);
161
+ if (colorMeshes.length > 1) {
162
+ // If the solid consists of multiple meshes we must keep the group
163
+ colorMeshes.forEach((key, i) => {
164
+ const mesh = new Mesh();
165
+ mesh.name = solidObject.name + '_' + i;
166
+ mesh.userData = { id: solidObject.id }; // add solid id to the userData
167
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
168
+ mesh.geometry = (BufferGeometryUtils.mergeBufferGeometries || BufferGeometryUtils.mergeGeometries)(colorMeshesMap[key].meshes.map(m => m.geometry));
169
+ mesh.material = new MeshStandardMaterial({
170
+ color: colorMeshesMap[key].color,
171
+ transparent: colorMeshesMap[key].opacity < 1,
172
+ opacity: colorMeshesMap[key].opacity,
173
+ });
174
+ mesh.material.name = mesh.name + '_material';
175
+ result.materials[mesh.material.name] = mesh.material;
176
+ }
177
+ result.nodes[mesh.name] = mesh; // Collect mesh to return result later
178
+ _solid.add(mesh); // Add mesh to solid of the scene
159
179
  });
160
- _solid.add(mesh); // Add mesh to solid of the scene
161
- result.meshes.push(mesh); // Collect mesh to return result later
162
- });
180
+ part.add(_solid);
181
+ result.nodes[_solid.name] = _solid;
182
+ }
183
+ else {
184
+ // Otherwise we can use the mesh itself
185
+ const key = colorMeshes[0];
186
+ const mesh = new Mesh();
187
+ mesh.name = solidObject.name;
188
+ mesh.userData = { id: solidObject.id }; // add solid id to the userData
189
+ if (!(options === null || options === void 0 ? void 0 : options.structureOnly)) {
190
+ mesh.geometry = (BufferGeometryUtils.mergeBufferGeometries || BufferGeometryUtils.mergeGeometries)(colorMeshesMap[key].meshes.map(m => m.geometry));
191
+ mesh.material = new MeshStandardMaterial({
192
+ color: colorMeshesMap[key].color,
193
+ transparent: colorMeshesMap[key].opacity < 1,
194
+ opacity: colorMeshesMap[key].opacity,
195
+ });
196
+ mesh.material.name = mesh.name + '_material';
197
+ result.materials[mesh.material.name] = mesh.material;
198
+ }
199
+ result.nodes[mesh.name] = mesh;
200
+ part.add(mesh);
201
+ }
163
202
  }
164
- part.add(_solid);
165
- result.solids.push(_solid);
166
203
  }
167
204
  }
168
205
  }
169
206
  root.add(part);
170
- result.nodes.push(part);
207
+ result.nodes[part.name] = part;
171
208
  }
172
209
  }
173
210
  else if (object.children) {
174
211
  // Assembly
175
212
  const assembly = new Group();
176
- assembly.matrix = object.coordinateSystem ? MathUtils.convertToMatrix4(object.coordinateSystem) : new Matrix4();
177
- assembly.matrixAutoUpdate = false;
213
+ const matrix = object.coordinateSystem ? MathUtils.convertToMatrix4(object.coordinateSystem) : new Matrix4();
214
+ matrix.decompose(assembly.position, assembly.quaternion, assembly.scale);
215
+ assembly.updateMatrix();
178
216
  assembly.userData = { id: object.id }; // assembly root or product reference id
179
217
  assembly.name = object.name; // assembly root or product reference name
180
- result.nodes.push(assembly);
218
+ result.nodes[assembly.name] = assembly;
181
219
  root.add(assembly);
182
220
  for (const child of object.children) {
183
221
  if (ccUtils.base.isA(tree[child].class, CCClasses.IProductReference)) {
@@ -1038,14 +1076,13 @@ export default class CAD {
1038
1076
  createScene: async (objectId, options) => {
1039
1077
  await this.queue;
1040
1078
  this.queue = Promise.resolve(0);
1041
- const rObjectId = await objectId;
1042
1079
  const cDrawing = getDrawing(drawingId);
1043
1080
  const structureTree = cDrawing.structure.tree;
1081
+ const rObjectId = objectId ? await objectId : cDrawing.structure.root;
1044
1082
  const scene = new Scene();
1045
1083
  const result = {
1046
- nodes: [],
1047
- solids: [],
1048
- meshes: [],
1084
+ nodes: {},
1085
+ materials: {},
1049
1086
  };
1050
1087
  if (rObjectId != null) {
1051
1088
  const rootObj = structureTree[rObjectId];
@@ -1,6 +1,6 @@
1
1
  import { BooleanOperationType, ChamferType, FlipType, ReorientedType, WorkAxisType, WorkCoordSystemType, WorkPlaneType, WorkPointType, ViewType, OrientationType, ExtrusionType, BrepElemType, WorkGeomType, SVGModusType, LimitsParam, LimitedValue } from '@buerli.io/classcad';
2
2
  import { ObjectIdent, ObjectID, PointMemValue } from '@buerli.io/core';
3
- import { BufferGeometry, Group, Mesh, Scene } from 'three';
3
+ import { BufferGeometry, Material, Object3D, Scene } from 'three';
4
4
  export declare type Vec = number[] | PointMemValue | THREE.Vector3 | THREE.Euler;
5
5
  /**
6
6
  * History based modeling is similar to solid modeling, but it is non-destructive and
@@ -610,13 +610,17 @@ export declare type ApiHistory = {
610
610
  * @return object containing all the created THREE objects like scene, nodes, solids and meshes
611
611
  * @example // tbd
612
612
  */
613
- createScene: (productId: VID, options?: {
613
+ createScene: (productId?: VID, options?: {
614
614
  meshPerGeometry?: boolean;
615
+ structureOnly?: boolean;
615
616
  }) => Promise<{
616
617
  scene: Scene;
617
- nodes: Group[];
618
- solids: Group[];
619
- meshes: Mesh[];
618
+ nodes: {
619
+ [key: string]: Object3D;
620
+ };
621
+ materials: {
622
+ [key: string]: Material;
623
+ };
620
624
  }>;
621
625
  /**
622
626
  * Creates one or more expressions.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buerli.io/headless",
3
- "version": "0.7.1-beta.2",
3
+ "version": "0.7.1",
4
4
  "description": "A non-manifold CAD workflow for the web",
5
5
  "author": "AWV Informatik AG <info@awv-informatik.ch>",
6
6
  "license": "ISC",
@@ -30,7 +30,7 @@
30
30
  "three": ">=0.138"
31
31
  },
32
32
  "dependencies": {
33
- "@buerli.io/classcad": "0.7.1-beta.2",
33
+ "@buerli.io/classcad": "0.7.1",
34
34
  "flat-promise": "^1.0.3"
35
35
  }
36
36
  }