128 lines
4.1 KiB
C++
128 lines
4.1 KiB
C++
// ============================================================================
|
|||
|
|
// dwg_annotate_edit.cpp
|
||
|
|
// 图纸批注 - 编辑类工具(均为 c++_dll 后端)
|
||
|
|
// delete_entities : 按 handle 列表删除实体
|
||
|
|
// set_entity_properties : 修改已有实体的图层 / 颜色
|
||
|
|
//
|
||
|
|
// 用于审图后清理 AI 自己产生的批注、删除图纸废线,或把问题实体标红/归层。
|
||
|
|
// ============================================================================
|
||
|
|
|
||
|
|
#include "KTCad.h"
|
||
|
|
#include "dwg_annotate_common.h"
|
||
|
|
#include "mcp_plugin_api.h"
|
||
|
|
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
using namespace dwg_annotate;
|
||
|
|
using namespace dwg_review;
|
||
|
|
|
||
|
|
namespace
|
||
|
|
{
|
||
|
|
// 打开指定句柄的实体(kForWrite);失败返回 false
|
||
|
|
bool openForWrite(const std::string& hs, AcDbEntity*& out)
|
||
|
|
{
|
||
|
|
out = nullptr;
|
||
|
|
AcDbObjectId id;
|
||
|
|
if (!objectIdFromHandle(hs, id)) return false;
|
||
|
|
if (acdbOpenObject(out, id, AcDb::kForWrite) != Acad::eOk) { out = nullptr; return false; }
|
||
|
|
return out != nullptr;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===========================================================================
|
||
|
|
// delete_entities
|
||
|
|
// ===========================================================================
|
||
|
|
class mcp_Tool_DeleteEntities : public mcp_Tool
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
nlohmann::json Execute(const nlohmann::json& args) override
|
||
|
|
{
|
||
|
|
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
|
||
|
|
if (!pDb) return makeError(kErrNoDocument, "no working database");
|
||
|
|
|
||
|
|
DocLock lock; // 写库前先锁文档,见 dwg_annotate_common.h
|
||
|
|
if (!lock.ok()) return makeError(kErrInternal, "cannot lock drawing document");
|
||
|
|
|
||
|
|
const std::vector<std::string> hs = collectHandlesArg(args);
|
||
|
|
if (hs.empty()) return makeError(kErrBadParam, "handle or handles is required");
|
||
|
|
|
||
|
|
int count = 0;
|
||
|
|
nlohmann::json failed = nlohmann::json::array();
|
||
|
|
|
||
|
|
for (size_t i = 0; i < hs.size(); ++i)
|
||
|
|
{
|
||
|
|
AcDbEntity* pEnt = nullptr;
|
||
|
|
if (!openForWrite(hs[i], pEnt)) { failed.push_back(hs[i]); continue; }
|
||
|
|
|
||
|
|
const Acad::ErrorStatus es = pEnt->erase();
|
||
|
|
pEnt->close();
|
||
|
|
if (es == Acad::eOk) ++count;
|
||
|
|
else failed.push_back(hs[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
nlohmann::json r;
|
||
|
|
r["ok"] = true;
|
||
|
|
r["count"] = count;
|
||
|
|
r["failed"] = failed;
|
||
|
|
return makeJsonResult(r);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
EXPORT_MCP_TOOL(mcp_Tool_DeleteEntities, CreateDeleteEntitiesTool)
|
||
|
|
|
||
|
|
// ===========================================================================
|
||
|
|
// set_entity_properties
|
||
|
|
// ===========================================================================
|
||
|
|
class mcp_Tool_SetEntityProperties : public mcp_Tool
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
nlohmann::json Execute(const nlohmann::json& args) override
|
||
|
|
{
|
||
|
|
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
|
||
|
|
if (!pDb) return makeError(kErrNoDocument, "no working database");
|
||
|
|
|
||
|
|
DocLock lock; // 写库前先锁文档,见 dwg_annotate_common.h
|
||
|
|
if (!lock.ok()) return makeError(kErrInternal, "cannot lock drawing document");
|
||
|
|
|
||
|
|
const std::vector<std::string> hs = collectHandlesArg(args);
|
||
|
|
if (hs.empty()) return makeError(kErrBadParam, "handle or handles is required");
|
||
|
|
|
||
|
|
const bool hasLayer = args.contains("layer") && args["layer"].is_string();
|
||
|
|
const bool hasColor = args.contains("color") && args["color"].is_number_integer();
|
||
|
|
if (!hasLayer && !hasColor)
|
||
|
|
return makeError(kErrBadParam, "at least one of layer / color is required");
|
||
|
|
|
||
|
|
const std::string layer = hasLayer ? args["layer"].get<std::string>() : std::string();
|
||
|
|
const int color = hasColor ? args["color"].get<int>() : -1;
|
||
|
|
|
||
|
|
if (hasLayer) ensureLayer(pDb, layer, 1);
|
||
|
|
|
||
|
|
int count = 0;
|
||
|
|
nlohmann::json failed = nlohmann::json::array();
|
||
|
|
|
||
|
|
for (size_t i = 0; i < hs.size(); ++i)
|
||
|
|
{
|
||
|
|
AcDbEntity* pEnt = nullptr;
|
||
|
|
if (!openForWrite(hs[i], pEnt)) { failed.push_back(hs[i]); continue; }
|
||
|
|
|
||
|
|
if (hasLayer)
|
||
|
|
{
|
||
|
|
const std::wstring wl = utf8ToWStr(layer);
|
||
|
|
pEnt->setLayer(wl.c_str());
|
||
|
|
}
|
||
|
|
if (hasColor)
|
||
|
|
pEnt->setColorIndex((Adesk::UInt16)color);
|
||
|
|
|
||
|
|
pEnt->draw();
|
||
|
|
pEnt->close();
|
||
|
|
++count;
|
||
|
|
}
|
||
|
|
|
||
|
|
nlohmann::json r;
|
||
|
|
r["ok"] = true;
|
||
|
|
r["count"] = count;
|
||
|
|
r["failed"] = failed;
|
||
|
|
return makeJsonResult(r);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
EXPORT_MCP_TOOL(mcp_Tool_SetEntityProperties, CreateSetEntityPropertiesTool)
|