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

来源:互联网 发布:fluent软件代理 编辑:程序博客网 时间:2024/05/08 21:27
// MyArxFirst.cpp : 定义 DLL 应用程序的导出函数。//ObjectArx开发对txt文本文件的操作一例#include "stdafx.h"#include <aced.h>#include <rxregsvc.h>#include <tchar.h>#include <fstream>#include <iostream>//#include <comdef.h>using namespace std;//定义两个函数void initApp();void unloadApp();//定义命令函数//------------------------------------------//打印"Hello world!"在AutoCAD Command上  的命令void hello();//打印文件内容 的命令void pfvalue();//ado连接数据库的方法 的命令void pdbvalue();//定义一般函数//------------------------------------------ACHAR* ConvertCharPtrToAcharPtr(const char* src);ACHAR* ConvertCharPtrToAcharPtr2(const char* src);//char* ConvertAcharPtrToCharPtr(const ACHAR* src);char* ConvertAcharPtrToCharPtr2(const ACHAR* src);//通用转换函数_bstr_t Get_bstr_t(char* src);_bstr_t Get_bstr_t_W(wchar_t* src);char* GetCharPtr(_bstr_t bstrt);wchar_t* GetWchar_t(_bstr_t bstrt);//打印函数void pfvalue_default(const ACHAR* filepath);//打印文件内容2 函数void pfvalue2(const ACHAR* filepath);//ado连接数据库的方法 函数void pdbvalue(const ACHAR *filepath);//------------------------------------------extern "C" AcRx::AppRetCodeacrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)//void acrxEntryPoint(void* pkt){ switch (msg){case AcRx::kInitAppMsg:acrxDynamicLinker->unlockApplication(pkt);acrxRegisterAppMDIAware(pkt);initApp();break;case AcRx::kUnloadAppMsg:unloadApp();break;default:    break;}return AcRx::kRetOK;}void initApp(){    //register a command with the AutoCAD command mechanism//string macro 用法://_T("helloworld") or  __T("helloworld")  or ACRX_T("helloworld")acedRegCmds->addCommand(ACRX_T("HELLOWORLD_COMMANDS"),                    ACRX_T("ArxHsgBag"),ACRX_T("Hello"),ACRX_CMD_TRANSPARENT,hello);acedRegCmds->addCommand(ACRX_T("PFVALUE_COMMANDS"),                    ACRX_T("ArxHsgBag"),ACRX_T("pfvalue"),ACRX_CMD_TRANSPARENT,pfvalue);acedRegCmds->addCommand(ACRX_T("PDBVALUE_COMMANDS"),                    ACRX_T("ArxHsgBag"),ACRX_T("pdbvalue"),ACRX_CMD_TRANSPARENT,pdbvalue);//}void unloadApp(){    acedRegCmds->removeGroup(ACRX_T("HELLOWORLD_COMMANDS"));acedRegCmds->removeGroup(ACRX_T("PFVALUE_COMMANDS"));acedRegCmds->removeGroup(ACRX_T("PDBVALUE_COMMANDS"));}//----------------------------------------------------//hello命令void hello(){    acutPrintf(ACRX_T("\n第一个Arx程序Hello World!"));}//打印文件内容 命令void pfvalue(){acutPrintf(_T("开始输出文件内信息:\n"));    const ACHAR* filepath=ACRX_T("d:\\test.txt");  //OKacutPrintf(filepath);//OKpfvalue_default(filepath); //OKpfvalue2(filepath);  //OK}//输出数据库表内记录的命令void pdbvalue(){    acutPrintf(_T("开始输出数据库表内记录:\n"));//...}//----------------------------------------------------ACHAR* ConvertCharPtrToAcharPtr(const char* src){ACHAR* tmp;_bstr_t AStr = src;    LPWSTR AstrW = LPTSTR(AStr);    tmp=(ACHAR *)AstrW;return tmp;}ACHAR* ConvertCharPtrToAcharPtr2(const char* src){// Convert to a wchar_t*    size_t srcsize = strlen(src) + 1;    size_t newsize = srcsize;    size_t convertedChars = 0;    wchar_t *wcstring;wcstring=new wchar_t[newsize];    mbstowcs_s(&convertedChars, wcstring, srcsize, src, _TRUNCATE);    //wcscat_s(wcstring, L" (wchar_t *)");    //wcout << wcstring << endl;return wcstring;}char* ConvertAcharPtrToCharPtr(const ACHAR* src)  //{    char* tmp;_bstr_t bstrt(src);tmp=GetCharPtr(bstrt);return tmp;}char* ConvertAcharPtrToCharPtr2(const ACHAR* src){    // Convert to a char*    size_t srcsize = wcslen(src) + 1;    size_t newsize = srcsize;    size_t convertedChars = 0;    char *nstring;nstring=new char[newsize];    wcstombs_s(&convertedChars, nstring, srcsize, src, _TRUNCATE);    return nstring;}//_bstr_t Get_bstr_t(char* src){     _bstr_t bstrt(src); return bstrt;}_bstr_t Get_bstr_t_W(wchar_t* src){    // Convert to a _bstr_t    _bstr_t bstrt(src);    //bstrt += " (_bstr_t)";    //cout << bstrt << endl;return bstrt;}char* GetCharPtr(_bstr_t bstrt){    // Convert to a char*    size_t newsize = bstrt.length()+1;    char *nstring;nstring=new char[newsize];    strcpy_s(nstring,newsize,(char *)bstrt);    //strcat_s(nstring, " (char *)");    //cout << nstring << endl;return nstring;}wchar_t* GetWchar_t(_bstr_t bstrt){    // Convert to a wchar_t*int srcsize=bstrt.length()+1;    wchar_t *wcstring;wcstring=new wchar_t[srcsize];    wcscpy_s(wcstring,srcsize,(wchar_t *)bstrt);    //wcscat_s(wcstring, L" (wchar_t *)");    //wcout << wcstring << endl;return wcstring;}//CComBSTR GetCComBSTR(char* src)//{//    // Convert to a CComBSTR//    CComBSTR ccombstr(src);//    /*if (ccombstr.Append(L" (CComBSTR)") == S_OK)//    {//        CW2A printstr(ccombstr);//        cout << printstr << endl;//    }*///return ccombstr;//}//----------------------------------------------------//打印文件内容1  C文件操作函数   OKvoid pfvalue_default(const ACHAR* filepath){  acutPrintf(_T("\n-----下面是c文件操作函数打开的内容-----\n"));    acutPrintf(filepath);acutPrintf(_T("\n"));      FILE *fp;  int linesize=4000;  char *line;line=new char[linesize];  const char* path=ConvertAcharPtrToCharPtr(filepath);  ACHAR* wtmp=ConvertCharPtrToAcharPtr(path);    acutPrintf(wtmp);acutPrintf(_T("\n"));  if((fp=fopen(path,"r"))==NULL)  {  acutPrintf(_T("\nfile cannot be opened\n"));    }    else  {  const ACHAR* tmp;  while(!feof(fp))  {  if(fgets(line,linesize,fp)!=NULL)  {  tmp=ConvertCharPtrToAcharPtr(line);  acutPrintf(tmp);  }  }  fclose(fp);  }}//打印文件内容2  ifstream类      OKvoid pfvalue2(const ACHAR* filepath){ //--file2 open //filepath="d:\\test.txt";acutPrintf(ACRX_T("\n"));acutPrintf(filepath);ifstream r(filepath,ifstream.in); if(!r) { acutPrintf(ACRX_T("打开文件出错!")); } else { const ACHAR* tmpAchar; //char line[4000];   //[100]2000 int linesize=4000; char* line;line=new char[linesize]; while(r>>line) {    tmpAchar=ConvertCharPtrToAcharPtr(line);  acutPrintf(tmpAchar);  acutPrintf(ACRX_T("\n")); } r.close(); acutPrintf(ACRX_T("输出文件内容完毕!")); }}//ObjectARX offers the following input functions. //Refer to the ObjectARX Online Help for a complete description of //how to use these functions. //acedGetInt        used to get an integer value//acedGetReal       used to get a real value//acedGetString     used to get a string//acedGetAngle      used to get a angle value//acedGetKword      used to get a key word//acedInitGet       used to initialize acedGetXXXX functions//acedGetFileD      used to retrieve file selection from a file dialog//acedGetPoint      used to pick a point//acedGetDist       used to get the distance between two points//acedGetCorner     see Online Help for a complete description ////ObjectARX offers the following functions for selection of AutoCAD entities. //(Again refer to the ObjectARX Online Help for a complete description of //how to use these functions). ////acedEntSel       used to select a single entity//acedNEntSel      used to select a single, nested entity//acedNEntSelP     used to select a single, nested entity//acutSSGet        used to select multiple entities//--the---end---