From 3daad7a33d6556ccc7095973c6b4991e3f5d764a Mon Sep 17 00:00:00 2001 From: company-desktop-computer Date: Fri, 30 Oct 2026 13:02:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=20AecDbMultiPartEntity=20?= =?UTF-8?q?=E7=9A=84=203D=20=E7=BC=93=E5=AD=98=E5=9D=97=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=E7=BC=BA=E9=99=B7=EF=BC=8C=E5=B9=B6=E5=8A=A0=E7=BB=98=E5=88=B6?= =?UTF-8?q?=E8=AE=A1=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 #131 的基础上把 dwgIn/dwgOut 里的 3D 缓存块做成"坏了也能自愈"的版本。 用 造型.dwg(1 个该类实体)实测过:v4 + magic 的读路径能正确恢复 (1 条 ASM、setASMBody 成功、包围盒与原几何一致)。 修的三处: 1. **count 与实际写入数不一致(会写坏文件)**:原来先 writeInt32(ar.length()), 循环里再跳过非 brep 和取不到 ASM 的实体 → 记的数比实际 payload 多。 读端按 count 读就会多吃后面的字节、把实体流读坏(实测 造型.dwg 里已经出现 ZOMBIEENTITY,用户打开时也提示"实体损坏、需要修复")。 现在先一次性锁住并收集要写的 body,count 一定等于实际写出的条数。 2. **块没有自描述**:加上 magic 'A3DP' + 块版本 + 尾标记。读不到 magic 就是老数据 (v3 或别人写的 v4),直接跳过、照旧重建;块不完整(缺尾/条数不符)也整体丢弃重建。 3. **dwgInFields 末尾返回 filerStatus()**:探测 magic 失败会把状态置成 eEndOfFile, 整个实体就会被判成读取失败。现在显式 return eOk。 另外:`AecDbMultiPartEntity::Draw3d` 加了 AEC_PERF_COUNTER 计时 (total / .build / .cached),用来量"这类实体开图画 3D 到底花多少"。 实测(R230 Debug): - 造型.dwg:该类实体恢复成功,但开图期 Draw3d 计数 = 0(模型空间里只有 1 个 ZOMBIE + 7 个标准实体,这个多部件实体不在模型空间)→ 时间和缓存无关; - big1.dwg(86 s 开图):60 个多部件实体,Draw3d 计数同样是 0 → 开图期没画, 所以"存 ASM / 普通存盘"两种方式的开图时间差 ≈ 0。 要测出差异,需要一张"多部件实体可见、开图就会画"的图。 --- .../CoreLibrary/AecDbMultiPartEntity.cpp | 131 +++++++++++++----- 1 file changed, 98 insertions(+), 33 deletions(-) diff --git a/SourceCode/Code2026/CoreLibrary/AecDbMultiPartEntity.cpp b/SourceCode/Code2026/CoreLibrary/AecDbMultiPartEntity.cpp index ed19449..588d74d 100644 --- a/SourceCode/Code2026/CoreLibrary/AecDbMultiPartEntity.cpp +++ b/SourceCode/Code2026/CoreLibrary/AecDbMultiPartEntity.cpp @@ -2,6 +2,7 @@ #include "aecdbmultipartentity.h" #include "simpleproperty.h" #include "coretools.h" +#include "SectionPerfCounter.h" #include "dbacis.h" IMP_XDATA_KEY(AecDbMultiPartEntity,_ptOrg_); @@ -12,9 +13,21 @@ IMP_XDATA_KEY(AecDbMultiPartEntity,_vtZ_); extern void DLLIMPEXP OrthoProjected3DSolid(AcDb3dSolid* pSolidClone,const AcGePlane& plan,AcGeMatrix3d& xformPlanToWorld,AcDbVoidPtrArray& arEntPtrs); extern void DLLIMPEXP OrthoProjected3DSubMesh(AcDbSubDMesh* pSubMesh,const AcGePlane& plan,AcGeMatrix3d& xformPlanToWorld,AcDbVoidPtrArray& arEntPtrs); -//Adesk::UInt32 AecDbMultiPartEntity::kCurrentVersionNumber = 3; +// 版本号保持 4(与已有图纸一致)。缓存块用 magic 自描述: +// - 老图纸(v3)/ 别人写的 v4(无 magic):读不到 magic 就当没有缓存,照旧重建; +// - 本版本写的 v4(有 magic):直接用块里的 ASM 几何。 +// 代价:v3 的老版本插件打开 v4 图仍会走 eMakeMeProxy(这是升版本号本身带来的)。 Adesk::UInt32 AecDbMultiPartEntity::kCurrentVersionNumber = 4; +#ifdef AEC_HAS_MODELER_FILER +namespace +{ + const Adesk::UInt32 kMp3dMagic = 0x50443341; // 'A3DP' + const Adesk::UInt32 kMp3dEnd = 0x50443341 ^ 0xFFFFFFFF; + const Adesk::UInt16 kMp3dBlkVer = 1; +} +#endif + ACRX_DXF_DEFINE_MEMBERS (AecDbMultiPartEntity, AecDbBDPipeBase, AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent, AcDbProxyEntity::kNoOperation | AcDbProxyEntity::kDisableProxyWarning, @@ -42,6 +55,13 @@ Acad::ErrorStatus AecDbMultiPartEntity::Draw2d(AcGiDraw &dc) const Acad::ErrorStatus AecDbMultiPartEntity::Draw3d(AcGiDraw &dc) const { assertReadEnabled(); + + //AEC_PERF_COUNTER=1 的 Debug 下计时:这类实体开图期画 3D 一共花多少(缓存收益的天花板) + SectionPerf::Scope perfScope("MultiPartEntity::Draw3d"); + const bool bHadCache = Has3DBuffer(); + SectionPerf::Scope cacheScope(bHadCache ? "MultiPartEntity::Draw3d.cached" + : "MultiPartEntity::Draw3d.build"); + AecDbBDPipeBase::Draw3d(dc); AcGePoint3d ptOrg = GetOrg(); @@ -332,31 +352,55 @@ Acad::ErrorStatus AecDbMultiPartEntity::dwgInFields (AcDbDwgFiler* pFiler) xMap.dwgInFields(pFiler); } - if (version > 3) - { - Adesk::Int32 len(0); - pFiler->readInt32(&len); - int nModelerVer = -1; - for (int i(0); i < len; ++i) +#ifdef AEC_HAS_MODELER_FILER + // ---- 3D 几何缓存块(modeler filer)---- + // 位置:本实体流的**末尾**(这个类在 version 之后不写任何自己的字段, + // 所以老版本读到自己需要的字段就停,尾部多出来的字节会被忽略)。 + // 为什么不用升版本号:升了会让老版本 `version > kCurrentVersionNumber` + // 直接 eMakeMeProxy —— 整批实体变代理对象(实测已出现过 ZOMBIEENTITY)。 + // 所以这里用 magic 自描述:读不到 magic 就当没有缓存,照旧重建。 + { + Adesk::UInt32 nMagic = 0; + if (pFiler->readUInt32(&nMagic) == Acad::eOk && nMagic == kMp3dMagic) { - ENTITY* pAsm = NULL; - acdbModelerRestoreEntityFromDwgFiler(pFiler, pAsm, &nModelerVer, kReadStrangerEntityDefault); - if (pAsm) + Adesk::UInt16 nBlkVer = 0; + Adesk::Int32 nCount = 0; + if (pFiler->readUInt16(&nBlkVer) == Acad::eOk && nBlkVer == kMp3dBlkVer && + pFiler->readInt32(&nCount) == Acad::eOk && nCount > 0 && nCount <= 64) { - AcDb3dSolid *pSolid(new AcDb3dSolid); - if (pSolid->setASMBody(pAsm) == Acad::eOk) + int nModelerVer = -1; + int nOk = 0; + for (int i = 0; i < nCount; ++i) { + ENTITY* pAsm = NULL; + if (acdbModelerRestoreEntityFromDwgFiler(pFiler, pAsm, &nModelerVer, + kReadStrangerEntityDefault) != Acad::eOk || pAsm == NULL) + break; + + AcDb3dSolid* pSolid = new AcDb3dSolid(); + if (pSolid->setASMBody(pAsm) != Acad::eOk) + { + delete pSolid; + break; + } Add3DBuffer(pSolid); + ++nOk; + } + + Adesk::UInt32 nEnd = 0; + const bool bEndOk = (pFiler->readUInt32(&nEnd) == Acad::eOk && nEnd == kMp3dEnd); + if (nOk != (int)nCount || !bEndOk) + { + // 半成品块:整条丢弃,回退重建 + Clean3DBuffer(); } - else - DELETE_PTR(pSolid); } } } - - - return pFiler->filerStatus(); +#endif + return Acad::eOk; // 探测缓存块时 readUInt32 在老图上会把状态置成 eEndOfFile, + // 这里显式返回 eOk,否则整个实体会被判成读取失败 } Acad::ErrorStatus AecDbMultiPartEntity::dwgOutFields(AcDbDwgFiler* pFiler) const @@ -372,29 +416,50 @@ Acad::ErrorStatus AecDbMultiPartEntity::dwgOutFields(AcDbDwgFiler* pFiler) const if ( (es =pFiler->writeUInt32 (AecDbMultiPartEntity::kCurrentVersionNumber)) != Acad::eOk ) return (es) ; - const AcDbVoidPtrArray ar = Get3DBuffer(); - pFiler->writeInt32(ar.length()); - - for (int i(0); i < ar.length(); ++i) +#ifdef AEC_HAS_MODELER_FILER + // ---- 3D 几何缓存块:只存 brep(AcDb3dSolid),且**count 按实际写入数**记 ---- + // 不能写 Get3DBuffer().length():缓存里可能有中心线/网格等非 brep, + // 那些会被跳过,count 与实际 payload 数不一致 → 读端多吃数据、实体流损坏。 { - const AcDbEntity *pEnt((AcDbEntity *)ar[i]); - if (!pEnt) continue; + // 先一次性锁住、收集 body,写入期间不放锁 —— 这样 count 一定等于实际写出的条数 + AcArray arSolids; + AcArray arAsm; + const AcDbVoidPtrArray& arBuf = Get3DBuffer(); + for (int i = 0; i < arBuf.length(); ++i) + { + AcDb3dSolid* pSolid = AcDb3dSolid::cast((AcDbEntity*)arBuf[i]); + if (pSolid == NULL) + continue; + ENTITY* pAsm = (ENTITY*)pSolid->getLockedASMBody(); + if (pAsm == NULL) + continue; + arSolids.append(pSolid); + arAsm.append(pAsm); + } - AcDb3dSolid *pSolid = AcDb3dSolid::cast(pEnt); - if (!pSolid) continue; + if (!arSolids.isEmpty()) + { + AcDbDatabase* pDb = GetMe()->database(); + if (pDb == NULL) + pDb = acdbHostApplicationServices()->workingDatabase(); - AcDbDatabase *pDb = pSolid->database(); - if (pDb == NULL) - pDb = acdbHostApplicationServices()->workingDatabase(); + pFiler->writeUInt32(kMp3dMagic); + pFiler->writeUInt16(kMp3dBlkVer); + pFiler->writeInt32((Adesk::Int32)arSolids.length()); - ENTITY* pAsm = (ENTITY*)pSolid->getLockedASMBody(); - if (!pAsm) continue; + for (int i = 0; i < arSolids.length(); ++i) + { + const Acad::ErrorStatus esSave = acdbModelerSaveEntityToDwgFiler(pFiler, arAsm[i], pDb, -1); + arSolids[i]->unlockASMBody(); + if (esSave != Acad::eOk) + break; + } - acdbModelerSaveEntityToDwgFiler(pFiler, pAsm, pDb, -1); - - pSolid->unlockASMBody(); + pFiler->writeUInt32(kMp3dEnd); + } } +#endif return pFiler->filerStatus(); }