chili调试笔记14 画线 页面布置 线条导出dxf
2025-05-08 09-05-06 llm画线
页面布置
expand有自己的格式
删了就会按照子元素格式
不加px无效
没有指定尺寸设置100%无效
怎么把线条导出dxf
@command({name: "file.export",display: "command.export",icon: "icon-export",
})
export class Export extends CancelableCommand {@Property.define("file.format")public get formats() {return this.getPrivateValue("formats", this.initCombobox());}public set formats(value: Combobox<string>) {this.setProperty("formats", value);}private initCombobox() {const box = new Combobox<string>();box.items.push(...this.application.dataExchange.exportFormats());return box;}protected async executeAsync() {const nodes = await this.selectNodesAsync();if (!nodes || nodes.length === 0) {PubSub.default.pub("showToast", "toast.select.noSelected");return;}PubSub.default.pub("showPermanent",async () => {const format = this.formats.selectedItem;if (format === undefined) return;const data = await this.application.dataExchange.export(format, nodes);if (!data) return;PubSub.default.pub("showToast", "toast.downloading");download(data, `${nodes[0].name}${format}`);},"toast.excuting{0}",I18n.translate("command.export"),);}
❓ 问题 3:缺少 TABLES
段定义图层等信息
如果你没有定义图层(Layer),某些软件可能会忽略你的图形。
✅ 推荐解决方案:补充 HEADER 和 TABLES 段落
下面是一个 完整且经过验证可以在 FreeCAD 中正常打开的 DXF 示例,基于你提供的线条数据:
dxf模板 不能用
0
SECTION
2
HEADER
9
$ACADVER
1
AC1027
9
$INSUNITS
70
4
0
ENDSEC0
SECTION
2
TABLES
0
TABLE
2
LAYER
701
0
LAYER
2
0
700
627
6
CONTINUOUS
0
ENDTAB
0
TABLE
2
LTYPE
701
0
LTYPE
2
CONTINUOUS
700
3
Solid line
7265
730
40
0.0
0
ENDTAB
0
ENDSEC0
SECTION
2
BLOCKS
0
BLOCK
8
0
2
*MODEL_SPACE
700
10
0.0
20
0.0
30
0.0
100
AcDbBlockBegin
3
*MODEL_SPACE
10
ENDBLK
100
AcDbBlockEnd
0
ENDSEC0
SECTION
2
ENTITIES
{{entities}}
0
ENDSEC0
SECTION
2
OBJECTS
0
DICTIONARY
5
F000
330
0
100
AcDbDictionary
3
ACAD_GROUP
350
F001
0
DICTIONARY
5
F001
330
F000
100
AcDbDictionary
0
ENDSEC0
EOF
让ai打工
放弃了,用三方库
cnpm install three-dxf
// three-dxf.d.ts
declare module 'three-dxf' {class Point3D {x: number;y: number;z: number;constructor(x: number, y: number, z?: number);}class Drawing {header: {setVersion(version: string): void;setUnit(unit: string): void;};addLayer(name: string, options?: any): void;addLine(start: Point3D, end: Point3D): void;toString(): string;}export { Drawing, Point3D };
}
freecad打不开别的打开了
cnpm install @tarikjabiri/dxf
@tarikjabiri/dxf CDN by jsDelivr - A CDN for npm and GitHub
private async handleDxfExport(nodes: VisualNode[]): Promise<string[]> {// 创建一个新的 DXF 文档const d = new DxfDocument();for (const node of nodes) {if (node instanceof LineNode) {const start = node.start;const end = node.end;// 创建线段并设置起点和终点const line = new Line({ x: start.x, y: start.y, z: Math.abs(start.z) < 1e-9 ? 0 : start.z },{ x: end.x, y: end.y, z: Math.abs(end.z) < 1e-9 ? 0 : end.z });// 添加到文档的实体集合中d.entities.modelSpace.addEntity(line);}}const fullDxfContent= d.stringify(); // 转换为字符串// 返回分割成行的数组,每行后加上换行符return fullDxfContent.split('\n').map(line => line + '\n');}
2年前的了,用python写吧
python直接写本地freecad可以打得开发回网页就打不开
import ezdxf
import tempfile
import osdef draw_lines_and_get_dxf(lines):"""根据给定的线段列表绘制直线,并返回 DXF 文档的字符串格式。:param lines: 线段列表,每个线段由起点和终点组成,格式为 [{"start": {"x": x1, "y": y1, "z": z1}, "end": {"x": x2, "y": y2, "z": z2}}, ...]:return: DXF 文档的字符串表示"""# 创建一个新的 DXF 文档doc = ezdxf.new('R2010')msp = doc.modelspace()# 遍历每条线段并绘制直线for line in lines:print(line)start = (line["start"]["x"], line["start"]["y"], line["start"]["z"])end = (line["end"]["x"], line["end"]["y"], line["end"]["z"])msp.add_line(start, end)# 保存到临时文件with tempfile.NamedTemporaryFile(suffix=".dxf", delete=False) as temp_file:doc.saveas(temp_file.name)temp_file_path = temp_file.name# 创建保存目录save_dir = './run/dxf'os.makedirs(save_dir, exist_ok=True)# 另存为指定路径save_path = os.path.join(save_dir, 'output.dxf')doc.saveas(save_path)# 读取文件内容with open(temp_file_path, "r") as file:dxf_content = file.read()# 删除临时文件os.remove(temp_file_path)return dxf_content
打不开的:
打得开的
浪费了一下午发现是freecad打不开中文命名的文档
不是卡中文可能@tarikjabiri/dxf包就能用了
2025-05-08 16-59-41 linenode导出dxf