Files
envi-code/SourceCode/Code2026/cad_mcp_aec/aec_mcp_common.cpp
T
company-desktop cabdd0601e feat: 开洞支持任意 AecDbBDPipeBase 母体(异形构筑物/柱/梁/堰)
原来 add_aec_opening 只认板/墙。其实洞口挂载用的是 AecDbBDPipeBase 上的通用接口
(AppendAssocPart + 绘制时 BoolSubtractAssoc),所以异形构筑物等实体一样能开洞。

- 母体判断放宽到 AecDbBDPipeBase;AppendAssocPart 统一走 pBase
- 板/墙保持原来的缺省(Z / 墙厚方向 / 板厚 / 墙厚)
- 其它母体没有"厚度"概念:必须给 direction,position 缺省用包围盒中心,
  洞深缺省 = 母体沿法向的尺寸(打穿整个实体)
- 开完调母体 ReCreateDraw3d(),与造型工具一致

两个实测撞出来的坑:
1. AecDb* 实体的 3D 缓存没建时,getGeomExtents() 照样返回 eOk,但给的是"未设置"的
   ±1e20。直接算洞深会得到 2e20,洞心飞到 1e20。现在 entityBBox() 校验这种值
   (>1e19 或 min>max 判失败),并新增 HostSizeAlong():bbox 不可用时当场向实体要
   一份 3D solid 来量,量完删掉。
2. 穿管开洞不能挪半深:其它洞的 position 是"厚度方向的起点(面/底面)",工具会自己
   +depth/2 挪到厚度中点;但 PL_YXCG 命令是拿管线与实体交点中点当洞心,position 已经
   是洞心。所以 source 存在时跳过偏移,否则洞会贴在面上。

实测(空图,R230 Debug):异形构筑物 + 1000x1000x1000 立方体(x,y∈[-500,500], z∈[0,1000])
- 穿管开洞:管道 DN200 沿 X 穿过,source 定位 -> 洞心 (0,0,500)、外径 0.20、洞深自动 1000
- 顶面圆洞:position=[0,0,1000]、direction=[0,0,-1]、D300/深1200 -> 洞心 (0,0,400)
- 侧面异形洞:position=[0,-500,500]、direction=[0,1,0]、400x400 多边形 -> 洞心 (0,100,500)
三个洞口图层都是 HOLE,都挂在构筑物的关联件表里(Draw3d 时自动扣掉)。
2026-10-30 22:05:22 +08:00

