// ============================================================================ // aec_mcp_window_tools.cpp // cad_mcp_aec 的门窗工具(工作计划第 6 项) // * add/update_aec_window —— 普通门窗 AecDbXOpening(命令 PL_QTCC 等"插入门窗") // // 命令的建模序列(Pool3dCmd/pool3dcmdCmd.cpp 的 InsertWindow): // 1. 在墙基线上取定位点 ptOn(用户点一下,投影到墙中心线) // 2. OpeningData:类型(1 窗/2 门)、宽、高、底标高、块名(2D/3D)、名称、编号、规格、厂家 // 3. pOpen->SetOpeningData(oData, idWall) + 图层 LAY_WIN // 4. pOpen->GetStandardDraw2d(win2d, false) —— 2D 表示,用来扣墙 // 5. 入库;然后 wall->AppendAssocPart(idOpen) + wall->SubtractDraw2d(win2d) // // 与作者确认过:普通门窗对话框参数不多,所以 add 就把参数给全(不套"新建简化"那条)。 // 组合门窗(PL_ZHMC) 等其它门窗功能本轮挂起。 // ============================================================================ #include "StdAfx.h" #include "KTCad.h" #include "aec_mcp_common.h" #include "mcp_plugin_api.h" #include "AecDbOpening.h" #include "AecDbWall.h" #include "AecDbObjXDataMap.h" #include "KTSetting.h" using namespace aec_mcp; namespace { std::string HandleOfId(const AcDbObjectId& id) { if (id.isNull()) return std::string(); AcDbObject* pObj = NULL; if (Acad::eOk != acdbOpenObject(pObj, id, AcDb::kForRead)) return std::string(); const std::string s = handleOf(pObj); pObj->close(); return s; } void CopyStr(TCHAR* pszDst, int nSize, const std::wstring& sSrc) { if (pszDst == NULL || nSize <= 0) return; _tcsncpy(pszDst, sSrc.c_str(), nSize - 1); pszDst[nSize - 1] = 0; } // 块名要在当前图纸里存在(命令里是对话框把门窗模板 DWG 导成块,这里不做导入) bool BlockExists(const std::wstring& sName) { if (sName.empty()) return false; AcDbDatabase* pDb = workingDb(); if (pDb == NULL) return false; AcDbBlockTable* pBT = NULL; if (pDb->getBlockTable(pBT, AcDb::kForRead) != Acad::eOk || pBT == NULL) return false; AcDbBlockTableRecord* pRec = NULL; const bool bHas = (pBT->getAt(sName.c_str(), pRec, AcDb::kForRead) == Acad::eOk && pRec != NULL); if (pRec != NULL) pRec->close(); pBT->close(); return bHas; } // OpeningData -> json(回给调用方 / update 时对比) nlohmann::json OpeningToJson(const OpeningData& d) { nlohmann::json o; o["type"] = (d.nType == 2) ? "door" : "window"; o["width"] = d.dWidth; o["height"] = d.dHeight; o["thickness"] = d.dThick; o["bottom_elevation"] = d.dBomEl; o["block"] = acharToUtf8(d.szBlockName2d); o["block_3d"] = acharToUtf8(d.szBlockName3d); o["name"] = acharToUtf8(d.szName); o["no"] = acharToUtf8(d.szNo); o["material"] = acharToUtf8(d.szSpec); o["factory"] = acharToUtf8(d.szFactory); o["position"] = { d.ptPos.x, d.ptPos.y, d.ptPos.z }; return o; } // 打开墙并按基线投影定位点 AecDbXWall* OpenWallForWindow(const nlohmann::json& a, AcDbEntity*& pOut, AcGePoint3d& ptOn, WallData& wd, std::wstring& err) { pOut = NULL; if (!hasKey(a, "wall")) { err = L"缺少 wall(墙的 handle)"; return NULL; } const std::wstring sWall = wstrOr(a, "wall", L""); pOut = openByHandle(acharToUtf8(sWall.c_str()), AcDb::kForWrite); if (pOut == NULL) { err = L"按 handle 打不开墙:" + sWall; return NULL; } AecDbXWall* pWall = AecDbXWall::cast(pOut); if (pWall == NULL) { const std::string sCls = classNameOf(pOut); err = L"handle " + sWall + L" 是 " + utf8ToWstr(sCls) + L",不是墙(AecDbXWall)"; pOut->close(); pOut = NULL; return NULL; } ::memset(&wd, 0, sizeof(WallData)); pWall->GetWallData(wd); return pWall; } } // --------------------------------------------------------------------------- // add_aec_window —— 插入普通门窗(AecDbXOpening) // --------------------------------------------------------------------------- class ToolAddWindow : public mcp_Tool { public: std::string private_name() const override { return "add_aec_window"; } std::string private_description() const override { return "在墙上插入一个普通门窗(菜单里的\"插入门窗\"系列,实体 AecDbXOpening):" "给墙 handle + 定位点(会自动投影到墙中心线)+ 门窗块名(块要已在当前图纸里)+ 宽/高/窗台高," "内部建 AecDbXOpening、扣墙的 2D 洞口、并挂到墙的关联件里。图层 LAY_WIN。\n" "门窗块是命名的图块(2D 用 `block`,三维显示用 `block_3d`,缺省同 block)。" "组合门窗(PL_ZHMC) 等其它门窗功能暂未提供。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "wall": { "type": "string", "description": "墙(AecDbXWall)的 handle" }, "position": { "type": "array", "items": { "type": "number" }, "description": "定位点 [x,y](会投影到墙中心线)" }, "block": { "type": "string", "description": "门窗的 2D 图块名(必须在当前图纸里已存在)" }, "block_3d": { "type": "string", "description": "三维显示的图块名,缺省同 block" }, "type": { "type": "string", "enum": [ "window", "door" ], "description": "window 窗(缺省)/ door 门" }, "width": { "type": "number", "description": "洞口宽(沿墙方向)" }, "height": { "type": "number", "description": "洞口高" }, "sill_height": { "type": "number", "description": "窗台高(相对墙底标高,缺省 0)" }, "thickness": { "type": "number", "description": "洞口厚,缺省取墙厚" }, "no": { "type": "string", "description": "编号(如 C1518),缺省用块名" }, "name": { "type": "string", "description": "名称,缺省用块名" }, "material": { "type": "string", "description": "规格/材料说明" }, "factory": { "type": "string", "description": "厂家" }, "layer": { "type": "string", "description": "图层,缺省 LAY_WIN" }, "color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" } }, "required": [ "wall", "position", "block", "width", "height" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); AcGePoint3d ptHit(0.0, 0.0, 0.0); if (!readPoint(args, "position", ptHit)) return errJson(kErrBadParam, L"需要 position([x,y] 定位点)"); const std::wstring sBlock = wstrOr(args, "block", L""); if (sBlock.empty()) return errJson(kErrBadParam, L"需要 block(门窗 2D 图块名)"); if (!BlockExists(sBlock)) return errJson(kErrNotFound, L"图纸里没有这个图块:" + sBlock + L"(命令是对话框把门窗模板 DWG 导成块,MCP 这边要求块已在图纸里)"); std::wstring err; AcDbEntity* pOut = NULL; AcGePoint3d ptOn(0.0, 0.0, 0.0); WallData wd; AecDbXWall* pWall = OpenWallForWindow(args, pOut, ptOn, wd, err); if (pWall == NULL) return errJson(kErrWrongType, err); // 定位点投影到墙中心线(命令里也是 getClosestPointTo) pWall->getClosestPointTo(ptHit, ptOn); OpeningData data; ::memset(&data, 0, sizeof(OpeningData)); data.nType = (wstrOr(args, "type", L"window") == L"door") ? 2 : 1; data.dWidth = numOr(args, "width", 0.0); data.dHeight = numOr(args, "height", 0.0); if (data.dWidth <= 0.0 || data.dHeight <= 0.0) { pOut->close(); return errJson(kErrBadParam, L"width / height 必须 > 0"); } data.dThick = numOr(args, "thickness", wd.dThick); data.dBomEl = wd.dBomEl + numOr(args, "sill_height", 0.0); // 存的是绝对标高 data.ptPos = ptOn; const std::wstring sBlk3d = hasKey(args, "block_3d") ? wstrOr(args, "block_3d", L"") : sBlock; const std::wstring sName = hasKey(args, "name") ? wstrOr(args, "name", L"") : sBlock; const std::wstring sNo = hasKey(args, "no") ? wstrOr(args, "no", L"") : sBlock; CopyStr(data.szBlockName2d, MAX_PATH, sBlock); CopyStr(data.szBlockName3d, MAX_PATH, sBlk3d); CopyStr(data.szName, MAX_PATH, sName); CopyStr(data.szNo, 65, sNo); if (hasKey(args, "material")) CopyStr(data.szSpec, MAX_PATH, wstrOr(args, "material", L"")); if (hasKey(args, "factory")) CopyStr(data.szFactory, MAX_PATH, wstrOr(args, "factory", L"")); AecDbXOpening* pOpen = new AecDbXOpening(); pOpen->SetOpeningData(data, pOut->objectId()); std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L""; if (sLay.empty()) sLay = L"LAY_WIN"; AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str()); if (!idLay.isNull()) pOpen->setLayer(idLay); if (hasKey(args, "color")) pOpen->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); AecComplexCurve win2d; pOpen->GetStandardDraw2d(win2d, false); AcDbObjectId idNew; Acad::ErrorStatus es = addToSpace(pOpen, &idNew); if (es != Acad::eOk) { delete pOpen; pOut->close(); return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es)); } pOpen->close(); // 挂到墙上并扣掉 2D 洞口(与命令一致) pWall->AppendAssocPart(idNew); pWall->SubtractDraw2d(win2d); pWall->ReCreateDraw3d(); touchEntity(pWall); pOut->close(); nlohmann::json body; body["ok"] = true; body["type"] = "opening_window"; body["handle"] = HandleOfId(idNew); body["wall"] = args["wall"]; body["params"] = OpeningToJson(data); return okJson(body); } }; EXPORT_MCP_TOOL(ToolAddWindow, AddAecWindowTool) // --------------------------------------------------------------------------- // update_aec_window —— 精细修改(对话框里的字段基本都收) // --------------------------------------------------------------------------- class ToolUpdateWindow : public mcp_Tool { public: std::string private_name() const override { return "update_aec_window"; } std::string private_description() const override { return "修改普通门窗(AecDbXOpening):只传要改的字段。宽/高/窗台高/块(2D、3D)/编号/名称/" "规格/厂家/门窗类型都能改,改完自动刷新墙上的 2D 洞口(UpdateOpeningData 内部先撤旧洞口)。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "handle": { "type": "string", "description": "门窗实体 handle" }, "type": { "type": "string", "enum": [ "window", "door" ] }, "width": { "type": "number" }, "height": { "type": "number" }, "sill_height": { "type": "number", "description": "窗台高(相对墙底)" }, "thickness": { "type": "number" }, "block": { "type": "string", "description": "新的 2D 图块名" }, "block_3d": { "type": "string", "description": "新的 3D 图块名" }, "no": { "type": "string" }, "name": { "type": "string" }, "material": { "type": "string" }, "factory": { "type": "string" }, "color": { "type": "integer" } }, "required": [ "handle" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); if (!hasKey(args, "handle")) return errJson(kErrBadParam, L"缺少 handle"); const std::string sHandle = args["handle"].get(); AcDbEntity* pEnt = openByHandle(sHandle, AcDb::kForWrite); if (pEnt == NULL) return errJson(kErrNotFound, L"按 handle 打不开实体:" + utf8ToWstr(sHandle)); AecDbXOpening* pOpen = AecDbXOpening::cast(pEnt); if (pOpen == NULL) { const std::string sCls = classNameOf(pEnt); pEnt->close(); return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是普通门窗(AecDbXOpening)"); } OpeningData data; ::memset(&data, 0, sizeof(OpeningData)); pOpen->GetOpeningData(data); if (hasKey(args, "type")) data.nType = (wstrOr(args, "type", L"window") == L"door") ? 2 : 1; if (hasKey(args, "width")) data.dWidth = numOr(args, "width", data.dWidth); if (hasKey(args, "height")) data.dHeight = numOr(args, "height", data.dHeight); if (hasKey(args, "thickness")) data.dThick = numOr(args, "thickness", data.dThick); if (hasKey(args, "block")) { const std::wstring sBlk = wstrOr(args, "block", L""); if (!BlockExists(sBlk)) { pEnt->close(); return errJson(kErrNotFound, L"图纸里没有这个图块:" + sBlk); } CopyStr(data.szBlockName2d, MAX_PATH, sBlk); } if (hasKey(args, "block_3d")) CopyStr(data.szBlockName3d, MAX_PATH, wstrOr(args, "block_3d", L"")); if (hasKey(args, "name")) CopyStr(data.szName, MAX_PATH, wstrOr(args, "name", L"")); if (hasKey(args, "no")) CopyStr(data.szNo, 65, wstrOr(args, "no", L"")); if (hasKey(args, "material")) CopyStr(data.szSpec, MAX_PATH, wstrOr(args, "material", L"")); if (hasKey(args, "factory")) CopyStr(data.szFactory, MAX_PATH, wstrOr(args, "factory", L"")); // 窗台高:UpdateOpeningData 会自己把墙底标高加上去,所以这里传"相对墙底"的值。 // 现有 dBomEl 是绝对的,先减掉墙底再作为相对值传进去。 if (hasKey(args, "sill_height")) { double dBomWall = 0.0; // 墙 id 存在专业数据里,GetOpeningData 已经读进 data.idWall(IntDbId) if (data.idWall != 0) { AcDbObjectId idWall = AcDbObjectId::kNull; idWall.setFromOldId((Adesk::ULongPtr)data.idWall); // IntDbId -> ObjectId AcDbEntity* pWallEnt = NULL; if (!idWall.isNull() && Acad::eOk == acdbOpenAcDbEntity(pWallEnt, idWall, AcDb::kForRead) && pWallEnt != NULL) { AecDbXWall* pWall = AecDbXWall::cast(pWallEnt); if (pWall != NULL) { WallData wd; ::memset(&wd, 0, sizeof(WallData)); pWall->GetWallData(wd); dBomWall = wd.dBomEl; } pWallEnt->close(); } } data.dBomEl = dBomWall + numOr(args, "sill_height", 0.0); } pOpen->UpdateOpeningData(data); if (hasKey(args, "color")) pOpen->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); touchEntity(pOpen); nlohmann::json body; body["ok"] = true; body["type"] = "opening_window"; body["handle"] = sHandle; body["params"] = OpeningToJson(data); pEnt->close(); return okJson(body); } }; EXPORT_MCP_TOOL(ToolUpdateWindow, UpdateAecWindowTool)