www.pudn.com > roll.rar > FIGURE.CPP


// Figure.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "roll.h" 
#include "Figure.h" 
#include "geomwnd.h" 
#include "bbroll.h" 
#include "comnfun.h" 
 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFigure 
 
CFigure::CFigure(CString filen) 
{ 
	filename=filen; 
	Type=1; 
} 
 
CFigure::~CFigure() 
{ 
} 
 
BEGIN_MESSAGE_MAP(CFigure, CWnd) 
	//{{AFX_MSG_MAP(CFigure) 
	ON_WM_PAINT() 
	ON_WM_RBUTTONDOWN() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFigure message handlers 
 
BOOL CFigure::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext); 
} 
 
// the function Line_Line_Int(...) calculate the intersection points of 
// the line and the line describled below: 
// the one line equation is  x = x1+(xx1-x1)*t1 and y = y1+(yy1-y1)*t1; 
// another line equation is  x = x2+(xx2-x2)*t2 and y = y2+(yy2-y2)*t2; 
// the output is the t1 and t2 parameters. 
// return 0 implied the two line section is not intersection. 
// return -1 implied the two lines are paralell. 
// return 1 implied the interpoint is between the two given points, 
// return 2 implied the interpoint located the first line, 
// return 3 implied the interpoint located the second line. 
/* 
int Line_Line_Int(double x1,double y1,double xx1,double yy1,double x2,double y2,double xx2,double yy2,double& t1,double& t2) 
{ 
	double dx1,dy1,dx2,dy2; 
	dx1 = xx1-x1; 
	dy1 = yy1-y1; 
	dx2 = xx2-x2; 
	dy2 = yy2-y2; 
	double t; 
	t = dx2*dy1-dx1*dy2; 
	if( t == 0.0 ) return -1; 
	t = 1.0/t; 
	t1 = (dx2*(y2-y1)-dy2*(x2-x1))*t; 
	t2 = (dx1*(y2-y1)-dy1*(x2-x1))*t; 
	if( t1 > 0.0 && t1 < 1.0 && t2 > 0.0 && t2 < 1.0 ) return 1; 
	if((t1 == 0.0 || t1 == 1.0) && (t2 == 0.0 || t2 == 1.0)) return 4; 
	if( t1 == 0.0 || t1 == 1.0 ) return 2; 
	if( t2 == 0.0 || t2 == 1.0 ) return 3; 
	return 0; 
} 
 
int Line_Line_Int(double x1,double y1,double xx1,double yy1,double x2,double y2,double xx2,double yy2,double *x,double *y) 
{ 
	int  ret; 
	double tt1,tt2; 
	ret = Line_Line_Int(x1,y1,xx1,yy1,x2,y2,xx2,yy2,tt1,tt2); 
	if(ret == 0) return 0; 
	if(ret == -1) { 
		*x = *y = 0.0; 
		return -1; 
		} 
	*x = x1+(xx1-x1)*tt1; 
	*y = y1+(yy1-y1)*tt1; 
	return ret; 
} 
*/ 
void CFigure::DrawRoller(CDC* dc) 
{ 
	char buf[150]; 
	CStdioFile f; 
	int i,shorti,nelem,numn; 
	int code[8],tcode; 
	short in[8]={4,1,5,2,6,3,7,0}; 
	double *xp,*yp,x,y,z,xmin,xmax,ymin,ymax; 
	if( !f.Open( CONTROLFILE, 
		CFile::modeRead | CFile::typeText ) ) { 
		MessageBox("Unable open data file !","Error"); 
		return; 
		} 
	f.ReadString(buf,149); 
	sscanf(buf,"%d %d",&numn,&nelem); 
	f.Close(); 
 
	if( !f.Open( NODALFILE, 
		CFile::modeRead | CFile::typeText ) ) { 
		MessageBox("Unable open data file !","Error"); 
		return; 
		} 
	xp = new double[numn]; 
	yp = new double[numn]; 
	f.ReadString(buf,149); 
	sscanf(buf,"%d %lf %lf %lf",&shorti,&x,&y,&z); 
	xp[0] = (x+z)*0.707107; 
	yp[0] = (x-z)*0.408248+y*0.816597; 
	xmin=xmax=xp[0]; 
	ymin=ymax=yp[0]; 
	for(i=1;ixmax) xmax=xp[i]; 
		if(yp[i]ymax) ymax=yp[i]; 
	} 
	f.Close(); 
	if( !f.Open( ELEMENTFILE, 
		CFile::modeRead | CFile::typeText ) ) { 
		MessageBox("Unable open data file !","Error"); 
		delete[] xp; 
		delete[] yp; 
		return; 
	} 
	CRect rect; 
	GetClientRect(&rect); 
	dc->SetMapMode(MM_ISOTROPIC); 
	dc->SetWindowExt(1,1); 
	dc->SetViewportExt(1,-1); 
	dc->SetViewportOrg(rect.right/2,rect.bottom/2); 
	x=xmax-xmin; 
	y=ymax-ymin; 
	x=(rect.right-10)/x; 
	y=(rect.bottom-10)/y; 
	z=x>y ? y : x; 
	x=0.5*(xmax+xmin); 
	y=0.5*(ymax+ymin); 
	for(i=0;ixmax) xmax=xp[i]; 
		if(yp[i]ymax) ymax=yp[i]; 
	} 
	CRect rect; 
	GetClientRect(&rect); 
	dc->SetMapMode(MM_ISOTROPIC); 
	dc->SetWindowExt(1,1); 
	dc->SetViewportExt(1,-1); 
	dc->SetViewportOrg(rect.right/2,rect.bottom/2); 
	x=xmax-xmin; 
	y=ymax-ymin; 
	x=(rect.right-10)/x; 
	y=(rect.bottom-10)/y; 
	z=x>y ? y : x; 
	x=0.5*(xmax+xmin); 
	y=0.5*(ymax+ymin); 
	for(i=0;ixmax) xmax=xp[i]; 
		if(yp[i]ymax) ymax=yp[i]; 
	} 
	f.Close(); 
	if( !f.Open( ELEMENTFILE, 
		CFile::modeRead | CFile::typeText ) ) { 
		MessageBox("Unable open data file !","Error"); 
		delete[] xp; 
		delete[] yp; 
		delete[] zp; 
		return; 
	} 
	CRect rect; 
	GetClientRect(&rect); 
	dc->SetMapMode(MM_ISOTROPIC); 
	dc->SetWindowExt(1,1); 
	dc->SetViewportExt(1,-1); 
	dc->SetViewportOrg(rect.right/2,rect.bottom/2); 
	x=xmax-xmin; 
	y=ymax-ymin; 
	x=(rect.right-10)/x; 
	y=(rect.bottom-10)/y; 
	z=(x>y) ? y : x; 
	x=0.5*(xmax+xmin); 
	y=0.5*(ymax+ymin); 
	for(i=0;iSetTextAlign(TA_CENTER|TA_BASELINE); 
	for(i=0;i0.0001) { 
				numn=1; 
				break; 
			} 
		} 
		if(numn) continue; 
		tcode=code[0]-1; 
		MoveTo_f(dc,xp[tcode],yp[tcode]); 
		for(shorti=0;shorti<8;shorti++) { 
			tcode=in[shorti]; 
			tcode=code[tcode]-1; 
			if(tcode<0) continue; 
			LineTo_f(dc,xp[tcode],yp[tcode]); 
		} 
