Skip to main content

Command Palette

Search for a command to run...

在vtk.js中stl和json的互相转化

Published
•1 min read•View as Markdown

stl如何转为json

import vtkSTLReader from '@kitware/vtk.js/IO/Geometry/STLReader';

const getStlModelFromPath = async (path: string) => {
  const response = await fetch(path);
  const stlArrayBuffer = await response.arrayBuffer();

  const stlReader = vtkSTLReader.newInstance();
  stlReader.parseAsArrayBuffer(stlArrayBuffer);

  const polyData = stlReader.getOutputData();
  return polyData;
};

const stlPath = '/path/to/your/model.stl';
const polyData = await getStlModelFromPath(stlPath);
const jsonData = polyData.toJSON();

json如何转为stl

import modelJSON from './model.json';

const convertPolyDataJSONToStl = (polyDataJSON: string, fileName: string = 'model.stl') => {
    const polyData = vtkPolyData.newInstance(polyDataJSON);
    const writer = vtkSTLWriter.newInstance();

    writer.setInputData(polyData);
    const fileContents = writer.getOutputData();

    // Create a blob and download link
    const blob = new Blob([fileContents], { type: 'application/octet-stream' });
    const a = window.document.createElement('a');
    a.href = window.URL.createObjectURL(blob);
    a.download = fileName;

    // Trigger download
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(a.href);
};


convertPolyDataJSONToStl(modelJSON);
12 views

More from this blog

分享一下我的zustand错误使用案例

什么是 zustand? zustand 是一个状态管理库,简单易用。 在使用 react-scan 的时候,我发现我在 A 组件中对于 store 的某个 state 的更新,导致了 B 组件的重新渲染,有比较严重的性能问题。 于是我又重新阅读了文档,发现我之前对于 zustand 的使用是错误的。 错误用法 1 没有使用 single selector 或者 shallow 去获取 state 这是之前的写法以及对应的组件,useGlobalStore 中有不只 uploadToServer...

Dec 17, 20242 min read10
分享一下我的zustand错误使用案例

使用google sheet + api定时获取油管播放量

起因是想要记录一下 milklove 的二搭剧 Whale Store xoxo 在油管上的预告片的播放量的 คุณวาฬร้านชำ (Whale Store xoxo) | GMMTV 2025 - YouTube 于是先搜了一下有没有可以直接使用的开源项目或者接口,然后在 批量统计YouTube视频播放量方法 - 杨哥的出海营销笔记 这篇文章中了解到了可以使用 google sheet 的 apps 脚本 直接获取油管的播放量。 不过这篇文章中的脚本更多的获取当前的播放量,而不是定时获取,...

Dec 15, 20242 min read16
使用google sheet + api定时获取油管播放量

raycast + imessage 2fa,快速获取验证码

很久之前做过一个 messauto 的安利 MessAuto + iMessage 实现iPhone和mac信息同步和自动复制验证码 但是最近收到评论才发现作者在前段时间删库了 目前找到另一个快速获取验证码的方法,就是 raycast + imessage 2fa 插件。 使用方法: 在获取到验证码之后,访问 imessage 2fa 插件。 可以看到目前接收到的含有验证码的消息,敲回车复制验证码到粘贴板。 缺点:没有 messauto 方便,多了访问 imessage 2fa 插...

Dec 11, 20241 min read2
声

声控烤箱-memos

12 posts

前端,设计,音乐,生活,杂谈。 之前的博客:https://blog.kazoottt.top/