// cdrawDoc.cpp : implementation of the CcdrawDoc class (continued...) // #include "stdafx.h" #include "femm.h" #include "cdrawDoc.h" #include "cdrawView.h" #include "cd_probdlg.h" #include "cd_PtProp.h" #include "cd_OpBlkDlg.h" #include "cd_OpNodeDlg.h" #include "cd_OpSegDlg.h" #include "cd_OpArcSegDlg.h" #include "ArcDlg.h" #include "DXFImport.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern BOOL bLinehook; //----------------------------------------------------------------- BOOL CcdrawDoc::ReadDXF(CString fname,double DefTol) { FILE *fp; int j,k; CArray layerlist; CString myLayer; if( (fp=fopen(fname,"rt"))==NULL ) return FALSE; NoDraw=TRUE; // clear out any old drawing that we might have; // nodelist.RemoveAll(); // linelist.RemoveAll(); // arclist.RemoveAll(); // blocklist.RemoveAll(); // undonodelist.RemoveAll(); // undolinelist.RemoveAll(); // undoarclist.RemoveAll(); // undoblocklist.RemoveAll(); // nodeproplist.RemoveAll(); // lineproplist.RemoveAll(); // blockproplist.RemoveAll(); // circproplist.RemoveAll(); // meshnode.RemoveAll(); // meshline.RemoveAll(); // greymeshline.RemoveAll(); char s[256]; char v[256]; BOOL PolylineFlag=FALSE; CNode n[4]; CSegment segm; CArcSegment asegm; CComplex c,p,q; double R,a0,a1; int xx,PolyLineClosed,firstpoint; double angle=0; while (fgets(s,256,fp)!=NULL) { if (strncmp(s,"LAYER",5)==0) { do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==2){ sscanf(v,"%s",s); myLayer=s; layerlist.Add(myLayer); } } while(1>0); } if (strncmp(s,"POINT",5)==0) { xx=0; n[0].InGroup=0; do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==10) { n[0].x=atof(v); xx=xx | 1; } if (k==20) { n[0].y=atof(v); xx=xx | 2; } if (k==8){ sscanf(v,"%s",s); myLayer=s; for(j=0;j0); if (xx==3) nodelist.Add(n[0]); } if (strncmp(s,"LWPOLYLINE",10)==0) { int segs=0; PolyLineClosed=FALSE; firstpoint=-1; angle = 0; n[0].InGroup=0; segm.InGroup=0; asegm.InGroup=0; int n0,n1; xx=0; do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==8){ sscanf(v,"%s",s); myLayer=s; for(j=0;j360.) angle=360.; } if (k==70) PolyLineClosed=TRUE; if (xx==3){ j=(int) nodelist.GetSize(); nodelist.Add(n[0]); xx=0; if(segs==0) firstpoint=j; else{ if(angle==0) { segm.n0=j; segm.n1=j-1; linelist.Add(segm); } else{ if(angle>0) { n0=j-1; n1=j; } else{ n0=j; n1=j-1; angle=fabs(angle); } if(angle>180.) { CComplex p0,p1,p2; angle/=2.; p0=nodelist[n0].x + I*nodelist[n0].y; p1=nodelist[n1].x + I*nodelist[n1].y; p2=(p0+p1)/2. - I*(1.-cos(angle*PI/180.))*(p1-p0)/ (2.*sin(angle*PI/180.)); n[0].x=Re(p2); n[0].y=Im(p2); nodelist.InsertAt(j,n[0]); asegm.ArcLength=angle; asegm.MaxSideLength=5.; if (n00); if ((PolyLineClosed==TRUE) && (firstpoint>=0) && (segs>0)) { // take care of closing the contour, if required. j=(int) nodelist.GetSize()-1; if(angle==0) { segm.n0=j; segm.n1=firstpoint; linelist.Add(segm); } else{ if(angle>0) { n0=j; n1=firstpoint; } else{ n0=firstpoint; n1=j; angle=fabs(angle); } if(angle>180.) { CComplex p0,p1,p2; angle/=2.; p0=nodelist[n0].x + I*nodelist[n0].y; p1=nodelist[n1].x + I*nodelist[n1].y; p2=(p0+p1)/2. - I*(1.-cos(angle*PI/180.))*(p1-p0)/ (2.*sin(angle*PI/180.)); n[0].x=Re(p2); n[0].y=Im(p2); nodelist.Add(n[0]); j++; asegm.ArcLength=angle; asegm.MaxSideLength=5.; if (n0>n1){ asegm.n0=n0; asegm.n1=j; } else{ asegm.n0=j; asegm.n1=n1; } arclist.Add(asegm); if(n1==j-1) n1=j; else n0=j; } asegm.n0=n0; asegm.n1=n1; asegm.ArcLength=angle; asegm.MaxSideLength=5.; arclist.Add(asegm); angle=0; } } } if (strncmp(s,"POLYLINE",8)==0) { PolylineFlag=-1; firstpoint=-1; PolyLineClosed=FALSE; do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==70) { j=atoi(v); if (j==1) PolyLineClosed=TRUE; } } while(1>0); } if (strncmp(s,"SEQEND",6)==0) { PolylineFlag=FALSE; int j = ((int) nodelist.GetSize())-1; if ((PolyLineClosed==TRUE) && (firstpoint>=0)) { if (angle == 0.0) { segm.n0=j; segm.n1=firstpoint; linelist.Add(segm); } else { int n0, n1; if(angle>0) { n0=j; n1=firstpoint; } else{ n0=firstpoint; n1=j; angle=fabs(angle); } if(angle>180.) { CComplex p0,p1,p2; angle/=2.; p0=nodelist[n0].x + I*nodelist[n0].y; p1=nodelist[n1].x + I*nodelist[n1].y; p2=(p0+p1)/2. - I*(1.-cos(angle*PI/180.))*(p1-p0)/ (2.*sin(angle*PI/180.)); n[0].x=Re(p2); n[0].y=Im(p2); nodelist.Add(n[0]); j++; asegm.ArcLength=angle; asegm.MaxSideLength=5.; if (n0>n1){ asegm.n0=n0; asegm.n1=j; } else{ asegm.n0=j; asegm.n1=n1; } arclist.Add(asegm); if(n1==j-1) n1=j; else n0=j; } asegm.n0=n0; asegm.n1=n1; asegm.ArcLength=angle; asegm.MaxSideLength=5.; arclist.Add(asegm); } } PolyLineClosed=FALSE; angle = 0.0; } if (strncmp(s,"VERTEX",6)==0) { n[0].InGroup=0; segm.InGroup=0; asegm.InGroup=0; double next_angle = 0.0; xx=0; do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==8){ sscanf(v,"%s",s); myLayer=s; for(j=0;j360.) next_angle=360.; } } while(1>0); if(xx==3) { nodelist.Add(n[0]); if (angle != 0.0) { j = (int)nodelist.GetSize(); int n0, n1; if(angle>0) { n0=j-2; n1=j-1; } else{ n0=j-1; n1=j-2; angle=fabs(angle); } if(angle>180.) { CComplex p0,p1,p2; angle/=2.; p0=nodelist[n0].x + I*nodelist[n0].y; p1=nodelist[n1].x + I*nodelist[n1].y; p2=(p0+p1)/2. - I*(1.-cos(angle*PI/180.))*(p1-p0)/ (2.*sin(angle*PI/180.)); n[0].x=Re(p2); n[0].y=Im(p2); nodelist.InsertAt(j,n[0]); asegm.ArcLength=angle; asegm.MaxSideLength=5.; if (n00); if (xx==15) { j=(int) nodelist.GetSize(); nodelist.Add(n[0]); nodelist.Add(n[1]); segm.n0=j; segm.n1=j+1; linelist.Add(segm); } } // catch ARCALIGNEDTEXT, which derails the ARC code... if (strncmp(s,"ARCA",4)==0) s[0]=NULL; if (strncmp(s,"ARC",3)==0) { n[0].InGroup=0; n[1].InGroup=0; asegm.InGroup=0; xx=0; do{ if (fgets(s,256,fp)==NULL) break; k=atoi(s); if (k==0) break; fgets(v,256,fp); if (k==8){ sscanf(v,"%s",s); myLayer=s; for(j=0;j0); if(xx==31) { if (a10); if(xx==7) { n[0].x=c.re+R; n[1].x=c.re-R; n[0].y=c.im; n[1].y=c.im; j=(int) nodelist.GetSize(); nodelist.Add(n[0]); nodelist.Add(n[1]); asegm.n0=j; asegm.n1=j+1; asegm.MaxSideLength=5.; asegm.ArcLength=180.; arclist.Add(asegm); asegm.n1=j; asegm.n0=j+1; arclist.Add(asegm); } } } fclose(fp); // could be that nothing actually got read. We want to // catch this case and tell the user about it. if(nodelist.GetSize()==0) return FALSE; // suggest the proper tolerance CComplex p0,p1; CDXFImport dlg; int i; p0=nodelist[0].CC(); p1=p0; for(i=1;ip1.re) p1.re=nodelist[i].x; if(nodelist[i].yp1.im) p1.im=nodelist[i].y; } R=abs(p1-p0)*1.e-04; dlg.m_dxftol=floor(R/pow(10.,floor(log10(R))))*pow(10.,floor(log10(R))); if(DefTol<0) { dlg.DoModal(); FancyEnforcePSLG(dlg.m_dxftol); } else { if (DefTol==0) DefTol=dlg.m_dxftol; FancyEnforcePSLG(DefTol); } NoDraw=FALSE; return TRUE; } void CcdrawDoc::RotateMove(CComplex c, double t, int EditAction) { int i; CComplex x,z; z=exp(I*t*PI/180); if(EditAction==0) { for(i=0;i newnodelist; CArray< CSegment, CSegment&> newlinelist; CArray< CArcSegment, CArcSegment&> newarclist; CArray< CBlockLabel, CBlockLabel&> newblocklist; int i; CComplex p0,p1; double d; newnodelist.RemoveAll(); newlinelist.RemoveAll(); newarclist.RemoveAll(); newblocklist.RemoveAll(); for(i=0;ip1.re) p1.re=newnodelist[i].x; if(newnodelist[i].yp1.im) p1.im=newnodelist[i].y; } d=abs(p1-p0)*CLOSE_ENOUGH; } } else d=tol; // put in all of the nodes; for(i=0;i newnodes; newnodes.RemoveAll(); n0=ClosestNode(p0.re,p0.im); n1=ClosestNode(p1.re,p1.im); // don't add if line is degenerate if (n0==n1) return FALSE; // don't add if the line is already in the list; for(i=0;i0) for(k=0;kp1.re) p1.re=nodelist[i].x; if(nodelist[i].yp1.im) p1.im=nodelist[i].y; } t=abs(p1-p0)*CLOSE_ENOUGH; } } else t=tol; for(i=0;i newnodes; double R,d,dmin,t; asegm.n0=ClosestNode(p0.re,p0.im); asegm.n1=ClosestNode(p1.re,p1.im); newnodes.RemoveAll(); // don't add if line is degenerate if (asegm.n0==asegm.n1) return FALSE; // don't add if the arc is already in the list; for(i=0;i0) for(k=0;k0) for(k=0;kp1.re) p1.re=nodelist[i].x; if(nodelist[i].yp1.im) p1.im=nodelist[i].y; } t=abs(p1-p0)*CLOSE_ENOUGH; } } else t=tol; for(i=0;iextmaxx) extmaxx=nodelist[i].x; if(nodelist[i].yextmaxy) extmaxy=nodelist[i].y; } } for(i=0;iextmaxx) extmaxx=p.re; if(p.imextmaxy) extmaxy=p.im; } } p.Set(extminx,extminy); s.Set(extmaxx,extmaxy); R=0.025*abs(s-p); // check out which layers are actually called out; for(i=0,nlaze=0;i<256;i++) laze[i]=FALSE; for(i=0;i newnodelist; CArray< CSegment, CSegment&> newlinelist; CArray< CArcSegment, CArcSegment&> newarclist; CArray< CBlockLabel, CBlockLabel&> newblocklist; int i,k; CComplex p0,p1; double d; bLinehook=ImportDXF; // kludge to stop the program from giving a crash if // the user tries to exit during a dxf import. CcdrawView *pView; POSITION pos; pos=GetFirstViewPosition(); pView=(CcdrawView *)GetNextView(pos); FirstDraw=TRUE; // pView->lua_zoomnatural(); dxf_line_hook(); pView->EditAction=4; newnodelist.RemoveAll(); newlinelist.RemoveAll(); newarclist.RemoveAll(); newblocklist.RemoveAll(); for(i=0;iInvalidateRect(NULL); dxf_line_hook(); // find out what tolerance is so that there are not nodes right on // top of each other; if(tol==0){ if (newnodelist.GetSize()<2) d=1.e-08; else{ p0=newnodelist[0].CC(); p1=p0; for(i=1;ip1.re) p1.re=newnodelist[i].x; if(newnodelist[i].yp1.im) p1.im=newnodelist[i].y; } d=abs(p1-p0)*CLOSE_ENOUGH; } } else d=tol; // put in all of the lines; for(i=0;iDrawPSLG(); if(dxf_line_hook()){ bLinehook=FALSE; return; } } } // put in all of the arcs; for(i=0;iDrawPSLG(); if(dxf_line_hook()){ bLinehook=FALSE; return; } } } UnselectAll(); // do one last check to eliminate shallow arcs that // link up the same two points as a line segment; for(i=0;i