Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC

StL Data File Viewer

Rate me:
Please Sign up or sign in to vote.
4.87/5 (43 votes)
5 Mar 2003CPOL2 min read 506.1K   14.7K   60  
A simple StereoLithography data file viewer.
// StLViewerDoc.cpp : implementation of the CStLViewerDoc class
//

#include "stdafx.h"
#include "StLViewer.h"

#include "StLViewerDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStLViewerDoc

IMPLEMENT_DYNCREATE(CStLViewerDoc, CDocument)

BEGIN_MESSAGE_MAP(CStLViewerDoc, CDocument)
	//{{AFX_MSG_MAP(CStLViewerDoc)
	ON_COMMAND(ID_READSTL, OnReadstl)
	ON_COMMAND(ID_STLOBJ_COLOR, OnStlobjColor)
	ON_COMMAND(ID_STLOBJ_MATERIAL, OnStlobjMaterial)
	ON_COMMAND(ID_STLOBJ_ERASE, OnStlobjErase)
	ON_COMMAND(ID_STLOBJ_DELETE, OnStlobjDelete)
	ON_COMMAND(ID_SCREEN_DISPLAYALL, OnScreenDisplayall)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

const TCHAR CStLViewerDoc::m_szFilters[] = _T("STL Files (*.stl)|*.stl|All Files (*.*)|*.*||");

/////////////////////////////////////////////////////////////////////////////
// CStLViewerDoc construction/destruction

CStLViewerDoc::CStLViewerDoc()
{
	// TODO: add one-time construction code here
	dContext = new CGLDisplayContext(this);
}

CStLViewerDoc::~CStLViewerDoc()
{
	delete dContext;
}

BOOL CStLViewerDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CStLViewerDoc serialization

void CStLViewerDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CStLViewerDoc diagnostics

#ifdef _DEBUG
void CStLViewerDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CStLViewerDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStLViewerDoc commands


void CStLViewerDoc::OnReadstl() 
{
	// TODO: Add your command handler code here
	CWaitCursor aWaitCursor;
	CFileDialog dlg(TRUE, _T("stl"), _T("*.stl"),OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
		m_szFilters);
	CString ext, m_stlPathName;
	if(dlg.DoModal()==IDOK)
	{
		m_stlPathName = dlg.GetPathName();
		ext = dlg.GetFileExt();
		if(strcmp("stl",(LPCTSTR)ext)==0 || strcmp("STL",(LPCTSTR)ext)==0)
		{
			CStLReader* m_Reader = new CStLReader(m_stlPathName);
			CStLObject* m_stlObj = new CStLObject(m_Reader);
			dContext->Display(m_stlObj);
			delete m_stlObj;
			delete m_Reader;
		}
		else
			MessageBox(0, "File Format Not Recognized!!","StLViewer Error", MB_OK | MB_ICONERROR);
	}
	UpdateAllViews(NULL);
}

//Popup Menu

void CStLViewerDoc::OnStlobjColor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.rgbResult = RGB(255, 0, 0);
	if(dlg.DoModal()==IDOK)
	{
		COLORREF color = dlg.GetColor();
		GLubyte m_Red = (GLubyte) GetRValue(color);
		GLubyte m_Green = (GLubyte) GetGValue(color);
		GLubyte m_Blue = (GLubyte) GetBValue(color);
		for(dContext->InitSelected(); dContext->MoreSelected(); dContext->NextSelected())
		{
			CGLObject* O = dContext->CurrentSelected();
			O->SetColor(m_Red, m_Green, m_Blue);
		}
		UpdateAllViews(NULL);
	}
}

#include "MaterialDlg.h"
void CStLViewerDoc::OnStlobjMaterial() 
{
	// TODO: Add your command handler code here
	CMaterialDlg dlg;
	if(dlg.DoModal() == IDOK)
	{
		for(dContext->InitSelected(); dContext->MoreSelected(); dContext->NextSelected())
		{
			CGLObject* O = dContext->CurrentSelected();
			O->SetMaterial(dlg.m_Matl);
		}
		UpdateAllViews(NULL);
	}
}

void CStLViewerDoc::OnStlobjErase() 
{
	// TODO: Add your command handler code here
	dContext->EraseSelected();
}

void CStLViewerDoc::OnStlobjDelete() 
{
	// TODO: Add your command handler code here
	dContext->DeleteSelected();
}


void CStLViewerDoc::OnScreenDisplayall() 
{
	// TODO: Add your command handler code here
	dContext->DisplayAll();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions