www.pudn.com > Visual C++ CAD应用程序开发技术源代码ch6.rar > Entity.cpp
// Entity.cpp: implementation of the CEntity class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Entity.h"
#include "..\inc\glContext\openGLDC.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEntity::CEntity()
{
m_bModified = FALSE;
m_id = -1;
m_pBox = NULL;
m_color = RGB(128,128,128);
m_name = _T("Unknow");
m_bVisible = TRUE;
m_bHighlight = FALSE;
}
CEntity::~CEntity()
{
}
void CEntity::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<>m_id>>m_name>>m_color>>m_bVisible;
}
BOOL CEntity::GetBox(CBox3D& box)
{
if(m_bModified)
UpdateBox();
if( m_pBox){
box = *m_pBox;
return TRUE;
}
else
return FALSE;
}
void CEntity::SetID(UINT nID)
{
m_id = nID;
}
UINT CEntity::GetID()
{
return m_id;
}
void CEntity::SetName(LPCTSTR name)
{
m_name = name;
}
CString CEntity::GetName()
{
return m_name;
}
void CEntity::SetColor(COLORREF color)
{
m_color = color;
}
COLORREF CEntity::GetColor()
{
return m_color;
}
void CEntity::SetVisible(BOOL bVisible)
{
m_bVisible = bVisible;
}
BOOL CEntity::IsVisible()
{
return m_bVisible;
}
void CEntity::SetHighlight(BOOL bHighlight)
{
m_bHighlight = bHighlight;
}
BOOL CEntity::IsHighlight()
{
return m_bHighlight;
}
///////////////////////////
//class tri chip
IMPLEMENT_SERIAL(CTriChip,CObject,0)
CTriChip::CTriChip()
{
}
CTriChip::CTriChip(const CPoint3D& v0,const CPoint3D& v1,const CPoint3D& v2,const CVector3D& nor)
{
vex[0] = v0;
vex[1] = v1;
vex[2] = v2;
normal = nor;
}
CTriChip::~CTriChip()
{
}
const CTriChip& CTriChip::operator=(const CTriChip& tri)
{
normal = tri.normal;
for(int i=0;i<3;i++)
vex[i] = tri.vex[i];
return *this;
}
void CTriChip::Draw(COpenGLDC* pDC)
{
pDC->DrawTriChip(normal.dx,normal.dy,normal.dz,
vex[0].x,vex[0].y,vex[0].z,
vex[1].x,vex[1].y,vex[1].z,
vex[2].x,vex[2].y,vex[2].z);
}
void CTriChip::Serialize(CArchive& ar)
{
if(ar.IsStoring()){
ar << normal.dx << normal.dy << normal.dz;
for(int i=0;i<3;i++)
ar << vex[i].x << vex[i].y << vex[i].z;
}
else{
ar >> normal.dx >> normal.dy >> normal.dz;
for(int i=0;i<3;i++)
ar >> vex[i].x >> vex[i].y >> vex[i].z;
}
}
//////////////////////////////////////////
//class CSTLModel
IMPLEMENT_SERIAL(CSTLModel,CObject,0)
CSTLModel::CSTLModel()
{
}
CSTLModel::~CSTLModel()
{
Clear();
}
void CSTLModel::Add(CTriChip* tri)
{
m_TriList.Add(tri);
}
void CSTLModel::UpdateBox()
{
if(m_pBox){
delete m_pBox;
m_pBox = NULL;
}
if(m_TriList.GetSize()==0)
return;
double x0,y0,z0,x1,y1,z1;
x0=y0=z0=10000;
x1=y1=z1=-10000;
CTriChip* tri;
for(int i=0;ivex[n].xvex[n].x;
if(tri->vex[n].x>x1) x1 = tri->vex[n].x;
if(tri->vex[n].yvex[n].y;
if(tri->vex[n].y>y1) y1 = tri->vex[n].y;
if(tri->vex[n].zvex[n].z;
if(tri->vex[n].z>z1) z1 = tri->vex[n].z;
}
}
m_pBox = new CBox3D(x0,y0,z0,x1,y1,z1);
m_bModified = FALSE;
}
//load with STL File
BOOL CSTLModel::LoadSTLFile(LPCTSTR stlfile)
{
FILE* file;
int type=0;
if((file = fopen(stlfile, "r")) == NULL)
return FALSE;
char str[80];
CTriChip* tri = NULL;
while(fscanf(file,"%s",str)==1){
if(strncmp(str,"normal",6)==0){
tri = new CTriChip();
fscanf(file,"%lf %lf %lf",&(tri->normal.dx),&(tri->normal.dy),&(tri->normal.dz));
fscanf(file,"%*s %*s");
fscanf(file,"%*s %lf %lf %lf",&(tri->vex[0].x),&(tri->vex[0].y),&(tri->vex[0].z));
fscanf(file,"%*s %lf %lf %lf",&(tri->vex[1].x),&(tri->vex[1].y),&(tri->vex[1].z));
fscanf(file,"%*s %lf %lf %lf",&(tri->vex[2].x),&(tri->vex[2].y),&(tri->vex[2].z));
Add(tri);
}
}
char title[80];
if(GetFileTitle(stlfile,title,80)==0){
SetName(title);
}
m_bModified = TRUE;
return TRUE;
}
//Serialize
void CSTLModel::Serialize(CArchive& ar)
{
CEntity::Serialize(ar);
if(ar.IsStoring()){
m_TriList.Serialize(ar);
}
else{ //IsLoading()
m_TriList.Serialize(ar);
m_bModified = TRUE;
}
}
void CSTLModel::Draw(COpenGLDC* pDC)
{
if(!m_bVisible) return;
if(m_bHighlight)
pDC->Highlight(TRUE);
else
pDC->SetMaterialColor(m_color);
if(pDC->IsSelectionMode()){
pDC->LoadName((UINT)this);
}
for(int i=0;iDraw(pDC);
}
}
void CSTLModel::Clear()
{
for(int i=0;iDraw(pDC);
}
//serialize
void CPart::Serialize(CArchive& ar)
{
CEntity::Serialize(ar);
if(ar.IsStoring()){
m_EntList.Serialize(ar);
}
else{
m_EntList.Serialize(ar);
m_bModified = TRUE;
}
}
//operation
void CPart::AddEntity(CEntity* ent)
{
m_EntList.Add(ent);
m_bModified = TRUE;
}
void CPart::RemoveEntity(CEntity* ent)
{
for(int i=0;iGetBox(box)){
if(m_pBox)
*m_pBox += box;
else{
m_pBox = new CBox3D();
*m_pBox = box;
}
}
}
m_bModified = FALSE;
}