Converterfile.cpp
上传用户:hell82222
上传日期:2013-12-19
资源大小:1872k
文件大小:11k
- // KmgConverter.cpp: implementation of the CKmgConverter class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "FileSwitch.h"
- #include "KmgConverter.h"
- #include "KmLayer.h"
- #include "KmArrowHead.h"
- #include "showdef.h"
- #include "entdef.h"
- #include "KmEntity.h"
- #include "Block.h"
- #include "WordStyle.h"
- #include "DwgReader.h"
- #include "Reader.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern CStdioFile g_CensorialFile; //监察转换进度的文件
- extern int g_mode;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- KMOBJECTID CKmgConverter::ConverterEntiey_KmObject(KmObject* pObj)
- {
- return AddObject(pObj);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmLine(double x1,double y1,double z1,double x2,double y2,double z2)
- {
- KmLine* pLine=new KmLine;
- pLine->bx=x1;
- pLine->by=y1;
- pLine->ex=x2;
- pLine->ey=y2;
- return AddObject(pLine);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmCircle(double x1,double y1,double z1,double ra)
- {
- KmCircle* pCircle=new KmCircle;
- pCircle->message=MESSAGE_CIRCLE_FULL;
- pCircle->xo=x1;
- pCircle->yo=y1;
- pCircle->r=ra;
- return AddObject(pCircle);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmEllipse(double x1,double y1,double z1,double lr,double sr,double angle,BOOL b90Mirror)
- {
- KmEllipse* pEllipse=new KmEllipse;
- pEllipse->SetCenter(KmPoint(x1,y1));
- pEllipse->SetLr(lr);
- pEllipse->SetSr(sr);
- pEllipse->SetAngle(angle*180/PI);
- pEllipse->SetMessage(0x400);
- if(b90Mirror)
- {
- pEllipse->Mirror(KmPoint(x1,y1),90);
- }
- return AddObject(pEllipse);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmEllipseArc(double x1,double y1,double z1,double lr,double sr,double angle,double ba,double ea,BOOL b90Mirror)
- {
- KmEllipse* pEllipse=new KmEllipse;
- pEllipse->SetMessage(MESSAGE_CIRCLE_POSITIVE);//逆椭圆弧
- pEllipse->SetCenter(KmPoint(x1,y1));
- pEllipse->SetLr(lr);
- pEllipse->SetSr(sr);
- pEllipse->SetAngle(::standard(angle*180/PI));
- pEllipse->SetBeginAngle(::standard(ba*180/PI));
- pEllipse->SetEndAngle(::standard(ea*180/PI));
- if(b90Mirror)
- {
- pEllipse->Mirror(KmPoint(x1,y1),90);
- }
- return AddObject(pEllipse);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmArc(double x1,double y1,double z1,double radius,double ba,double ea)
- {
- KmCircle* pCircle=new KmCircle;
- pCircle->message=MESSAGE_CIRCLE_POSITIVE;
- pCircle->xo=x1;
- pCircle->yo=y1;
- pCircle->r=radius;
- pCircle->b_angle=ba*180/PI;
- pCircle->e_angle=ea*180/PI;
- return AddObject(pCircle);
- }
- KMOBJECTID CKmgConverter::ConverterEntiey_KmSPline(int mode,AD_VMADDR ldblob,unsigned short numknots,unsigned short numctlpts,unsigned short numfittps)
- {
- KmSpLine* pSpLine=new KmSpLine;
- pSpLine->m_nNodeNum=0;
- pSpLine->m_aNode.RemoveAll();
- if(mode==2)
- {
- pSpLine->m_nNodeNum=numctlpts;
- pSpLine->m_iType=T_BPLINE; //B样条曲线
- }
- else if(mode==3)
- {
- pSpLine->m_nNodeNum=numfittps;
- pSpLine->m_iType=T_SPLINE; //三次样条曲线
- }
- else if(mode==(10+2))
- {
- pSpLine->m_nNodeNum=numctlpts;
- pSpLine->m_iType=T_ZLINE; //B样条曲线
- mode=2;
- }
- else if(mode==(10+3))
- {
- pSpLine->m_nNodeNum=numfittps;
- pSpLine->m_iType=T_ZLINE; //三次样条曲线
- mode=3;
- }
- else
- {
- delete pSpLine;
- KMOBJECTID id;
- return id;
- }
- double dtemp;
- double dpt[3];
- short sReturnValue;
- //PAD_BLOB_CTRL adStartBlobRead (AD_VMADDR blob);
- PAD_BLOB_CTRL bcptr=adStartBlobRead(ldblob);
- for(unsigned short i=0;i<numknots;i++)
- {
- //short adReadBlobDouble (PAD_BLOB_CTRL bcptr,double *doub);
- sReturnValue=adReadBlobDouble(bcptr,&dtemp);
- if(sReturnValue!=1)
- {
- _ERROR;
- break;
- }
- }
- for(i=0;i<numctlpts;i++)
- {
- //short adReadBlob3Double (PAD_BLOB_CTRL bcptr,double *doub);
- sReturnValue=adReadBlob3Double(bcptr,dpt);
- if(sReturnValue!=1)
- {
- _ERROR;
- break;
- }
- if(mode==2)
- {
- pSpLine->m_aNode.Add(KmPoint(dpt[0],dpt[1]));
- }
- }
- for(i=0;i<numfittps;i++)
- {
- //short adReadBlob3Double (PAD_BLOB_CTRL bcptr,double *doub);
- sReturnValue=adReadBlob3Double(bcptr,dpt);
- if(sReturnValue!=1)
- {
- _ERROR;
- break;
- }
- if(mode==3)
- {
- pSpLine->m_aNode.Add(KmPoint(dpt[0],dpt[1]));
- }
- }
- return AddObject(pSpLine);
- }
-
- KMOBJECTID CKmgConverter::ConverterEntiey_KmBlockref(double x1,double y1,double z1,double xs,double ys,double zs,double ra,DWORD id,BOOL b90Mirror)
- {
- KmBlockRef* pBlockRef=new KmBlockRef;
- pBlockRef->SetBlockDefID(id);
- pBlockRef->SetInsertPoint(KmPoint(x1,y1));
- if(xs<-0.000001&&ys<-0.000001)//关于XY轴镜像
- {
- pBlockRef->Rotate(180,pBlockRef->GetInsertPoint());
- }
- else if(xs<-0.000001)//关于Y轴镜像
- {
- pBlockRef->Mirror(pBlockRef->GetInsertPoint(),90);
- }
- else if(ys<-0.000001)//关于X轴镜像
- {
- pBlockRef->Mirror(pBlockRef->GetInsertPoint(),0);
- }
- if(b90Mirror)
- {
- pBlockRef->Mirror(KmPoint(0,0),90);
- }
- pBlockRef->Rotate(ra*180/PI,pBlockRef->GetInsertPoint());
- pBlockRef->Zoom(fabs(xs),pBlockRef->GetInsertPoint());
- return AddObject(pBlockRef);
- /*
-
- pBlockRef->SetInsertPoint(KmPoint(x1,y1));
-
- pBlockRef->Rotate(ra*180/PI,KmPoint(x1,y1));
-
- if(xs<-0.000001&&ys<-0.000001)//关于X轴镜像
- {
- pBlockRef->Rotate(180,KmPoint(x1,y1));
- }
- else if(xs<-0.000001)
- {
- //pBlockRef->SetMirrorData(90,TRUE);
- pBlockRef->Mirror(KmPoint(x1,y1),90);
- }
- else if(ys<-0.000001)
- {
- //pBlockRef->SetMirrorData(0,TRUE);
- pBlockRef->Mirror(KmPoint(x1,y1),0);
- }
- if(b90Mirror)
- {
- pBlockRef->Mirror(KmPoint(0,0),90);
- }
-
- xs=fabs(xs);
- ys=fabs(ys);
- pBlockRef->SetScale_x(xs);
- pBlockRef->SetScale_y(ys);
- return AddObject(pBlockRef);*/
- }
- BOOL CKmgConverter::ConverterEntiey_KmBlockdef(double x1,double y1,double z1)
- {
- switch(m_dDataVessel)
- {
- case DATAVESSEL_BLOCKDEF:
- if(m_pActiveBlockDef)
- {
- // m_pActiveBlockDef->SetLayerID(m_enthead.idlayer);
- // m_pActiveBlockDef->SetColor(m_enthead.icolor);
- // m_pActiveBlockDef->SetView(VIEW_MAIN);
- m_pActiveBlockDef->SetBasePt(KmPoint(x1,y1));
- return TRUE;
- }
- _WRITE(str="当前的数据容器不是有效的块定义");
- return FALSE;
- }
- return TRUE;
- }
- BOOL CKmgConverter::ConverterEntiey_KmArrowHead(double x1,double y1,double x2,double y2,double dwidth)
- {
- KmArrowHead* pArrowHead=new KmArrowHead;
- pArrowHead->m_dLength=::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
- pArrowHead->m_dWidth=dwidth;
- pArrowHead->m_dAngle=::GetAngle(KmPoint(x2,y2),KmPoint(x1,y1));
- pArrowHead->m_bIsSolid=TRUE;
- pArrowHead->m_bFixed=FALSE;
- pArrowHead->m_Vertex=KmPoint(x1,y1);
- AddObject(pArrowHead);
- return TRUE;
- }
- BOOL CKmgConverter::ConverterEntiey_KmArrowHead(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
- {
- //是否三个点.
- if( fabs(x1-x2)<PRECISION&&fabs(y1-y2)<PRECISION )
- {//12
- x2=x4;
- y2=y4;
- }
- else if( fabs(x1-x3)<PRECISION&&fabs(y1-y3)<PRECISION )
- {//13
- x3=x4;
- y3=y4;
- }
- else if( fabs(x1-x4)<PRECISION&&fabs(y1-y4)<PRECISION )
- {//14
- }
- else if( fabs(x2-x3)<PRECISION&&fabs(y2-y3)<PRECISION )
- {//23
- x3=x4;
- y3=y4;
- }
- else if( fabs(x2-x4)<PRECISION&&fabs(y2-y4)<PRECISION )
- {//24
- }
- else if( fabs(x3-x4)<PRECISION&&fabs(y3-y4)<PRECISION )
- {//34
- }
- else
- {
- return FALSE;
- }
- //是否等腰
- double d12=::sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
- double d23=::sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) );
- double d31=::sqrt( (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) );
- double dLength; //箭头长度
- double dWidth; //箭头宽度
- double dAngle; //箭头角度
- KmPoint Vertex; //箭头顶点
- KmPoint Middle;
- if( fabs(d12-d23)<PRECISION )
- {
- Vertex.x=x2;
- Vertex.y=y2;
- Middle.x=(x1+x3)*0.5;
- Middle.y=(y1+y3)*0.5;
- dWidth=d31;
- }
- else if( fabs(d23-d31)<PRECISION )
- {
- Vertex.x=x3;
- Vertex.y=y3;
- Middle.x=(x2+x1)*0.5;
- Middle.y=(y2+y1)*0.5;
- dWidth=d12;
- }
- else if( fabs(d31-d12)<PRECISION )
- {
- Vertex.x=x1;
- Vertex.y=y1;
- Middle.x=(x3+x2)*0.5;
- Middle.y=(y3+y2)*0.5;
- dWidth=d23;
- }
- else
- {
- return FALSE;
- }
- dLength=::sqrt( (Middle.x-Vertex.x)*(Middle.x-Vertex.x)+(Middle.y-Vertex.y)*(Middle.y-Vertex.y) );
- dAngle=::GetAngle(Middle,Vertex);
- KmArrowHead* pArrowHead=new KmArrowHead;
- pArrowHead->m_dLength=dLength;
- pArrowHead->m_dWidth=dWidth;
- pArrowHead->m_dAngle=dAngle;
- pArrowHead->m_bIsSolid=TRUE;
- pArrowHead->m_bFixed=FALSE;
- pArrowHead->m_Vertex=Vertex;
- AddObject(pArrowHead);
- return TRUE;
- }
- void CKmgConverter::ConverterEntiey_KmArrow(double x1,double y1,double z1,double x2,double y2,double z2,double dWidth)
- {
- KmPoint p1,p2,tp1,tp2;
- p1.x=x1;p1.y=y1;p2.x=x2;p2.y=y2;
- if(fabs(p1.x - p2.x) < 0.0001)//竖
- {
- tp1.x = p2.x - dWidth/2;
- tp1.y = p2.y;
- tp2.x = p2.x + dWidth/2;
- tp2.y = p2.y;
- }
- else if(fabs(p1.y - p2.y) < 0.0001)//横
- {
- tp1.x = p2.x;
- tp1.y = p2.y - dWidth/2;
- tp2.x = p2.x;
- tp2.y = p2.y + dWidth/2;
- }
- else //一般情况
- {
- double k, b1, b2;
- k = (p2.y - p1.y) / (p2.x - p1.x);
- b1 = p2.y - k*p2.x;
- b2 = p2.y + (1/k)*p2.x;
- double s = sqrt(1 + k*k);
- tp1.x = -(dWidth/2*s + b1 -b2) / (1/k + k);
- tp1.y = (dWidth/2*s + b1 -b2) / (1 + k*k) + b2;
- tp2.x = -(-dWidth/2*s + b1 -b2) / (1/k + k);
- tp2.y = (-dWidth/2*s + b1 -b2) / (1 + k*k) + b2;
- }
- ConverterEntiey_KmLine(p1.x,p1.y,z1,tp1.x,tp1.y,z2);
- ConverterEntiey_KmLine(p1.x,p1.y,z1,tp2.x,tp2.y,z2);
- ConverterEntiey_KmLine(tp1.x,tp1.y,z1,tp2.x,tp2.y,z2);
- }
- BOOL CKmgConverter::ConverterEntiey_KmBuffer(KmBuffer& buff,CString blockname,CString blockdescrip,KmPoint po)
- {
- blockname+="#shape";
- blockdescrip="转换至形#"+blockdescrip;
- UINT dwKmID=ConverterEnthead_Blockdefine(blockname,blockdescrip);
- DWORD dwoldKmID;
- int OldDataVessel=GetDataVessel(dwoldKmID);
- if(!SetDataVessel(DATAVESSEL_BLOCKDEF,dwKmID))
- {
- _WRITE(str.Format("无法找到第%d号块定义",dwKmID));
- return FALSE;
- }
- KmLine* pLine;
- int size=buff.GetLineBuffSize();
- for(int i=0;i<size;i++)
- {
- pLine=new KmLine;
- pLine->SetBegin(buff.GetLineEnt(i)->P1);
- pLine->SetEnd(buff.GetLineEnt(i)->P2);
- pLine->color=COLOR_BYBLOCK;
- pLine->style=buff.GetLineEnt(i)->LineType;
- pLine->SetWidth(ABS_THIN_WIDTH);
- AddObject(pLine,FALSE);
- }
- KmCircle* pCircle;
- size=buff.GetCircleBuffSize();
- for(i=0;i<size;i++)
- {
- pCircle=new KmCircle;
- pCircle->SetCenter(buff.GetCircleEnt(i)->center);
- pCircle->SetRadius(buff.GetCircleEnt(i)->radius);
- pCircle->SetBAngle(buff.GetCircleEnt(i)->b_angle);
- pCircle->SetEAngle(buff.GetCircleEnt(i)->e_angle);
- pCircle->message=buff.GetCircleEnt(i)->message;
- pCircle->color=COLOR_BYBLOCK;
- pCircle->style=buff.GetCircleEnt(i)->LineType;
- pCircle->SetWidth(ABS_THIN_WIDTH);
- AddObject(pCircle,FALSE);
- }
- if(!SetDataVessel(OldDataVessel,dwoldKmID))
- {
- _WRITE(str.Format("无法找到第%d号块定义",dwKmID));
- return FALSE;
- }
- if(!m_pActiveBlockDef)return FALSE;
- m_pActiveBlockDef->SetBasePt(po);
- KmBlockRef* pRef=new KmBlockRef;
- pRef->SetBlockDefID(m_pActiveBlockDef->GetID());
- pRef->SetInsertPoint(po);
- AddObject(pRef);
- return TRUE;
- }