Converterfile.cpp
上传用户:hell82222
上传日期:2013-12-19
资源大小:1872k
文件大小:11k
源码类别:

CAD

开发平台:

Visual C++

  1. // KmgConverter.cpp: implementation of the CKmgConverter class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "FileSwitch.h"
  6. #include "KmgConverter.h"
  7. #include "KmLayer.h"
  8. #include "KmArrowHead.h"
  9. #include "showdef.h"
  10. #include "entdef.h"
  11. #include "KmEntity.h"
  12. #include "Block.h"
  13. #include "WordStyle.h"
  14. #include "DwgReader.h"
  15. #include "Reader.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. extern CStdioFile g_CensorialFile; //监察转换进度的文件
  22. extern int g_mode;
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction/Destruction
  25. //////////////////////////////////////////////////////////////////////
  26. KMOBJECTID CKmgConverter::ConverterEntiey_KmObject(KmObject* pObj)
  27. {
  28. return AddObject(pObj);
  29. }
  30. KMOBJECTID CKmgConverter::ConverterEntiey_KmLine(double x1,double y1,double z1,double x2,double y2,double z2)
  31. {
  32. KmLine* pLine=new KmLine;
  33. pLine->bx=x1;
  34. pLine->by=y1;
  35. pLine->ex=x2;
  36. pLine->ey=y2;
  37. return AddObject(pLine);
  38. }
  39. KMOBJECTID CKmgConverter::ConverterEntiey_KmCircle(double x1,double y1,double z1,double ra)
  40. {
  41. KmCircle* pCircle=new KmCircle;
  42. pCircle->message=MESSAGE_CIRCLE_FULL;
  43. pCircle->xo=x1;
  44. pCircle->yo=y1;
  45. pCircle->r=ra;
  46. return AddObject(pCircle);
  47. }
  48. KMOBJECTID CKmgConverter::ConverterEntiey_KmEllipse(double x1,double y1,double z1,double lr,double sr,double angle,BOOL b90Mirror)
  49. {
  50. KmEllipse* pEllipse=new KmEllipse;
  51. pEllipse->SetCenter(KmPoint(x1,y1));
  52. pEllipse->SetLr(lr);
  53. pEllipse->SetSr(sr);
  54. pEllipse->SetAngle(angle*180/PI);
  55. pEllipse->SetMessage(0x400);
  56. if(b90Mirror)
  57. {
  58. pEllipse->Mirror(KmPoint(x1,y1),90);
  59. }
  60. return AddObject(pEllipse);
  61. }
  62. KMOBJECTID CKmgConverter::ConverterEntiey_KmEllipseArc(double x1,double y1,double z1,double lr,double sr,double angle,double ba,double ea,BOOL b90Mirror)
  63. {
  64. KmEllipse* pEllipse=new KmEllipse;
  65. pEllipse->SetMessage(MESSAGE_CIRCLE_POSITIVE);//逆椭圆弧
  66. pEllipse->SetCenter(KmPoint(x1,y1));
  67. pEllipse->SetLr(lr);
  68. pEllipse->SetSr(sr);
  69. pEllipse->SetAngle(::standard(angle*180/PI));
  70. pEllipse->SetBeginAngle(::standard(ba*180/PI));
  71. pEllipse->SetEndAngle(::standard(ea*180/PI));
  72. if(b90Mirror)
  73. {
  74. pEllipse->Mirror(KmPoint(x1,y1),90);
  75. }
  76. return AddObject(pEllipse);
  77. }
  78. KMOBJECTID CKmgConverter::ConverterEntiey_KmArc(double x1,double y1,double z1,double radius,double ba,double ea)
  79. {
  80. KmCircle* pCircle=new KmCircle;
  81. pCircle->message=MESSAGE_CIRCLE_POSITIVE;
  82. pCircle->xo=x1;
  83. pCircle->yo=y1;
  84. pCircle->r=radius;
  85. pCircle->b_angle=ba*180/PI;
  86. pCircle->e_angle=ea*180/PI;
  87. return AddObject(pCircle);
  88. }
  89. KMOBJECTID CKmgConverter::ConverterEntiey_KmSPline(int mode,AD_VMADDR ldblob,unsigned short numknots,unsigned short numctlpts,unsigned short numfittps)
  90. {
  91. KmSpLine* pSpLine=new KmSpLine;
  92. pSpLine->m_nNodeNum=0;
  93. pSpLine->m_aNode.RemoveAll();
  94. if(mode==2)
  95. {
  96. pSpLine->m_nNodeNum=numctlpts;
  97. pSpLine->m_iType=T_BPLINE;     //B样条曲线   
  98. }
  99. else if(mode==3)
  100. {
  101. pSpLine->m_nNodeNum=numfittps;
  102. pSpLine->m_iType=T_SPLINE;     //三次样条曲线   
  103. }
  104. else if(mode==(10+2))
  105. {
  106. pSpLine->m_nNodeNum=numctlpts;
  107. pSpLine->m_iType=T_ZLINE;     //B样条曲线  
  108. mode=2;
  109. }
  110. else if(mode==(10+3))
  111. {
  112. pSpLine->m_nNodeNum=numfittps;
  113. pSpLine->m_iType=T_ZLINE;     //三次样条曲线    
  114. mode=3;
  115. }
  116. else
  117. {
  118. delete pSpLine;
  119. KMOBJECTID id;
  120. return id;
  121. }
  122. double dtemp;
  123. double dpt[3];
  124. short sReturnValue;
  125. //PAD_BLOB_CTRL adStartBlobRead (AD_VMADDR blob);
  126. PAD_BLOB_CTRL bcptr=adStartBlobRead(ldblob);
  127. for(unsigned short i=0;i<numknots;i++)
  128. {
  129. //short adReadBlobDouble (PAD_BLOB_CTRL bcptr,double *doub);
  130. sReturnValue=adReadBlobDouble(bcptr,&dtemp);
  131. if(sReturnValue!=1)
  132. {
  133. _ERROR;
  134. break;
  135. }
  136. }
  137. for(i=0;i<numctlpts;i++)
  138. {
  139. //short adReadBlob3Double (PAD_BLOB_CTRL bcptr,double *doub);
  140. sReturnValue=adReadBlob3Double(bcptr,dpt);
  141. if(sReturnValue!=1)
  142. {
  143. _ERROR;
  144. break;
  145. }
  146. if(mode==2)
  147. {
  148. pSpLine->m_aNode.Add(KmPoint(dpt[0],dpt[1]));
  149. }
  150. }
  151. for(i=0;i<numfittps;i++)
  152. {
  153. //short adReadBlob3Double (PAD_BLOB_CTRL bcptr,double *doub);
  154. sReturnValue=adReadBlob3Double(bcptr,dpt);
  155. if(sReturnValue!=1)
  156. {
  157. _ERROR;
  158. break;
  159. }
  160. if(mode==3)
  161. {
  162. pSpLine->m_aNode.Add(KmPoint(dpt[0],dpt[1]));
  163. }
  164. }
  165. return AddObject(pSpLine);
  166. }
  167. KMOBJECTID CKmgConverter::ConverterEntiey_KmBlockref(double x1,double y1,double z1,double xs,double ys,double zs,double ra,DWORD id,BOOL b90Mirror)
  168. {
  169. KmBlockRef* pBlockRef=new KmBlockRef;
  170. pBlockRef->SetBlockDefID(id);
  171. pBlockRef->SetInsertPoint(KmPoint(x1,y1));
  172. if(xs<-0.000001&&ys<-0.000001)//关于XY轴镜像
  173. {
  174. pBlockRef->Rotate(180,pBlockRef->GetInsertPoint());
  175. }
  176. else if(xs<-0.000001)//关于Y轴镜像
  177. {
  178. pBlockRef->Mirror(pBlockRef->GetInsertPoint(),90);
  179. }
  180. else if(ys<-0.000001)//关于X轴镜像
  181. {
  182. pBlockRef->Mirror(pBlockRef->GetInsertPoint(),0);
  183. }
  184. if(b90Mirror)
  185. {
  186. pBlockRef->Mirror(KmPoint(0,0),90);
  187. }
  188. pBlockRef->Rotate(ra*180/PI,pBlockRef->GetInsertPoint());
  189. pBlockRef->Zoom(fabs(xs),pBlockRef->GetInsertPoint());
  190. return AddObject(pBlockRef);
  191. /*
  192. pBlockRef->SetInsertPoint(KmPoint(x1,y1));
  193. pBlockRef->Rotate(ra*180/PI,KmPoint(x1,y1));
  194. if(xs<-0.000001&&ys<-0.000001)//关于X轴镜像
  195. {
  196. pBlockRef->Rotate(180,KmPoint(x1,y1));
  197. }
  198. else if(xs<-0.000001)
  199. {
  200. //pBlockRef->SetMirrorData(90,TRUE);
  201. pBlockRef->Mirror(KmPoint(x1,y1),90);
  202. }
  203. else if(ys<-0.000001)
  204. {
  205. //pBlockRef->SetMirrorData(0,TRUE);
  206. pBlockRef->Mirror(KmPoint(x1,y1),0);
  207. }
  208. if(b90Mirror)
  209. {
  210. pBlockRef->Mirror(KmPoint(0,0),90);
  211. }
  212. xs=fabs(xs);
  213. ys=fabs(ys);
  214. pBlockRef->SetScale_x(xs);
  215. pBlockRef->SetScale_y(ys);
  216. return AddObject(pBlockRef);*/
  217. }
  218. BOOL CKmgConverter::ConverterEntiey_KmBlockdef(double x1,double y1,double z1)
  219. {
  220. switch(m_dDataVessel)
  221. {
  222. case DATAVESSEL_BLOCKDEF:
  223. if(m_pActiveBlockDef)
  224. {
  225. // m_pActiveBlockDef->SetLayerID(m_enthead.idlayer);
  226. // m_pActiveBlockDef->SetColor(m_enthead.icolor);
  227. // m_pActiveBlockDef->SetView(VIEW_MAIN);
  228. m_pActiveBlockDef->SetBasePt(KmPoint(x1,y1));
  229. return TRUE;
  230. }
  231. _WRITE(str="当前的数据容器不是有效的块定义");
  232. return FALSE;
  233. }
  234. return TRUE;
  235. }
  236. BOOL CKmgConverter::ConverterEntiey_KmArrowHead(double x1,double y1,double x2,double y2,double dwidth)
  237. {
  238. KmArrowHead* pArrowHead=new KmArrowHead;
  239. pArrowHead->m_dLength=::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  240. pArrowHead->m_dWidth=dwidth;
  241. pArrowHead->m_dAngle=::GetAngle(KmPoint(x2,y2),KmPoint(x1,y1));
  242. pArrowHead->m_bIsSolid=TRUE;
  243. pArrowHead->m_bFixed=FALSE;
  244. pArrowHead->m_Vertex=KmPoint(x1,y1);
  245. AddObject(pArrowHead);
  246. return TRUE;
  247. }
  248. BOOL CKmgConverter::ConverterEntiey_KmArrowHead(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
  249. {
  250. //是否三个点.
  251. if( fabs(x1-x2)<PRECISION&&fabs(y1-y2)<PRECISION )
  252. {//12
  253. x2=x4;
  254. y2=y4;
  255. }
  256. else if( fabs(x1-x3)<PRECISION&&fabs(y1-y3)<PRECISION )
  257. {//13
  258. x3=x4;
  259. y3=y4;
  260. }
  261. else if( fabs(x1-x4)<PRECISION&&fabs(y1-y4)<PRECISION )
  262. {//14
  263. }
  264. else if( fabs(x2-x3)<PRECISION&&fabs(y2-y3)<PRECISION )
  265. {//23
  266. x3=x4;
  267. y3=y4;
  268. }
  269. else if( fabs(x2-x4)<PRECISION&&fabs(y2-y4)<PRECISION )
  270. {//24
  271. }
  272. else if( fabs(x3-x4)<PRECISION&&fabs(y3-y4)<PRECISION )
  273. {//34
  274. }
  275. else
  276. {
  277. return FALSE;
  278. }
  279. //是否等腰
  280. double d12=::sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
  281. double d23=::sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) );
  282. double d31=::sqrt( (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) );
  283. double  dLength; //箭头长度
  284. double  dWidth; //箭头宽度
  285. double  dAngle; //箭头角度
  286. KmPoint Vertex; //箭头顶点
  287. KmPoint Middle;
  288. if( fabs(d12-d23)<PRECISION )
  289. {
  290. Vertex.x=x2;
  291. Vertex.y=y2;
  292. Middle.x=(x1+x3)*0.5;
  293. Middle.y=(y1+y3)*0.5;
  294. dWidth=d31;
  295. }
  296. else if( fabs(d23-d31)<PRECISION )
  297. {
  298. Vertex.x=x3;
  299. Vertex.y=y3;
  300. Middle.x=(x2+x1)*0.5;
  301. Middle.y=(y2+y1)*0.5;
  302. dWidth=d12;
  303. }
  304. else if( fabs(d31-d12)<PRECISION )
  305. {
  306. Vertex.x=x1;
  307. Vertex.y=y1;
  308. Middle.x=(x3+x2)*0.5;
  309. Middle.y=(y3+y2)*0.5;
  310. dWidth=d23;
  311. }
  312. else
  313. {
  314. return FALSE;
  315. }
  316. dLength=::sqrt( (Middle.x-Vertex.x)*(Middle.x-Vertex.x)+(Middle.y-Vertex.y)*(Middle.y-Vertex.y) );
  317. dAngle=::GetAngle(Middle,Vertex);
  318. KmArrowHead* pArrowHead=new KmArrowHead;
  319. pArrowHead->m_dLength=dLength;
  320. pArrowHead->m_dWidth=dWidth;
  321. pArrowHead->m_dAngle=dAngle;
  322. pArrowHead->m_bIsSolid=TRUE;
  323. pArrowHead->m_bFixed=FALSE;
  324. pArrowHead->m_Vertex=Vertex;
  325. AddObject(pArrowHead);
  326. return TRUE;
  327. }
  328. void CKmgConverter::ConverterEntiey_KmArrow(double x1,double y1,double z1,double x2,double y2,double z2,double dWidth)
  329. {
  330. KmPoint p1,p2,tp1,tp2;
  331. p1.x=x1;p1.y=y1;p2.x=x2;p2.y=y2;
  332. if(fabs(p1.x - p2.x) < 0.0001)//竖
  333. {
  334. tp1.x = p2.x - dWidth/2;
  335. tp1.y = p2.y;
  336. tp2.x = p2.x + dWidth/2;
  337. tp2.y = p2.y;
  338. }
  339. else if(fabs(p1.y - p2.y) < 0.0001)//横
  340. {
  341. tp1.x = p2.x;
  342. tp1.y = p2.y - dWidth/2;
  343. tp2.x = p2.x;
  344. tp2.y = p2.y + dWidth/2;
  345. }
  346. else  //一般情况
  347. {
  348. double k, b1, b2;
  349. k = (p2.y - p1.y) / (p2.x - p1.x);
  350. b1 = p2.y - k*p2.x;
  351. b2 = p2.y + (1/k)*p2.x;
  352. double s = sqrt(1 + k*k);
  353. tp1.x = -(dWidth/2*s + b1 -b2) / (1/k + k);
  354. tp1.y = (dWidth/2*s + b1 -b2) / (1 + k*k) + b2;
  355. tp2.x = -(-dWidth/2*s + b1 -b2) / (1/k + k);
  356. tp2.y = (-dWidth/2*s + b1 -b2) / (1 + k*k) + b2;
  357. }
  358. ConverterEntiey_KmLine(p1.x,p1.y,z1,tp1.x,tp1.y,z2);
  359. ConverterEntiey_KmLine(p1.x,p1.y,z1,tp2.x,tp2.y,z2);
  360. ConverterEntiey_KmLine(tp1.x,tp1.y,z1,tp2.x,tp2.y,z2);
  361. }
  362. BOOL CKmgConverter::ConverterEntiey_KmBuffer(KmBuffer& buff,CString blockname,CString blockdescrip,KmPoint po)
  363. {
  364. blockname+="#shape";
  365. blockdescrip="转换至形#"+blockdescrip;
  366. UINT dwKmID=ConverterEnthead_Blockdefine(blockname,blockdescrip);
  367. DWORD dwoldKmID;
  368. int OldDataVessel=GetDataVessel(dwoldKmID);
  369. if(!SetDataVessel(DATAVESSEL_BLOCKDEF,dwKmID))
  370. {
  371. _WRITE(str.Format("无法找到第%d号块定义",dwKmID));
  372. return FALSE;
  373. }
  374. KmLine* pLine;
  375. int size=buff.GetLineBuffSize();
  376. for(int i=0;i<size;i++)
  377. {
  378. pLine=new KmLine;
  379. pLine->SetBegin(buff.GetLineEnt(i)->P1);
  380. pLine->SetEnd(buff.GetLineEnt(i)->P2);
  381. pLine->color=COLOR_BYBLOCK;
  382. pLine->style=buff.GetLineEnt(i)->LineType;
  383. pLine->SetWidth(ABS_THIN_WIDTH);
  384. AddObject(pLine,FALSE);
  385. }
  386. KmCircle* pCircle;
  387. size=buff.GetCircleBuffSize();
  388. for(i=0;i<size;i++)
  389. {
  390. pCircle=new KmCircle;
  391. pCircle->SetCenter(buff.GetCircleEnt(i)->center);
  392. pCircle->SetRadius(buff.GetCircleEnt(i)->radius);
  393. pCircle->SetBAngle(buff.GetCircleEnt(i)->b_angle);
  394. pCircle->SetEAngle(buff.GetCircleEnt(i)->e_angle);
  395. pCircle->message=buff.GetCircleEnt(i)->message;
  396. pCircle->color=COLOR_BYBLOCK;
  397. pCircle->style=buff.GetCircleEnt(i)->LineType;
  398. pCircle->SetWidth(ABS_THIN_WIDTH);
  399. AddObject(pCircle,FALSE);
  400. }
  401. if(!SetDataVessel(OldDataVessel,dwoldKmID))
  402. {
  403. _WRITE(str.Format("无法找到第%d号块定义",dwKmID));
  404. return FALSE;
  405. }
  406. if(!m_pActiveBlockDef)return FALSE;
  407. m_pActiveBlockDef->SetBasePt(po);
  408. KmBlockRef* pRef=new KmBlockRef;
  409. pRef->SetBlockDefID(m_pActiveBlockDef->GetID());
  410. pRef->SetInsertPoint(po);
  411. AddObject(pRef);
  412. return TRUE;
  413. }