Processfile5.cpp
上传用户:hell82222
上传日期:2013-12-19
资源大小:1872k
文件大小:17k
- #include "stdafx.h"
- #include "FileSwitch.h"
- #include "DwgReader.h"
- #include "Reader.h"
- #include "blockdeffunc.h"
- #include "blockdeffunc2.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern CStdioFile g_CensorialFile; //监察转换进度的文件
- extern int g_mode;
- //------------------多行文字转换为单行文字
- /* Pull the string out of the mtext entity. */
- short CDwgReader::preprocess_ExtractMTextString(PAD_MTEXT mtext, unsigned char **cptr)
- {
- unsigned short len;
- PAD_BLOB_CTRL bcptr;
-
- if (mtext->ldblob != NULL)
- {
- bcptr = adStartBlobRead(mtext->ldblob);
- len = (unsigned short)adBlobSize(bcptr);
- *cptr = (unsigned char*)malloc(len + 1);
- if (*cptr == NULL)
- {
- return(0);
- }
- adReadBlobBytes(bcptr, *cptr, len);
- (*cptr)[len] = 0;
- adEndBlobRead(bcptr);
- }
- else
- {
- *cptr = (unsigned char*)malloc(strlen(mtext->textstr) + 1);
- if (*cptr == NULL)
- {
- return(0);
- }
- strcpy((char *)*cptr, mtext->textstr);
- }
- return(1);
- }
- void CDwgReader::preprocess_AddWord(AD_DB_HANDLE dbh,PAD_MTEXT_WORD pWord,PAD_TDATA pTData,PAD_SHPTB pShp,PAD_BLOB_CTRL bcptr,int type,double height)
- {
- DOUBRECTFULL rect;
- double bottomy;
-
- adTextBoundingBox(dbh, (unsigned char*)pWord->text, pTData, 0,
- pShp->file[0] ? pShp->file : NULL,
- pShp->bigfontname[0] ? pShp->bigfontname : NULL,
- 1, &rect, &bottomy);
- pWord->width = rect.pt1[0] - rect.pt0[0];
- pWord->type = type;
- pWord->height = height;
- adWriteBlobBytes(bcptr, pWord, sizeof(AD_MTEXT_WORD));
- }
- /* Build a word list out of the passed in mtext entity, pEnt. Store
- the word list in the passed in blob. */
- int CDwgReader::preprocess_BuildWordList(AD_DB_HANDLE dbh, PAD_ENT pEnt, AD_VMADDR blob)
- {
- int wordCount = 0;
- int charCount;
- AD_MTEXT_WORD word;
- unsigned char* cptr;
- unsigned char* origPtr;
- AD_TDATA tdata;
- PAD_BLOB_CTRL bcptr = adStartBlobWrite(blob);
- AD_SHPTB shp;
- int whiteCount;
- unsigned char code;
- unsigned char* tempPtr;
- unsigned char temp[100];
- double currHeight = pEnt->mtext.ht;
- int i;
-
- short sReturn=adSeekShapefile(dbh, pEnt->mtext.shapefileobjhandle, &shp);
- if(sReturn!=1)
- {
- _ERROR;
- return 0;
- }
- if(shp.bigfontname[0]==0&&shp.file[0]==0)
- {
- _ERROR;
- return 0;
- }
-
- if (preprocess_ExtractMTextString(&pEnt->mtext, &cptr))
- {
- origPtr = cptr;
- memset(&tdata, 0, sizeof(AD_TDATA));
- tdata.height = currHeight;
- tdata.widthfactor = 1.0;
- adHancpy(tdata.shapefileobjhandle, pEnt->mtext.shapefileobjhandle);
-
- while (*cptr)
- {
- /* Skip whitespace. */
- whiteCount = 0;
- while (*cptr && *cptr < 0x21)
- {
- cptr++;
- whiteCount++;
- }
- if (whiteCount)
- {
- /* adTextBoundingBox doesn't work if you give it spaces. */
- memset(word.text, 'e', whiteCount);
- word.text[whiteCount] = 0x00;
- preprocess_AddWord(dbh, &word, &tdata, &shp, bcptr, AD_BLANKSPACE, currHeight);
- wordCount++;
- }
- charCount = 0;
- while (*cptr && *cptr >= 0x21)
- {
- switch (*cptr)
- {
- case '\':
- code = toupper(*(cptr + 1));
- switch (code)
- {
- case 'F': /* Change of font--ignore. */
- case 'C': /* Change of color--ignore. */
- case 'T': /* Adjusts space between characters--ignore. */
- case 'Q': /* Changes obliquing angle--ignore. */
- case 'W': /* Changes the width factor--ignore. */
- case 'A': /* Changes alignment--ignore. */
- cptr += 2; /* Skip control code. */
- /* Ignore all control data, which is terminated by a semicolon. */
- while (*cptr && *cptr != ';')
- {
- cptr++;
- }
- if (*cptr == ';')
- {
- cptr++;
- }
- continue;
- break;
-
- case 'H': /* Change of height--not supported. */
- i = 0;
- while (*cptr && *cptr != ';')
- {
- temp[i++] = *cptr++;
- }
- if (*cptr == ';')
- {
- cptr++;
- temp[i] = 0;
- /* This will not work correctly unless an attribute stack is
- implemented. Height is valid only within the { ... } body
- in which it is set. */
- //currHeight = atof(&temp[2]); /* Skip leading H. */
- //tdata.height = currHeight;
- }
- continue;
- break;
-
- case 'P': /* Paragraph break. */
- if (charCount)
- {
- word.text[charCount] = 0x00;
- preprocess_AddWord(dbh, &word, &tdata, &shp, bcptr, AD_WORD, currHeight);
- wordCount++;
- }
-
- word.type = AD_END_OF_PARAGRAPH;
- adWriteBlobBytes(bcptr, &word, sizeof(AD_MTEXT_WORD));
- wordCount++;
- cptr += 2;
-
- charCount = 0;
- continue;
- break;
-
- case 'O': /* Overline on/off */
- case 'L': /* Underline on/off */
- cptr += 2;
- continue;
- break;
-
- case '\':
- case '{':
- case '}': /* Use this character as is. */
- cptr++;
- break;
-
- case '~': /* Non-breaking space. */
- cptr++;
- *cptr = 0x20; /* Turn this into a regular space. */
- continue;
- break;
-
- case 'S': /* Stacked text. */
- cptr += 2; /* Skip S and process the rest as a normal string. */
- /* Blank out the '^', '', or '#', which separates the top and bottom,
- and blank out the trailing semicolon. */
- /* Note that this needs to be fixed for the case where you have
- backslashes preceding the '#', '', or '^'. */
- tempPtr = cptr;
- while (*tempPtr && (*tempPtr != ';'))
- {
- if (*tempPtr == '^' ||
- *tempPtr == '\' ||
- *tempPtr == '#')
- {
- *tempPtr = ' ';
- }
- tempPtr++;
- }
- if (*tempPtr == ';')
- {
- *tempPtr = ' ';
- }
- continue;
- break;
-
- default:
- break;
- }
- break;
-
- case '{':
- case '}':
- cptr++;
- continue;
- break;
-
- default:
- break;
- }
-
- word.text[charCount++] = *cptr++;
- }
- if (charCount)
- {
- word.text[charCount] = 0x00;
- preprocess_AddWord(dbh, &word, &tdata, &shp, bcptr, AD_WORD, currHeight);
- wordCount++;
- }
- }
- free(origPtr);
- }
- adEndBlobWrite(bcptr);
-
- return(wordCount);
- }
- /* Break the word list stored in "blob" into individual text entities, and
- add the entities to entList. */
- void CDwgReader::preprocess_BreakParagraph(AD_DB_HANDLE dbh,PAD_ENT pEnt,AD_VMADDR blob,int wordCount,AD_VMADDR entList,PAD_ENT_HDR pHdr)
- {
- double currPt[3];
- double currLineWidth;
- int firstOnLine;
- int centered;
- PAD_BLOB_CTRL bcptr;
- AD_MTEXT_WORD word;
- AD_OBJHANDLE saveH;
- int j;
- AD_ENT txtEnt;
- AD_ENT_HDR hdr2;
-
- /* Set default values for text entity. */
- hdr2.enttype = AD_ENT_TEXT;
- adSetEntityDefaults(dbh, &hdr2, &txtEnt); /* Initialize txtEnt */
- pHdr->enttype = AD_ENT_TEXT;
- txtEnt.text.tdata.height = pEnt->mtext.ht;
- adHancpy(txtEnt.text.tdata.shapefileobjhandle, pEnt->mtext.shapefileobjhandle);
-
- memcpy(currPt, pEnt->mtext.pt0, 3 * sizeof(double));
- currPt[1] -= pEnt->mtext.ht;
- currLineWidth = 0.0;
- firstOnLine = 1;
- bcptr = adStartBlobRead(blob);
- if (wordCount == 1 &&
- (pEnt->mtext.attachpt == 2 || /* Top Center */
- pEnt->mtext.attachpt == 5 || /* Middle Center */
- pEnt->mtext.attachpt == 8)) /* Bottom Center */
- {
- centered = 1;
- }
- else
- {
- centered = 0;
- }
-
- for (j = 0; j < wordCount; j++)
- {
- adReadBlobBytes(bcptr, &word, sizeof(AD_MTEXT_WORD));
- strcpy(txtEnt.text.textstr, word.text);
- txtEnt.text.tdata.height = word.height;
-
- if (word.type == AD_BLANKSPACE)
- {
- /* Update current position if not at the end of a line. */
- if (pEnt->mtext.boxwid == 0.0 ||
- (currLineWidth + word.width) < pEnt->mtext.boxwid)
- {
- currLineWidth += word.width;
- currPt[0] += word.width;
- }
- else
- {
- /* Newline, but whitespace does not wrap. */
- currLineWidth = 0.0;
- currPt[0] = pEnt->mtext.pt0[0];
- currPt[1] -= (pEnt->mtext.ht * 1.7);
- }
- }
- else if (word.type == AD_END_OF_PARAGRAPH)
- {
- currLineWidth = 0.0;
- currPt[0] = pEnt->mtext.pt0[0];
- currPt[1] -= (pEnt->mtext.ht * 1.7);
- }
- else if (word.type == AD_WORD)
- {
- if (firstOnLine)
- {
- /* Always fits. */
- currLineWidth += word.width;
- memcpy(txtEnt.text.pt0, currPt, 3 * sizeof(double));
- if (centered)
- {
- /* Special case of 1 word centered--adjust the text
- point to simulate centering. */
- txtEnt.text.pt0[0] = currPt[0] - (word.width / 2.0);
- if (pEnt->mtext.attachpt == 5)
- {
- /* Middle centered. */
- txtEnt.text.pt0[1] += (word.height / 2.0);
- }
- }
- currPt[0] += word.width;
- firstOnLine = 0;
- }
- else
- {
- if (pEnt->mtext.boxwid == 0.0 ||
- (currLineWidth + word.width) < pEnt->mtext.boxwid)
- {
- memcpy(txtEnt.text.pt0, currPt, 3 * sizeof(double));
- currLineWidth += word.width;
- currPt[0] += word.width;
- }
- else
- {
- /* Go to next line. */
- currPt[0] = pEnt->mtext.pt0[0];
- currPt[1] -= (pEnt->mtext.ht * 1.7);
- currLineWidth = word.width;
- memcpy(txtEnt.text.pt0, currPt, 3 * sizeof(double));
- currPt[0] += word.width;
- }
- }
-
- adHancpy(saveH, pHdr->enthandle);
- if (j == 0)
- {
- pHdr->xdblob = NULL;
- pHdr->reactorblob = NULL;
- adReplaceEntity(dbh, entList, pHdr->enthandle, pHdr, &txtEnt);
- }
- else
- {
- adGenerateObjhandle(dbh, pHdr->enthandle);
- adAddEntityToListAfter(dbh, entList, pHdr, &txtEnt, saveH);
- }
- }
- }
- }
- /* Convert all mtext entities in the passed in database to text entities. */
- BOOL CDwgReader::preprocess_Convertmtext(AD_DB_HANDLE dbh,int mode)
- {
- if(mode!=2)
- {
- return TRUE;
- }
- short res;
- AD_BLKH blkh;
- AD_ENT ent;
- AD_ENT_HDR hdr;
- long i;
- AD_VMADDR blob;
- int wordCount;
-
- adStartBlockheaderGet(dbh);
- for (i = 0; i < adNumBlockheaders(dbh); i++)
- {
- if (adGetBlockheader(dbh, &blkh))
- {
- if (adStartEntityGet(blkh.entitylist))
- {
- do
- {
- if (res = adGetEntity(blkh.entitylist, &hdr, &ent))
- {
- if (hdr.enttype == AD_ENT_MTEXT)
- {
- blob = adCreateBlob();
- wordCount = preprocess_BuildWordList(dbh, &ent, blob);
-
- preprocess_BreakParagraph(dbh, &ent, blob, wordCount, blkh.entitylist, &hdr);
-
- adDeleteBlob(blob);
- }
- }
- } while (res && hdr.enttype!=AD_ENT_ENDBLK);
- }
- }
- }
- return TRUE;
- }
- BOOL CDwgReader::preprocess_Convertproxy(AD_DB_HANDLE handle,int mode)
- {
- BOOL flags;
- switch(mode)
- {
- case 1://代理实体转换为块
- flags=TRUE;
- break;
- case 2://代理实体转换为实体
- flags=FALSE;
- break;
- default://代理实体转换为实体
- flags=FALSE;
- break;
- }
- short res;
- AD_BLKH blkh;
- AD_ENT ent;
- AD_ENT_HDR hdr;
- AD_OBJHANDLE *BlkHandles;
- long i, j, nBlkHdrCnt;
-
- if(res = adStartBlockheaderGet(handle))
- {
- nBlkHdrCnt = adNumBlockheaders(handle);
- if(flags)
- {
- BlkHandles = (AD_OBJHANDLE*)malloc(sizeof(AD_OBJHANDLE)*nBlkHdrCnt);
- for(i=j=0; i<nBlkHdrCnt; i++)
- {
- if(adGetBlockheader(handle, &blkh))
- {
- if(!blkh.purgedflag)
- {
- adHancpy(BlkHandles[j++], blkh.objhandle);
- }
- }
- }
- nBlkHdrCnt = j;
- }
- for(i=0; i<nBlkHdrCnt; i++)
- {
- if(flags)
- {
- res = adSeekBlockheader(handle, BlkHandles[i], &blkh);
- }
- else
- {
- res = adGetBlockheader(handle, &blkh);
- }
- if(res && (res = adStartEntityGet(blkh.entitylist)))
- {
- do
- {
- if(res = adGetEntity(blkh.entitylist, &hdr, &ent))
- {
- switch(hdr.enttype)
- {
- case AD_ENT_LINE://1
- case AD_ENT_POINT://2
- case AD_ENT_CIRCLE://3
- case AD_ENT_SHAPE:/*4*/
- case AD_ENT_ELLIPSE://5
- case AD_ENT_SPLINE://
- case AD_ENT_TEXT:/*7*/
- case AD_ENT_ARC://8
- case AD_ENT_TRACE://9转换为4根线 宽度线
- case AD_ENT_REGION:/*10*/
- case AD_ENT_BODY:
- case AD_ENT_SOLID3D:
- case AD_ENT_SOLID:/*11*/
- case AD_ENT_BLOCK://12
- case AD_ENT_ENDBLK://13
- case AD_ENT_INSERT://14
- case AD_ENT_ATTDEF:
- case AD_ENT_ATTRIB:
- case AD_ENT_POLYLINE://多段线开始标志
- case AD_ENT_VERTEX://多段线顶点
- case AD_ENT_SEQEND://insert+attribs或polyline+vertexs的结束标志,前者不予考虑 以上三种实体配合起来实现了多段线的完整转换.
- case AD_ENT_LINE3D:
- case AD_ENT_FACE3D:
- case AD_ENT_DIMENSION:
- case AD_ENT_VIEWPORT://公共部分不予处理
- case AD_ENT_RAY:
- case AD_ENT_XLINE:
- case AD_ENT_MTEXT:
- case AD_ENT_LEADER://完成但仍然可以进一步优化 样条
- case AD_ENT_TOLERANCE:
- case AD_ENT_MLINE://完成 平行线
- break;
- default:
- if(hdr.enttype!=adArcAlignedTextEnttype(handle))
- if(hdr.enttype!=adHatchEnttype(handle))
- if(hdr.enttype!=adImageEnttype(handle))
- if(hdr.enttype!=adLwplineEnttype(handle))
- if(hdr.enttype!=adOle2frameEnttype(handle))
- if(hdr.enttype!=adRtextEnttype(handle))
- if(hdr.enttype!=adWipeoutEnttype(handle))
- if(AD_IS_A_PROXYENT(hdr.enttype))
- {
- if(adExplodeProxyToList(handle, &hdr, &ent, blkh.entitylist, flags, hdr.enthandle))
- {
- adDeleteEntity(handle, blkh.entitylist, hdr.enthandle, AD_DELETE_BLOBS);
- }
- }
- break;
- }
- }
- } while (res && hdr.enttype!=AD_ENT_ENDBLK);
- }
- }
- }
-
- if(flags)
- {
- free(BlkHandles);
- }
- return TRUE;
- }
- BOOL CDwgReader::preprocess_Convertxref(AD_DB_HANDLE dwghandle)
- {
- sReturnValue=adStartBlockheaderGet(dwghandle);
- if(sReturnValue!=1)
- {
- _ERROR;
- return FALSE;
- }
- //long adNumBlockheaders (AD_DB_HANDLE handle);
- for(long i=0; i<adNumBlockheaders(dwghandle);i++) // note --无0 -2 so we don't get the model and paperspace blocks
- {
- //short adGetBlockheader (AD_DB_HANDLE handle, PAD_BLKH adblkh);
- sReturnValue=adGetBlockheader(dwghandle,&adtb->blkh);
- if(sReturnValue!=1)
- {
- _ERROR;
- return FALSE;
- }
- if(adtb->blkh.purgedflag==1)
- {
- continue;
- }
- if((adtb->blkh.flag&AD_BLOCK_XREF)&&(!adtb->blkh.unloaded))
- {
- //short adStartEntityGet (AD_VMADDR list);
- sReturnValue=adStartEntityGet(adtb->blkh.entitylist);
- if(sReturnValue!=1)
- {
- _ERROR;
- return FALSE;
- }
- //short adGetEntity (AD_VMADDR list,PAD_ENT_HDR adenhd,PAD_ENT aden);
- if(adGetEntity(adtb->blkh.entitylist,adenhd,aden))
- {
- if(adenhd->enttype==AD_ENT_BLOCK)
- {
- if(aden->block.xrefpath[0]==0)
- {
- continue;
- }
-
- CString strTmpFile;
- char path[MAX_PATH];
- GetTempPath(MAX_PATH,path);
- strTmpFile.Format("%s",path);
- strTmpFile += "ConvXRef.kmg";
- AD_DB_HANDLE refhandle;
- //AD_DB_HANDLE adGetXrefDbHandle(AD_DB_HANDLE main, void *filename);
- refhandle=adGetXrefDbHandle(dwghandle,aden->block.xrefpath);
- try
- {
- adFileVersionLoaded(refhandle);
- }
- catch(...)
- {
- //无法读出数据来,只有转换为东方CAD的外部引用
- CString str=aden->block.xrefpath;
- if(str.Find('.')==-1)
- {
- str+=".kmg";
- }
- else
- {
- if(str.GetLength()<3)
- {
- continue;
- }
- str=str.Left(str.GetLength()-3)+"kmg";
- }
- if(str.Find('\')==-1)
- {
- {
-
- CString strPath;
- TCHAR szFileName[MAX_PATH];
- ::memcpy(szFileName,(char*)(LPCTSTR)m_pCommand->m_strSourceName,MAX_PATH);
- (*_tcsrchr(szFileName,'\'))=' ';
- strPath=szFileName;
- strPath+="\";
- strPath+=str;
- str=strPath;
- }
- }
- UINT id=m_pConverter->ConverterEnthead_Blockdefine(aden->block.xrefpath,aden->block.name2);
- m_pConverter->ConverterEnthead_XrefBlock(id,aden->block.base[0],aden->block.base[1],aden->block.base[2],str);
- m_ablock.Add(adtb->blkh.objhandle,id,1.0,FALSE);
- continue;
- }
- CProgressInfo* pProgress=new CProgressInfo;
- CCommandInfo* pCommand=new CCommandInfo;
- pCommand->m_strSourceName=aden->block.xrefpath;
- pCommand->m_strAimName=strTmpFile;
- pCommand->m_hmainfrm=m_pCommand->m_hmainfrm;
- CKmgConverter* pConverter=new CKmgConverter(pCommand);
- CDwgReader* pReader=new CDwgReader(pProgress,pCommand,pConverter);
- BOOL bSucceed=pReader->BeginConvert();
- if(bSucceed)
- {
- bSucceed=pReader->Parse(refhandle);
- }
- if(bSucceed)
- {
- bSucceed=pReader->EndConvert();
- }
- delete pProgress;
- delete pCommand;
- delete pReader;
- delete pConverter;
- m_pConverter->ReActiveDataManager();
- CKmDataManager* pTempDB=new CKmDataManager;
- pTempDB->Init();
- BOOL bAllowReadOld=FALSE;
- bSucceed=pTempDB->LoadFile(strTmpFile,bAllowReadOld);
- if(bSucceed)
- {
- KmBlockDef* pTempDef=::CreateDBBlockDef(pTempDB,strTmpFile,"外部转换块");
- pTempDef->SetBasePt(KmPoint(aden->block.base[0],aden->block.base[1]));
- if(pTempDef)
- {
- UINT newid;
- if(::PrepareInsertExternalBlockDef(pTempDB,pTempDef,newid))
- {
- m_ablock.Add(adtb->blkh.objhandle,newid,1.0,FALSE);
- }
- }
- //?? delete pTempDef;
- }
- delete pTempDB;
- ::DeleteFile(strTmpFile);
- }
- }
- }
- }
- return TRUE;
- }