/*		shorti=Line_Line_Int(xp[code[0]-1],yp[code[0]-1],xp[code[2]-1],yp[code[2]-1], 
			xp[code[1]-1],yp[code[1]-1],xp[code[3]-1],yp[code[3]-1],&x,&y); 
		if(shorti<=0) continue; 
		sprintf(buf,"%1d",kkk); 
		dc->TextOut(int(x),int(y),buf); 
		Circle_f(dc,x,y,2); 
*/	} 
	f.Close(); 
	delete[] xp; 
	delete[] yp; 
	delete[] zp; 
} 
 
void CFigure::DrawRollerTranData(CDC* dc) 
{ 
	char buf[150]; 
	CStdioFile f; 
	int i,shorti,nelem,numn; 
	int code[8],tcode; 
	short in[8]={4,1,5,2,6,3,7,0}; 
	double *xp,*yp,*zp,x,y,z,xmin,xmax,ymin,ymax; 
	if( !f.Open( filename, 
		CFile::modeRead | CFile::typeText ) ) { 
		MessageBox("Unable open data file !","Error"); 
		return; 
		} 
 
	f.ReadString(buf,149); 
	while(strlen(buf)<3) 
		f.ReadString(buf,149); 
 
	f.ReadString(buf,149); 
	while(strlen(buf)<3) 
		f.ReadString(buf,149); 
	sscanf(buf,"%d %d",&numn,&nelem); 
 
	for(i=0;i<4;i++) { 
		f.ReadString(buf,149); 
		while(strlen(buf)<3) 
			f.ReadString(buf,149); 
	} 
	xp = new double[numn]; 
	yp = new double[numn]; 
	zp = new double[numn]; 
	f.ReadString(buf,149); 
	while(strlen(buf)<3) 
		f.ReadString(buf,149); 
	sscanf(buf,"%d %lf %lf %lf",&shorti,&x,&y,&z); 
	xp[0]=xmin=xmax=x; 
	yp[0]=ymin=ymax=y; 
	zp[0]=z; 
	for(i=1;ixmax) xmax=xp[i]; 
		if(yp[i]ymax) ymax=yp[i]; 
	} 
	CRect rect; 
	GetClientRect(&rect); 
	dc->SetMapMode(MM_ISOTROPIC); 
	dc->SetWindowExt(1,1); 
	dc->SetViewportExt(1,-1); 
	dc->SetViewportOrg(rect.right/2,rect.bottom/2); 
	x=xmax-xmin; 
	y=ymax-ymin; 
	x=(rect.right-10)/x; 
	y=(rect.bottom-10)/y; 
	z=(x>y) ? y : x; 
	x=0.5*(xmax+xmin); 
	y=0.5*(ymax+ymin); 
	for(i=0;iSetTextAlign(TA_CENTER|TA_BASELINE); 
	f.ReadString(buf,149); 
	while(strlen(buf)<3) 
		f.ReadString(buf,149); 
	for(i=0;i0.0001) { 
				numn=1; 
				break; 
			} 
		} 
		if(numn) goto next; 
		tcode=code[0]-1; 
		MoveTo_f(dc,xp[tcode],yp[tcode]); 
		for(shorti=0;shorti<8;shorti++) { 
			tcode=in[shorti]; 
			tcode=code[tcode]-1; 
			if(tcode<0) continue; 
			LineTo_f(dc,xp[tcode],yp[tcode]); 
		} 
/*		shorti=Line_Line_Int(xp[code[0]-1],yp[code[0]-1],xp[code[2]-1],yp[code[2]-1], 
			xp[code[1]-1],yp[code[1]-1],xp[code[3]-1],yp[code[3]-1],&x,&y); 
		if(shorti<=0) continue; 
		sprintf(buf,"%1d",kkk); 
		dc->TextOut(int(x),int(y),buf); 
		Circle_f(dc,x,y,2); 
*/	 
next: 
		f.ReadString(buf,149); 
 
	} 
	f.Close(); 
	delete[] xp; 
	delete[] yp; 
	delete[] zp; 
} 
 
void CFigure::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	_Global_Ratio=1.0; 
	if(Type==1) DrawRollerTranData( &dc ); 
	else DrawRollerData( &dc ); 
} 
 
void CFigure::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CMenu menu; 
	menu.CreatePopupMenu( ); 
	menu.AppendMenu(MF_ENABLED,1,"Transverse grid Figure"); 
	menu.AppendMenu(MF_ENABLED,2,"Global grid Figure"); 
	menu.TrackPopupMenu(TPM_CENTERALIGN|TPM_LEFTBUTTON,point.x,point.y,this); 
	CWnd::OnRButtonDown(nFlags, point); 
} 
 
BOOL CFigure::OnCommand(WPARAM wParam, LPARAM lParam)  
{ 
	if(wParam==1 || wParam==2) 
	if(wParam!=Type) { 
		Type=wParam; 
		InvalidateRect(NULL); 
	} 
	return CWnd::OnCommand(wParam, lParam); 
}