天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 75|回复: 0

object arx 编程实现 AutoCAD 的查找命令 部分源代码

[复制链接]
  • TA的每日心情
    开心
    2024-8-31 15:58
  • 签到天数: 89 天

    [LV.6]常住居民II

    488

    主题

    207

    回帖

    3366

    积分

    管理员

    积分
    3366
    发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
    object arx 编程实现 AutoCAD 的查找命令 部分源代码
    1. // CArxDlg.cpp: 实现文件
    2. //
    3. #include "StdAfx.h"
    4. #include "afxdialogex.h"
    5. #include "resource.h"
    6. #include <stdexcept>
    7. #include "SelectSet.h"
    8. #include "View.h"
    9. #include "CArxDlg.h"
    10. // CArxDlg 对话框
    11. IMPLEMENT_DYNAMIC(CArxDlg, CAcUiDialog)
    12. CArxDlg::CArxDlg(CWnd* pParent /*=nullptr*/)
    13.         : CAcUiDialog(IDD_ARX_MFC, pParent)
    14.         , mRadioSelect(0)
    15. {
    16. }
    17. CArxDlg::~CArxDlg()
    18. {
    19. }
    20. void CArxDlg::DoDataExchange(CDataExchange* pDX)
    21. {
    22.         CAcUiDialog::DoDataExchange(pDX);
    23.         DDX_Radio(pDX, IDC_RADIO_BlockRef, mRadioSelect);
    24. }
    25. // CArxDlg 消息处理程序
    26. BEGIN_MESSAGE_MAP(CArxDlg, CAcUiDialog)
    27.         ON_BN_CLICKED(IDC_BTN_FIND, &CArxDlg::OnClickedBtnFind)
    28.         ON_LBN_DBLCLK(IDC_LIST_NAME, &CArxDlg::OnLbnDblclkListName)
    29. END_MESSAGE_MAP()
    30. std::vector<AcGePoint3d> selectPoint;
    31. //
    32. //
    33. void CArxDlg::OnClickedBtnFind()
    34. {
    35.         UpdateData(); // 关键
    36.         CListBox* listbox = (CListBox *)GetDlgItem(IDC_LIST_NAME);
    37.         listbox->ResetContent();  // 列表框清空
    38.         selectPoint.clear();          // 清空 查找到的 姓名 的 点 的坐标
    39.         // 获得 编辑框 的输入内容
    40.         CStringW name;
    41.         CEdit* edit = (CEdit *)GetDlgItem(IDC_EDIT_NAME);
    42.         edit->GetWindowTextW(name);
    43.        
    44.         switch (mRadioSelect)
    45.         {
    46.         case 0:
    47.                 FindNameFromBlkRef(name, listbox);
    48.                 break;
    49.         case 1:
    50.                 FindNameFromText(name, listbox);
    51.                 break;
    52.         case 2: // rb = acutBuildList(RTDXF0, _T("MTEXT"), 8, _T("注记"), RTNONE);
    53.                 break;
    54.         default:
    55.                 break;
    56.         }
    57. }
    58. //
    59. //
    60. //
    61. void CArxDlg::FindNameFromBlkRef(CStringW &name, CListBox *listbox)
    62. {
    63.         struct resbuf *rb; CSelectSet ss;
    64.         rb = acutBuildList(RTDXF0, _T("INSERT"), 8, _T("注记"), RTNONE);
    65.         std::vector<AcDbBlockReference*> blkrefs = ss.GetAllEntitys<AcDbBlockReference>(rb);
    66.         ss.GetAttribute(blkrefs);
    67.         std::vector<AcGePoint3d>::vector::iterator itp = namePoints.points.begin();
    68.         for (std::vector<std::wstring>::iterator itn = namePoints.names.begin(); itn != namePoints.names.end(); itn++, itp++)
    69.         {
    70.                 if (!itn->compare(name.GetString()))
    71.                 {
    72.                         listbox->AddString(name.GetString());
    73.                         selectPoint.push_back(*itp);
    74.                 }
    75.         }
    76. }
    77. //
    78. //
    79. //
    80. void CArxDlg::FindNameFromText(CStringW &name, CListBox *listbox) {
    81.         struct resbuf *rb; CSelectSet ss;
    82.         rb = acutBuildList(RTDXF0, _T("TEXT"), 8, _T("汉字"), RTNONE);
    83.         std::vector<AcDbText*> texts = ss.GetAllEntitys<AcDbText>(rb);
    84.         for (std::vector<AcDbText*>::iterator it = texts.begin(); it != texts.end(); it++)
    85.         {
    86.                 AcString text;
    87.                 (*it)->textString(text);
    88.                 // acutPrintf(_T("%s"), text.substr(5, -1).kACharPtr());
    89.                 if (!text.substr(4, -1).compare(name)) {  //  截取字符串 从第五个字符开始
    90.                         listbox->AddString(name.GetString());
    91.                         selectPoint.push_back((*it)->position());
    92.                         acutPrintf(_T("\n%f,%f"), (*it)->position().x, (*it)->position().y);
    93.                 }
    94.                 (*it)->close();  //  释放 对象
    95.         }
    96. }
    97. void CArxDlg::OnLbnDblclkListName()
    98. {
    99.         UpdateData();
    100.         CListBox* listbox = (CListBox *)GetDlgItem(IDC_LIST_NAME);
    101.         int index = listbox->GetCurSel();
    102.         AcGePoint3d point = selectPoint.at(index);
    103.         switch (mRadioSelect)
    104.         {
    105.         case 0:CMyView::setViewOnPointBlockRef(point);
    106.                 break;
    107.         case 1:CMyView::setViewOnPointText(point);
    108.                 break;
    109.         case 2:
    110.         default:
    111.                 break;
    112.         }
    113.        
    114. }
    复制代码

    1. #pragma once
    2. #include <gepnt2d.h>
    3. #include <gepnt3d.h>
    4. #include <dbsymtb.h>
    5. #include <aced.h>
    6. class CMyView
    7. {
    8. public:
    9.         CMyView();
    10.         ~CMyView();
    11.         static void setViewOnPointBlockRef(const AcGePoint3d& point);
    12.         static void setViewOnPointText(const AcGePoint3d &point);
    13.         static AcGePoint3d WcsToDcs(const AcGePoint3d& point);
    14. private:
    15. };
    16. CMyView::CMyView()
    17. {
    18. }
    19. CMyView::~CMyView()
    20. {
    21. }
    22. //
    23. // 根据 查找到内容的 坐标点 缩放视图 并且 查找内容属于 AcDbBlockReference
    24. //
    25. void CMyView::setViewOnPointBlockRef(const AcGePoint3d & point)
    26. {
    27.         // acutPrintf(_T("\nsetView %f,%f"), CMyView::WcsToDcs(point).x, CMyView::WcsToDcs(point).y);
    28.         // acutPrintf(_T("\n 2d %f,%f"), point.convert2d(AcGePlane()).x, point.convert2d(AcGePlane()).y);
    29.         AcGePoint2d pnt2D = CMyView::WcsToDcs(point).convert2d(AcGePlane());
    30.         AcDbViewTableRecord view;
    31.         view.setCenterPoint(AcGePoint2d(pnt2D.x, pnt2D.y-15));
    32.         view.setHeight(35);
    33.         view.setWidth(35);
    34.         acedSetCurrentView(&view, NULL);
    35.         view.close();
    36. }
    37. //
    38. //  根据 查找到内容的 坐标点 缩放视图 并且 查找内容属于 AcDbText
    39. //
    40. void CMyView::setViewOnPointText(const AcGePoint3d &point) {
    41.         AcDbViewTableRecord view;
    42.         // AcGePoint2d pnt2d = point.convert2d(AcGePlane());
    43.         view.setCenterPoint(AcGePoint2d(point.x, point.y - 15));
    44.         view.setHeight(35);
    45.         view.setWidth(35);
    46.         acedSetCurrentView(&view, NULL);
    47.         view.close();
    48. }
    49. //
    50. //  世界坐标系 转 用户坐标系
    51. //
    52. AcGePoint3d CMyView::WcsToDcs(const AcGePoint3d & point)
    53. {
    54.         AcGePoint3d pnt;
    55.         struct resbuf rbFrom, rbTo;
    56.         rbFrom.restype = RTSHORT;
    57.         rbFrom.resval.rint = 0;
    58.         rbTo.restype = RTSHORT;
    59.         rbTo.resval.rint = 2;
    60.         acedTrans(asDblArray(point), &rbFrom, &rbTo, Adesk::kFalse, asDblArray(pnt));
    61.         return pnt;
    62. }
    复制代码

    1. #pragma once
    2. #include <ads.h>
    3. #include <adsdef.h>
    4. #include <acedads.h>
    5. #include <vector>
    6. #include <adscodes.h>  // RTDXF0
    7. #include <tchar.h>
    8. #include <winnt.h>
    9. #include <gepnt2d.h>
    10. #include <gepnt3d.h>
    11. #include <dbents.h>
    12. #include <string>
    13. // #define RESBUF(classname) \
    14. //        struct resbuf *rb = acutBuildList(RTDXF0, _T(#classname), /*8, _T(""),*/ RTNONE)
    15. struct Namepoint {
    16.         std::vector<std::wstring> names;
    17.         std::vector<AcGePoint3d> points;
    18. } namePoints;
    19. //
    20. // 选择集类
    21. //
    22. class CSelectSet
    23. {
    24. public:
    25.         int GetSelectSetMousePick(ads_name);
    26.         int GetSelectSetResBuf(ads_name, resbuf*);
    27.         template<class T>
    28.         std::vector<T*> GetAllEntitysMousePick();
    29.         template<class T>
    30.         std::vector<T*> GetAllEntitys(resbuf*);
    31.         void
    32.                 GetAttribute(std::vector<AcDbBlockReference*> blkrefs);
    33. };
    34. //
    35. // 选择全部实体
    36. // 鼠标选取
    37. //
    38. int CSelectSet::GetSelectSetMousePick(ads_name outSS)
    39. {
    40.         int rc;
    41.         rc = acedSSGet(NULL, NULL, NULL, NULL, outSS);
    42.         return 0;
    43. }
    44. //
    45. // 选择全部实体
    46. // 根据选择集
    47. //
    48. int CSelectSet::GetSelectSetResBuf(ads_name outSS, resbuf *rb) {
    49.         int rc;
    50.         rc = acedSSGet(_T("X"), NULL, NULL, rb, outSS);  //  根据选择集选取
    51.         return 0;
    52. }
    53. // 获得全部某种类型实体
    54. // 鼠标选取
    55. //
    56. template<class T>
    57. std::vector<T*> CSelectSet::GetAllEntitysMousePick() {
    58.         int rc;  ads_name ssEntName, outSS;  int len;
    59.         GetSelectSetMousePick(outSS);
    60.         std::vector<T*> rets;
    61.         acedSSLength(outSS, &len);
    62.         for (size_t i = 0; i < len; i++)
    63.         {
    64.                 acedSSName(outSS, i, ssEntName);
    65.                 AcDbObjectId objId;
    66.                 acdbGetObjectId(objId, ssEntName);
    67.                 AcDbEntity* pEnt;
    68.                 acdbOpenObject(pEnt, objId, AcDb::kForRead);
    69.                 if (pEnt->isKindOf(T::desc())) {
    70.                         rets.push_back(T::cast(pEnt));
    71.                 }
    72.                 else {
    73.                         pEnt->close();
    74.                 }
    75.         }
    76.         acedSSFree(outSS);  // 选择集释放
    77.         return rets;
    78. }
    79. //
    80. // 获得所有某种类型实体
    81. // 过滤器全部选中
    82. //
    83. template<class T>
    84. std::vector<T*> CSelectSet::GetAllEntitys(resbuf *rb) {
    85.         int rc, len;
    86.         ads_name outSS, ssEntName;
    87.         std::vector<T*> rets;
    88.         // struct resbuf *rb = acutBuildList(RTDXF0, _T("INSERT"), /*8, _T(""),*/ RTNONE); // 过滤属性块
    89.         // RESBUF(insert);
    90.         GetSelectSetResBuf(outSS, rb);
    91.         acedSSLength(outSS, &len);
    92.         for (size_t i = 0; i < len; i++)
    93.         {
    94.                 acedSSName(outSS, i, ssEntName);
    95.                 AcDbObjectId objId;
    96.                 acdbGetObjectId(objId, ssEntName);
    97.                 AcDbEntity* pEnt;
    98.                 acdbOpenObject(pEnt, objId, AcDb::kForRead);
    99.                 if (pEnt->isKindOf(T::desc())) {
    100.                         rets.push_back(T::cast(pEnt));
    101.                 }
    102.                 else {
    103.                         pEnt->close();
    104.                 }
    105.         }
    106.         acedSSFree(outSS); acutRelRb(rb);
    107.         return rets;
    108. }
    109. //
    110. // 获得块参照各个属性及坐标
    111. // 产权人
    112. void
    113. CSelectSet::GetAttribute(std::vector<AcDbBlockReference*> blkrefs)
    114. {
    115.         namePoints.names.clear(); namePoints.points.clear();
    116.         for (std::vector<AcDbBlockReference*>::iterator it = blkrefs.begin(); it != blkrefs.end(); it++)
    117.         {
    118.                 // 为块参照新建一个属性迭代器
    119.                 AcDbObjectIterator* pAttIterator = (*it)->attributeIterator();
    120.                 for (pAttIterator->start(); !pAttIterator->done(); pAttIterator->step())
    121.                 {
    122.                         AcDbObjectId attId = pAttIterator->objectId();
    123.                         AcDbAttribute *pAtt = NULL;
    124.                         (*it)->openAttribute(pAtt, attId, AcDb::kForRead);
    125.                         // 获得标签
    126.                         AcString tag;
    127.                         pAtt->tag(tag);
    128.                         // 获得指定标签的属性
    129.                         if (!tag.compare(_T("产权人"))) {
    130.                                 namePoints.names.push_back(pAtt->textString());
    131.                                 namePoints.points.push_back(pAtt->position());
    132.                         }
    133.                                
    134.                         /*        else if (tag == "编号")
    135.                                         no = pAtt->textString();
    136.                                 else if (tag == "坐落门牌")
    137.                                         address = pAtt->textString();*/
    138.                         pAtt->close();   // 属性
    139.                 }
    140.                 (*it)->close();      // 块参照
    141.                 delete pAttIterator; // 块参照迭代器
    142.                 pAttIterator = NULL;
    143.         }
    144.         // return namePoints;
    145. }
    复制代码

     

     

     

     

    object arx 编程实现 AutoCAD 的查找命令 部分源代码
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池| |网站地图

    GMT+8, 2024-9-8 09:31 , Processed in 0.062307 second(s), 25 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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