⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paintdoc.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
}

void CPaintobjDoc::OnUpdateViewZoomin(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bZoomIn);	
	// TODO: Add your command update UI handler code here
	
}

void CPaintobjDoc::OnViewZoomout() //图象放大
{	
	m_bZoomIn= FALSE;
	m_bZoomOut= TRUE;
	m_nSelectedTool = IDC_CURSORZOOMIN;
	GenerateTool();	// TODO: Add your command handler code here	
}

void CPaintobjDoc::OnUpdateViewZoomout(CCmdUI* pCmdUI) 
{
		pCmdUI->SetCheck(m_bZoomOut);
	// TODO: Add your command update UI handler code here	
}

CPainted * CPaintobjDoc::GetShape(int item)
{
    return m_Objects[item];
}


int CPaintobjDoc::NumShapes()
{
    return m_Objects.GetSize();
}

void CPaintobjDoc::GetData()//先组织节点数组后组织路径数组
{
	int nItems = m_Objectsnode.GetSize();
    for (int i = 0; i < nItems; i++)
    {
        delete m_Objectsnode[i];
    }
	m_Objectsnode.RemoveAll();

	nItems = m_Objectspath.GetSize();
    for (int j = 0; j < nItems; j++)
    {
        delete m_Objectspath[j];
    }
	m_Objectspath.RemoveAll();
	UINT index;
	UINT nShapes;
	CPainted*	pPainted;
	nShapes = m_Objects.GetSize();
    for (index=0;index<m_uCurNodeIdMax;index++)//建立节点数组
    {        
        for (UINT i= 0; i< nShapes; i++)
		{	pPainted =(CPainted*) m_Objects[i];
			if( pPainted->m_uStartNodeId==index && pPainted->m_uObjectType==0)
			{
				m_pNode= new CNode;
				m_pNode->m_uId=(USHORT) pPainted->m_uStartNodeId;
				m_pNode->m_uType=(USHORT) pPainted->m_uStartNodeType;
				m_pNode->m_uExtType=(USHORT) pPainted->m_uStartNodeExtType;
				m_pNode->x=(float) pPainted->m_pointPrimary.x;
				m_pNode->y=(float) pPainted->m_pointPrimary.y;
				m_pNode->m_fHeading=(float) pPainted->m_fStartHeading;
				m_Objectsnode.Add(m_pNode);								 
				m_pNode = NULL;
				break;
			}
			else if( pPainted->m_uEndNodeId==index && pPainted->m_uObjectType==0)
			{
				m_pNode= new CNode;
				m_pNode->m_uId=(USHORT) pPainted->m_uEndNodeId;
				m_pNode->m_uType=(USHORT) pPainted->m_uEndNodeType;
				m_pNode->m_uExtType=(USHORT) pPainted->m_uEndNodeExtType;
				m_pNode->x=(float) pPainted->returnattr2().x;
				m_pNode->y=(float) pPainted->returnattr2().y;
				m_pNode->m_fHeading=(float) pPainted->m_fEndHeading;
				m_Objectsnode.Add(m_pNode);					 
				m_pNode = NULL;
				break;
			}
		}
	 }
	nShapes = m_Objects.GetSize();
	for (index = 0; index <m_uCurPathIdMax; index++)//建立路径数组
    {
        for (UINT i= 0; i< nShapes; i++)
		{	pPainted =(CPainted*) m_Objects[i];
			if( pPainted->m_uPathId==index && pPainted->m_uObjectType==0)
			{	if(pPainted->m_uFigureType==0)//line
				{
					m_pPath= new CLinePath;
					m_pPath->m_uType=(USHORT) pPainted->m_uFigureType;
					m_pPath->m_uId=(USHORT) pPainted->m_uPathId;
					m_pPath->m_uStartNode=(USHORT) pPainted->m_uStartNodeId;
					m_pPath->m_uEndNode=(USHORT) pPainted->m_uEndNodeId;										
					m_pPath->m_fVeloLimit=(float) pPainted->m_fPathLimit;
					m_pPath->m_uGuideType=(SHORT) pPainted->m_uPathGuideType;
					m_pPath->m_uExtType=(USHORT) pPainted->m_uPathExtType;
					((CLinePath*) m_pPath)->m_uObstacle=(USHORT) pPainted->m_uPathObstacle;
					((CLinePath*) m_pPath)->m_uHeading=(USHORT) pPainted->m_uPathHeading;
					m_Objectspath.Add(m_pPath);									 
					m_pPath = NULL;
				}
				else if(pPainted->m_uFigureType==1)//arc
				{
					m_pPath= new CArcPath;
					m_pPath->m_uType=(USHORT) ((CPaintArc*) pPainted)->m_nArcType;
					m_pPath->m_uId=(USHORT) pPainted->m_uPathId;
					m_pPath->m_uStartNode=(USHORT) pPainted->m_uStartNodeId;
					m_pPath->m_uEndNode=(USHORT) pPainted->m_uEndNodeId;									
					m_pPath->m_fVeloLimit=(float) pPainted->m_fPathLimit;
					m_pPath->m_uGuideType=(SHORT) pPainted->m_uPathGuideType;
					m_pPath->m_uExtType=(USHORT) pPainted->m_uPathExtType;
					((CArcPath*) m_pPath)->m_uObstacle=(USHORT) pPainted->m_uPathObstacle;
					((CArcPath*) m_pPath)->m_uHeading=(USHORT) pPainted->m_uPathHeading;
					((CArcPath*) m_pPath)->m_fCenterx=(float) ((CPaintArc*) pPainted)->m_pcirclecenter.x;
					((CArcPath*) m_pPath)->m_fCentery=(float) ((CPaintArc*) pPainted)->m_pcirclecenter.y;
					((CArcPath*) m_pPath)->m_uDirection=(USHORT) ((CPaintArc*) pPainted)->m_uArcPathDirection;
					m_Objectspath.Add(m_pPath);								 
					m_pPath = NULL;
				}
				else if(pPainted->m_uFigureType==2)//lazy-s
				{
					m_pPath= new CLazysPath;
					m_pPath->m_uType=LAZY_S_TYPE;
					m_pPath->m_uId=(USHORT) pPainted->m_uPathId;
					m_pPath->m_uStartNode=(USHORT) pPainted->m_uStartNodeId;
					m_pPath->m_uEndNode=(USHORT) pPainted->m_uEndNodeId;									
					m_pPath->m_fVeloLimit=(float) pPainted->m_fPathLimit;
					m_pPath->m_uGuideType=(SHORT) pPainted->m_uPathGuideType;
					m_pPath->m_uExtType=(USHORT) pPainted->m_uPathExtType;
					((CLazysPath*) m_pPath)->m_angStart=(float) ((CPaintLazys*) pPainted)->m_angStart;
					m_Objectspath.Add(m_pPath);								 
					m_pPath = NULL;
				}
				//其它路径
				break;
			}
		}
	 }
}


