www.pudn.com > ReadCADPic.rar > dd.cpp, change:2005-08-08,size:13922b


// dd.cpp : Initialization functions 
#include "StdAfx.h" 
#include "StdArx.h" 
#include "resource.h" 
#include <afxdllx.h> 
#include "dbsymtb.h" 
#include "migrtion.h" 
#include "dbxline.h" 
//include "math.h" 
#include "dbents.h" 
//#include "dbidmap.h" 
#include "dbgroup.h" 
#include "dbxrecrd.h" 
 
//#define THJS_MSG_PICKUP_OK	WM_USER+1//WM_USER=0X0400 
//用户自定义过程说明 
AcDbObjectId acdbGetNewTfId(); 
Acad::ErrorStatus acdbGetNewTFDate(AcDbObjectId objId,double *Height,double *Width,CString *Scale,CString *Name,CString *Number); 
AcDbObjectId acdbGetOldTfBTLId(); 
Acad::ErrorStatus acdbGetOldTFDate(AcDbObjectId objId,CString *Scale,CString *Name,CString *Number); 
Acad::ErrorStatus acdbGetOldTFSize(double *Height,double *Width); 
void result(CString strRtn); 
double getScale(CString strScale); 
CString getTFSize(double height,double width); 
void GetTFData(); 
//////////////////////////////////////////////////////////////////////////// 
// 
// Define the sole extension module object. 
extern "C" HWND adsw_acadMainWnd(); 
AC_IMPLEMENT_EXTENSION_MODULE(theArxDll); 
 
//应用程序加载时运行的初始化 
static void initApp() 
{ 
	CAcModuleResourceOverride resOverride; 
	//无对话框 
	acedRegCmds->addCommand( 
		"TF", 
		"dd", 
		"dd", 
		ACRX_CMD_MODAL,//命令模式(透明或模态) 
		GetTFData); 
 
} 
//应用程序缷载时运行 
static void unloadApp() 
{ 
	//在此处进行其它清理工作 
	acedRegCmds->removeGroup("TF"); 
} 
//应用程序入口点 
extern "C" AcRx::AppRetCode  
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt) 
{ 
	switch (msg) { 
	case AcRx::kInitAppMsg: 
		// Comment out the following line if your 
		// application should be locked into memory 
		acrxDynamicLinker->unlockApplication(pkt); 
		acrxDynamicLinker->registerAppMDIAware(pkt); 
		initApp(); 
		break; 
	case AcRx::kUnloadAppMsg: 
		unloadApp(); 
		break; 
	case AcRx::kInitDialogMsg: 
		break; 
	default: 
		break; 
	} 
	return AcRx::kRetOK; 
} 
//新的DllMain() 
extern "C" int APIENTRY 
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) 
{ 
	// 
	UNREFERENCED_PARAMETER(lpReserved); 
	if (dwReason == DLL_PROCESS_ATTACH) 
	{ 
		theArxDll.AttachInstance(hInstance); 
	} else if (dwReason == DLL_PROCESS_DETACH) { 
		// Terminate the library before destructors are called 
		theArxDll.DetachInstance(); 
	} 
	//userMsg=RegisterWindowMessage("THJS_MSG_PICKUP_OK"); 
	//if(userMsg==0) userMsg+=1; 
	return 1;    // ok 
} 
//用户自定义过程 
 
