diff --git a/SourceCode/Code2026/CoreLibrary/AecDbX3dPart.cpp b/SourceCode/Code2026/CoreLibrary/AecDbX3dPart.cpp index 30ab4ce..3c33923 100644 --- a/SourceCode/Code2026/CoreLibrary/AecDbX3dPart.cpp +++ b/SourceCode/Code2026/CoreLibrary/AecDbX3dPart.cpp @@ -948,6 +948,33 @@ const AecDbObjXDataMap* AecDbX3dPart::GetSpecialtyData() const return &m_mapXData; } +#ifdef AEC_HAS_MODELER_FILER +AcDb3dSolid* AecDbX3dPart::GetDwgCacheSolid() const +{ + // m_bNeedCreate = 参数变了/还没建;m_bCreated = 建过一次(可能按设计为空) + if (m_bNeedCreate || !m_bCreated) + return NULL; + return m_pSolid; +} + +void AecDbX3dPart::SetDwgCacheSolid(AcDb3dSolid* pSolid) +{ + if (pSolid == NULL) + return; + + if (m_pSolid != NULL) + DELETE_PTR(m_pSolid); + m_pSolid = pSolid; + + // 缓存就是当前几何:别再重建,也别再"没建过" + m_bNeedCreate = false; + m_bCreated = true; + + // 绘制副本按 m_pSolid 克隆出来,缓存换了要让它重新生成 + if (m_pSolidDraw != NULL) + DELETE_PTR(m_pSolidDraw); +} +#endif bool AecDbX3dPart::Create() { //显式调用一律重建:外部可能直接改过 x3dsolid(如对 GetX3dSolid(0) diff --git a/SourceCode/Code2026/CoreLibrary/AecDbX3dPartEntity.cpp b/SourceCode/Code2026/CoreLibrary/AecDbX3dPartEntity.cpp index ccac833..d98d173 100644 --- a/SourceCode/Code2026/CoreLibrary/AecDbX3dPartEntity.cpp +++ b/SourceCode/Code2026/CoreLibrary/AecDbX3dPartEntity.cpp @@ -12,6 +12,10 @@ #include "AcGiExplodeDraw.h" #include "AecDbSlab.h" #include "AecDbXDrawPointer.h" +#include "SectionPerfCounter.h" +#ifdef AEC_HAS_MODELER_FILER +#include "dbacis.h" +#endif extern CString IDCreate(); extern double PixelToWcs(int nPixel); @@ -45,6 +49,183 @@ static const Adesk::UInt16 kColorByLayer = 256; Adesk::UInt32 AecDbX3dPartEntity::kCurrentVersionNumber = 2; +// =========================================================================== +// 3D 几何缓存随存盘(modeler filer 通道) +// +// 为什么挂在 AecDbX3dPartEntity 这一层: +// - 3D 几何(AecDbX3dPart::m_pSolid)是布尔运算的结果,重建一次 15 ms 量级; +// 而把它当 ASM body 存取只要不到 1 ms(实测见 3d-cache-dwg-verdict.md §7)。 +// - AecDbX3dPart 是嵌在实体流**中间**的子流(它后面还有 m_drawable2d), +// 往里加块会让老版本插件把块的字节当成它后面的字段读(管道那次就这样把 XWall 读崩了)。 +// 而 AecDbX3dPartEntity 的 dwgOutFields 末尾就是本实体流的结尾,老版本读到自己 +// 需要的字段就停,尾部多出来的字节会被忽略(AutoCAD 流按对象定长)。 +// - 不升 kCurrentVersionNumber:老版本读新图 = 读不到块 = 照旧重建。 +// +// 语义: +// - 存:part 的几何有效(没被置脏、且建过)才写;没有就写 count=0,等于没存; +// - 读:读不到 magic(老图/开关关掉)或任何一步失败 → 静默回退重建。 +// =========================================================================== +namespace +{ +#ifdef AEC_HAS_MODELER_FILER + + // 开关:AEC_3D_DWG_CACHE=0 时既不再写块、也不读块(A/B 对照用) + bool IsX3dDwgCacheEnabled() + { + static int s_nOn = -1; + if (s_nOn < 0) + { + const char* psz = ::getenv("AEC_3D_DWG_CACHE"); + s_nOn = (psz != NULL && psz[0] != '0') ? 1 : 0; // default off until the win is proven + } + return s_nOn == 1; + } + + // 试点跟踪(AEC_3D_CACHE_TRACE=1):确认存盘/开图时这个块的调用情况 + void Trace3DCache(const char* pszFmt, ...) + { + static int s_nOn = -1; + if (s_nOn < 0) + { + const char* psz = ::getenv("AEC_3D_CACHE_TRACE"); + s_nOn = (psz != NULL && psz[0] != '0') ? 1 : 0; + } + if (s_nOn != 1) return; + + TCHAR szTmp[MAX_PATH + 1]; + ::memset(szTmp, 0, sizeof(szTmp)); + if (::GetTempPath(MAX_PATH, szTmp) == 0) return; + TCHAR szFile[MAX_PATH + 1]; + ::_stprintf_s(szFile, MAX_PATH, _T("%saec_x3d_cache.log"), szTmp); + FILE* fp = NULL; + if (_tfopen_s(&fp, szFile, _T("a")) != 0 || fp == NULL) return; + va_list args; + va_start(args, pszFmt); + vfprintf(fp, pszFmt, args); + va_end(args); + fputc(10, fp); + fclose(fp); + } + const Adesk::UInt32 kX3dMagic = 0x50443341; // 'A3DP' + const Adesk::UInt32 kX3dMagicEnd = 0x50443341 ^ 0xFFFFFFFF; + const Adesk::UInt16 kX3dBlkVer = 1; + + // 内存 filer(clone / copyFrom / 剪贴板)不参与:它的读取语义与 DWG 流不同, + // 写进去也只会让拷贝出来的实体带着一份用不上的缓存 + bool IsMemoryFiler(AcDbDwgFiler* pFiler) + { + return (pFiler != NULL) && pFiler->isKindOf(CMFCDbDwgFiler::desc()); + } + + void WriteX3dGeomBlock(AcDbDwgFiler* pFiler, const AecDbX3dPart* pPart) + { + if (pFiler == NULL || pPart == NULL) + return; + if (IsMemoryFiler(pFiler) || !IsX3dDwgCacheEnabled()) + return; + + AcDb3dSolid* pSolid = pPart->GetDwgCacheSolid(); + const bool bHasSolid = (pSolid != NULL); + Trace3DCache("write hasSolid=%d", bHasSolid ? 1 : 0); + + // 没有几何也写头(count=0):读端才知道"这次真的没缓存",而不是老数据 + pFiler->writeUInt32(kX3dMagic); + pFiler->writeUInt16(kX3dBlkVer); + pFiler->writeUInt16(0); // flags 预留 + pFiler->writeUInt32(bHasSolid ? 1 : 0); + + if (!bHasSolid) + { + pFiler->writeUInt32(kX3dMagicEnd); + return; + } + + SectionPerf::Scope perfScope("X3dGeom.write"); + + ENTITY* pAsm = (ENTITY*)pSolid->getLockedASMBody(); + if (pAsm == NULL) + { + // 拿不到 body:块已经写了一半,补 0 让读端当"没有缓存" + pFiler->writeUInt32(0); + return; + } + + AcDbDatabase* pDb = pSolid->database(); + if (pDb == NULL) + pDb = acdbHostApplicationServices()->workingDatabase(); + + // modelerVersion = -1:跟随 filer 的 DWG 版本,不硬编码 + Acad::ErrorStatus es = acdbModelerSaveEntityToDwgFiler(pFiler, pAsm, pDb, -1); + pSolid->unlockASMBody(); + + if (es != Acad::eOk) + { + pFiler->writeUInt32(0); // 同上:让读端回退 + return; + } + + pFiler->writeUInt32(kX3dMagicEnd); + } + + // 返回 true = 成功用块里的几何接管了 part + bool ReadX3dGeomBlock(AcDbDwgFiler* pFiler, AecDbX3dPart* pPart) + { + if (pFiler == NULL || pPart == NULL) + return false; + if (IsMemoryFiler(pFiler)) + return false; + + // 老图 / 写端关掉:读到的不是 magic,直接回退(不刷日志) + Adesk::UInt32 nMagic = 0; + if (pFiler->readUInt32(&nMagic) != Acad::eOk || nMagic != kX3dMagic) + { + SectionPerf::Scope missScope("X3dGeom.miss"); + Trace3DCache("read nomagic"); + return false; + } + + Adesk::UInt16 nBlkVer = 0; + Adesk::UInt16 nFlags = 0; + Adesk::UInt32 nCount = 0; + if (pFiler->readUInt16(&nBlkVer) != Acad::eOk || nBlkVer != kX3dBlkVer) + return false; + if (pFiler->readUInt16(&nFlags) != Acad::eOk) + return false; + if (pFiler->readUInt32(&nCount) != Acad::eOk) + return false; + + Trace3DCache("read magic=ok count=%u", (unsigned)nCount); + if (nCount == 0 || nCount > 4) + return false; // 没缓存 / 数据不可信 + + ENTITY* pAsm = NULL; + int nModelerVer = -1; + if (acdbModelerRestoreEntityFromDwgFiler(pFiler, pAsm, &nModelerVer, kReadStrangerEntityDefault) != Acad::eOk) + return false; + + Adesk::UInt32 nEnd = 0; + if (pFiler->readUInt32(&nEnd) != Acad::eOk || nEnd != kX3dMagicEnd) + return false; // 半成品块:回退重建 + + if (pAsm == NULL) + return false; + + AcDb3dSolid* pSolid = new AcDb3dSolid(); + if (pSolid->setASMBody(pAsm) != Acad::eOk) + { + delete pSolid; + return false; + } + + pPart->SetDwgCacheSolid(pSolid); + + SectionPerf::Scope hitScope("X3dGeom.read.ok"); + return true; + } + +#endif // AEC_HAS_MODELER_FILER +} + ACRX_DXF_DEFINE_MEMBERS (AecDbX3dPartEntity, AecDbEntityBase, AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent, AcDbProxyEntity::kNoOperation | AcDbProxyEntity::kDisableProxyWarning, @@ -1824,8 +2005,17 @@ Acad::ErrorStatus AecDbX3dPartEntity::dwgInFields (AcDbDwgFiler* pFiler) //开图时不建布尔结果:dwgInFields 里已置脏,首次绘制或取几何时由 EnsureCreated 重建 m_drawable2d.dwgInFields(pFiler, version); + if (pFiler->filerStatus() != Acad::eOk) + return (pFiler->filerStatus ()); - return (pFiler->filerStatus ()); +#ifdef AEC_HAS_MODELER_FILER + // 末尾探测 3D 几何块;读不到就是老图(或开关关掉),part 保持"待重建" + ReadX3dGeomBlock(pFiler, m_pPart); +#endif + + // 注意:不能用 return pFiler->filerStatus()。探测块时 readUInt32 在老图上会 + // 把状态置成 eEndOfFile,那样整个实体都会被判成读取失败。 + return Acad::eOk; } Acad::ErrorStatus AecDbX3dPartEntity::dwgOutFields(AcDbDwgFiler* pFiler) const @@ -1848,6 +2038,11 @@ Acad::ErrorStatus AecDbX3dPartEntity::dwgOutFields(AcDbDwgFiler* pFiler) const m_pPart->dwgOutFields(pFiler); m_drawable2d.dwgOutFields(pFiler, AecDbX3dPartEntity::kCurrentVersionNumber); +#ifdef AEC_HAS_MODELER_FILER + // 3D 几何块:只能在**本实体流末尾**追加(见文件顶部那段说明) + WriteX3dGeomBlock(pFiler, m_pPart); +#endif + return (pFiler->filerStatus ()); } diff --git a/SourceCode/Code2026/cmakelists.txt b/SourceCode/Code2026/cmakelists.txt index 79a49c4..0aa750a 100644 --- a/SourceCode/Code2026/cmakelists.txt +++ b/SourceCode/Code2026/cmakelists.txt @@ -80,6 +80,16 @@ if(MSVC) add_compile_definitions(ZWCAD) endif() + # ObjectARX modeler filer API (acdbModelerSave/RestoreEntities*Filer) exists from ARX R20 on. + # ZWCAD has it too: ZRX/Z20xx/arxport/inc/dbacis.h maps acdbModeler* to zcdbModeler* + # and ZwDatabase.lib exports them (checked on Z2023/Z2024 headers + Z2026 lib). + # ZRX CAD_SDK_VERSION is a year (2023..2026) whose range overlaps ARX version numbers, + # so gate on the year for ZWCAD and on the ARX version otherwise (AGENTS.md red line). + if ((IS_ZWCAD AND CAD_SDK_VERSION GREATER_EQUAL 2023) OR (NOT IS_ZWCAD AND CAD_SDK_VERSION GREATER_EQUAL 20)) + message(STATUS ">>> modeler filer enabled (3D geometry cache can be saved into the DWG)") + add_compile_definitions(AEC_HAS_MODELER_FILER) + endif() + add_compile_options(/Zc:forScope-) add_compile_options(/wd4819 /wd4996) endif() diff --git a/SourceCode/Code2026/inc/AecDbX3dPart.h b/SourceCode/Code2026/inc/AecDbX3dPart.h index 6f3c771..b51a5dc 100644 --- a/SourceCode/Code2026/inc/AecDbX3dPart.h +++ b/SourceCode/Code2026/inc/AecDbX3dPart.h @@ -72,6 +72,15 @@ public: bool Create(); bool EnsureCreated() const; + +#ifdef AEC_HAS_MODELER_FILER + // ---- 几何缓存随存盘(AecDbX3dPartEntity 的 dwgOut/dwgInFields 调用)---- + // 存盘用:几何有效才返回 solid;脏了/没建过返回 NULL(调用方按"这次不存"处理)。 + // 返回 NULL 也可能表示"建过但本来就空"(Hide 构件),两者对存盘都是一个意思。 + AcDb3dSolid* GetDwgCacheSolid() const; + // 恢复用:接管从 DWG 反序列化出来的 solid(不重新做布尔)。 + void SetDwgCacheSolid(AcDb3dSolid* pSolid); +#endif AcDb3dSolid* CreateDraw3dSolid() const; int GetForHouseSubtract(AcDbVoidPtrArray& arSolidPtrs) const;