一、带形窗 AecDbZLineXOpening(新文件 aec_mcp_zline_window_tools.cpp) 命令:GetPathOnWall 取墙路径 -> CDXCHDlg 的 OpeningData(dXData[0..10] 存柱距/首排高/ 框料厚宽/玻璃厚/外伸×2/挑出×2/玻璃内偏/挡板厚)-> SetOpeningData(data, arIds)(数组重载) + SetZLinePoints + KEY_StartDir -> 入库后逐面墙 AppendAssocPart + SubtractDraw2d。 按总原则:add 只给窗高/窗台高/柱距/凸窗,update 收全套框料/玻璃/外伸/挑出/挡板。 二、支吊架(新文件 aec_mcp_holder_tools.cpp,6 个工具) - PL_FZZJ 支架 AecDbHolder、PL_FZDJ 吊架 AecDbHanger、PL_FZZD 支墩 AecDbBossHolder 套路一致:对话框填结构体 -> jig 画路径 -> 点一下给支臂方向 -> 沿路径按 "个数=路径长/间距" 摆一串,每个 SetPosition(ptPos, vtLL) + LAY_SUPPORT。 工具里 points/spacing/direction_point,一次建整串,返回 count + handles。 顺带把"沿墙取路径"从附属构件里提到 aec_mcp_common(buildWallPath), 带形窗与阳台/台阶共用;前向声明放全局,避免和 using namespace aec_mcp 打架。 两个坑: 1. PL_FZDJ 建的是 AecDbHanger,不是 AecDbPipeHanger(后者没被这条命令用到) 2. AcDbCurve::getFirstDeriv 收参数、getPointAtDist 收距离,混用会报"沿路径取点/切线失败"; 改成 getParamAtDist 再取切线 做不了的:PL_ZJCH 转角窗 —— PL_ZJCHCmdFnc 打开对话框后函数体是空的、注册也被注释掉了。 实测(R230 Debug,空图): - 带形窗(墙 12000、沿墙 1000~9000、柱距 1200)-> 311,bbox x 967~9000; 精修框料 80/玻璃 40/首排 900/加挡板 回读正确 - 支架(9000 路径 / 间距 1500)-> 6 个;吊架 -> 4 个;支墩 -> 4 个;三者 update 都回读正确 工具总数 81 -> 89(本插件 53 -> 61),R230/Z2026 都编过。
367 lines
9.4 KiB
C++
367 lines
9.4 KiB
C++
#include "StdAfx.h"
|
||
#include "KTCad.h"
|
||
#include "aec_mcp_common.h"
|
||
#include "AecDbWall.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);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// 沿墙取路径(附属构件、带形窗共用)
|
||
// ---------------------------------------------------------------------------
|
||
AcDbPolyline* buildWallPath(AecDbXWall* pWall, const AcGePoint3d& ptStart, const AcGePoint3d& ptEnd,
|
||
const AcGePoint3d& ptSide, AcGeVector3d& vtDir, std::wstring& err)
|
||
{
|
||
if (pWall == NULL) { err = L"墙为空"; return NULL; }
|
||
AcDbCurve* pCrv = AcDbCurve::cast((AcDbEntity*)pWall);
|
||
if (pCrv == NULL) { err = L"墙不是曲线,取不到中心线"; return NULL; }
|
||
|
||
AcGePoint3d pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
|
||
if (pCrv->getClosestPointTo(ptStart, pt1) != Acad::eOk ||
|
||
pCrv->getClosestPointTo(ptEnd, pt2) != Acad::eOk)
|
||
{
|
||
err = L"start / end 投影到墙中心线失败";
|
||
return NULL;
|
||
}
|
||
|
||
double d1 = 0.0, d2 = 0.0;
|
||
if (pCrv->getDistAtPoint(pt1, d1) != Acad::eOk || pCrv->getDistAtPoint(pt2, d2) != Acad::eOk)
|
||
{
|
||
err = L"取墙中心线上的距离失败";
|
||
return NULL;
|
||
}
|
||
if (fabs(d2 - d1) < 1e-6)
|
||
{
|
||
err = L"start 与 end 在墙上的位置太近";
|
||
return NULL;
|
||
}
|
||
|
||
int nSeg = (int)ceil(fabs(d2 - d1) / 200.0); // 直墙两点就够,弧形按 200 一段
|
||
if (nSeg < 1) nSeg = 1;
|
||
if (nSeg > 200) nSeg = 200;
|
||
|
||
AcDbPolyline* pPl = new AcDbPolyline(nSeg + 1);
|
||
for (int i = 0; i <= nSeg; ++i)
|
||
{
|
||
AcGePoint3d ptOn(0.0, 0.0, 0.0);
|
||
const double d = d1 + (d2 - d1) * (double)i / (double)nSeg;
|
||
if (pCrv->getPointAtDist(d, ptOn) != Acad::eOk)
|
||
{
|
||
delete pPl;
|
||
err = L"沿墙中心线取点失败";
|
||
return NULL;
|
||
}
|
||
pPl->addVertexAt(i, AcGePoint2d(ptOn.x, ptOn.y), 0.0, 0.0, 0.0);
|
||
}
|
||
pPl->setElevation(0.0);
|
||
|
||
AcGePoint3d ptMid(0.0, 0.0, 0.0);
|
||
AcGeVector3d vtTan(1.0, 0.0, 0.0);
|
||
if (pCrv->getPointAtDist(0.5 * (d1 + d2), ptMid) == Acad::eOk &&
|
||
pCrv->getFirstDeriv(ptMid, vtTan) == Acad::eOk)
|
||
{
|
||
vtTan.normalize();
|
||
vtDir = AcGeVector3d::kZAxis.crossProduct(vtTan);
|
||
if (vtDir.length() < 1e-9) vtDir = AcGeVector3d::kYAxis;
|
||
vtDir.normalize();
|
||
if (vtDir.dotProduct(ptSide - ptMid) < 0.0)
|
||
vtDir = -vtDir;
|
||
}
|
||
else
|
||
{
|
||
err = L"取墙中心线切向失败(side_point 定不了墙侧方向)";
|
||
delete pPl;
|
||
return NULL;
|
||
}
|
||
return pPl;
|
||
}
|
||
|
||
} // namespace aec_mcp
|