void CPaintobjDoc::SaveBin1(CArchive& ar)
{
	CFile  file1;
	BOOL bbb=file1.Open("world.bin",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
	if(!bbb)//打开文件不成功
	{
		AfxMessageBox("打开文件不成功!");
		return;
	}
	CString str1="w";
	USHORT  ver=12345; 
	CString str2="2000/9/14";
	CString str3="GOOD MORNING";
	//char * str1="w";
	//USHORT  ver=12345; 
	//char * str2="2000/9/14";
	//char * str3="GOOD MORNING";
	USHORT count=m_Objectsnode.GetSize();
	file1.Write(str1,1);
	file1.Write((unsigned char *)&ver,sizeof(ver));
	file1.Write(str2,9);
	file1.Write(str3,12);
	file1.Write((unsigned char *)&count,sizeof(count));		
	//ar<<"w";
	//ar<<12345;
	//ar<<"2000/9/14";
	//ar<<"GOOD MORNING";
	//ar<<m_Objectsnode.GetSize();

	// Load the nodes data one by one
	for (USHORT i = 0; i < m_Objectsnode.GetSize(); i++)
	{
		CNode* pNode = m_Objectsnode[i];
		//ar << pNode->m_uId << pNode->m_uType << pNode->m_uExtType
		//<< pNode->x << pNode->y<<pNode->m_fHeading;
		file1.Write((unsigned char *)&pNode->m_uId,sizeof(pNode->m_uId));	
		file1.Write((unsigned char *)&pNode->m_uType,sizeof(pNode->m_uType));	
		file1.Write((unsigned char *)&pNode->m_uExtType,sizeof(pNode->m_uExtType));	
		file1.Write((unsigned char *)&pNode->x,sizeof(pNode->x));	
		file1.Write((unsigned char *)&pNode->y,sizeof(pNode->y));	
		file1.Write((unsigned char *)&pNode->m_fHeading,sizeof(pNode->m_fHeading));	
	}
	//ar << m_Objectspath.GetSize();
	USHORT count1=m_Objectspath.GetSize();
	file1.Write((unsigned char *)&count1,sizeof(count1));
	// Save the path objects one by one
	for (USHORT j = 0; j < m_Objectspath.GetSize(); j++)
	{
		CPath* pPath = m_Objectspath[j];
		// First, Save the type of path
		//ar << pPath->m_uType;
		file1.Write((unsigned char *)&pPath->m_uType,sizeof(pPath->m_uType));
		if(pPath->m_uType==0)
		{
			//ar << pPath->m_uId
			//<< pPath->m_uStartNode
			//<< pPath->m_uEndNode
			//<< pPath->m_fVeloLimit
			//<< pPath->m_uGuideType
			//<< pPath->m_uExtType
			//<< ((CLinePath*) pPath)->m_uObstacle
			//<< ((CLinePath*) pPath)->m_uHeading;
			file1.Write((unsigned char *)&pPath->m_uId,sizeof(pPath->m_uId));
			file1.Write((unsigned char *)&pPath->m_uStartNode,sizeof(pPath->m_uStartNode));
			file1.Write((unsigned char *)&pPath->m_uEndNode,sizeof(pPath->m_uEndNode));
			file1.Write((unsigned char *)&pPath->m_fVeloLimit,sizeof(pPath->m_fVeloLimit));
			file1.Write((unsigned char *)&pPath->m_uGuideType,sizeof(pPath->m_uGuideType));
			file1.Write((unsigned char *)&pPath->m_uExtType,sizeof(pPath->m_uExtType));
			file1.Write((unsigned char *)&((CLinePath*) pPath)->m_uObstacle,sizeof(((CLinePath*) pPath)->m_uObstacle));
			file1.Write((unsigned char *)&((CLinePath*) pPath)->m_uHeading,sizeof(((CLinePath*) pPath)->m_uHeading));
		}
		else if(pPath->m_uType==1)
		{	
			//ar <<pPath->m_uId
		    //<< pPath->m_uStartNode
			//<< pPath->m_uEndNode
			//<< pPath->m_fVeloLimit
			//<< pPath->m_uGuideType
			//<< pPath->m_uExtType
			//<< ((CArcPath*) pPath)->m_uObstacle
			//<< ((CArcPath*) pPath)->m_uHeading
			//<< ((CArcPath*) pPath)->m_fCenterx
			//<< ((CArcPath*) pPath)->m_fCentery
			//<< ((CArcPath*) pPath)->m_uDirection;
			file1.Write((unsigned char *)&pPath->m_uId,sizeof(pPath->m_uId));
			file1.Write((unsigned char *)&pPath->m_uStartNode,sizeof(pPath->m_uStartNode));
			file1.Write((unsigned char *)&pPath->m_uEndNode,sizeof(pPath->m_uEndNode));
			file1.Write((unsigned char *)&pPath->m_fVeloLimit,sizeof(pPath->m_fVeloLimit));
			file1.Write((unsigned char *)&pPath->m_uGuideType,sizeof(pPath->m_uGuideType));
			file1.Write((unsigned char *)&pPath->m_uExtType,sizeof(pPath->m_uExtType));
			file1.Write((unsigned char *)&((CArcPath*) pPath)->m_uObstacle,sizeof(((CArcPath*) pPath)->m_uObstacle));
			file1.Write((unsigned char *)&((CArcPath*) pPath)->m_uHeading,sizeof(((CArcPath*) pPath)->m_uHeading));
			file1.Write((unsigned char *)&((CArcPath*) pPath)->m_fCenterx,sizeof(((CArcPath*) pPath)->m_fCenterx));
			file1.Write((unsigned char *)&((CArcPath*) pPath)->m_fCentery,sizeof(((CArcPath*) pPath)->m_fCentery));
			file1.Write((unsigned char *)&((CArcPath*) pPath)->m_uDirection,sizeof(((CArcPath*) pPath)->m_uDirection));
		}
	}
	file1.Close();
}
//
//返回值为0表示正常读取文件
//返回非0值表示下一次应采用的版本号
//
UINT CPaintobjDoc::LoadBin(CArchive& ar,CString extname,USHORT ver)
{
	extname.MakeLower();
	if(extname=="map")
	{
		if(ar.IsLoading())
		{
			ar >>fxmin;
			ar >>fxmax;
			ar >>fymin;
			ar >>fymax;
			ar >>m_uCurNodeIdMax;
			ar >>m_uCurPathIdMax;			
			m_Objects.Serialize(ar);			
		}		
	}
	else if(extname=="dat" || extname=="bin1" || extname=="bin2") //支持的文件格式
	{
		//clear the node bank and path bank
		int nItems = m_Objectsnode.GetSize();
		for (int i = 0; i < nItems; i++)
		{
			delete m_Objectsnode[i];
		}
		m_Objectsnode.RemoveAll();
		
		nItems = m_Objectspath.GetSize();
		for (int j = 0; j < nItems; j++)
		{
			delete m_Objectspath[j];
		}
		m_Objectspath.RemoveAll();

		if(ver==2)//版本2
		{
			char str1;//='W';
			USHORT version;//=ver; 	
			CTime Time;// = CTime::GetCurrentTime();	
			CString str2;//="GOOD MORNING";
			
			ar >> str1;	
			ar >> version;			
			if(str1=='W'&&version==2)//版本2
			{
				ar >> Time;
				ar >> str2;
				USHORT count;	
				ar >> count;//node count
				for (USHORT i = 0; i < count; i++)
				{
					CNode* pNode = new CNode;
					pNode->Create(ar,ver);
					m_Objectsnode.Add(pNode);	
				}

				ar >> count;//path count				
				for (USHORT j = 0; j < count; j++)
				{
					USHORT type;
					CPath* pPath;
					ar >> type;				
					switch(type)
					{
					case 0:
						pPath = new CLinePath;
						break;						
					case 1:
					case 2:
					case 6:
						pPath = new CArcPath;
						break;
					case 5:
						pPath = new CLazysPath;
						break;
					}
					// No enough memory, return FALSE
					assert(pPath != NULL);					
					// If failed to load data, return FALSE
					if (!pPath->Create(ar,ver))
						assert(FALSE);					
					pPath->m_uType = type;
					m_Objectspath.Add(pPath);					
				}
			}
			else//
			{
				return 1;
			}
		}
		else if(ver==1)//版本1
		{
			USHORT count;	
			ar >> count;//node count
			for (USHORT i = 0; i < count; i++)
			{
				CNode* pNode = new CNode;
				pNode->Create(ar,ver);
				m_Objectsnode.Add(pNode);	
			}
			
			ar >> count;//path count				
			for (USHORT j = 0; j < count; j++)
			{
				USHORT type;
				CPath* pPath;
				ar >> type;				
				switch(type)
				{
				case 0:
					pPath = new CLinePath;
					break;						
				case 1:
				case 2:
				case 6:
					pPath = new CArcPath;
					break;	
				case 5:
					pPath = new CLazysPath;
					break;
				}
				// No enough memory, return FALSE
				assert(pPath != NULL);					
				// If failed to load data, return FALSE
				if (!pPath->Create(ar,ver))
					assert(FALSE);					
				pPath->m_uType = type;
				m_Objectspath.Add(pPath);				
			}
		}	
		//其它版本
		//else if(ver==other)
		DataConversion();
		//得到m_uCurNodeIdMax其值比最大的节点编号大1。
		GetNodeIdMax();
		//得到m_uCurPathIdMax其值比最大的路径编号大1。
		GetPathIdMax();
		CRect rect;
		ExpandBounds(&rect);					

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -