void CVTestView::OnDraw(CDC* pDC) { CVTestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: この場所にネイティブ データ用の描画コードを追加します。 CRect r; GetClientRect(r); pDC->SetMapMode(MM_ANISOTROPIC); pDC->SetWindowExt(1000, 600); // 窓の論理サイズの指定 pDC->SetViewportExt(r.right, -r.bottom); //ビューポートの指定 pDC->SetViewportOrg(r.right/2, r.bottom/2); //原点の指定 CPen p(PS_SOLID, 1, RGB(0, 255, 0)); CPen* oldp=pDC->SelectObject(&p); pDC->MoveTo(-500, 0); pDC->LineTo(500, 0); pDC->MoveTo(0,300); pDC->LineTo(0, -300); FILE *fp; char *str,buf[1000]; char subbuf[100],*s1; char buf2[100],*s2; char buf3[100],*s3; char buf4[100],*s4; static double bx=0; static double by=0; if ((fp = fopen("test.txt", "r")) == NULL) { printf("ファイルが開けません\n"); return EXIT_SUCCESS; } while (fgets(buf, 1000, fp) != NULL) { str=buf; while((*str!='\0'){ if(*str!='\0' && *str=='G'){ *str++; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) s2=buf2; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) while(*str!='\0'&&(isdigit(*str) || *str=='-' || *str=='.')) *s2++=*str++; *s2='\0'; } if(*str!='\0' && *str=='X'){ *str++; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) s3=buf3; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) while(*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) *s3++=*str++; *s3='\0'; } if(*str!='\0' && *str=='Y'){ *str++; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) s4=buf4; if((*str!='\0' &&(isdigit(*str) || *str=='-' || *str=='.')) while(*str!='\0' &&(isdigit(*str) |*str=='||*str=='.')) *s4++=*str++; *s4='\0'; } else str++; } if((int)atof(buf2)==92){ pDC->MoveTo((int)atof(buf3),(int)atof(buf4)); bx=(int)atof(buf3); by=(int)atof(buf4); } else if((int)atof(buf2)==01){ pDC->MoveTo((int)bx,(int)by); pDC->LineTo((int)atof(buf3),(int)atof(buf4)); bx=(int)atof(buf3); by=(int)atof(buf4); } } fclose(fp); return 0; }
色々使ってみたんですが、nonさんの原点移動を使用させていただくことにしました。
それでこういうプログラムに結局なったんですけども1つだけ欠点があり、このプログラムは
①原点移動、ビューポートの指定など行う
②ファイルを読み込みG,X,Y,I,Jに付属する数字を格納
③その数字をもとに図形を作図する
というプログラムになってるわけですが、
格納の作業はokなんですが図形作図に問題があってどんな場合にも作図の作業を行う。つまり
G01X100Y100
G03X200Y200I50J50
M00
03のプログラムはまだ書いてないんですがこれは円弧の作図になります。
問題はこのプログラムだと
G01で直線を書く→G03で円弧を書く→M00でも前の値が残っていてG03と同じ円弧作画作業をしてしまいます。
要は図形描画の作業を今はどんなときにも行っていますが、G,X,Yの文字を読み込まれたときに限定したいわけです。
それで図形を書くプログラムの所に、つまり
if(*str!='\0' &&(*str=='G' || *str=='X' || *str=='Y')){ if((int)atof(buf2)==92){ pDC->MoveTo((int)atof(buf3),(int)atof(buf4)); bx=(int)atof(buf3); by=(int)atof(buf4); } else if((int)atof(buf2)==01){ pDC->MoveTo((int)bx,(int)by); pDC->LineTo((int)atof(buf3),(int)atof(buf4)); bx=(int)atof(buf3); by=(int)atof(buf4); } }にこうして入れてみたんですが図形がさっぱり作画されなくなりました。
原因と対策を教えてください。