明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 254|回复: 0

ObjectArx 对 adsdef.h 中 ads_u_val 以及 resbuf 进行改写重构

  [复制链接]
发表于 2025-6-28 10:25:23 | 显示全部楼层 |阅读模式
本帖最后由 枫叶棋语 于 2025-6-28 10:31 编辑
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Copyright 2023 Autodesk, Inc.  All rights reserved.
  4. //
  5. //  Use of this software is subject to the terms of the Autodesk license
  6. //  agreement provided at the time of installation or download, or which
  7. //  otherwise accompanies this software in either electronic or hard copy form.   
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. #pragma once
  12. #ifndef _adsdef_h
  13. #define _adsdef_h 1

  14. #include "adesk.h"
  15. #include <AcString.h>
  16. #include <gepnt2d.h>
  17. #include <gepnt3d.h>
  18. #include <gevec2d.h>
  19. #include <gevec3d.h>
  20. #define TRUE    1
  21. #define FALSE   0
  22. #ifndef EOS
  23. #define EOS     ACRX_T('\0')
  24. #endif

  25. #pragma pack(push, 8)

  26. typedef double   ads_real;
  27. typedef ads_real ads_point[3];
  28. #if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__clang__) && defined(__LP64__))
  29. typedef int64_t ads_name[2];
  30. typedef int64_t* ads_namep;
  31. #else
  32. typedef int32_t ads_name[2];
  33. typedef int32_t* ads_namep;
  34. #endif
  35. typedef ads_real ads_matrix[4][4];

  36. /* When you want something that's explicitly a pointer type and not an array
  37.    type, use ads_pointp and ads_namep.  Remember that if your function takes an
  38.    *array* of points, it still declares ads_point[] foo; */
  39. typedef ads_real* ads_pointp;

  40. /* To protect macro-redefinition of the X, Y, Z, and T enumerated
  41.    constants by legacy #define's, we remove their definition. */

  42. #ifdef X
  43. #undef X
  44. #endif
  45. #ifdef Y
  46. #undef Y
  47. #endif
  48. #ifdef Z
  49. #undef Z
  50. #endif
  51. #ifdef T
  52. #undef T
  53. #endif

  54. #ifndef _XYZT_DEFINED
  55. #define _XYZT_DEFINED
  56. enum { X = 0, Y = 1, Z = 2 }; /* The coordinates of a point. */
  57. enum { T = 3 };               /* The translation vector of a 3D matrix */
  58. #endif // !_XYZT_DEFINED

  59. /* The PAUSE token for ads_command and ads_cmd
  60. */
  61. #define PAUSE ACRX_T("\")

  62. /* The ADS_INITGET control bits
  63.   */

  64. enum {
  65.   RSG_NONULL = 0x0001,  // Disallow null input
  66.   RSG_NOZERO = 0x0002,  // Disallow zero input
  67.   RSG_NONEG = 0x0004,  // Disallow negative input
  68.   RSG_NOLIM = 0x0008,  // Do not check limits
  69.   RSG_GETZ = 0x0010,  // Get Z coordinate
  70.   RSG_DASH = 0x0020,  // Draw dashed rubber band/box
  71.   // (not a GEDIT control bit)
  72.   RSG_2D = 0x0040,  // Restrict (getdist) to 2D (causes the
  73.   // UD_GETZ control bit to be cleared)
  74.   RSG_OTHER = 0x0080,  // Return input string if unknown
  75.   RSG_DDISTFIRST = 0x0100, // Give DD entry precedence over arbitrary input
  76.   RSG_TRACKUCS = 0x0200, // Allow UCS tracking for faces
  77.   // (causes the UD_TRACKUCS control bit to be set)
  78.   RSG_NOORTHOZ = 0x0400, // Disables polar/ortho tracking in the Z direction
  79.   // (causes the UD_NOORTHOZ control bit to be set)
  80.   RSG_NOOSNAP = 0x0800,  // Disables object snaps
  81.   RSG_NODDIST = 0x1000,  // No direct distance
  82. };

  83. /* The following control bits are the old names for the RSG_
  84.    control bits above.  These names are provided for
  85.    backward compatibility.  You should use the RSG_ names
  86.    above.
  87. */

  88. enum {
  89.   INP_NNULL = RSG_NONULL,
  90.   INP_NZERO = RSG_NOZERO,
  91.   INP_NNEG = RSG_NONEG,
  92.   INP_NLIM = RSG_NOLIM,
  93.   INP_DASH = RSG_DASH,
  94.   INP_NZCOORD = RSG_2D
  95. };

  96. enum SelectorType {
  97.   SELECT_TYPE_FREE = 0,
  98.   SELECT_TYPE_PICK,
  99.   SELECT_TYPE_WPOLY,
  100.   SELECT_TYPE_CPOLY,
  101.   SELECT_TYPE_FENCE,
  102.   SELECT_TYPE_SUBENTITY,
  103.   SELECT_TYPE_BAG,
  104.   SELECT_TYPE_LAST,
  105.   SELECT_TYPE_LEAF,
  106.   SELECT_TYPE_GROUP,
  107.   SELECT_TYPE_PASTE,
  108.   SELECT_TYPE_HATCHASC,
  109.   SELECT_TYPE_NRVP,
  110.   SELECT_TYPE_OCCLASS
  111. };

  112. /* Binary data stream structure
  113. */


  114. struct ads_binary {    // Binary data chunk structure
  115.   int clen;        // length of chunk in bytes (change to int?)
  116.   char* buf;         // binary data
  117. };

  118. union ads_u_val {
  119.   //------------------------------
  120.   // 基础成员 (保持现有结构)
  121.   //------------------------------
  122.   wchar_t* rstring;
  123.   double   rreal;
  124.   double   rpoint[3];
  125.   short    rint;
  126.   int64_t  rlname[2];
  127.   int64_t  mnLongPtr;
  128.   int32_t  rlong;
  129.   int64_t  mnInt64;
  130.   uint64_t mn64data;
  131.   struct ads_binary rbinary;
  132.   uint8_t ihandle[8];

  133.   //------------------------------
  134.   // 赋值方法集合
  135.   //------------------------------

  136.   //字符串赋值
  137.   void setString(const wchar_t* str) {
  138.     rstring = copyString(str);
  139.   }

  140.   //ads_binary赋值
  141.   void setBinary(const ads_binary& bin) {
  142.     rbinary = copyBinary(bin);
  143.   }
  144.   //double arr[3] 赋值,包括坐标,向量
  145.   void setArray(double x, double y, double z) {
  146.     rpoint[0] = x;
  147.     rpoint[1] = y;
  148.     rpoint[2] = z;
  149.   }


  150.   // 句柄赋值,可以先转为64无符号整型
  151.   void setHandle(uint64_t handle) {
  152.     mn64data = handle;
  153.   }

  154.   // 32位整数
  155.   void setInt32(int32_t value) {
  156.     rlong = value;
  157.   }

  158.   // 16位短整数
  159.   void setShort(short value) {
  160.     rint = value;
  161.   }

  162.   // 64位浮点数
  163.   void setDouble(double value) {
  164.     rreal = value;
  165.   }

  166. private:
  167.   //------------------------------
  168.   // 深拷贝工具函数
  169.   //------------------------------
  170.   static wchar_t* copyString(const wchar_t* src) {
  171.     if (!src) return nullptr;
  172.     const size_t len = wcslen(src) + 1;
  173.     wchar_t* dst = new wchar_t[len];
  174.     wcscpy_s(dst, len, src);
  175.     return dst;
  176.   }

  177.   static ads_binary copyBinary(const ads_binary& bin) {
  178.     // 有效性简写检查
  179.     const bool is_valid_input =
  180.       (bin.clen >= 0) && ((bin.buf == nullptr) == (bin.clen == 0)); // buf空时长度必须为0

  181.     if (!is_valid_input) {
  182.       return { 0, nullptr }; // 输入非法时返回无害空数据
  183.     }

  184.     // 无数据可直接返回
  185.     if (bin.clen == 0) {
  186.       return { 0, nullptr };
  187.     }

  188.     // 执行深拷贝
  189.     char* dst = new char[bin.clen];
  190.     memcpy(dst, bin.buf, bin.clen);
  191.     return { bin.clen, dst };
  192.   }


  193. public:
  194.   //------------------------------
  195.   // 释放方法 (根据需求调用)
  196.   //------------------------------
  197.   void releaseString() {
  198.     if (rstring) {
  199.       delete[] rstring;
  200.       rstring = nullptr;
  201.     }
  202.   }

  203.   void releaseBinary() {
  204.     if (rbinary.buf) {
  205.       delete[] rbinary.buf;
  206.       rbinary.buf = nullptr;
  207.       rbinary.clen = 0;
  208.     }
  209.   }
  210. };

  211. struct resbuf {
  212.   struct resbuf* rbnext; // Allows them to be "linked"
  213.   short restype;
  214.   union ads_u_val resval;

  215.   // 设置字符串
  216.   void setValue(const wchar_t* str) {
  217.     resval.setString(str);
  218.   }
  219.   void setValue(AcString str) {
  220.     resval.setString(str);
  221.   }

  222.   // 设置二进制数据(参数匹配 ads_binary 结构)
  223.   void setValue(ads_binary bin) {
  224.     resval.setBinary(bin);
  225.   }
  226.   // 设置三维点(初始化 rpoint 数组)
  227.   void setValue(double x, double y, double z) {
  228.     resval.setArray(x, y, z);
  229.   }
  230.   void setValue(AcGePoint3d pt3d) {
  231.     resval.setArray(pt3d.x, pt3d.y, pt3d.z);
  232.   }
  233.   void setValue(AcGeVector3d vec3d) {
  234.     resval.setArray(vec3d.x, vec3d.y, vec3d.z);
  235.   }
  236.   //设置二维点(z 设置为0)
  237.   void setValue(AcGePoint2d pt2d) {
  238.     resval.setArray(pt2d.x, pt2d.y, 0.0);
  239.   }
  240.   void setValue(AcGeVector2d vec2d) {
  241.     resval.setArray(vec2d.x, vec2d.y, 0);
  242.   }

  243.   // 设置 64 位句柄数值(对应 mn64data)
  244.   void setValue(uint64_t handleValue) {
  245.     resval.setHandle(handleValue);
  246.   }

  247.   // 设置 32 位整数(对应 rlong)
  248.   void setValue(int32_t num) {
  249.     resval.setInt32(num);
  250.   }

  251.   // 设置 16 位短整数(对应 rint)
  252.   void setValue(short num) {
  253.     resval.setShort(num);
  254.   }

  255.   // 设置双精度浮点数(对应 rreal)
  256.   void setValue(double num) {
  257.     resval.setDouble(num);
  258.   }

  259.   void releaseString() {
  260.     resval.releaseString();
  261.   }
  262.   void releaseBinary() {
  263.     resval.releaseBinary();
  264.   }

  265. };

  266. typedef struct resbuf* pResbuf;
  267. typedef const struct resbuf* kpResbuf;  // for declarations

  268. #pragma pack(pop)

  269. #endif


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-7-27 04:20 , Processed in 0.145847 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表