欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > chili3d笔记19 读取dxf

chili3d笔记19 读取dxf

2025/6/21 6:26:56 来源:https://blog.csdn.net/njsgcs/article/details/148770188  浏览:    关键词:chili3d笔记19 读取dxf

读取dxf

import { Logger, PubSub } from "chili-core";
import DxfParser, { ILineEntity } from 'dxf-parser';export function rebuild3D(document: Document) {const fileInput = document.createElement("input");fileInput.type = "file";fileInput.accept = ".dxf"; // 修改为正确的文件类型fileInput.style.display = "none"; // 隐藏输入框fileInput.click(); // 触发文件选择// 处理文件选择事件fileInput.addEventListener("change", async (event) => {const target = event.target as HTMLInputElement;if (target.files && target.files.length > 0) {const file = target.files[0];Logger.info(`Selected file: ${file.name}`);try {const reader = new FileReader();reader.onload = function () {const dxfText = reader.result as string;const parser = new DxfParser();try {const dxf = parser.parseSync(dxfText);if (dxf && dxf.entities) {dxf.entities.forEach(entity => {if (entity.type === 'LINE') {const lineEntity = entity as ILineEntity;const start =lineEntity.vertices[0];const end = lineEntity.vertices[1];// 确保起点和终点都有坐标if (start && end) {PubSub.default.pub("njsgcs_makeline",start.x, start.y, start.z,end.x, end.y, end.z);}}});
}Logger.info('成功解析 DXF:', dxf);} catch (err) {Logger.error('解析失败:', err);}};reader.readAsText(file);} catch (error) {Logger.error("读取文件失败:", error);}}});
}
    async newDocument(name: string): Promise<IDocument> {const document = new Document(this, name);const lightGray = new Material(document, "LightGray", 0xdedede);const deepGray = new Material(document, "DeepGray", 0x898989);const redMaterial = new Material(document, "Red", 0xff0000,"1");  
const greenMaterial = new Material(document, "Green", 0x00ff00,"2");  
const blueMaterial = new Material(document, "Blue", 0x0000ff,"3");  const yellowMaterial = new Material(document, "Yellow", 0xffff00, "4"); // 黄色
const cyanMaterial = new Material(document, "Cyan", 0x00ffff, "5");     // 青色
document.materials.push(lightGray, deepGray,redMaterial, greenMaterial, blueMaterial,yellowMaterial, cyanMaterial);await this.createActiveView(document);return document;}

const materialIds = app.activeView?.document!.materials.map(material => material.id);  
console.log("所有材质ID:", materialIds);

可以将 Canvas 的原点从默认的左上角改为左下角,以实现与标准 CAD 坐标系一致的效果。可以通过以下方式实现:✅ 实现方法
使用变换矩阵翻转 Y 轴: 在绘制前对 ctx 应用一个变换,将 Y 轴翻转,使原点位于左下角。调整绘制逻辑中的画布高度偏移: 通过 ctx.setTransform 或手动调整坐标来实现。🔧 修改建议
在调用 ctx.beginPath() 之前,添加如下代码:ts
ctx.setTransform(1, 0, 0, -1, 0, ctx.canvas.height); // 翻转Y轴,并将原点移到左下角

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词