' ============================================================================= ' NX Journal: 参数化母模 + 参数表 → 批量导出 GLB 轻量化网格 (供网页 3D 预览) ' ' 背景: 网页 3D 走"轻量网格"路线 (参照 CADENAS 服务器端轻量化思路, 但用 NX ' 离线预转换, 零服务器成本): 网页优先加载 GLB (快/稳/手机流畅), ' STEP 照旧提供下载。见 项目总览.md 待办 9。 ' ' 使用方法: ' 1) 与 nx-batch-export.vb 相同: 母模用表达式驱动, 表达式名与 CSV 列名一致 ' 2) 关键一步 (NX 2506): 菜单 工具→日记→录制 → 手动执行 ' 文件→导出→Extended Reality (导出类型选 GLB, 单个二进制文件) ' → 停止录制 → 把录出的代码替换下方 ExportGlb 函数体 ' (XR 导出的 API 与 DexBuilder 不同, 录出的代码就是你这台 NX 的真实接口, ' 这是 Siemens 官方推荐的获取方式; 不同小版本录出的代码可能不同, ' 所以模板不硬写该 API) ' 3) 工具→日记→播放 选本文件运行 ' ' 输出: 每个变体一个 .glb 文件, 放到网页站点的 mesh/ 目录, ' app.js 优先用 three.js GLTFLoader 加载, 失败回退 STEP 查看器/Canvas。 ' ' 注意: 中文注释需文件保存为 UTF-8 带 BOM (fix-bom.ps1 已处理)。 ' ============================================================================= Option Strict Off Imports System Imports System.IO Imports NXOpen Imports NXOpen.UF Module BatchGlbExport Dim theSession As Session = Session.GetSession() Dim workPart As Part = theSession.Parts.Work Dim lw As ListingWindow = theSession.ListingWindow Sub Main() ' ============================ CONFIG 区 ============================ Dim csvPath As String = "D:\catalog\params.csv" ' 参数表 (与 STEP 导出同一张表) Dim outDir As String = "D:\catalog\glb_out\" ' GLB 输出目录 ' 表达式名 → CSV 列名 映射 (母模表达式名 : 参数表列名) Dim exprMap As New System.Collections.Generic.Dictionary(Of String, String)() exprMap.Add("bore", "bore") exprMap.Add("stroke", "stroke") ' ================================================================ lw.Open() lw.WriteLine("GLB 批量导出开始: " & csvPath) Directory.CreateDirectory(outDir) Dim lines() As String = File.ReadAllLines(csvPath) If lines.Length < 2 Then lw.WriteLine("参数表为空!") Return End If Dim header() As String = lines(0).Split(","c) Dim colStep As Integer = Array.IndexOf(header, "step_file") If colStep < 0 Then lw.WriteLine("参数表缺少 step_file 列 (用于推导 GLB 文件名)!") Return End If ' 预解析列号 Dim colOf As New System.Collections.Generic.Dictionary(Of String, Integer)() For Each kv As KeyValuePair(Of String, String) In exprMap colOf(kv.Key) = Array.IndexOf(header, kv.Value) If colOf(kv.Key) < 0 Then lw.WriteLine("参数表缺少列: " & kv.Value) Return End If Next Dim okCount As Integer = 0 Dim failCount As Integer = 0 For i As Integer = 1 To lines.Length - 1 If lines(i).Trim().Length = 0 Then Continue For Dim c() As String = lines(i).Split(","c) Dim model As String = c(0).Trim() ' GLB 文件名与 STEP 同名不同后缀 (网页端按型号拼 glb 路径) Dim glbFile As String = Path.ChangeExtension(c(colStep).Trim(), ".glb") lw.Write("[" & i & "/" & (lines.Length - 1) & "] " & model & " ... ") Try ' ---- 1) 设置表达式 ---- For Each kv As KeyValuePair(Of String, String) In exprMap workPart.Expressions.Edit(kv.Key, c(colOf(kv.Key)).Trim()) Next ' ---- 2) 更新模型 ---- Dim update As NXOpen.Update = theSession.CreateUpdate() Dim nErrs As Integer = 0 update.InterpartDelay = False update.ModelingInterpartUpdate = True update.DoUpdate(nErrs) update.Dispose() If nErrs > 0 Then lw.WriteLine("警告: 更新有 " & nErrs & " 个错误") End If ' ---- 3) 导出 GLB (用录制日记替换此函数体) ---- ExportGlb(Path.Combine(outDir, glbFile)) okCount += 1 lw.WriteLine("OK") Catch ex As Exception failCount += 1 lw.WriteLine("FAIL: " & ex.Message) End Try Next lw.WriteLine("") lw.WriteLine("完成: " & okCount & " 成功, " & failCount & " 失败, 输出目录: " & outDir) End Sub ' GLB 导出: 用【工具→日记→录制】录一次 文件→导出→Extended Reality 后, ' 把录出的代码粘进这里, 并把其中写死的输出路径改成参数 fileName。 Sub ExportGlb(fileName As String) lw.WriteLine("ExportGlb 未实现: 请按文件头说明录制导出操作并替换本函数") Throw New Exception("ExportGlb 未实现") End Sub End Module