Skip to content

Commit 3a0750c

Browse files
committed
use buildShape in objToMesh
1 parent c246bf2 commit 3a0750c

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

packages/base/src/3dview/mainview.tsx

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { TransformControls } from 'three/examples/jsm/controls/TransformControls
2222
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js';
2323
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';
2424
import { ViewHelper } from 'three/examples/jsm/helpers/ViewHelper';
25+
import { IParsedShape } from '@jupytercad/schema';
2526

2627
import { FloatingAnnotation } from '../annotation';
2728
import { getCSSVariableColor, throttle } from '../tools';
@@ -996,38 +997,65 @@ export class MainView extends React.Component<IProps, IStates> {
996997
postResult: IPostResult
997998
): Promise<void> {
998999
const { binary, format, value } = postResult;
999-
let obj: THREE.BufferGeometry | undefined = undefined;
1000-
if (format === 'STL') {
1001-
let buff: string | ArrayBuffer;
1002-
if (binary) {
1003-
const str = `data:application/octet-stream;base64,${value}`;
1004-
const b = await fetch(str);
1005-
buff = await b.arrayBuffer();
1006-
} else {
1007-
buff = value;
1008-
}
1009-
const loader = new STLLoader();
1010-
obj = loader.parse(buff);
1000+
if (format !== 'STL') {
1001+
return;
1002+
}
1003+
let buff: string | ArrayBuffer;
1004+
if (binary) {
1005+
const str = `data:application/octet-stream;base64,${value}`;
1006+
const b = await fetch(str);
1007+
buff = await b.arrayBuffer();
1008+
} else {
1009+
buff = value;
10111010
}
1011+
const loader = new STLLoader();
1012+
const geometry = loader.parse(buff);
10121013

1013-
if (!obj) {
1014+
if (!geometry) {
10141015
return;
10151016
}
10161017

1017-
const material = new THREE.MeshPhongMaterial({
1018-
color: DEFAULT_MESH_COLOR,
1019-
wireframe: this.state.wireframe
1018+
const parsedShape: IParsedShape = {
1019+
jcObject: { name: 'example', visible: true },
1020+
faceList: [],
1021+
edgeList: []
1022+
};
1023+
1024+
const obj = this._model.sharedModel.getObjectByName(name);
1025+
const objColor = obj?.parameters?.Color;
1026+
const isWireframe = this.state.wireframe;
1027+
1028+
const output = buildShape({
1029+
objName: name,
1030+
data: parsedShape,
1031+
clippingPlanes: this._clippingPlanes,
1032+
isSolid: true,
1033+
isWireframe,
1034+
objColor
10201035
});
1021-
const mesh = new THREE.Mesh(obj, material);
1022-
1023-
const lineGeo = new THREE.WireframeGeometry(mesh.geometry);
1024-
const mat = new THREE.LineBasicMaterial({ color: 'black' });
1025-
const wireframe = new THREE.LineSegments(lineGeo, mat);
1026-
mesh.add(wireframe);
1027-
mesh.name = name;
1028-
if (this._meshGroup) {
1029-
this._meshGroup.add(mesh);
1030-
this._boundingGroup?.expandByObject(mesh);
1036+
1037+
if (output) {
1038+
const { meshGroup, mainMesh, edgesMeshes } = output;
1039+
1040+
if (meshGroup.userData.jcObject.visible) {
1041+
this._boundingGroup?.expandByObject(meshGroup);
1042+
}
1043+
1044+
if (mainMesh.material?.color) {
1045+
const originalMeshColor = new THREE.Color(
1046+
objColor || DEFAULT_MESH_COLOR
1047+
);
1048+
mainMesh.material.color = originalMeshColor;
1049+
mainMesh.userData.originalColor = originalMeshColor.clone();
1050+
}
1051+
1052+
edgesMeshes.forEach(edgeMesh => {
1053+
this._edgeMaterials.push(edgeMesh.material);
1054+
const edgeColor = new THREE.Color(objColor || DEFAULT_EDGE_COLOR);
1055+
edgeMesh.material.color = edgeColor;
1056+
});
1057+
1058+
this._meshGroup?.add(meshGroup);
10311059
}
10321060
this._updateRefLength(true);
10331061
}

0 commit comments

Comments
 (0)