StxModel.js
4.17 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
import { BufferGeometry, BufferAttribute, Float32BufferAttribute } from 'three';
import SupportMesh from '@/utility/jsm/mesh/SupportMesh';
import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
const transferToGeometry = (meshstruct) => {
const { vertex, index } = meshstruct;
const geometry = new BufferGeometry();
if (index.length > 0) {
geometry.setIndex(new BufferAttribute(new Uint32Array(index), 1));
}
if (vertex.length > 0) {
geometry.setAttribute('position', new BufferAttribute(new Float32Array(vertex), 3));
}
geometry.setAttribute('uv', new Float32BufferAttribute([], 2));
geometry.computeVertexNormals();
return geometry;
};
class MeshInfo {
constructor({ name = '', numberOfVertex = 0, numberOfIndex = 0, vertex = [], index = [], support = null }) {
this.name = name;
this.numberOfVertex = numberOfVertex;
this.numberOfIndex = numberOfIndex;
this.vertex = vertex;
this.index = index;
this.support = support;
this.geometry = null;
}
}
class StxModel {
constructor({
headerName = '',
stxVersion = 0,
luxflowVersion = 0,
imageBase64 = '',
imageWidth = 0,
imageHeight = 0,
imageContent = '',
numberOfMesh = 0,
meshs = []
}) {
this.headerName = headerName;
this.stxVersion = stxVersion;
this.luxflowVersion = luxflowVersion;
this.imageBase64 = imageBase64;
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
this.imageContent = imageContent;
this.numberOfMesh = numberOfMesh;
this.mesh = [];
this.setMesh(meshs);
this.type = 'StxModel';
}
setMesh = (meshs) => {
meshs.forEach((el) => {
const { name, numberOfVertex, numberOfIndex, vertex, index, support, floors } = el;
// support
const supportMeshInfo = new MeshInfo({
name: support.name,
numberOfVertex: support.numberOfVertex,
numberOfIndex: support.numberOfIndex,
vertex: support.vertex,
index: support.index,
mesh: null
});
const supportGeometry = transferToGeometry({ vertex: support.vertex, index: support.index });
supportGeometry.name = support.name;
supportMeshInfo.geometry = supportGeometry;
const meshinfo = new MeshInfo({
name,
numberOfVertex,
numberOfIndex,
vertex,
index,
support: support.numberOfVertex > 0 ? supportMeshInfo : null
});
const geometry = transferToGeometry({ vertex, index });
geometry.name = name;
meshinfo.geometry = geometry;
const geoms = [];
floors.forEach((ele) => {
const supportFloorGeometry = transferToGeometry({ vertex: ele.vertex, index: ele.index });
if (supportFloorGeometry) geoms.push(supportFloorGeometry);
});
const { generalSupports, parameters } = el;
const supportMeshs = new SupportMesh();
supportMeshs.setSupports(generalSupports);
supportMeshs.setParameters(parameters);
generalSupports.forEach((ele) => {
const geom = supportMeshs.updateGeometry({ ele });
if (geom) geoms.push(geom);
});
const supprtGeometry = geoms.length > 0 ? mergeGeometries(geoms, true) : null;
meshinfo.supprtGeometry = supprtGeometry;
this.mesh.push(meshinfo);
});
// this.showConsole();
};
showConsole = () => {
console.log('文件名稱', this.headerName);
console.log('stx版本', this.stxVersion);
console.log('版本號', this.luxflowVersion);
console.log('圖片號', this.imageBase64);
console.log('圖片寬', this.imageWidth);
console.log('圖片高', this.imageHeight);
console.log('模型個數', this.numberOfMesh);
console.log('模型', this.mesh);
};
}
export default StxModel;