296 lines
7.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "StdAfx.h"
#include "KTCad.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
namespace aec_mcp
{
// ===========================================================================
// 结果封装
// ===========================================================================
nlohmann::json okJson(const nlohmann::json& body)
{
// 统一信封:{"ok":true, ...业务字段}
nlohmann::json o = nlohmann::json::object();
o["ok"] = true;
if (body.is_object())
{
for (nlohmann::json::const_iterator it = body.begin(); it != body.end(); ++it)
o[it.key()] = it.value();
}
else if (!body.is_null())
{
o["data"] = body;
}
std::string s = o.dump();
return mcp_Tool_Utility::make_text_result(mcp_Tool_Utility::utf8ToWString(s));
}
nlohmann::json errJson(int code, const std::wstring& msg)
{
return mcp_Tool_Utility::make_error(code, msg);
}
// ===========================================================================
// 参数读取
// ===========================================================================
bool hasKey(const nlohmann::json& a, const char* key)
{
return a.is_object() && a.contains(key) && !a[key].is_null();
}
double numOr(const nlohmann::json& a, const char* key, double defVal)
{
if (!hasKey(a, key)) return defVal;
const nlohmann::json& v = a[key];
if (v.is_number()) return v.get<double>();
if (v.is_boolean()) return v.get<bool>() ? 1.0 : 0.0;
if (v.is_string())
{
try { return std::stod(v.get<std::string>()); }
catch (...) { return defVal; }
}
return defVal;
}
int intOr(const nlohmann::json& a, const char* key, int defVal)
{
return (int)numOr(a, key, (double)defVal);
}
std::wstring wstrOr(const nlohmann::json& a, const char* key, const wchar_t* defVal)
{
if (!hasKey(a, key) || !a[key].is_string()) return std::wstring(defVal ? defVal : L"");
return mcp_Tool_Utility::utf8ToWString(a[key].get<std::string>());
}
bool pointOf(const nlohmann::json& v, AcGePoint3d& pt)
{
if (v.is_object())
{
if (!v.contains("x") || !v.contains("y")) return false;
pt.x = v.value("x", 0.0);
pt.y = v.value("y", 0.0);
pt.z = v.value("z", 0.0);
return true;
}
if (v.is_array() && v.size() >= 2)
{
pt.x = v[0].get<double>();
pt.y = v[1].get<double>();
pt.z = (v.size() >= 3) ? v[2].get<double>() : 0.0;
return true;
}
return false;
}
bool readPoint(const nlohmann::json& a, const char* key, AcGePoint3d& pt)
{
if (!hasKey(a, key)) return false;
return pointOf(a[key], pt);
}
bool readPoint2dArray(const nlohmann::json& a, const char* key, AcGePoint2dArray& pts)
{
if (!hasKey(a, key) || !a[key].is_array()) return false;
const nlohmann::json& arr = a[key];
for (size_t i = 0; i < arr.size(); ++i)
{
const nlohmann::json& e = arr[i];
if (e.is_object() && e.contains("x") && e.contains("y"))
pts.append(AcGePoint2d(e.value("x", 0.0), e.value("y", 0.0)));
else if (e.is_array() && e.size() >= 2)
pts.append(AcGePoint2d(e[0].get<double>(), e[1].get<double>()));
else
return false;
}
return !pts.isEmpty();
}
// ===========================================================================
// 图纸 / 对象
// ===========================================================================
AcDbDatabase* workingDb()
{
return acdbHostApplicationServices()->workingDatabase();
}
DocLock::DocLock()
: m_bLocked(false), m_pDoc(NULL)
{
m_pDoc = acDocManager->curDocument();
if (m_pDoc != NULL && acDocManager->lockDocument(m_pDoc) == Acad::eOk)
m_bLocked = true;
}
DocLock::~DocLock()
{
if (m_bLocked && m_pDoc != NULL)
acDocManager->unlockDocument(m_pDoc);
}
AcDbEntity* openByHandle(const std::string& handleStr, AcDb::OpenMode mode, AcDbObjectId* pIdOut)
{
if (handleStr.empty()) return NULL;
AcDbDatabase* pDb = workingDb();
if (pDb == NULL) return NULL;
std::wstring ws = utf8ToWstr(handleStr);
AcDbHandle h(ws.c_str());
AcDbObjectId id;
if (pDb->getAcDbObjectId(id, false, h) != Acad::eOk || id.isNull())
return NULL;
AcDbEntity* pEnt = NULL;
if (acdbOpenObject(pEnt, id, mode) != Acad::eOk)
return NULL;
if (pIdOut != NULL)
*pIdOut = id;
return pEnt;
}
std::string handleOf(AcDbObject* pObj)
{
if (pObj == NULL) return std::string();
AcDbHandle h;
pObj->getAcDbHandle(h);
ACHAR szBuf[64] = { 0 };
h.getIntoAsciiBuffer(szBuf, 64);
return acharToUtf8(szBuf);
}
Acad::ErrorStatus addToSpace(AcDbEntity* pEnt, AcDbObjectId* pIdOut, const std::wstring& space)
{
AcDbDatabase* pDb = workingDb();
if (pDb == NULL || pEnt == NULL)
return Acad::eInvalidInput;
AcDbBlockTable* pBT = NULL;
if (pDb->getBlockTable(pBT, AcDb::kForRead) != Acad::eOk || pBT == NULL)
return Acad::eInvalidInput;
AcDbObjectId idSpace;
Acad::ErrorStatus es = Acad::eKeyNotFound;
if (space.empty() || space == L"Model" || space == L"model")
{
es = pBT->getAt(ACDB_MODEL_SPACE, idSpace);
}
else
{
es = pBT->getAt(space.c_str(), idSpace);
}
pBT->close();
if (es != Acad::eOk)
return es;
AcDbBlockTableRecord* pRec = NULL;
es = acdbOpenObject(pRec, idSpace, AcDb::kForWrite);
if (es != Acad::eOk || pRec == NULL)
return es;
AcDbObjectId idNew;
es = pRec->appendAcDbEntity(idNew, pEnt);
pRec->close();
if (es != Acad::eOk)
return es;
if (pIdOut != NULL)
*pIdOut = idNew;
// appendAcDbEntity 不接管关闭;调用方仍然负责 close
return Acad::eOk;
}
AcDbObjectId layerIdByName(const std::wstring& name)
{
AcDbObjectId id;
if (name.empty()) return id;
AcDbDatabase* pDb = workingDb();
if (pDb == NULL) return id;
AcDbLayerTable* pTable = NULL;
if (pDb->getLayerTable(pTable, AcDb::kForRead) != Acad::eOk || pTable == NULL)
return id;
pTable->getAt(name.c_str(), id);
pTable->close();
return id;
}
// ===========================================================================
// 其它
// ===========================================================================
std::wstring utf8ToWstr(const std::string& s)
{
return mcp_Tool_Utility::utf8ToWString(s);
}
std::string acharToUtf8(const ACHAR* s)
{
if (s == NULL) return std::string();
#ifdef _UNICODE
return mcp_Tool_Utility::WStringToUtf8(std::wstring(s));
#else
return std::string(s);
#endif
}
std::string classNameOf(AcDbObject* pObj)
{
if (pObj == NULL || pObj->isA() == NULL) return std::string();
std::string n = acharToUtf8(pObj->isA()->name());
if (n.rfind("AecDb", 0) == 0) n = n.substr(5);
return n;
}
bool entityBBox(AcDbEntity* pEnt, AcGePoint3d& ptMin, AcGePoint3d& ptMax)
{
if (pEnt == NULL) return false;
try
{
AcDbExtents ext;
if (pEnt->getGeomExtents(ext) != Acad::eOk) return false;
// AecDb* 实体的 3D 缓存还没建时,getGeomExtents 照样返回 eOk,
// 但给的是"未设置"值 ±1e20 —— 直接当有效值用会把洞深算成 2e20。
const AcGePoint3d mn = ext.minPoint();
const AcGePoint3d mx = ext.maxPoint();
for (int i = 0; i < 3; ++i)
{
const double a = (i == 0) ? mn.x : (i == 1) ? mn.y : mn.z;
const double b = (i == 0) ? mx.x : (i == 1) ? mx.y : mx.z;
if (fabs(a) > 1e19 || fabs(b) > 1e19) return false;
if (a > b) return false;
}
ptMin = mn;
ptMax = mx;
return true;
}
catch (...)
{
return false;
}
}
void touchEntity(AcDbEntity* pEnt)
{
if (pEnt == NULL) return;
// 几何改了要让显示缓存失效(AecDb* 的 2D/3D 缓存由 assertWriteEnabled 清),
// 再通知 CAD 重画这一段
pEnt->recordGraphicsModified(Adesk::kTrue);
}
} // namespace aec_mcp