void GetTFData(){ 
	 
	//************************************** 
	//要得到的结果 
	//************************************** 
	double tfWidth=0;				//图幅宽 
	double tfHeight=0;				//图幅高 
	CString strName,strNumber,strScale,strSize; 
	//************************************** 
	double dscale; 
	AcDbObjectId idNewTF,idOldTFBTL; 
	idNewTF=acdbGetNewTfId(); 
	if(idNewTF!=AcDbObjectId::kNull){	//新图幅 
		//读出图幅的数据 
		//TO DO.... 
		if(acdbGetNewTFDate(idNewTF,&tfHeight,&tfWidth,&strScale,&strName,&strNumber)!=Acad::eOk){ 
			//返回错误 
			result("ERROR"); 
			return; 
		} 
	} 
	else{								//旧图幅 
		idOldTFBTL=acdbGetOldTfBTLId(); 
		if(idOldTFBTL!=AcDbObjectId::kNull){	//找到标题栏 
			//读出标题栏的信息(比例,名称,代号) 
			if(acdbGetOldTFDate(idOldTFBTL,&strScale,&strName,&strNumber)==Acad::eOk){ 
				//继续读高和宽的值 
				if(acdbGetOldTFSize(&tfHeight,&tfWidth)!=Acad::eOk){ 
					//返回错误 
					result("ERROR"); 
					return; 
				} 
			} 
			else{ 
				//返回错误 
				result("ERROR"); 
				return; 
			} 
		} 
		else{									//找不到标题栏 
			//返回错误 
			result("ERROR"); 
			return; 
		} 
	} 
	dscale=getScale(strScale); 
	if(dscale!=0){ 
		tfHeight=tfHeight/dscale; 
		tfWidth=tfWidth/dscale; 
	} 
	else{ 
		result("ERROR"); 
		return; 
	} 
	strSize=getTFSize(tfHeight,tfWidth); 
	//MessageBox(adsw_acadMainWnd(),strName+"::"+strNumber+"::"+strSize,"acad",MB_OK); 
	result(strName+"::"+strNumber+"::"+strSize); 
	return; 
} 
//TO DO 
//**************函数定义区—开始******************** 
//函数名:acdbGetNewTfId 
//返回图幅的AcDbObjectId 
//如果没有找到返回AcDbObjectId::kNull 
AcDbObjectId acdbGetNewTfId() 
{ 
	int status; 
	char strBlockTableRecordName[50]; 
	AcDbObjectId objid;		//如果找到则返回此ID 
	bool bfind=false;		//找到标记 
	if(acdbCurDwg()->tilemode()==Adesk::kFalse){ 
		struct resbuf rb; 
		ads_getvar("cvport",&rb); 
		if(rb.resval.rint==1) strcpy(strBlockTableRecordName,ACDB_PAPER_SPACE); 
		else strcpy(strBlockTableRecordName,ACDB_MODEL_SPACE); 
	} 
	else strcpy(strBlockTableRecordName,ACDB_MODEL_SPACE); 
	//得到图块表记录 
	AcDbBlockTable *pBlockTable; 
	AcDbBlockTableRecord *pBlockTableRecord; 
	status=acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForRead); 
	if(status!= Acad::eOk){ 
		return AcDbObjectId::kNull; 
	} 
	status=pBlockTable->getAt(strBlockTableRecordName,pBlockTableRecord,AcDb::kForRead); 
	if(status!= Acad::eOk){ 
		pBlockTable->close(); 
		return AcDbObjectId::kNull; 
	} 
	pBlockTable->close(); 
	AcDbBlockTableRecordIterator *pBlockIterator;			//新建一个过滤器 
	status=pBlockTableRecord->newIterator(pBlockIterator);	// 
	if(status!= Acad::eOk){ 
		pBlockTableRecord->close(); 
		return AcDbObjectId::kNull; 
	} 
	for(;!pBlockIterator->done();pBlockIterator->step()){//过滤 
		AcDbEntity *pEntity; 
		pBlockIterator->getEntity(pEntity,AcDb::kForRead); 
		const char* pCname=pEntity->isA()->name(); 
		if(!strcmp(pCname,"THJSTHFrame")){ 
			objid=pEntity->objectId(); 
			bfind=true; 
		} 
		if(bfind){ 
			pEntity->close(); 
			break; 
		} 
		else pEntity->close(); 
	} 
	delete pBlockIterator; 
	pBlockTableRecord->close(); 
	if(bfind) return objid; 
	else return AcDbObjectId::kNull; 
} 
//返回新图幅的信息 
// 
// 
Acad::ErrorStatus acdbGetNewTFDate(AcDbObjectId objId,double *Height,double *Width,CString *Scale,CString *Name,CString *Number){ 
	//*****要返回的数据**** 
	double dHeight; 
	double dWidth; 
	CString strScale; 
	CString strName; 
	CString strNumber; 
	//********************* 
	struct resbuf *ed,*cb; 
	ads_name tfname; 
	if(acdbGetAdsName(tfname,objId)!=Acad::eOk){ 
		return Acad::eNullObjectId; 
	} 
	ed=acdbEntGet(tfname); 
	//读出新图幅中信息 
	for(cb=ed;cb!=NULL;cb=cb->rbnext){ 
		if(cb->restype==AcDb::kDxfXReal) dHeight=cb->resval.rreal; 
		if(cb->restype==AcDb::kDxfXReal + 1) dWidth=cb->resval.rreal; 
		if(cb->restype==AcDb::kDxfXTextString + 1) strName=cb->resval.rstring; 
		if(cb->restype==AcDb::kDxfXTextString + 2) strNumber=cb->resval.rstring; 
	} 
	acutRelRb(ed); 
	strScale="1:1";			//对新图幅来说,dHeight和dWidth为标准值,所以比例为1:1 
	//操作完成 
	//设置返回值 
	*Height=dHeight; 
	*Width=dWidth; 
	*Scale=strScale; 
	*Name=strName; 
	*Number=strNumber; 
	return Acad::eOk;		//返回成功信息 
} 
//返回旧图幅标题栏的ACDBOBJECTID 
AcDbObjectId acdbGetOldTfBTLId() 
{ 
	int status; 
	char strBlockTableRecordName[50]; 
	AcDbObjectId objid,tempid;		//如果找到则返回此ID 
	bool bfind=false;		//找到标记 
	if(acdbCurDwg()->tilemode()==Adesk::kFalse){ 
		struct resbuf rb; 
		ads_getvar("cvport",&rb); 
		if(rb.resval.rint==1) strcpy(strBlockTableRecordName,ACDB_PAPER_SPACE); 
		else strcpy(strBlockTableRecordName,ACDB_MODEL_SPACE); 
	} 
	else strcpy(strBlockTableRecordName,ACDB_MODEL_SPACE); 
	//得到图块表记录 
	AcDbBlockTable *pBlockTable; 
	AcDbBlockTableRecord *pBlockTableRecord; 
	status=acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForRead); 
	if(status!= Acad::eOk){ 
		return AcDbObjectId::kNull; 
	} 
	status=pBlockTable->getAt(strBlockTableRecordName,pBlockTableRecord,AcDb::kForRead); 
	if(status!= Acad::eOk){ 
		pBlockTable->close(); 
		return AcDbObjectId::kNull; 
	} 
	pBlockTable->close(); 
	AcDbBlockReference *pBlkRef;//定义一个块参照指针 
	AcDbBlockTableRecord *pBlkRefOfBlkRec;//pBlkRef所依赖的图块表记录 
	AcDbBlockTableRecordIterator *pBlockIterator;			//新建一个过滤器 
	status=pBlockTableRecord->newIterator(pBlockIterator);	// 
	if(status!= Acad::eOk){ 
		pBlockTableRecord->close(); 
		return AcDbObjectId::kNull; 
	} 
	for(;!pBlockIterator->done();pBlockIterator->step()){//过滤 
		AcDbEntity *pEntity; 
		pBlockIterator->getEntity(pEntity,AcDb::kForRead); 
		const char* pCname=pEntity->isA()->name(); 
		if(!strcmp(pCname,"AcDbBlockReference")){ 
			objid=pEntity->objectId(); 
			pBlkRef=(AcDbBlockReference *)pEntity;	//因为pEnt实际就是一个块参照指针 
			tempid=pBlkRef->blockTableRecord();	//pBlkRef所依赖的图块表记录ID 
			acdbOpenObject(pBlkRefOfBlkRec,tempid,AcDb::kForRead);//打开pBlkRef所依赖的图块表记录 
			pBlkRefOfBlkRec->getName(pCname); 
			pBlkRefOfBlkRec->close();			//关闭pBlkRef所依赖的图块表记录 
			if(!strcmp(pCname,"BTL") || !strcmp(pCname,"LBTL") || !strcmp(pCname,"ZBTL") || !strcmp(pCname,"BTL-L") || !strcmp(pCname,"BTL-Z")){ 
				bfind=true; 
			} 
		} 
		if(bfind){ 
			pEntity->close(); 
			break; 
		} 
		else pEntity->close(); 
	} 
	delete pBlockIterator; 
	pBlockTableRecord->close(); 
	if(bfind) return objid; 
	else return AcDbObjectId::kNull; 
} 
//得到旧图幅标题栏的数据(比例,名称,代号) 
Acad::ErrorStatus acdbGetOldTFDate(AcDbObjectId objId,CString *Scale,CString *Name,CString *Number){ 
	//*****要返回的数据**** 
	CString strScale="NULL"; 
	CString strName="NULL"; 
	CString strNumber="NULL"; 
	//********************* 
	Acad::ErrorStatus status; 
	AcDbBlockReference *pBlkRef;//定义一个块参照指针 
	AcDbObjectIterator *pIterator;	//对象过滤器 
	AcDbAttribute * pAttrib;		//属性指针 
	AcDbObjectId attId;				//属性ID 
	status=acdbOpenObject(pBlkRef,objId,AcDb::kForRead); 
	if(status!=Acad::eOk){//打开pBlkRef所依赖的图块表记录 
		return status; 
	} 
	pIterator=pBlkRef->attributeIterator(); 
	for(;!pIterator->done();pIterator->step()){ 
		attId=pIterator->objectId(); 
		acdbOpenObject(pAttrib,attId,AcDb::kForRead); 
		if(!strcmp(pAttrib->tag(),"图名")) strName=pAttrib->textString(); 
		if(!strcmp(pAttrib->tag(),"图号")) strNumber=pAttrib->textString(); 
		if(!strcmp(pAttrib->tag(),"比例")) strScale=pAttrib->textString(); 
		if(!strcmp(pAttrib->tag(),"KYS_MC")) strName=pAttrib->textString(); 
		if(!strcmp(pAttrib->tag(),"KYS_DH")) strNumber=pAttrib->textString(); 
		if(!strcmp(pAttrib->tag(),"KYS_BL")) strScale=pAttrib->textString(); 
		pAttrib->close(); 
	} 
	delete pIterator; 
	pBlkRef->close(); 
	//操作完成 
	//设置返回值 
	*Scale=strScale; 
	*Name=strName; 
	*Number=strNumber; 
	return Acad::eOk;		//返回成功信息 
} 
//得到旧图幅的尺寸 
Acad::ErrorStatus acdbGetOldTFSize(double *Height,double *Width){ 
	//*****要返回的数据**** 
	double dHeight=0; 
	double dWidth=0; 
	//********************* 
	Acad::ErrorStatus status; 
	 
	long i,SetLength; 
	//新建一个选择集 
	ads_name ssname,entname; 
	struct resbuf *eb; 
	eb=acutBuildList(-4,"<and",100,"AcDbBlockReference",2,"LC_TF",-4,"and>",0); 
	if(acedSSGet("X",NULL,NULL,eb,ssname)==RTERROR){ 
		acedPrompt("err"); 
	} 
	acutRelRb(eb); 
	acedSSLength(ssname,&SetLength); 
	for(i=0;i<SetLength;i++){ 
		acedSSName(ssname,i,entname); 
		acedCommand(RTSTR, "explode",RTENAME,entname,0); 
	} 
	//新建一个选择集 
	eb=acutBuildList(-4,"<or",RTDXF0,"LINE",RTDXF0,"LWPOLYLINE",RTDXF0,"POLYLINE",-4,"or>",0); 
	if(acedSSGet("X",NULL,NULL,eb,ssname)==RTERROR){ 
		acedPrompt("err"); 
	} 
	acutRelRb(eb); 
	// 
	AcDbBlockTableRecord *pBlockTableRec=new AcDbBlockTableRecord(); 
	status=pBlockTableRec->setName("XXXXXXXX"); 
	if(status!= Acad::eOk){ 
		pBlockTableRec->close(); 
		return status; 
	} 
	//取图块表 
	AcDbBlockTable *pBlockTable; 
	status=acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForWrite); 
	if(status!= Acad::eOk){ 
		pBlockTableRec->close();		//"XXXXXXXX"图块表记录已打开,关闭它! 
		return status; 
	} 
	//添加新的图块表记录 
	AcDbObjectId blockTableRecordId; 
	status=pBlockTable->add(blockTableRecordId,pBlockTableRec); 
	if(status!= Acad::eOk){ 
		pBlockTableRec->close();		//"XXXXXXXX"图块表记录已打开,关闭它! 
		pBlockTable->close();			//图块表已打开,关闭它! 
		return Acad::eNotImplementedYet; 
	} 
	pBlockTable->close();				//图块表已打开,关闭它! 
	//添加实体 
	AcDbObjectIdArray objIds; 
	AcDbObjectId objId; 
	 
	acedSSLength(ssname,&SetLength); 
	for(i=0;i<SetLength;i++){ 
		acedSSName(ssname,i,entname); 
		acdbGetObjectId(objId,entname); 
		objIds.append(objId); 
	} 
	pBlockTableRec->assumeOwnershipOf(objIds); 
	 
	pBlockTableRec->close();		//"XXXXXXXX"图块表记录已打开,关闭它! 
	AcGePoint3d pnt,minpnt,maxpnt; 
	pnt.x=0.0; 
	pnt.y=0.0; 
	pnt.z=0.0; 
 
	AcDbBlockReference *pBlkRef= new AcDbBlockReference(pnt,blockTableRecordId); 
	AcDbExtents extents; 
	pBlkRef->geomExtentsBestFit(extents,AcGeMatrix3d::kIdentity); 
	pBlkRef->close(); 
	minpnt=extents.minPoint(); 
	maxpnt=extents.maxPoint(); 
	dHeight=maxpnt.y-minpnt.y; 
	dWidth=maxpnt.x-minpnt.x; 
	*Height=dHeight; 
	*Width=dWidth; 
	return Acad::eOk;		//返回成功信息 
} 
 
