动态读取Excel中的内容
本帖最后由 springwillow 于 2017-7-31 16:29 编辑最近研究多线程,试验编写了一个动态读取Excel中内容的程序,程序读取第一个打开的Excel的选中区域,并生成到CAD中,创建一个线程每隔3s读取一次excel,如果发生变化则更新CAD。目前只编译了R19。发现bug可以跟帖回复,能完善的我尽量完善。
从技术的角度来说,很不错。从实用的角度来说,华而不实。 把主要源码给大家,有想学的可以看看,写的比较烂,大家对付看吧
// (C) Copyright 2002-2007 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "CApplication.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
#include "CRange.h"
#include "CBorder.h"
#include "CBorders.h"
#include "CFont0.h"
#include <comdef.h>
#include <list>
#include <vector>
#include "Calculation.h"
//-----------------------------------------------------------------------------
#define szRDS _RXST("YCL")
using namespace std;
bool IsExitThreade = false;
// 边框的类型 Borders
// xlEdgeLeft 左=1
// xlEdgeRight 右=2
// xlEdgeTop 顶=3
// xlEdgeBottom 底=4
// xlDiagonalUp 左上右下=5
// xlDiagonalDown 左下右上=6
// xlEdgeLeft 外部左边框=7
// xlEdgeTop 外部上边框=8
// xlEdgeBottom 外部下边框=9
// xlEdgeRight 外部右边框=10
// xlInsideVertical 内部竖线=11
// xlInsideHorizontal 内部横线=12
// (其中1:为左 2:右 3:顶4:底5:斜\ 6:斜/)
// #define xlEdgeLeft 1
// #define xlEdgeTop 2
// #define xlEdgeBottom 3
// #define xlEdgeRight 4
// #define xlDiagonalUp 5
// #define xlDiagonalDown 6
// #define xlEdgeLeft 7
// #define xlEdgeTop 8
// #define xlEdgeBottom 9
// #define xlEdgeRight 10
// #define xlInsideVertical 11
// #define xlInsideHorizontal 12
#define Aligndefault 0
#define Alignleft 1
#define AlignCenter 2
#define AlignRight 3
//定义几个传递表信息所需要的结构体
struct TableLine
{
//线的起始点
bool IsRowcontinue;
bool IsColcontinue;
AcGePoint3d TopStartPoint;
AcGePoint3d TopEndPoint;
AcGePoint3d BottomStartPoint;
AcGePoint3d BottomEndPoint;
AcGePoint3d TopCurentPoint;
AcGePoint3d BottomCurentPoint;
};
//单元格信息
struct TextInfo
{
//行号
int RowNum;
//列号
int ColNum;
//行高
float rowheiht;
//列宽
float colwidth;
//单元格内容
TCHAR text;
//单元格字高
float texthight;
//是否是合并单元格
bool IsMergeCells;
//合并单无格的行数
int UnionRowNums;
//合并单元格的列数
int UnionColNums;
//合并单元格的起始行
int UnionStartRowNum;
//合并单元格的起始列
int UnionStartColNum;
AcGePoint3d LUPoint;
AcGePoint3d RUPoint;
AcGePoint3d LBPoint;
AcGePoint3d RBPoint;
bool IsTop;
bool IsBottom;
bool IsLeft;
bool IsRight;
int HorizontalAlignment;
AcDbObjectId Objectid;
};
AcGePoint3d ptInsert;
ads_real Scale =1;
// 将实体添加到图形数据库的模型空间
AcDbObjectId PostToModelSpace(AcDbEntity* pEnt)
{
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
acDocManager->lockDocument(curDoc());
if(pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite) == Acad::eOk)
{
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pBlockTable->close();
pBlockTableRecord->close();
pEnt->close();
// 解锁文档
acDocManager->unlockDocument(curDoc());
return entId;
}
// 解锁文档
acDocManager->unlockDocument(curDoc());
return NULL;
}
//其中CurrTextStyle(styleName)是得到当前字体样式,函数如下:
bool FindTextStyle(TCHAR* textStyle)
{
bool ret = false;
AcDbTextStyleTable *pTextStyleTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTbl, AcDb::kForRead);
AcDbTextStyleTableIterator *pIt = NULL;
pTextStyleTbl->newIterator(pIt);
for (;!pIt->done();pIt->step())
{
AcDbTextStyleTableRecord *pRcd = NULL;
if (pIt->getRecord(pRcd, AcDb::kForRead) == Acad::eOk)
{
TCHAR *szName = NULL;
pRcd->getName(szName);
if (_tcscmp(szName,textStyle) == 0)
{
pRcd->close();
ret = true;
break;
}
acutDelString(szName);
pRcd->close();
}
}
delete pIt;
pTextStyleTbl->close();
return ret;
}
bool CurrTextStyle(TCHAR *&styleName,TCHAR *&fontName,TCHAR *&bigFontName,AcDbObjectId &Id)
{
Id = acdbHostApplicationServices()->workingDatabase()->textstyle();
AcDbTextStyleTableRecord* pStyleRecord;
if(acdbOpenObject((AcDbObject *&)pStyleRecord, Id, AcDb::kForRead) == Acad::eOk) {
const TCHAR *name;
pStyleRecord->getName(name);
_tcscpy(styleName, name);
pStyleRecord->fileName(name);
_tcscpy(fontName, name);
pStyleRecord->bigFontFileName(name);
_tcscpy(bigFontName, name);
pStyleRecord->close();
return true;
}
return false;
// else _tcscpy(styleName, _T("STANDARD"));
}
//===========================创建文字样式===========================================
AcDbObjectId createTextStyle(TCHAR* fontName,TCHAR* bigFontName,TCHAR* textStyleName)
{
AcGiTextStyle *TextStyle=new AcGiTextStyle
(fontName,
bigFontName,
0,
1,
0,
0,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
textStyleName); //字体名
AcDbObjectId textStyleId;
toAcDbTextStyle(*TextStyle,textStyleId);
return textStyleId;
}
//函数 CreateText,用于创建文字对象
AcDbObjectId CreateText(const AcGePoint3d& LUInsert,const AcGePoint3d& RBInsert, const TCHAR *text, AcDbObjectId style, double height, double rotation,int HorizontalAlignment)
{
AcGePoint3d InsertPoint;
switch (HorizontalAlignment)
{
case Aligndefault:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
AcDbText *pText = new AcDbText(InsertPoint,text, style, height,rotation);
pText->setJustification(pText->kTextAlignmentMiddle);
pText->setAlignmentPoint(InsertPoint);
return PostToModelSpace(pText);
}
case Alignleft:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
InsertPoint.x = LUInsert.x;
// InsertPoint.y -= height * 0.5;
AcDbText *pText = new AcDbText(InsertPoint,text, style, height,rotation);
pText->setJustification(pText->kTextAlignmentMiddleLeft);
pText->setAlignmentPoint(InsertPoint);
return PostToModelSpace(pText);
}
case AlignCenter:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
// InsertPoint.y -= height * 0.5;
AcDbText *pText = new AcDbText(InsertPoint,text, style, height,rotation);
pText->setJustification(pText->kTextAlignmentMiddleCenter);
pText->setAlignmentPoint(InsertPoint);
return PostToModelSpace(pText);
}
case AlignRight:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
InsertPoint.x = RBInsert.x;
// InsertPoint.y -= height * 0.5;
AcDbText *pText = new AcDbText(InsertPoint,text, style, height,rotation);
pText->setJustification(pText->kTextAlignmentMiddleRight);
pText->setAlignmentPoint(InsertPoint);
return PostToModelSpace(pText);
}
};
return NULL;
}
//CreateMText 函数,用于添加多行文字
AcDbObjectId CreateMText(const AcGePoint3d& LUInsert,const AcGePoint3d& RBInsert, const TCHAR *text, AcDbObjectId style, double height, int HorizontalAlignment,const AcGePoint3d& PointInsert,CString Caption,CString BookName,CString SheetName,int start_row,int start_col,int row_num,int col_num)
{
AcDbMText *pMText = new AcDbMText();
AcGePoint3d InsertPoint;
double width = RBInsert.x - LUInsert.x - height * 0.4;
/*
//向对象写入扩展数据
TCHAR AppName[] ={ _T("ReadeEXCEL")};
ads_point pt;
pt = PointInsert.x;
pt = PointInsert.y;
pt = PointInsert.z;
struct resbuf* pRb = acutBuildList(AcDb::kDxfRegAppName, AppName, // 应用程序名称
AcDb::kDxfXdAsciiString, Caption, // Excel标题
AcDb::kDxfXdAsciiString, BookName, // 字符串
AcDb::kDxfXdAsciiString, SheetName, // 字符串
AcDb::kDxfXdInteger32,start_row,
AcDb::kDxfXdInteger32,start_col,
AcDb::kDxfXdInteger32,row_num,
AcDb::kDxfXdInteger32,col_num,
AcDb::kDxfXdWorldXCoord,pt,
RTNONE);
pMText->setXData(pRb);
*/
switch (HorizontalAlignment)
{
case Aligndefault:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
// 设置多行文字的特性
pMText->setTextStyle(style);
pMText->setContents(text);
pMText->setLocation(InsertPoint);
pMText->setTextHeight(height);
pMText->setWidth(width);
pMText->setAttachment(AcDbMText::kMiddleCenter);
return PostToModelSpace(pMText);
}
case Alignleft:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
InsertPoint.x = LUInsert.x + height * 0.2;
pMText->setTextStyle(style);
pMText->setContents(text);
pMText->setLocation(InsertPoint);
pMText->setTextHeight(height);
pMText->setWidth(width);
pMText->setAttachment(AcDbMText::kMiddleLeft);
return PostToModelSpace(pMText);
}
case AlignCenter:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
pMText->setTextStyle(style);
pMText->setContents(text);
pMText->setLocation(InsertPoint);
pMText->setTextHeight(height);
pMText->setWidth(width);
pMText->setAttachment(AcDbMText::kMiddleCenter);
return PostToModelSpace(pMText);
}
case AlignRight:
{
InsertPoint =CCalculation::MiddlePoint(LUInsert,RBInsert);
InsertPoint.x = RBInsert.x - height * 0.2;
pMText->setTextStyle(style);
pMText->setContents(text);
pMText->setLocation(InsertPoint);
pMText->setTextHeight(height);
pMText->setWidth(width);
pMText->setAttachment(AcDbMText::kMiddleRight);
return PostToModelSpace(pMText);
}
};
return NULL;
}
// 创建直线
AcDbObjectId CreateLine(AcGePoint3d ptStart, AcGePoint3d ptEnd)
{
AcDbLine *pLine = new AcDbLine(ptStart, ptEnd);
// 将实体添加到图形数据库
AcDbObjectId lineId;
lineId = PostToModelSpace(pLine);
return lineId;
}
//该函数根据两个角点和线宽来创建矩形
AcDbObjectId CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2, double width)
{
// 提取两个角点的坐标值
double x1 = pt1.x, x2 = pt2.x;
double y1 = pt1.y, y2 = pt2.y;
// 计算矩形的角点
AcGePoint2d ptLeftBottom(CCalculation::Min(x1, x2), CCalculation::Min(y1, y2));
AcGePoint2d ptRightBottom(CCalculation::Max(x1, x2), CCalculation::Min(y1, y2));
AcGePoint2d ptRightTop(CCalculation::Max(x1, x2), CCalculation::Max(y1, y2));
AcGePoint2d ptLeftTop(CCalculation::Min(x1, x2), CCalculation::Max(y1, y2));
// 创建对应的多段线
AcDbPolyline *pPoly = new AcDbPolyline(4);
pPoly->addVertexAt(0, ptLeftBottom, 0, width, width);
pPoly->addVertexAt(1, ptRightBottom, 0, width, width);
pPoly->addVertexAt(2, ptRightTop, 0, width, width);
pPoly->addVertexAt(3, ptLeftTop, 0, width, width);
pPoly->setClosed(Adesk::kTrue);
// 将多段线添加到模型空间
AcDbObjectId polyId;
polyId = PostToModelSpace(pPoly);
return polyId;
}
//表格到处到Excel
int DynamicWriteToExcel()
{
ads_name ssname;
struct resbuf *rb;
rb = acutBuildList(RTDXF0,_T("TEXT,LWPOLYLINE,LINE"),RTNONE);
if(acedSSGet(NULL,NULL,NULL,rb,ssname) != RTNORM)
{
acutRelRb(rb);
}
else
{
acutRelRb(rb);
Adesk::Int32 sslength;
acedSSLength(ssname, &sslength);
// acutPrintf(_T("%d\n"),sslength);
AcDbObjectId objId;
AcDbObjectIdArrayLWpolyLineObjectIdArray ,LineObjectIdArray,TextObjectIdArray;
for(int i = 0;i < sslength;i++)
{
ads_name ent;
acedSSName(ssname, i, ent);
acdbGetObjectId(objId, ent);
AcDbEntity *pEnt;
//将对象按类型分类
if(acdbOpenAcDbEntity((AcDbEntity *&)pEnt, objId, AcDb::kForRead) == Acad::eOk)
{
if (pEnt->isKindOf(AcDb2dPolyline::desc()))
{
LWpolyLineObjectIdArray.append(objId);
}
else if(pEnt->isKindOf(AcDbLine::desc()))
{
LineObjectIdArray.append(objId);
}
else if(pEnt->isKindOf(AcDbLine::desc()))
{
TextObjectIdArray.append(objId);
}
pEnt->close();
}
}
}
return 0;
}
BOOL CALLBACK EnumChildProc(
HWND hwnd, // handle to child window
LPARAM lParam // application-defined value
)
{
TCHAR szText;
CString strBuffer;
::GetWindowText(hwnd,szText,50);
strBuffer.Format(szText);
if (strBuffer.Find(_T("Book")) != -1)
{
::PostMessage(hwnd,WM_KEYDOWN,VK_TAB,0);
::PostMessage(hwnd,WM_KEYUP,VK_TAB,0);
Sleep(500);
::PostMessage(hwnd,WM_KEYDOWN,VK_LEFT,0);
::PostMessage(hwnd,WM_KEYUP,VK_LEFT,0);
}
// PWINDOWINFO pwindowinfo = NULL;
// ::GetWindowInfo(hwnd,pwindowinfo);
// if (pwindowinfo != NULL)
// {
// if( pwindowinfo->dwWindowStatus == 0)
// IsContinue = true;
// else
// IsContinue = false;
// }
return TRUE;
// //需要枚举子窗口,因为状态栏为子窗口,获取状态栏的句柄
// TCHAR temp1;
// if(hwnd)
// {
// ::GetClassName(hwnd,temp1,255);
// if(_tcscmp(temp1,_T("NetUIHWND")))//这个字符串使用spy++或者spyxx获得的状态栏类名
// {
// int nItemCount;
// int i;
// TCHAR chBuffer;
// DWORD dwProcessID;
// HANDLE hProcess;
// void * Pointer;
// SIZE_T nNumberOfBytesRead;
// nItemCount = ::SendMessage(hwnd, SB_GETPARTS, 0, 0);
// ::GetWindowThreadProcessId(hwnd, &dwProcessID);//和dwProcessID=pID效果一样
// hProcess = OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE,false,dwProcessID);
// Pointer = VirtualAllocEx(hProcess,NULL,4096,MEM_RESERVE | MEM_COMMIT,PAGE_READWRITE);
// for(i= 0 ; i < 4 ; ++i)
// {
// ::SendMessage(hwnd, SB_GETTEXT, i, (LPARAM)Pointer);
// ::ReadProcessMemory(hProcess, Pointer,chBuffer, sizeof(chBuffer), &nNumberOfBytesRead);
// AfxMessageBox(chBuffer);
// }
// VirtualFreeEx(hProcess,Pointer,0, MEM_RELEASE);
// CloseHandle(hProcess);
// return true;
// }
// }
// return true;
}
//关联线程函数
DWORD WINAPI ThreadProc(
LPVOID lpParameter // thread data
)
{
// //检查当前字体样式
// TCHARStrStyleName;
// TCHAR*CurStyleName = new TCHAR;
// TCHAR* StrFileName = new TCHAR;
// TCHAR* StrBigFontFileName =new TCHAR;
// AcDbObjectId CurSthyleID=NULL,StyleID=NULL;
// if (CurrTextStyle(CurStyleName,StrFileName,StrBigFontFileName,CurSthyleID))
// {
// _tcscpy_s(StrStyleName,CurStyleName);
// _tcscat(StrStyleName,_T("_xls"));
//
// if (!FindTextStyle(StrStyleName))
// {
// if (_tcscmp(StrFileName,_T("")) == 0)
// {
// StyleID = createTextStyle(_T("arial.ttf"),_T("gbcbig.shx"),StrStyleName);
// }
// else
// {
// StyleID = createTextStyle(StrFileName,StrBigFontFileName,StrStyleName);
// }
// }
// }
// else
// {
// _tcscpy_s(StrStyleName,_T("Standard_xls"));
// createTextStyle(_T("gbenor.shx"),_T("gbcbig.shx"),StrStyleName);
// }
// delete [] StrFileName;
// delete [] StrBigFontFileName;
//常用变量定义
CApplication app;
CWorkbooks Workbooks;
CWorkbook Workbook;
CWorksheets Worksheets;
CWorksheet Worksheet;
CRange ranges;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant vResult;
TextInfo* TextInfoArr =(TextInfo*)lpParameter;
//采用MFC方式初始化COM库,程序结束时COM库会自动释放
// if(!AfxOleInit())
// {
//
// MessageBox(NULL,_T("初始化COM支持库失败!\n无法控制Excel!更新线程退出!"),_T("警告"),MB_ICONERROR | MB_OK);
// delete[] TextInfoArr;
// ::ExitThread(1);
// return 0;
// }
//关联已经运行的Excel实例
CString str = TextInfoArr.text;
int booknamelen = str.Find(_T(","));
CString BookName = str.Left(booknamelen);
CString Caption = str.TrimLeft(BookName);
Caption = Caption.TrimLeft(_T(","));
booknamelen = Caption.Find(_T(","));
CString SheetName=Caption.Left(booknamelen);
Caption = Caption.TrimLeft(SheetName);
Caption = Caption.TrimLeft(_T(","));
int start_row = TextInfoArr.UnionStartRowNum;
int start_col = TextInfoArr.UnionStartColNum;
int row_nums = TextInfoArr.RowNum;
int col_nums = TextInfoArr.ColNum;
bool IsContinue = false;
HWND hExcel = NULL;
while (TRUE)
{
if (IsExitThreade)
{
app.ReleaseDispatch ();
delete[] TextInfoArr;
AfxOleTerm();
return 2;;
}
//如果当前窗口是Excel窗口,则不读取
HWND hwnd = FindWindow(TEXT("XLMAIN"),Caption);
HWND Forehwnd =GetForegroundWindow();
if (hwnd == NULL)
{
app.ReleaseDispatch ();
delete[] TextInfoArr;
AfxOleTerm();
::MessageBox(NULL, _T("关联的Excel关闭或有其它Excel打开,关联线程退出!"), _T("警告"), MB_ICONHAND);
return 2;
}
else if(Forehwnd == hwnd)
{
Sleep(2000);
IsContinue = true;
continue;
}
else if(FindWindow(NULL,TEXT("Microsoft Office Excel")) != Forehwnd)
{
if(IsContinue)
{
EnumChildWindows(hwnd,EnumChildProc,NULL);
IsContinue = false;
}
else
{
Sleep(2000);
continue;
}
}
else
{
Sleep(2000);
continue;
}
CLSID clsid;
CLSIDFromProgID(L"Excel.Application", &clsid);
IUnknown *pUnk = NULL;
IDispatch *pRunDisp = NULL;
for(long i=1;i<=5;i++) //做5次尝试
{
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
if(SUCCEEDED(hr))
{
hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pRunDisp);
break;
}
::Sleep(10);
}
if (!pRunDisp)
{
::MessageBox(NULL, _T("没有发现Excel!更新线程退出!"), _T("警告"), MB_ICONHAND);
//如果VectorTableinfo不为空则将其清空
delete[] TextInfoArr;
/* delete[] CurStyleName;*/
AfxOleTerm();
return 2;
}
if (pUnk)pUnk->Release();
//关联Excel
app.AttachDispatch (pRunDisp);
//获取当前激活的workbooks
lpDisp=app.get_Workbooks();
if(lpDisp==NULL)
{
MessageBox(NULL,_T( "没有发现有效的Book!"), _T("TrueTable"),MB_ICONERROR | MB_OK);
app.ReleaseDispatch ();
//如果VectorTableinfo不为空则将其清空
delete[] TextInfoArr;
AfxOleTerm();
return 2;
}
Workbooks.AttachDispatch(lpDisp);
for (int i=0;i<Workbooks.get_Count();i++)
{
bool IsBreak=false;
lpDisp=Workbooks.get_Item(COleVariant((long)i+1));
if(lpDisp==NULL)
continue;
Workbook.AttachDispatch(lpDisp);
CString WorkBookName = Workbook.get_Name();
if(_tcscmp(WorkBookName,BookName) != 0)
continue;
lpDisp = Workbook.get_Worksheets();
if(lpDisp == NULL)
{
app.ReleaseDispatch ();
Workbooks.ReleaseDispatch();
//如果VectorTableinfo不为空则将其清空
delete[] TextInfoArr;
AfxOleTerm();
return 2;
}
Worksheets.AttachDispatch(lpDisp);
for (int j=0;j<Worksheets.get_Count();j++)
{
lpDisp = Worksheets.get_Item(COleVariant((long)j+1));
if(lpDisp == NULL)
{
app.ReleaseDispatch ();
Workbooks.ReleaseDispatch();
Workbook.ReleaseDispatch();
//如果VectorTableinfo不为空则将其清空
delete[] TextInfoArr;
/* delete[] CurStyleName;*/
AfxOleTerm();
return 2;
}
Worksheet.AttachDispatch(lpDisp);
if (_tcscmp(Worksheet.get_Name(),SheetName) != 0)
continue;
ranges.AttachDispatch(Worksheet.get_Cells());
//已经获取了想要的sheet,下一步就需要对表格的内容进行检测和修改了
for (int m=1;m<=TextInfoArr.RowNum * TextInfoArr.ColNum;m++)
{
TextInfo textinfo= TextInfoArr;
iCell.AttachDispatch(ranges.get_Item (COleVariant((long)textinfo.RowNum),COleVariant((long)textinfo.ColNum)).pdispVal );
CString text = iCell.get_Text();
iCell.ReleaseDispatch ();
if (_tcscmp(text,textinfo.text) == 0)
{
continue;
}
if (_tcscmp(_T(""),text) == 0)
{
AcDbObjectId ObjectId = textinfo.Objectid;
AcDbText* pEnt=NULL;
// 锁定文档
acDocManager->lockDocument(curDoc());
Acad::ErrorStatus es = acdbOpenObject((AcDbEntity *&)pEnt,ObjectId,AcDb::kForWrite);
if (es == Acad::eOk)
{
pEnt->erase();
_tcscpy(TextInfoArr.text,text);
TextInfoArr.Objectid = NULL;
pEnt->close();
//更新屏幕显示
// acedUpdateDisplay();
}
else if(es == Acad::eWasErased)
{
// 解锁文档
acDocManager->unlockDocument(curDoc());
ranges.ReleaseDispatch();
Worksheet.ReleaseDispatch ();
Worksheets.ReleaseDispatch ();
Workbook.ReleaseDispatch ();
Workbooks.ReleaseDispatch ();
delete[] TextInfoArr;
::MessageBox(NULL, _T("有手动删除对象,线程退出!"), _T("警告"), MB_ICONHAND);
/* delete[] CurStyleName;*/
AfxOleTerm();
return 2;
}
// 解锁文档
acDocManager->unlockDocument(curDoc());
}
else
{
if (_tcscmp(_T(""),textinfo.text) == 0)
{
_tcscpy(TextInfoArr.text,text);
_tcscpy(textinfo.text,text);
// AcGePoint3d InsertPoint =CCalculation::MiddlePoint(textinfo.LUPoint,textinfo.RBPoint);
AcDbObjectId Obj = CreateMText(textinfo.LUPoint,textinfo.RBPoint,textinfo.text,AcDbObjectId::kNull,textinfo.texthight,textinfo.HorizontalAlignment,ptInsert,Caption,BookName,SheetName,start_row,start_col,row_nums,col_nums);
if (Obj != NULL)
{
TextInfoArr.Objectid = Obj;
// acedUpdateDisplay();
}
}
else
{
AcDbObjectId ObjectId = textinfo.Objectid;
AcDbMText* pEnt=NULL;
// 锁定文档
acDocManager->lockDocument(curDoc());
Acad::ErrorStatus es = acdbOpenObject((AcDbEntity *&)pEnt,ObjectId,AcDb::kForWrite);
AcDbObjectIdStyleObjectId;
if (es == Acad::eOk)
{
pEnt->setContents(text);
// pEnt->adjustAlignment(acdbHostApplicationServices()->workingDatabase());
// pEnt->recordGraphicsModified(true);
// acedUpdateDisplay();
// StyleObjectId = pEnt->textStyle();
// if (StyleID != NULL && StyleObjectId != StyleID)
// {
// pEnt->setTextStyle(StyleID);
// }
// else if (CurSthyleID != NULL && CurSthyleID != StyleObjectId)
// {
// pEnt->setTextStyle(StyleObjectId);
// }
_tcscpy(TextInfoArr.text,text);
pEnt->close();
}
else if(es == Acad::eWasErased)
{
// 解锁文档
acDocManager->unlockDocument(curDoc());
ranges.ReleaseDispatch();
Worksheet.ReleaseDispatch ();
Worksheets.ReleaseDispatch ();
Workbook.ReleaseDispatch ();
Workbooks.ReleaseDispatch ();
delete[] TextInfoArr;
::MessageBox(NULL, _T("有手动删除对象,线程退出!"), _T("警告"), MB_ICONHAND);
/* delete[] CurStyleName;*/
AfxOleTerm();
return 2;
}
// 解锁文档
acDocManager->unlockDocument(curDoc());
}
}
}
ranges.ReleaseDispatch();
Worksheet.ReleaseDispatch ();
Worksheets.ReleaseDispatch ();
Workbook.ReleaseDispatch ();
Workbooks.ReleaseDispatch ();
IsBreak=true;
break;
}
if(IsBreak)
break;
}
}
//释放Dispatch
//如果VectorTableinfo不为空则将其清空
delete[] TextInfoArr;
app.ReleaseDispatch ();
return 1;
}
//动态从Excel读取数据
int DynamicReadFromExcel()
{
//常用变量定义
CApplication app;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange ranges,range;
CRange iCell,UnionRange,UnionRanges;
CRange RangeRow,RangeCol;
CFont0 Font0;
LPDISPATCH lpDisp;
LPDISPATCH Workbooks;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant vResult;
acdbRegApp(_T("ReadeEXCEL"));
//采用MFC方式初始化COM库,程序结束时COM库会自动释放
if(!AfxOleInit())
{
MessageBox(NULL,_T("初始化COM支持库失败!\n无法控制Excel!"),_T("TrueTable"),MB_ICONERROR | MB_OK);
return RTERROR;
}
//关联已经运行的Excel实例
CLSID clsid;
CLSIDFromProgID(L"Excel.Application", &clsid);
IUnknown *pUnk = NULL;
IDispatch *pRunDisp = NULL;
for(long i=1;i<=5;i++) //做5次尝试
{
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
if(SUCCEEDED(hr))
{
hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pRunDisp);
break;
}
::Sleep(10);
}
if (!pRunDisp)
{
::MessageBox(NULL, _T("没有发现Excel!"), _T("TrueTable"), MB_ICONHAND);
return RTERROR;
}
if (pUnk)pUnk->Release();
//关联Excel
app.AttachDispatch (pRunDisp);
CString Caption = app.get_Caption();
//获取当前激活的sheet
lpDisp=app.get_ActiveSheet();
if(lpDisp==NULL)
{
MessageBox(NULL,_T( "没有发现有效的表格!"), _T("TrueTable"),MB_ICONERROR | MB_OK);
app.ReleaseDispatch ();
return RTERROR;
}
sheet.AttachDispatch(lpDisp);
//读取sheet名
lpDisp=sheet.get_Parent();
if(lpDisp==NULL)
{
MessageBox(NULL,_T( "没有发现有效的Book!"), _T("TrueTable"),MB_ICONERROR | MB_OK);
app.ReleaseDispatch ();
sheet.ReleaseDispatch();
return RTERROR;
}
book.AttachDispatch(lpDisp);
CString BookName = book.get_Name();
CString SheetName=sheet.get_Name();
//获取选择区
LPDISPATCH Selectioned = app.get_Selection();
if (Selectioned == NULL)
{
MessageBox(NULL,_T( "没有发现有效的表格!"), _T("TrueTable"),MB_ICONERROR | MB_OK);
app.ReleaseDispatch ();
sheet.ReleaseDispatch();
book.ReleaseDispatch();
return RTERROR;
}
vector<vector<TextInfo>>VectorTableinfo;
//选择的行数、列数:
long row_num,col_num,StartRow,StartCol;
ranges.AttachDispatch(Selectioned);
range.AttachDispatch(ranges.get_Rows());
row_num=range.get_Count();
range.AttachDispatch(ranges.get_Columns());
col_num=range.get_Count();
// ranges.AttachDispatch(Selectioned);
StartRow=range.get_Row(); //起始行
StartCol=range.get_Column(); //起始列
if(col_num<2 && row_num<2) //未选中区域
{
MessageBox(NULL,_T("\n当前表格未选中区域数据!"),_T("TrueTable"),MB_ICONERROR | MB_OK);
app.ReleaseDispatch ();
return RTERROR;
}
else
{
ads_printf(_T("\n表格%s共选中%d行,%d列"),SheetName,row_num,col_num);
}
acutPrintf(_T("\nSheet名称:%s\n"),SheetName);
//得到全部Cells,此时,range是cells的集合
range.AttachDispatch(sheet.get_Cells());
int rowhight,colwide;
CString cstr;
TableLine tableline;
//表格信息二维数组
AcGePoint3d RowStartPoint = ptInsert;
AcGePoint3d ColStartPoint;
for(long i=StartRow;i<StartRow+row_num;i++)
{
vector<TextInfo>Vectortextinfo;
AcGePoint3d ptRowCurren = RowStartPoint;
for(long j=StartCol;j<StartCol+col_num;j++)
{
TextInfo textinfo;
// _tcscpy_s(textinfo.SheetName,SheetName);
// _tcscpy_s(textinfo.BookName,BookName);
AcGePoint3d ptColCurren = ColStartPoint;
//读取单元格文本
iCell.AttachDispatch(range.get_Item (COleVariant(i),COleVariant(j)).pdispVal );
RangeRow.AttachDispatch(iCell.get_EntireRow());
COleVariant hReasult = RangeRow.get_Hidden();
if (hReasult.boolVal == VARIANT_TRUE)
break;
RangeCol.AttachDispatch(iCell.get_EntireColumn());
hReasult = RangeCol.get_Hidden();
if (hReasult.boolVal == VARIANT_TRUE)
continue;
//获取单元格的对齐方式
hReasult.lVal=0;
hReasult=iCell.get_HorizontalAlignment();
if(hReasult.lVal!=0)
{
switch (hReasult.lVal)
{
case 1: //默认
{
textinfo.HorizontalAlignment =Aligndefault;
break;
}
case -4108://居中
{
textinfo.HorizontalAlignment = AlignCenter;
break;
}
case -4131 : //靠左
{
textinfo.HorizontalAlignment = Alignleft;
break;
}
case -4152 : //靠右
{
textinfo.HorizontalAlignment = AlignRight;
break;
}
}
}
//获取单元格所处的行号列号和文字
textinfo.RowNum = i;
textinfo.ColNum = j;
vResult =iCell.get_Text();
cstr=vResult.bstrVal;
_tcscpy(textinfo.text,cstr);
//获取单元格字体
Font0 = iCell.get_Font();
COleVariant varFontInfo;
//字体高度
varFontInfo = Font0.get_Size();
textinfo.texthight = abs(varFontInfo.dblVal * Scale * 0.7);
//单元格行高列宽
COleVariant Var=iCell.get_RowHeight();
//行高
textinfo.rowheiht=Var.dblVal * Scale;
Var=iCell.get_ColumnWidth();
//列宽
textinfo.colwidth = Var.dblVal * 5 * Scale;
vResult = iCell.get_MergeCells();
if (vResult.boolVal == -1)
{ //是合并单元格
textinfo.IsMergeCells = true;
UnionRanges.AttachDispatch(iCell.get_MergeArea());
//合并单元格的行数
UnionRange.AttachDispatch(UnionRanges.get_Rows());
textinfo.UnionRowNums = UnionRange.get_Count();
//合并单元格的列数
UnionRange.AttachDispatch(UnionRanges.get_Columns());
textinfo.UnionColNums = UnionRange.get_Count();
//合并单元格的起始行列号
textinfo.UnionStartRowNum = UnionRanges.get_Row();
textinfo.UnionStartColNum = UnionRanges.get_Column();
//合并单元格的行判断
if (textinfo.UnionRowNums >1)
{
if (textinfo.RowNum == textinfo.UnionStartRowNum)
{ //如果当前单元格的行是合并单元格第一行
textinfo.IsTop = true;
textinfo.IsBottom = false;
textinfo.LUPoint = ptRowCurren;
textinfo.RUPoint = textinfo.LUPoint;
textinfo.RUPoint.x += textinfo.colwidth;
}
else if(textinfo.RowNum == textinfo.UnionStartRowNum + textinfo.UnionRowNums-1)
{ //如果当前单元格的行是合并单元格最后一行
textinfo.IsBottom = true;
textinfo.IsTop = false;
textinfo.LBPoint = ptRowCurren;
textinfo.LBPoint.y -= textinfo.rowheiht;
textinfo.RBPoint = textinfo.LBPoint;
textinfo.RBPoint.x += textinfo.colwidth;
}
else
{ //如果当前单元格的行不是合并单元格第一行也不是最后一行
textinfo.IsBottom = false;
textinfo.IsTop = false;
}
}
else
{
textinfo.IsBottom = true;
textinfo.IsTop = true;
textinfo.LUPoint = ptRowCurren;
textinfo.RUPoint = textinfo.LUPoint;
textinfo.RUPoint.x += textinfo.colwidth;
textinfo.LBPoint = textinfo.LUPoint;
textinfo.LBPoint.y -= textinfo.rowheiht;
textinfo.RBPoint = textinfo.LBPoint;
textinfo.RBPoint.x += textinfo.colwidth;
}
//合并单元格的列判断
if (textinfo.UnionColNums>1)
{
if (textinfo.ColNum == textinfo.UnionStartColNum)
{ //如果当前单元格的行是合并单元格第一列
textinfo.IsLeft = true;
textinfo.IsRight = false;
textinfo.LUPoint = ptRowCurren;
textinfo.LBPoint = textinfo.LUPoint;
textinfo.LBPoint.y -= textinfo.rowheiht;
}
else if(textinfo.ColNum == textinfo.UnionStartColNum + textinfo.UnionColNums-1)
{ //如果当前单元格的行是合并单元格最后一列
textinfo.IsRight = true;
textinfo.IsLeft = false;
textinfo.RUPoint = ptRowCurren;
textinfo.RUPoint.x += textinfo.colwidth;
textinfo.RBPoint = textinfo.RUPoint;
textinfo.RBPoint.y -= textinfo.rowheiht;
}
else
{ //如果当前单元格的行不是合并单元格第一行也不是最后一行
textinfo.IsRight = false;
textinfo.IsLeft = false;
}
}
else
{
textinfo.IsLeft = true;
textinfo.IsRight = true;
textinfo.LUPoint = ptRowCurren;
textinfo.LBPoint = textinfo.LUPoint;
textinfo.LBPoint.y -= textinfo.rowheiht;
textinfo.RUPoint = textinfo.LUPoint;
textinfo.RUPoint.x += textinfo.colwidth;
textinfo.RBPoint = textinfo.RUPoint;
textinfo.RBPoint.y -= textinfo.rowheiht;
}
}
else
{ //不是合并单元格
textinfo.IsMergeCells = false;
textinfo.IsBottom = true;
textinfo.IsTop= true;
textinfo.IsLeft = true;
textinfo.IsRight = true;
textinfo.LUPoint = ptRowCurren;
textinfo.RUPoint = textinfo.LUPoint;
textinfo.RUPoint.x += textinfo.colwidth;
textinfo.LBPoint = textinfo.LUPoint;
textinfo.LBPoint.y -= textinfo.rowheiht;
textinfo.RBPoint = textinfo.LBPoint;
textinfo.RBPoint.x += textinfo.colwidth;
}
Vectortextinfo.push_back(textinfo);
ptRowCurren.x += textinfo.colwidth;
}
if (!Vectortextinfo.empty())
{
VectorTableinfo.push_back(Vectortextinfo);
RowStartPoint.y -= Vectortextinfo.back().rowheiht;
}
}
//释放Dispatch
iCell.ReleaseDispatch ();
range.ReleaseDispatch ();
ranges.ReleaseDispatch();
RangeRow.ReleaseDispatch();
RangeCol.ReleaseDispatch();
UnionRanges.ReleaseDispatch();
UnionRange.ReleaseDispatch();
sheet.ReleaseDispatch ();
sheets.ReleaseDispatch ();
book.ReleaseDispatch ();
books.ReleaseDispatch ();
Font0.ReleaseDispatch();
app.ReleaseDispatch ();
//行表格线列表
//行表格线列表
vector<list<TableLine>>lRow;
vector<list<TableLine>>lCol;
AcGePoint3d StartPoint = ptInsert;
AcGePoint2d ptlu,ptrb;
ptlu.x = ptInsert.x;
ptlu.y=ptInsert.y;
TextInfo * TextInfoArry = new TextInfo.size()+1];
int number =1;
TextInfoArry.RowNum = VectorTableinfo.size() * VectorTableinfo.size();
BookName.Append(_T(","));
BookName.Append(SheetName);
BookName.Append(_T(","));
BookName.Append(Caption);
_tcscpy(TextInfoArry.text,BookName);
TextInfoArry.RowNum=row_num;
TextInfoArry.ColNum=col_num;
TextInfoArry.UnionStartRowNum =StartRow;
TextInfoArry.UnionStartColNum=StartCol;
for (int i = 0;i < VectorTableinfo.size();i++)
{
vector<TextInfo>Vectortextinfo = VectorTableinfo;
list<TableLine> ListTableRowline;
bool iscontinue = false;
TableLine RowTableLine;
for (int j = 0;j<Vectortextinfo.size();j++)
{
TextInfo textinfo = Vectortextinfo;
list<TableLine> ListTableColline;
TableLine RowTableLine,ColTableLine;
if (i > 0)
{ //第一行顶线不用判断
if (textinfo.IsMergeCells)
{ //是合并单元格
if (textinfo.IsTop)
{
if (ListTableRowline.empty() || iscontinue)
{
RowTableLine.TopStartPoint=textinfo.LUPoint;
RowTableLine.TopEndPoint = textinfo.RUPoint;
ListTableRowline.push_back(RowTableLine);
iscontinue = false;
}
else
{
ListTableRowline.back().TopEndPoint=textinfo.RUPoint;
}
}
else
{
iscontinue =true;
}
}
else
{ //不是合并单元格
if (ListTableRowline.empty()|| iscontinue)
{
RowTableLine.TopStartPoint=textinfo.LUPoint;
RowTableLine.TopEndPoint = textinfo.RUPoint;
ListTableRowline.push_back(RowTableLine);
}
else
{
ListTableRowline.back().TopEndPoint=textinfo.RUPoint;
}
}
}
//列向竖线
if (j>0)
{
if (textinfo.IsMergeCells)
{
//是合并单元格
if (textinfo.IsLeft)
{ //单元格有左线
if (i == 0)
{
//当前单元格未创建左线链表
ColTableLine.TopStartPoint=textinfo.LUPoint;
ColTableLine.BottomEndPoint=textinfo.LBPoint;
ColTableLine.IsColcontinue = true;
ListTableColline.push_back(ColTableLine);
lCol.push_back(ListTableColline);
}
else
{ //当前单元格已创建左线链表
if (lCol.back().IsColcontinue)
{ //上段线连续
lCol.back().BottomEndPoint=textinfo.LBPoint;
}
else
{ //上段线不连续
ColTableLine.TopStartPoint=textinfo.LUPoint;
ColTableLine.BottomEndPoint=textinfo.LBPoint;
ColTableLine.IsColcontinue = true;
lCol.push_back(ColTableLine);
}
}
}
else
{
if (i == 0)
{
//当前单元格未创建左线链表
ColTableLine.TopStartPoint=AcGePoint3d(0,0,0);
ColTableLine.BottomEndPoint=AcGePoint3d(0,0,0);
ColTableLine.IsColcontinue = false;
ListTableColline.push_back(ColTableLine);
lCol.push_back(ListTableColline);
}
if (j<lCol.size())
{
// CreateLine(lCol.back().TopStartPoint,lCol.back().BottomEndPoint);
lCol.back().IsColcontinue = false;
}
}
}
else
{//不是合并单元格
if (i == 0)
{
//当前单元格未创建左线链表
ColTableLine.TopStartPoint=textinfo.LUPoint;
ColTableLine.BottomEndPoint=textinfo.LBPoint;
ColTableLine.IsColcontinue = true;
ListTableColline.push_back(ColTableLine);
lCol.push_back(ListTableColline);
}
else
{ //当前单元格已创建左线链表
if (lCol.back().IsColcontinue)
{ //上段线连续
lCol.back().BottomEndPoint=textinfo.LBPoint;
}
else
{ //上段线不连续
ColTableLine.TopStartPoint=textinfo.LUPoint;
ColTableLine.BottomEndPoint=textinfo.LBPoint;
ColTableLine.IsColcontinue = true;
lCol.push_back(ColTableLine);
}
}
}
}
//表的右下角点
if (i+1 == VectorTableinfo.size() && j+1 == Vectortextinfo.size())
{
ptrb.x = textinfo.RBPoint.x;
ptrb.y = textinfo.RBPoint.y;
}
//向CAD中写字
if (_tcscmp(textinfo.text,_T("")) != 0)
{
if (textinfo.IsMergeCells)
{
if (textinfo.ColNum == textinfo.UnionStartColNum && textinfo.RowNum == textinfo.UnionStartRowNum)
{
AcGePoint3d InsertPt1 =textinfo.LUPoint;
AcGePoint3d InsertPt2 = VectorTableinfo.RBPoint;
textinfo.RBPoint = InsertPt2;
// AcGePoint3d InsertPoint =CCalculation::MiddlePoint(InsertPt1,InsertPt2);
AcDbObjectId Obj = CreateMText(InsertPt1,InsertPt2,textinfo.text,AcDbObjectId::kNull,textinfo.texthight,textinfo.HorizontalAlignment,ptInsert,Caption,BookName,SheetName,StartRow,StartCol,row_num,col_num);
if (Obj != NULL)
{
textinfo.Objectid = Obj;
// AcDbText* pEntity;
// if(acdbOpenObject(pEntity,Obj,AcDb::kForRead) == Acad::eOk)
// {
// AcGePoint3d position;
// position = pEntity->position();
// position = pEntity->alignmentPoint();
// pEntity->close();
// }
}
}
}
else
{
// AcGePoint3d InsertPoint =CCalculation::MiddlePoint(textinfo.LUPoint,textinfo.RBPoint);
AcDbObjectId Obj = CreateMText(textinfo.LUPoint,textinfo.RBPoint,textinfo.text,AcDbObjectId::kNull,textinfo.texthight,textinfo.HorizontalAlignment,ptInsert,Caption,BookName,SheetName,StartRow,StartCol,row_num,col_num);
if (Obj != NULL)
{
textinfo.Objectid = Obj;
}
}
}
else
{ //如果单元格为空
if (textinfo.IsMergeCells)
{ //单元格为合并单元格
if (textinfo.ColNum == textinfo.UnionStartColNum && textinfo.RowNum == textinfo.UnionStartRowNum)
{ //设置第一个合并单元格左上角和右下角的坐标
AcGePoint3d InsertPt1 =textinfo.LUPoint;
AcGePoint3d InsertPt2 = VectorTableinfo.RBPoint;
textinfo.RBPoint = InsertPt2;
}
}
}
TextInfoArry=textinfo;
number++;
}
lRow.push_back(ListTableRowline);
ListTableRowline.clear();
}
//画列线
for (int i =0;i<lCol.size();i++)
{
list<TableLine> ListTableColline = lCol;
list<TableLine>::const_iterator Iterator;
for (Iterator=ListTableColline.begin();Iterator != ListTableColline.end();Iterator++)
{
if (Iterator->TopStartPoint != Iterator->BottomEndPoint)
{
CreateLine(Iterator->TopStartPoint,Iterator->BottomEndPoint);
}
}
}
//画行线
for (int i =0;i<lRow.size();i++)
{
list<TableLine> ListTableRowline = lRow;
list<TableLine>::const_iterator Iterator;
for (Iterator=ListTableRowline.begin();Iterator != ListTableRowline.end();Iterator++)
{
if (Iterator->TopStartPoint != Iterator->BottomEndPoint)
{
CreateLine(Iterator->TopStartPoint,Iterator->TopEndPoint);
}
}
}
CreateRectangle(ptlu,ptrb,0);
LPVOID lpParameter =(LPVOID)TextInfoArry;
HANDLE hThreade = ::CreateThread(
NULL,// pointer to security attributes
NULL, // initial thread stack size
ThreadProc, // pointer to thread function
lpParameter, // argument for new thread
NULL, // creation flags
NULL // pointer to receive thread ID
);
::CloseHandle(hThreade);
if (!VectorTableinfo.empty())
{
for (int i=0;i<VectorTableinfo.size();i++)
{
VectorTableinfo.clear();
}
VectorTableinfo.clear();
}
return RTNORM;
}
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CExcelApp : public AcRxArxApp {
public:
CExcelApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
// TODO: Load dependencies here
// You *must* call On_kInitAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
// TODO: Add your initialization code here
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
// TODO: Add your code here
// You *must* call On_kUnloadAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
// TODO: Unload dependencies here
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
// - YCLExcel.ReadExcel command (do not rename)
static void YCLExcelReadExcel(void)
{
// Add your code for command YCLExcel.ReadExcel here
IsExitThreade = true;
acutPrintf(_T("本插件由孤帆制作,仅供试用!用前请保存工作,CAD随时可能崩溃!\n"));
ads_real ScaleBak=1.0;
TCHAR str={0};
TCHAR str1 = {0};
_tcscpy(str, _T("请输入生成表格的比例:<"));
_stprintf(str1,_T("%f"),Scale);
_tcscat(str,str1);
_tcscat(str,_T(">:"));
if (acedGetReal(str,&ScaleBak) == RTNORM)
{
Scale = ScaleBak;
}
ads_point pt;
if(acedGetPoint(NULL,_T("请拾取插入点:\n"),pt)!= RTNORM)
return;
ptInsert.x=pt;
ptInsert.y=pt;
ptInsert.z=pt;
IsExitThreade = false;
DynamicReadFromExcel() ;
}
// - YCLExcel.WriteExcel command (do not rename)
static void YCLExcelWriteExcel(void)
{
// Add your code for command YCLExcel.WriteExcel here
DynamicWriteToExcel();
}
// // - YCLExcel.ReAccess command (do not rename)
// static void YCLExcelReAccess(void)
// {
// // Add your code for command YCLExcel.ReAccess here
// IsExitThreade = true;
// CString Caption,BookName,SheetName;
// int Start_Row,Start_Col,Row_Nums,Col_Nums;
// AcGePoint3d pt;
// struct resbuf *rb; // 结果缓冲区链表
// ads_name ssname;
// rb = acutBuildList(RTDXF0, _T("MTEXT"), // 实体类型
// RTNONE);
// // 选择复合要求的文字
// if(acedSSGet(NULL,NULL,NULL,rb,ssname) != RTNORM)
// {
// acutRelRb(rb);
// AfxMessageBox(_T("选择对象失败!"));
// return;
// }
// long sslength;
// acedSSLength(ssname, &sslength);
// acutRelRb(rb);
// bool IsXData = false;
// for(int i = 0;i < sslength;i++)
// {
// ads_name ent;
// AcDbObjectId objId;
// acedSSName(ssname, i, ent);
// acdbGetObjectId(objId, ent);
// AcDbEntity *pEnt;
// if(acdbOpenAcDbEntity(pEnt, objId, AcDb::kForRead) == Acad::eOk)
// {
// struct resbuf*pXData,*pTemp;
// pXData = pEnt->xData(_T("ReadeEXCEL"));
// pEnt->close();
// if (pXData != NULL) // 如果已经包含扩展数据,就不再添加新的扩展数据
// {
// pTemp = pXData->rbnext;
// Caption = pTemp->resval.rstring;
// pTemp = pTemp->rbnext;
// BookName = pTemp->resval.rstring;
// pTemp = pTemp->rbnext;
// SheetName = pTemp->resval.rstring;
// pTemp = pTemp->rbnext;
// Start_Row = pTemp->resval.rint;
// pTemp = pTemp->rbnext;
// Start_Col = pTemp->resval.rint;
// pTemp = pTemp->rbnext;
// Row_Nums = pTemp->resval.rint;
// pTemp = pTemp->rbnext;
// Col_Nums = pTemp->resval.rint;
// pTemp = pTemp->rbnext;
// pt.x = pTemp->resval.rpoint;
// pt.y = pTemp->resval.rpoint;
// pt.z = pTemp->resval.rpoint;
// acutRelRb(pXData);
// IsXData = true;
// break;
// }
// }
// }
// acedSSFree(ssname);
// if (IsXData)
// {
// ReReadeExcel(Caption,BookName,SheetName,Start_Row,Start_Col,Row_Nums,Col_Nums,pt);
// }
// else
// {
// AfxMessageBox(_T("未找到有效的扩展数据!"));
// }
// }
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CExcelApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CExcelApp, YCLExcel, ReadExcel, ReadExcel, ACRX_CMD_TRANSPARENT, NULL)
ACED_ARXCOMMAND_ENTRY_AUTO(CExcelApp, YCLExcel, WriteExcel, WriteExcel, ACRX_CMD_TRANSPARENT, NULL)
//ACED_ARXCOMMAND_ENTRY_AUTO(CExcelApp, YCLExcel, ReAccess, ReAccess, ACRX_CMD_TRANSPARENT, NULL)
mikewolf2k 发表于 2017-8-18 09:00
从技术的角度来说,很不错。从实用的角度来说,华而不实。
说的是,只是练一下多线程,确实不怎么实用。后面可以用对话框外建线程处理,这样对话框本身就可以不死了 多线程?高级技术,怎么实现的,学习一下。 留个脚印,我能用到这个 这个不错,顶一个,不知道支持2018不 这个厉害,支持一个 有权限设置,下载不了:Q 留个脚印,我能用到这个 学习多线程 很好的材料,学习学习