天气与日历 切换到窄版

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

ObjectArx开发对txt文本文件的操作一例

[复制链接]
  • TA的每日心情
    开心
    昨天 11:26
  • 签到天数: 86 天

    [LV.6]常住居民II

    1640

    主题

    207

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
    发表于 2024-3-16 09:45:50 | 显示全部楼层 |阅读模式
    1. // MyArxFirst.cpp : 定义 DLL 应用程序的导出函数。
    2. //ObjectArx开发对txt文本文件的操作一例

    3. #include "stdafx.h"
    4. #include <aced.h>
    5. #include <rxregsvc.h>
    6. #include <tchar.h>

    7. #include <fstream>
    8. #include <iostream>
    9. //
    10. #include <comdef.h>


    11. using namespace std;

    12. //定义两个函数
    13. void initApp();
    14. void unloadApp();

    15. //定义命令函数
    16. //------------------------------------------
    17. //打印"Hello world!"在AutoCAD Command上  的命令
    18. void hello();

    19. //打印文件内容 的命令
    20. void pfvalue();

    21. //ado连接数据库的方法 的命令
    22. void pdbvalue();

    23. //定义一般函数
    24. //------------------------------------------
    25. ACHAR* ConvertCharPtrToAcharPtr(const char* src);
    26. ACHAR* ConvertCharPtrToAcharPtr2(const char* src);
    27. //
    28. char* ConvertAcharPtrToCharPtr(const ACHAR* src);
    29. char* ConvertAcharPtrToCharPtr2(const ACHAR* src);
    30. //通用转换函数
    31. _bstr_t Get_bstr_t(char* src);
    32. _bstr_t Get_bstr_t_W(wchar_t* src);
    33. char* GetCharPtr(_bstr_t bstrt);
    34. wchar_t* GetWchar_t(_bstr_t bstrt);

    35. //打印函数
    36. void pfvalue_default(const ACHAR* filepath);

    37. //打印文件内容2 函数
    38. void pfvalue2(const ACHAR* filepath);

    39. //ado连接数据库的方法 函数
    40. void pdbvalue(const ACHAR *filepath);
    41. //------------------------------------------

    42. extern "C"
    43. AcRx::AppRetCode
    44. acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
    45. //void acrxEntryPoint(void* pkt)
    46. {
    47.         switch (msg)
    48.         {
    49.                 case AcRx::kInitAppMsg:
    50.                         acrxDynamicLinker->unlockApplication(pkt);
    51.                         acrxRegisterAppMDIAware(pkt);
    52.                         initApp();
    53.                         break;
    54.                 case AcRx::kUnloadAppMsg:
    55.                         unloadApp();
    56.                         break;
    57.                 default:
    58.                     break;
    59.         }
    60.         return AcRx::kRetOK;
    61. }



    62. void initApp()
    63. {
    64.     //register a command with the AutoCAD command mechanism
    65.         //string macro 用法:
    66.         //_T("helloworld") or  __T("helloworld")  or ACRX_T("helloworld")
    67.         acedRegCmds->addCommand(ACRX_T("HELLOWORLD_COMMANDS"),
    68.                                     ACRX_T("ArxHsgBag"),
    69.                                                         ACRX_T("Hello"),
    70.                                                         ACRX_CMD_TRANSPARENT,
    71.                                                         hello);
    72.         acedRegCmds->addCommand(ACRX_T("PFVALUE_COMMANDS"),
    73.                                     ACRX_T("ArxHsgBag"),
    74.                                                         ACRX_T("pfvalue"),
    75.                                                         ACRX_CMD_TRANSPARENT,
    76.                                                         pfvalue);
    77.         acedRegCmds->addCommand(ACRX_T("PDBVALUE_COMMANDS"),
    78.                                     ACRX_T("ArxHsgBag"),
    79.                                                         ACRX_T("pdbvalue"),
    80.                                                         ACRX_CMD_TRANSPARENT,
    81.                                                         pdbvalue);
    82.         //
    83. }


    84. void unloadApp()
    85. {
    86.     acedRegCmds->removeGroup(ACRX_T("HELLOWORLD_COMMANDS"));
    87.         acedRegCmds->removeGroup(ACRX_T("PFVALUE_COMMANDS"));
    88.         acedRegCmds->removeGroup(ACRX_T("PDBVALUE_COMMANDS"));
    89. }


    90. //----------------------------------------------------
    91. //hello命令
    92. void hello()
    93. {
    94.     acutPrintf(ACRX_T("\n第一个Arx程序Hello World!"));
    95.        
    96. }
    97. //打印文件内容 命令
    98. void pfvalue()
    99. {       
    100.         acutPrintf(_T("开始输出文件内信息:\n"));
    101.     const ACHAR* filepath=ACRX_T("d:\\test.txt");  //OK
    102.         acutPrintf(filepath);        //OK
    103.         pfvalue_default(filepath); //OK
    104.         pfvalue2(filepath);  //OK
    105. }
    106. //输出数据库表内记录的命令
    107. void pdbvalue()
    108. {
    109.     acutPrintf(_T("开始输出数据库表内记录:\n"));
    110.         //...
    111. }
    112. //----------------------------------------------------
    113. ACHAR* ConvertCharPtrToAcharPtr(const char* src)
    114. {
    115.         ACHAR* tmp;
    116.         _bstr_t AStr = src;       
    117.     LPWSTR AstrW = LPTSTR(AStr);
    118.     tmp=(ACHAR *)AstrW;
    119.         return tmp;
    120. }
    121. ACHAR* ConvertCharPtrToAcharPtr2(const char* src)
    122. {
    123.         // Convert to a wchar_t*
    124.     size_t srcsize = strlen(src) + 1;
    125.     size_t newsize = srcsize;       
    126.     size_t convertedChars = 0;
    127.     wchar_t *wcstring;
    128.         wcstring=new wchar_t[newsize];
    129.     mbstowcs_s(&convertedChars, wcstring, srcsize, src, _TRUNCATE);
    130.     //wcscat_s(wcstring, L" (wchar_t *)");
    131.     //wcout << wcstring << endl;
    132.         return wcstring;
    133. }
    134. char* ConvertAcharPtrToCharPtr(const ACHAR* src)  //
    135. {
    136.     char* tmp;
    137.         _bstr_t bstrt(src);
    138.         tmp=GetCharPtr(bstrt);
    139.         return tmp;
    140. }
    141. char* ConvertAcharPtrToCharPtr2(const ACHAR* src)
    142. {
    143.     // Convert to a char*
    144.     size_t srcsize = wcslen(src) + 1;   
    145.         size_t newsize = srcsize;
    146.     size_t convertedChars = 0;
    147.     char *nstring;
    148.         nstring=new char[newsize];
    149.     wcstombs_s(&convertedChars, nstring, srcsize, src, _TRUNCATE);   
    150.         return nstring;
    151. }
    152. //
    153. _bstr_t Get_bstr_t(char* src)
    154. {
    155.      _bstr_t bstrt(src);
    156.          return bstrt;
    157. }
    158. _bstr_t Get_bstr_t_W(wchar_t* src)
    159. {
    160.     // Convert to a _bstr_t
    161.     _bstr_t bstrt(src);
    162.     //bstrt += " (_bstr_t)";
    163.     //cout << bstrt << endl;
    164.         return bstrt;
    165. }
    166. char* GetCharPtr(_bstr_t bstrt)
    167. {
    168.     // Convert to a char*
    169.     size_t newsize = bstrt.length()+1;
    170.     char *nstring;nstring=new char[newsize];
    171.     strcpy_s(nstring,newsize,(char *)bstrt);
    172.     //strcat_s(nstring, " (char *)");
    173.     //cout << nstring << endl;
    174.         return nstring;
    175. }
    176. wchar_t* GetWchar_t(_bstr_t bstrt)
    177. {
    178.     // Convert to a wchar_t*
    179.         int srcsize=bstrt.length()+1;
    180.     wchar_t *wcstring;wcstring=new wchar_t[srcsize];
    181.     wcscpy_s(wcstring,srcsize,(wchar_t *)bstrt);
    182.     //wcscat_s(wcstring, L" (wchar_t *)");
    183.     //wcout << wcstring << endl;
    184.         return wcstring;
    185. }
    186. //CComBSTR GetCComBSTR(char* src)
    187. //{
    188. //    // Convert to a CComBSTR
    189. //    CComBSTR ccombstr(src);
    190. //    /*if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    191. //    {
    192. //        CW2A printstr(ccombstr);
    193. //        cout << printstr << endl;
    194. //    }*/
    195. //        return ccombstr;
    196. //}
    197. //----------------------------------------------------
    198. //打印文件内容1  C文件操作函数   OK
    199. void pfvalue_default(const ACHAR* filepath)
    200. {
    201.           acutPrintf(_T("\n-----下面是c文件操作函数打开的内容-----\n"));          
    202.           acutPrintf(filepath);acutPrintf(_T("\n"));
    203.       FILE *fp;
    204.           int linesize=4000;
    205.           char *line;line=new char[linesize];
    206.           const char* path=ConvertAcharPtrToCharPtr(filepath);
    207.           ACHAR* wtmp=ConvertCharPtrToAcharPtr(path);          
    208.           acutPrintf(wtmp);acutPrintf(_T("\n"));
    209.           if((fp=fopen(path,"r"))==NULL)
    210.           {
    211.                   acutPrintf(_T("\nfile cannot be opened\n"));                  
    212.           }          
    213.           else
    214.           {
    215.                   const ACHAR* tmp;
    216.                   while(!feof(fp))
    217.                   {
    218.                           if(fgets(line,linesize,fp)!=NULL)
    219.                           {
    220.                                   tmp=ConvertCharPtrToAcharPtr(line);
    221.                                   acutPrintf(tmp);
    222.                           }
    223.                   }
    224.                   fclose(fp);
    225.           }
    226. }

    227. //打印文件内容2  ifstream类      OK
    228. void pfvalue2(const ACHAR* filepath)
    229. {       
    230.          //--file2 open
    231.          //filepath="d:\\test.txt";
    232.         acutPrintf(ACRX_T("\n"));
    233.         acutPrintf(filepath);
    234.         ifstream r(filepath,ifstream.in);
    235.          if(!r)
    236.          {
    237.                  acutPrintf(ACRX_T("打开文件出错!"));
    238.          }
    239.          else
    240.          {
    241.                  const ACHAR* tmpAchar;
    242.                  //char line[4000];   //[100]2000
    243.                  int linesize=4000;
    244.                  char* line;line=new char[linesize];
    245.                  while(r>>line)
    246.                  {                          
    247.                           tmpAchar=ConvertCharPtrToAcharPtr(line);
    248.                           acutPrintf(tmpAchar);
    249.                           acutPrintf(ACRX_T("\n"));
    250.                  }
    251.                  r.close();       
    252.                  acutPrintf(ACRX_T("输出文件内容完毕!"));
    253.          }
    254. }


    255. //ObjectARX offers the following input functions.
    256. //Refer to the ObjectARX Online Help for a complete description of
    257. //how to use these functions.
    258. //acedGetInt        used to get an integer value
    259. //acedGetReal       used to get a real value
    260. //acedGetString     used to get a string
    261. //acedGetAngle      used to get a angle value
    262. //acedGetKword      used to get a key word
    263. //acedInitGet       used to initialize acedGetXXXX functions
    264. //acedGetFileD      used to retrieve file selection from a file dialog
    265. //acedGetPoint      used to pick a point
    266. //acedGetDist       used to get the distance between two points
    267. //acedGetCorner     see Online Help for a complete description
    268. //
    269. //ObjectARX offers the following functions for selection of AutoCAD entities.
    270. //(Again refer to the ObjectARX Online Help for a complete description of
    271. //how to use these functions).
    272. //
    273. //acedEntSel       used to select a single entity
    274. //acedNEntSel      used to select a single, nested entity
    275. //acedNEntSelP     used to select a single, nested entity
    276. //acutSSGet        used to select multiple entities

    277. //--the---end---
    复制代码

     

     

     

     

    ObjectArx开发对txt文本文件的操作一例
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-6-2 11:25 , Processed in 0.058085 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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