- 积分
- 2191
- 明经币
- 个
- 注册时间
- 2022-4-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- #pragma once
- #include <acdb.h>
- #include <acedads.h>
- #include <AcString.h>
- #include <acutads.h>
- #include <adsdef.h>
- #include <cstdint>
- #include <dbhandle.h>
- #include <dbObject.h>
- #include <gepnt2d.h>
- #include <gepnt3d.h>
- #include <gevec2d.h>
- #include <gevec3d.h>
- class Xdata {
- public:
- ~Xdata() {
- clear();
- }
- Xdata(const AcString& appName) : appName(appName) {
- acdbRegApp(appName); // 注册应用名
- pRb = createRegAppNode(appName);
- pTail = pRb;
- }
- void write(AcDbObject* pObj) {
- if (pRb == nullptr || pRb->restype != Code::RegAppCode) {
- auto head = createRegAppNode(appName);
- head->rbnext = pRb;
- pRb = head;
- }
- pObj->setXData(pRb);
- }
- void read(AcDbObject* pObj) {
- clear();
- resbuf* xdata = pObj->xData(appName);
- if (!xdata) {
- this->pRb = nullptr;
- return;
- };
- copyResbufChain(xdata, &pRb, &pTail);
- }
- void print() {
- if (pRb == nullptr) {
- acutPrintf(L"\nNo Xdata for AppName [%s].\n", appName);
- }
- resbuf* next = pRb;
- //int flag = 0;
- while (true) {
- if (next == nullptr)
- return;
- printNode(next);
- next = next->rbnext;
- }
- }
- //------------------------------
- // 数据添加方法
- //------------------------------
- void addLayerName(const AcString& name) {
- ads_u_val val;
- val.setString(name);
- addNode(Code::LayerCode, val);
- }
- void addString(const AcString& str) {
- ads_u_val val;
- val.setString(str);
- addNode(Code::WstrCode, val);
- }
- void addInt16(short num) {
- ads_u_val val;
- val.setShort(num);
- addNode(Code::Int16Code, val);
- }
- void addInt32(int32_t num) {
- ads_u_val val;
- val.setInt32(num);
- addNode(Code::Int32Code, val);
- }
- void addDouble(double num) {
- ads_u_val val;
- val.setDouble(num);
- addNode(Code::DoubleCode, val);
- }
- void addDist(double dist) {
- ads_u_val val;
- val.setDouble(dist);
- addNode(Code::DistCode, val);
- }
- void addScale(double scale) {
- ads_u_val val;
- val.setDouble(scale);
- addNode(Code::ScaleCode, val);
- }
- void addArray(double arr[3]) {
- ads_u_val val;
- val.setArray(arr[0], arr[1], arr[2]);
- addNode(Code::ArrayCode, val);
- }
- void addPoint2d(const AcGePoint2d& pt) {
- ads_u_val val;
- val.setArray(pt.x, pt.y, 0);
- addNode(Code::PointCode, val);
- }
- void addPoint3d(const AcGePoint3d& pt) {
- ads_u_val val;
- val.setArray(pt.x, pt.y, pt.z);
- addNode(Code::PointCode, val);
- }
- void addDisp2d(const AcGeVector2d& vec) {
- ads_u_val val;
- val.setArray(vec.x, vec.y, 0);
- addNode(Code::DispCode, val);
- }
- void addDisp3d(const AcGeVector3d& vec) {
- ads_u_val val;
- val.setArray(vec.x, vec.y, vec.z);
- addNode(Code::DispCode, val);
- }
- void addDirect2d(const AcGeVector2d& vec) {
- ads_u_val val;
- val.setArray(vec.x, vec.y, 0);
- addNode(Code::DirCode, val);
- }
- void addDirect3d(const AcGeVector3d& vec) {
- ads_u_val val;
- val.setArray(vec.x, vec.y, vec.z);
- addNode(Code::DirCode, val);
- }
- void addHandle(const AcDbHandle& handle) {
- ads_u_val val;
- val.mn64data = handle.get64BitVal(); // 核心改动:使用原始字节写入
- addNode(Code::HandleCode, val);
- }
- void clear() {
- resbuf* curr = pRb;
- while (curr) {
- resbuf* next = curr->rbnext;
- switch (curr->restype) { // 去掉了rbinary处理
- case Code::RegAppCode:
- case Code::WstrCode:
- case Code::LayerCode:
- curr->releaseString();
- break;
- }
- delete curr;
- curr = next;
- }
- pRb = pTail = nullptr;
- }
- resbuf* createRegAppNode(const AcString& name) {
- resbuf* node = new resbuf();
- node->restype = AcDb::kDxfRegAppName;
- node->setValue(name);
- node->rbnext = nullptr;
- return node;
- }
- protected:
- //------------------------------
- // 内存管理核心方法
- //------------------------------
- resbuf* pRb = nullptr;
- resbuf* pTail = nullptr;
- AcString appName;
- void copyResbufChain(const resbuf* src, resbuf** dstHead, resbuf** dstTail) {
- resbuf* prev = nullptr;
- for (const resbuf* curr = src; curr; curr = curr->rbnext) {
- resbuf* node = new resbuf();
- node->restype = curr->restype;
- switch (curr->restype) { // 移除了rbinary处理
- case Code::RegAppCode:
- case Code::LayerCode:
- case Code::WstrCode:
- node->setValue(curr->resval.rstring);
- break;
- default:
- node->resval = curr->resval;
- }
- node->rbnext = nullptr;
- if (prev) prev->rbnext = node;
- else *dstHead = node;
- prev = node;
- }
- *dstTail = prev;
- }
- //------------------------------
- // 工具方法
- //------------------------------
- //添加节点
- void addNode(short restype, const ads_u_val& val) {
- resbuf* node = new resbuf();
- node->restype = restype;
- switch (restype) {
- case Code::RegAppCode:
- case Code::LayerCode:
- case Code::WstrCode:
- node->setValue(val.rstring);
- break;
- default:
- node->resval = val;
- }
- if (pTail) {
- pTail->rbnext = node;
- pTail = node;
- }
- else {
- pRb = pTail = node;
- }
- }
- //打印坐标
- void printPoint(const double* point) {
- acutPrintf(L"(%.2f, %.2f, %.2f)\n", point[0], point[1], point[2]);
- }
- // 打印节点
- void printNode(resbuf* pNode) {
- if (pNode == nullptr) {
- acutPrintf(L"空节点");
- return;
- }
- switch (pNode->restype) {
- case Code::RegAppCode:
- acutPrintf(L"[AppName] %s\n", pNode->resval.rstring);
- break;
- case Code::LayerCode:
- acutPrintf(L"[LayerName] %s\n", pNode->resval.rstring);
- break;
- case Code::WstrCode:
- acutPrintf(L"[AcString] %s\n", pNode->resval.rstring);
- break;
- case Code::Int16Code:
- acutPrintf(L"[int16] %d\n", pNode->resval.rint);
- break;
- case Code::Int32Code:
- acutPrintf(L"[int32] %ld\n", pNode->resval.rlong);
- break;
- case Code::DoubleCode:
- acutPrintf(L"[double] %.3f\n", pNode->resval.rreal);
- break;
- case Code::ArrayCode:
- case Code::PointCode:
- case Code::DispCode:
- case Code::DirCode:
- acutPrintf(L"[int16] %d\n:", pNode->restype);
- printPoint(pNode->resval.rpoint);
- break;
- case Code::HandleCode:
- // 直接输出64位无符号整型
- acutPrintf(L"[句柄] %I64u\n", pNode->resval.mn64data); // 修改此处格式说明符
- break;
- case Code::DistCode:
- acutPrintf(L"[距离] %.3f\n", pNode->resval.rreal);
- break;
- case Code::ScaleCode:
- acutPrintf(L"[比例] %.3f\n", pNode->resval.rreal);
- break;
- default:
- acutPrintf(L"[未知类型:%d] \n", pNode->restype);
- break;
- }
- }
- enum Code :short
- {
- RegAppCode = 1001, // Xdata 的 application name(组码1001)
- WstrCode = 1000, // ASCII 字符串的最大长度为 255 个字节(组码 1000)
- LayerCode = 1003, // 图层名(组码 1003)
- ArrayCode = 1010, // 3 个实数(组码 1010)
- PointCode = 1011, // 三维世界空间位置(组码 1011)
- DispCode = 1012, // 三维世界空间位移(组码 1012)
- DirCode = 1013, // 三维世界空间方向(组码 1013)
- DoubleCode = 1040, // 实数(组码 1040)
- DistCode = 1041, // 距离(组码 1041)
- ScaleCode = 1042, // 比例因子(组码 1042)
- Int16Code = 1070, // 一个 16 位整数(组码 1070)
- Int32Code = 1071, // 一个 32 位有符号长整数(组码 1071)
- HandleCode = 1005 //一个句柄(组码1005)
- };
- };
复制代码
|
|