double getScale(CString strScale){ 
	double fz; 
	double fm; 
	CString strFz,strFm; 
	int index; 
	index=strScale.Find(':'); 
	if(index==-1) index=strScale.Find(":"); 
	if(index==-1) index=strScale.Find(';'); 
	if(index==-1) return 0; 
	if(strScale.Find(":")!=-1){ 
		strFz=strScale.Right(strScale.GetLength()-index-2); 
		strFm=strScale.Left(index); 
	} 
	else{ 
		strFz=strScale.Right(strScale.GetLength()-index-1); 
		strFm=strScale.Left(index); 
	} 
	fz=atof((LPCTSTR)strFz); 
	fm=atof((LPCTSTR)strFm); 
	if(fm==0) return 0; 
	else return (fz/fm); 
} 
CString getTFSize(double height,double width){ 
	double x,y; 
	if(height>width){ 
		x=height; 
		y=width; 
	} 
	else{ 
		x=width; 
		y=height; 
	} 
	if((y-210.0)<0.1){ 
		if((x-297.0)<0.1) return "A4"; 
		else return "A4+"; 
	} 
	if((y-297.0)<0.1){ 
		if((x-420.0)<0.1) return "A3"; 
		else return "A3+"; 
	} 
	if((y-420.0)<0.1){ 
		if((x-594.0)<0.1) return "A2"; 
		else return "A2+"; 
	} 
	if((y-594.0)<0.1){ 
		if((x-841.0)<0.1) return "A1"; 
		else return "A1+"; 
	} 
	if((y-841.0)<0.1){ 
		if((x-1189.0)<0.1) return "A0"; 
		else return "A0+"; 
	} 
	return "XXXX"; 
} 
void result(CString strRtn){ 
	char rtn[132]="error"; 
	strcpy(rtn,(LPCTSTR)strRtn); 
	acedCommand(RTSTR,"USERS3",RTSTR,rtn,0); 
}