按用户提示看了 PL_DHGD 与 Draw3d:
1. PL_wpipedrawEx -> TDbPipeJig::DoIt -> AecDbEwPipe::SetPipeParam(PipeParam*),
一次填 ~30 个字段(PIPE_EAPP/PIPE_DRAWDIR/KEY_2DSTYLE=2/KEY_SINGLE=0/PIPE_D/
SetSection(D,thick)/PIPE_DN/PIPE_OD/PIPE_TDN/PIPE_TOD/PIPE_RATIO/PIPE_THICK/
PIPE_MATERIAL/PIPE_SYS/PIPE_LEVEL/PIPE_CONNECT/PIPE_INTERFACED*/PIPE_FLANGF/标高等)。
2. 真正决定单线/双线的不是 KEY_2DSTYLE:AecDbEwPipe::BaseDraw2d 里取完 KEY_2DSTYLE
立刻用 PIPE_DN 覆盖:
nStyle = (dDn - GetPipeDoubleLineMaxD() > 1.e-3) ? 2 : 1;
GetPipeDoubleLineMaxD()(AecDbEwPipeBase.cpp:215)有 EW_EARTHWORMINIT 时缺省 500 换米,
否则直接用 KT_PIPEINIT 的 DOC_PIPEMAXDIA 原值(不换单位)。
3. 所以 PIPE_DN 与 DOC_PIPEMAXDIA 必须同单位。我们之前按米写 PIPE_DN(AS_MUNIT(50)=0.05),
而 DOC_PIPEMAXDIA 是 10,0.05-10<0 -> 永远单线。
改法:专业数据里 PIPE_DN/PIPE_OD/PIPE_THICK 一律按"图纸单位"存。
aec_mcp_common 新增 mmToDwg/dwgToMm,用产品自己的 CoreTools::IsMetersUnit() 判断
(米图纸 ×0.001,毫米图纸原值);查询/阀门/法兰读回来统一过 dwgToMm。
实测(毫米图纸):add_aec_pipe DN50 -> dn_mm 50 / od_mm 50 / kt_max 10 / style_2d 2,
50-10>0.001 -> BaseDraw2d 走 DrawDouble2d。
get_aec_entity_detail 的 pipe 参数留了 kt_max / ew_max 两个调试字段。
482 lines
13 KiB
C++
482 lines
13 KiB
C++
#include "StdAfx.h"
|
||
#include "KTCad.h"
|
||
#include "KTModelLayoutXData.h"
|
||
#include "CoreTools.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
|
||
|
||
// pl_cssz 的缺省参数(照 PL_CSSZ 抄),没有 KT_PIPEINIT 时直接写进图纸。
|
||
// 注意 CoreTools::hasCSSZ 还额外要求 DOC_PIDPROJECT(等级/项目),pl_cssz 里没写这个字段,这里补上。
|
||
namespace aec_mcp {
|
||
bool ensurePipeInit(std::wstring& err)
|
||
{
|
||
// 已有 KT_PIPEINIT 也要顺手修一遍单位(老版本写进去的 DOC_PIPEMAXDIA 是 10.0,单位没换)
|
||
{
|
||
AecDbObjXDataMap xOld;
|
||
if (KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xOld))
|
||
{
|
||
bool bChanged = false;
|
||
if (CoreTools::IsMetersUnit())
|
||
{
|
||
LPCTSTR pszKeys[] = { DOC_PIPEMINDIA, DOC_PIPEMAXDIA, DOC_PERELBOWDIA };
|
||
for (int i = 0; i < 3; ++i)
|
||
{
|
||
double d = 0.0;
|
||
if (xOld.GetAt(pszKeys[i], d) && d > 1.0) { xOld.SetAt(pszKeys[i], d * 0.001); bChanged = true; }
|
||
}
|
||
}
|
||
if (bChanged) KTModelLayoutXData::WriteModelXData(KT_PIPEINIT, xOld);
|
||
}
|
||
}
|
||
|
||
if (CoreTools::hasCSSZ(false)) return true;
|
||
|
||
AecDbObjXDataMap x;
|
||
x.SetAt(DOC_TRADE, WPipe::eCivial);
|
||
x.SetAt(DOC_SCALE, _T("1:100"));
|
||
x.SetAt(DOC_PLOTWIDTH, 0.5);
|
||
x.SetAt(DOC_DPLOTWIDTH, 0.0);
|
||
x.SetAt(DOC_WELDDIA, 0.0);
|
||
x.SetAt(DOC_PIPEMINDIA, 1.0);
|
||
x.SetAt(DOC_PIPEMAXDIA, 10.0);
|
||
x.SetAt(DOC_PERELBOWDIA, 100.0);
|
||
x.SetAt(DOC_BREAKSPACE, 2.0);
|
||
x.SetAt(DOC_UNIT, 0);
|
||
x.SetAt(DOC_DIMTERM, 0);
|
||
x.SetAt(DOC_ELTYPE, 0);
|
||
x.SetAt(DOC_ELDESCR, _T("--"));
|
||
x.SetAt(DOC_DIMTXTH, 2.0);
|
||
x.SetAt(DOC_DIMTXTF, 0.7);
|
||
x.SetAt(DOC_EQPTXTH, 2.0);
|
||
x.SetAt(DOC_EQPTXTF, 1.0);
|
||
x.SetAt(DOC_STRTXTH, 2.0);
|
||
x.SetAt(DOC_STRTXTF, 0.7);
|
||
x.SetAt(DOC_SCRIPTT, _T("{0}{-}{10}{-}{2}"));
|
||
x.SetAt(DOC_SCRIPTB, _T("{4}{-}{5}"));
|
||
x.SetAt(DOC_PIDMPIPE1W, 1.0);
|
||
x.SetAt(DOC_PIDSPIPE1W, 0.25);
|
||
x.SetAt(DOC_PIDINSRDN, 12);
|
||
x.SetAt(DOC_CONSIGN, 30.0);
|
||
x.SetAt(DOC_EQUSCALE, 6.0);
|
||
x.SetAt(DOC_LINEARSCALE, 10.0);
|
||
x.SetAt(DOC_VALVESCALE, 100.0);
|
||
x.SetAt(DOC_NODESCALE, 6.0);
|
||
x.SetAt(DOC_PIDBARROW, 6.0);
|
||
x.SetAt(DOC_PIDMARROW, 3.0);
|
||
x.SetAt(DOC_PIDSARROW, 1.5);
|
||
x.SetAt(DOC_PIDEWITHDIM, TRUE);
|
||
x.SetAt(DOC_PIDVWITHDIM, TRUE);
|
||
x.SetAt(DOC_PIDBRKSTYLE, 1);
|
||
x.SetAt(PID_PIPE_DPRESS, 1.0);
|
||
x.SetAt(DOC_PIDPROJECT, _T("工业")); // 等级/项目:hasCSSZ 会检查这个字段
|
||
|
||
// 图纸单位是米时,这三个"直径"要按毫米→米换算(PL_CSSZ 写库前会走 ModelXData2CurUnit)。
|
||
// 不换算的话 DOC_PIPEMAXDIA 存成 10.0(米!),isDoubleLine 永远为假 → 管道全按单线画。
|
||
// 用 >1.0 兜底做幂等(单位是米时 DN 不会超过 1 米),这样已有的错图纸也能自动修回来。
|
||
if (CoreTools::IsMetersUnit())
|
||
{
|
||
LPCTSTR pszDiaKeys[] = { DOC_PIPEMINDIA, DOC_PIPEMAXDIA, DOC_PERELBOWDIA };
|
||
for (int i = 0; i < 3; ++i)
|
||
{
|
||
double d = 0.0;
|
||
if (x.GetAt(pszDiaKeys[i], d) && d > 1.0) x.SetAt(pszDiaKeys[i], d * 0.001);
|
||
}
|
||
}
|
||
|
||
if (!KTModelLayoutXData::WriteModelXData(KT_PIPEINIT, x))
|
||
{
|
||
err = L"写入管道参数设置(KT_PIPEINIT)失败";
|
||
return false;
|
||
}
|
||
|
||
// pl_cssz 还会按出图比例设 ltscale
|
||
struct resbuf rb;
|
||
if (acedGetVar(_T("ltscale"), &rb) == RTNORM)
|
||
{
|
||
rb.restype = RTREAL;
|
||
rb.resval.rreal = 100.0; // 缺省比例 1:100
|
||
acedSetVar(_T("ltscale"), &rb);
|
||
}
|
||
|
||
if (!CoreTools::hasCSSZ(false))
|
||
{
|
||
err = L"参数设置写进去了但 hasCSSZ 仍然为假";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
double mmToDwg(double dMm)
|
||
{
|
||
return CoreTools::IsMetersUnit() ? dMm * 0.001 : dMm;
|
||
}
|
||
|
||
double dwgToMm(double dDwg)
|
||
{
|
||
return CoreTools::IsMetersUnit() ? dDwg * 1000.0 : dDwg;
|
||
}
|
||
|
||
} // namespace aec_mcp
|