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

// 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::AppRetCode
acrxEntryPoint(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");  //OK
	acutPrintf(filepath);	//OK
	pfvalue_default(filepath); //OK
	pfvalue2(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文件操作函数   OK
void 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类      OK
void 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---

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值