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(); }