DwgConverter.cpp
上传用户:hell82222
上传日期:2013-12-19
资源大小:1872k
文件大小:10k
- // KMCAD40-FileSwitch-DwgConverter.cpp-1.0
- // DwgConverter.cpp: implementation of the CDwgConverter class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "FileSwitch.h"
- #include "DwgConverter.h"
- #include "Reader.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- extern CStdioFile g_CensorialFile; //监察转换进度的文件
- extern int g_mode;
- CDwgConverter::CDwgConverter(CCommandInfo* pCommand)
- {
- HWND hWnd=::GetDesktopWindow();
- if(hWnd)
- {
- CWnd* pWnd=CWnd::FromHandle(hWnd);
- if(pWnd)
- {
- m_pTextCalcShow = new CGLShow(pWnd);
- }
- else
- {
- m_pTextCalcShow = new CGLShow;
- }
- }
- else
- {
- m_pTextCalcShow = new CGLShow;
- }
-
- m_pCommand=pCommand;
- m_lShapeIndex=0;
- }
- CDwgConverter::~CDwgConverter()
- {
- delete m_pTextCalcShow;
- }
- BOOL CDwgConverter::BeginConverter()
- {
- m_lShapeIndex=0;
- CWinApp* pApp=::AfxGetApp();
- if(pApp==NULL)
- {
- return FALSE;
- }
- short sfilevar;
- CString filevar;
- CString strTemp=_T("ACAD2000");
- filevar = pApp->GetProfileString(_T("Kmg2Dwg"),_T("FileVar"),strTemp);
- filevar.MakeUpper();
- if (filevar=="ACAD25")
- {
- sfilevar=AD_ACAD25;
- }
- else if (filevar=="ACAD9")
- {
- sfilevar=AD_ACAD9;
- }
- else if (filevar=="ACAD10")
- {
- sfilevar=AD_ACAD10;
- }
- else if (filevar=="ACAD11")
- {
- sfilevar=AD_ACAD11;
- }
- else if (filevar=="ACAD13")
- {
- sfilevar=AD_ACAD13;
- }
- else if (filevar=="ACAD14")
- {
- sfilevar=AD_ACAD14;
- }
- else
- {
- sfilevar=AD_ACAD2000;
- filevar="ACAD2000";
- }
- _WRITE(str.Format("tip:转换文件类型为%s版本的文件",filevar));
- pApp->WriteProfileString(_T("Kmg2Dwg"),_T("FileVar"),filevar);
- if(!allocateadptrs())
- {
- _WRITE(str="内存不足,无法启动转换服务");
- return FALSE;
- }
- //AD_DB_HANDLE adNewFile (AD_OBJHANDLE *stockobjhandle, PAD_CLASS classes, short numClasses, short version);
- if((m_dwghandle=adNewFile(NULL,NULL,0,sfilevar))==NULL)
- {
- _ERROR;
- return FALSE;
- }
- SetModelDataVassel();
- /*
- adSetDefaultShapefile(&adtb->shptb);
- strcpy(adtb->shptb.name,"文字样式");
- adtb->shptb.height=0.2;
- double m_dtextposmodify=0.15;
- adtb->shptb.width=1.0/(1+2*m_dtextposmodify);
- adtb->shptb.angle=0.0;
- adtb->shptb.flag2=0;
- adtb->shptb.lastshapeheight= 5 ;
- strcpy(adtb->shptb.file,"SimSun.ttf");
- {
- adtb->shptb.xdblob=adCreateBlob();
- PAD_BLOB_CTRL aa;
- aa=adStartBlobWrite(adtb->shptb.xdblob);
- //extern short ADCDECL adWriteExtendeddata _((PAD_BLOB_CTRL bcptr,PAD_XD adxd));
- adxd->xddxfnumber=1001;
- //extern short ADCDECL adStartRegappGet _((AD_DB_HANDLE handle));
- adStartRegappGet(m_dwghandle);
- //extern short ADCDECL adGetRegapp _((AD_DB_HANDLE handle,PAD_APP adapp));
- {
- AD_APP adapp;
- if(adGetRegapp(m_dwghandle,&adapp)!=-1)
- {
- adHancpy(adxd->xddata.xdappobjhandle,adapp.objhandle);
- }
- }
- //adxd->xddxfnumber=1001;
- adWriteExtendeddata(aa,adxd);
- adxd->xddxfnumber=1000;
- strcpy(adxd->xddata.xdstring,"SimSun");
- adWriteExtendeddata(aa,adxd);
- adxd->xddxfnumber=1071;
- adxd->xddata.xdlong=0x8602;
- adWriteExtendeddata(aa,adxd);
- adEndBlobWrite(aa);
- }
- adGenerateObjhandle(m_dwghandle,adtb->shptb.objhandle);
- adHancpy(m_Shape, adtb->shptb.objhandle);
- adAddShapefile(m_dwghandle,&adtb->shptb);
- */
- return TRUE;
- }
- void CDwgConverter::ConverterKmLegend(double x0,double y0,double dx,double dy)
- {
- //获得头指针,并设置AutoCAD的系统变量
- PAD_DWGHDR dwghd=adHeaderPointer(m_dwghandle);
- // dwghd->textsize = 5;
- // adHancpy(dwghd->curtextstyleobjhandle, m_Shape);
- dwghd->extmin[0] = x0 ;
- dwghd->extmin[1] = y0 ;
- dwghd->extmin[2] = 0.0 ;
- dwghd->extmax[0] = x0 + dx;
- dwghd->extmax[1] = y0 + dy;
- dwghd->extmax[2] = 0.0;
- dwghd->pextmin[0] = 0.0;
- dwghd->pextmin[1] = 0.0;
- dwghd->pextmin[2] = 0.0;
- dwghd->pextmax[0] = dx;
- dwghd->pextmax[1] = dy;
- dwghd->pextmax[2] = 0.0;
- dwghd->viewsize = dx>dy ? dx:dy;
- dwghd->viewctr[0] = x0 + dx * 0.5;
- dwghd->viewctr[1] = y0 + dy * 0.5;
- dwghd->plimmax[0] = dx;
- dwghd->plimmax[1] = dy;
- //获得当前的视口并修改
- //short adStartVportGet (AD_DB_HANDLE handle);
- if(adStartVportGet(m_dwghandle)!=0)
- {
- AD_VPORT vport;
- for (short i=0; i<(short)adNumVports(m_dwghandle); i++)
- {
- if(i==0)
- {
- adGetVport(m_dwghandle,&vport);
- vport.center[0]=x0 + dx * 0.5;
- vport.center[1]=y0 + dy * 0.5;
- vport.viewheight=dy;
- try
- {
- vport.aspect=dx/dy;
- }
- catch(...)
- {
- vport.aspect=1.0;
- }
- }
- }
- if(i!=0)
- {
- adReplaceVport(m_dwghandle,&vport);
- }
- }
- dwghd->tilemode = 1;//设置为模型空间
- // extern short ADCDECL adAsciiCodePageToShort _((char *codepagestr,short *index));
- short ccodepage;
- if(adAsciiCodePageToShort("ANSI_936",&ccodepage )!=0)
- {
- dwghd->dwgcodepage = ccodepage;
- }
- else
- {
- dwghd->dwgcodepage = (short)0x001f;//31: "gb2312"
- }
- }
- BOOL CDwgConverter::EndConverter()
- {
- CWinApp* pApp=::AfxGetApp();
- if(pApp==NULL)
- {
- adCloseFile(m_dwghandle);
- freeadptrs();
- return FALSE;
- }
- short sfiletype=AD_DWG;
- CString filetype=m_pCommand->m_strAimName;
- if(filetype.GetLength()>=3)
- {
- filetype=filetype.Right(3);
- filetype.MakeLower();
- if(filetype=="dxf")
- {
- sfiletype=AD_DXF;
- }
- }
- /* CString strTemp=_T("DWG");
- filetype = pApp->GetProfileString(_T("Kmg2Dwg"),_T("FileType"),strTemp);
- filetype.MakeUpper();
- if(filetype=="DXF")
- {
- sfiletype=AD_DXF;
- }
- else
- {
- sfiletype=AD_DWG;
- filetype=="DWG";
- }
- _WRITE(str.Format("tip:转换文件类型为%s文件",filetype));
- pApp->WriteProfileString(_T("Kmg2Dwg"),_T("FileType"),filetype);*/
- short sfilevar;
- CString filevar;
- CString strTemp=_T("ACAD2000");
- filevar = pApp->GetProfileString(_T("Kmg2Dwg"),_T("FileVar"),strTemp);
- filevar.MakeUpper();
- if (filevar=="ACAD25")
- {
- sfilevar=AD_ACAD25;
- }
- else if (filevar=="ACAD9")
- {
- sfilevar=AD_ACAD9;
- }
- else if (filevar=="ACAD10")
- {
- sfilevar=AD_ACAD10;
- }
- else if (filevar=="ACAD11")
- {
- sfilevar=AD_ACAD11;
- }
- else if (filevar=="ACAD13")
- {
- sfilevar=AD_ACAD13;
- }
- else if (filevar=="ACAD14")
- {
- sfilevar=AD_ACAD14;
- }
- else
- {
- sfilevar=AD_ACAD2000;
- filevar="ACAD2000";
- }
- _WRITE(str.Format("tip:转换文件类型为%s版本的文件",filevar));
- pApp->WriteProfileString(_T("Kmg2Dwg"),_T("FileVar"),filevar);
-
- //short adSaveFile (AD_DB_HANDLE handle, void *name, char filetype, short version, short dxfnegz, short dxfdecprec, short dxfwritezeroes, char r12dxfvbls);
- sReturnValue=adSaveFile(m_dwghandle,(void*)(LPCTSTR)m_pCommand->m_strAimName,(char)sfiletype,sfilevar,0,6,1,1);
- adCloseFile(m_dwghandle);
- if(sReturnValue!=1)
- {
- _ERROR;
- freeadptrs();
- return FALSE;
- }
- freeadptrs();
- return TRUE;
- }
- BOOL CDwgConverter::allocateadptrs(void)
- {
- if((adhd=(PAD_DWGHDR)malloc(sizeof(AD_DWGHDR)))!=NULL)
- {
- if((adenhd=(PAD_ENT_HDR)malloc(sizeof(AD_ENT_HDR)))!=NULL)
- {
- if((aden=(PAD_ENT)malloc(sizeof(AD_ENT)))!=NULL)
- {
- if((adtb=(PAD_TB)malloc(sizeof(AD_TB)))!=NULL)
- {
- if((adxd=(PAD_XD)malloc(sizeof(AD_XD)))!=NULL)
- {
- if((adobhd=(PAD_OBJ_HDR)malloc(sizeof(AD_OBJ_HDR)))!=NULL)
- {
- if((adob=(PAD_OBJ)malloc(sizeof(AD_OBJ)))!=NULL)
- {
- return TRUE;
- }
- free(adobhd);
- }
- free(adxd);
- }
- free(adtb);
- }
- free(aden);
- }
- free(adenhd);
- }
- free(adhd);
- }
- return FALSE;
- }
- void CDwgConverter::freeadptrs(void)
- {
- free(adxd);
- free(adtb);
- free(aden);
- free(adenhd);
- free(adhd);
- free(adobhd);
- free(adob);
- }
- void CDwgConverter::WriteEnthead(short scolor,short swidth,double dscale,AD_OBJHANDLE& hltype,AD_OBJHANDLE& hlayer)
- {
- m_enthead.scolor=scolor;
- m_enthead.swidth=swidth;
- m_enthead.dscale=dscale;
- adHancpy(m_enthead.hltype,hltype);
- adHancpy(m_enthead.hlayer,hlayer);
- }
- void CDwgConverter::SetBlockDataVassel(AD_OBJHANDLE handle)
- {
- //第三步:从数据库的块链表中取出AD_BLKH实体.(*请理解为值加入而非地址加入)
- adSeekBlockheader(m_dwghandle,handle,&adtb->blkh);
- m_DataVassel=adtb->blkh.entitylist;
- if(m_DataVassel==NULL)
- {
- SetModelDataVassel();
- }
- }
- void CDwgConverter::SetModelDataVassel()
- {
- AD_OBJHANDLE *ohptr=adShadowObjhandlePtr(m_dwghandle);
- m_DataVassel=adEntityList(m_dwghandle,ohptr[MSPACEBLKSHADOW]);
- }
- void CDwgConverter::AddEndBlkSign()
- {
- //第六步:构造AD_ENT_ENDBLK实体,并添加到AD_BLKH的实体链中.
- adenhd->enttype=AD_ENT_ENDBLK;
- adSetEntityDefaults(m_dwghandle,adenhd,aden);
- adGenerateObjhandle(m_dwghandle,adenhd->enthandle);
- AD_OBJHANDLE *ohptr=adShadowObjhandlePtr(m_dwghandle);
- //CONTINUOUSLTPSHADOW
- //extern short ADCDECL adStartLayerGet _((AD_DB_HANDLE handle));
- if(adStartLayerGet(m_dwghandle)!=1)
- {
- _ERROR;
- }
- //extern short ADCDECL adGetLayer _((AD_DB_HANDLE handle,PAD_LAY adlay));
- if(!adGetLayer(m_dwghandle,&adtb->lay))
- {
- _ERROR;
- }
- WriteEnthead(AD_COLOR_BYLAYER,-1,1.0,ohptr[BYLAYERLTPSHADOW],adtb->lay.objhandle);
- AddObject();
- SetModelDataVassel();
- }
- void CDwgConverter::AddBegBlkSign(double x0,double y0,double z0)
- {
- //第四步:构造AD_ENT_BLOCK实体.并添加到AD_BLKH的实体链中.
- //Short adAddEntityToList _((AD_DB_HANDLE,AD_VMADDR,PAD_ENT_HDR,PAD_ENT));
- adenhd->enttype=AD_ENT_BLOCK;
- adSetEntityDefaults(m_dwghandle,adenhd,aden);
- adGenerateObjhandle(m_dwghandle,adenhd->enthandle);
- aden->block.base[0]=x0;
- aden->block.base[1]=y0;
- aden->block.base[2]=z0;
- AD_OBJHANDLE *ohptr=adShadowObjhandlePtr(m_dwghandle);
- //CONTINUOUSLTPSHADOW
- //extern short ADCDECL adStartLayerGet _((AD_DB_HANDLE handle));
- if(adStartLayerGet(m_dwghandle)!=1)
- {
- _ERROR;
- }
- //extern short ADCDECL adGetLayer _((AD_DB_HANDLE handle,PAD_LAY adlay));
- if(!adGetLayer(m_dwghandle,&adtb->lay))
- {
- _ERROR;
- }
- WriteEnthead(AD_COLOR_BYLAYER,-1,1.0,ohptr[BYLAYERLTPSHADOW],adtb->lay.objhandle);
- AddObject();
- //第五步:构造块定义中的实体并添加到块定义中.
- //第六步:构造AD_ENT_ENDBLK实体,并添加到AD_BLKH的实体链中.
- // 第五步在CKmgReader::processentity(pData,pObj)中
- // 第六步在函数void CDwgConverter::AddEndBlkSign()中
- }