Files
hm aad63f9241
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat(brep): B-Rep 解析质量度量(对标SW) + demo 诊断/平滑着色 + vde_format v2
- measure: analytic_measure 单趟自适应 Gauss 积分,在裁剪 NURBS 上求
  体积/面积(对标 SolidWorks MassProperty),消除网格弦高 3-8% 偏差;
  修复球极点 u 伪周期折叠致自适应细分不收敛(257s->1.6s)
- vde_step_probe: STEP 导入度量探针,支持 --analytic-only/--mesh-only
- ui_io_demo/web: 导入诊断面板、错误面红色高亮、平滑填充视图、
  交互 LOD(60k/250k/800k 三角形)与分块描边修复 Chrome 渲染失败
- vde_format: 格式 v2 + vde_compress 压缩;io_gltf/brep_to_mesh 同步
- sw_probe: SolidWorks COM 对照工具(质量属性数值列)
2026-09-07 22:52:06 +08:00

174 lines
7.4 KiB
C#

// SwCal — 校准台:验证 STEP 导入后 SW 侧各度量(面/边/Check 错误面/Diagnose 间隙/体积面积)
// 用法: SwCal.exe <step-file>
// 对同一文件依次用 3D Interconnect ON / OFF 打开并度量,定位 IFace2.Check 语义与单位问题。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
class SwCal
{
[STAThread]
static int Main(string[] args)
{
if (args.Length < 1) { Console.Error.WriteLine("usage: SwCal <step-file>"); return 1; }
string path = args[0];
// 扫描 interop 程序集里 Import 相关类型,找真正的 import-data 接口名
ScanImportTypes();
ISldWorks sw = null;
try
{
Type t = Type.GetTypeFromProgID("SldWorks.Application");
sw = (ISldWorks)Activator.CreateInstance(t);
}
catch (Exception e) { Console.Error.WriteLine("[cal] create fail: " + e.Message); return 2; }
sw.Visible = true;
WaitReady(sw);
foreach (bool ic in new[] { true, false })
{
try { sw.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swMultiCAD_Enable3DInterconnect, ic); }
catch (Exception e) { Console.Error.WriteLine("[cal] toggle threw: " + e.Message); }
Console.Error.WriteLine("[cal] === 3DInterconnect=" + (ic ? "ON" : "OFF") + " ===");
int e2 = 0;
IModelDoc2 doc = null;
try
{
doc = sw.LoadFile4(path, "", null, ref e2);
Console.Error.WriteLine("[cal] LoadFile4 -> doc=" + (doc != null) + " err=" + e2);
if (doc != null) Probe(doc);
}
catch (Exception ex) { Console.Error.WriteLine("[cal] LoadFile4 threw: " + ex.GetType().Name + ": " + ex.Message); }
if (doc != null) { try { sw.CloseDoc(doc.GetTitle()); } catch { } }
}
Console.Error.WriteLine("[cal] done");
try { sw.ExitApp(); } catch { }
return 0;
}
static void Probe(IModelDoc2 doc)
{
Console.Error.WriteLine("[cal] title=" + doc.GetTitle() + " type=" + doc.GetType());
IPartDoc part = doc as IPartDoc;
if (part == null) { Console.Error.WriteLine("[cal] (not a part doc)"); return; }
object[] bodies = null;
try { bodies = (object[])part.GetBodies2((int)swBodyType_e.swAllBodies, true); }
catch (Exception e) { Console.Error.WriteLine("[cal] GetBodies2 threw: " + e.Message); return; }
int nB = bodies == null ? 0 : bodies.Length;
Console.Error.WriteLine("[cal] bodies=" + nB);
if (bodies == null) return;
int faces = 0, edges = 0, gaps = 0, checkBad = 0;
foreach (object o in bodies)
{
IBody2 b = (IBody2)o;
faces += b.GetFaceCount();
edges += b.GetEdgeCount();
Console.Error.WriteLine("[cal] body: faces=" + b.GetFaceCount() + " edges=" + b.GetEdgeCount());
try
{
IDiagnoseResult dr = b.Diagnose();
if (dr != null) { Console.Error.WriteLine("[cal] Diagnose gaps=" + dr.GetGapsCount()); gaps += dr.GetGapsCount(); }
}
catch (Exception e) { Console.Error.WriteLine("[cal] Diagnose threw: " + e.Message); }
try
{
object[] fcs = (object[])b.GetFaces();
if (fcs != null)
foreach (object fo in fcs)
{
IFace2 f = (IFace2)fo;
object chk = null;
try { chk = f.Check; } catch { }
if (chk != null)
{
checkBad++;
if (checkBad <= 3) Console.Error.WriteLine("[cal] face Check -> " + DumpFault(chk));
}
}
Console.Error.WriteLine("[cal] faces with Check!=null: " + checkBad);
}
catch (Exception e) { Console.Error.WriteLine("[cal] face loop threw: " + e.Message); }
}
Console.Error.WriteLine("[cal] TOTAL faces=" + faces + " edges=" + edges + " gaps=" + gaps + " checkBad=" + checkBad);
// 质量属性:system units(真) + 换算;doc units(假)
try
{
object mpo = doc.Extension.CreateMassProperty2();
IMassProperty2 mp = (IMassProperty2)mpo;
mp.UseSystemUnits = true;
Console.Error.WriteLine("[cal] sys vol_m3=" + mp.Volume + " area_m2=" + mp.SurfaceArea
+ " -> mm3=" + (mp.Volume * 1e9) + " mm2=" + (mp.SurfaceArea * 1e6));
}
catch (Exception e) { Console.Error.WriteLine("[cal] mass threw: " + e.Message); }
}
static string DumpFault(object chk)
{
var sb = new System.Text.StringBuilder();
try
{
int cnt = ((IFaultEntity)chk).Count;
sb.Append("faultCount=").Append(cnt);
Type t = chk.GetType();
for (int i = 0; i < cnt && i < 8; ++i)
{
try
{
object ec = t.InvokeMember("ErrorCode", System.Reflection.BindingFlags.GetProperty, null, chk, new object[] { i });
sb.Append(" [").Append(i).Append("]=").Append(ec);
}
catch { }
}
}
catch (Exception e) { sb.Append(" dumpFail:").Append(e.Message); }
return sb.ToString();
}
static void ScanImportTypes()
{
Console.Error.WriteLine("[cal] -- import-related interop types --");
try
{
Type[] ts = typeof(ISldWorks).Assembly.GetTypes();
foreach (Type t in ts)
if ((t.Name.Contains("Import") || t.Name.Contains("Step") || t.Name.Contains("Interconnect")) && !t.IsEnum)
Console.Error.WriteLine("[cal] " + t.FullName);
}
catch (Exception e) { Console.Error.WriteLine("[cal] scan threw: " + e.Message); }
// 诊断接口成员
foreach (string nm in new[] { "IDiagnoseResult", "IFaultEntity", "IFaultEntityCollection",
"IImportStepData", "IImport3DInterconnectData", "IImportFileData" })
{
Type it = typeof(ISldWorks).Assembly.GetType("SolidWorks.Interop.sldworks." + nm);
Console.Error.WriteLine("[cal] == " + nm + " ==" + (it == null ? " (absent)" : ""));
if (it == null) continue;
foreach (var m in it.GetMethods())
if (!m.IsSpecialName)
{
var ps = new System.Collections.Generic.List<string>();
foreach (var p in m.GetParameters()) ps.Add(p.ParameterType.Name + " " + p.Name);
Console.Error.WriteLine("[cal] " + m.ReturnType.Name + " " + m.Name + "(" + string.Join(", ", ps) + ")");
}
foreach (var p in it.GetProperties())
Console.Error.WriteLine("[cal] prop " + p.PropertyType.Name + " " + p.Name);
}
}
static void WaitReady(ISldWorks sw)
{
int t0 = System.Environment.TickCount;
for (int i = 0; i < 120; ++i)
{
Thread.Sleep(500);
try { string rev = sw.RevisionNumber(); Console.Error.WriteLine("[cal] ready @" + (System.Environment.TickCount - t0) + "ms rev=" + rev); return; }
catch { }
}
}
}