Click here to Skip to main content
15,883,705 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 509.9K   14.7K   60  
A simple StereoLithography data file viewer.
// StLViewerView.cpp : implementation of the CStLViewerView class
//

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

#include "StLViewerDoc.h"
#include "StLViewerView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView

IMPLEMENT_DYNCREATE(CStLViewerView, CView)

BEGIN_MESSAGE_MAP(CStLViewerView, CView)
	//{{AFX_MSG_MAP(CStLViewerView)
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MBUTTONDOWN()
	ON_WM_MBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_DESTROY()
	ON_COMMAND(ID_TOPVIEW, OnTopview)
	ON_COMMAND(ID_BOTTOMVIEW, OnBottomview)
	ON_COMMAND(ID_FRONTVIEW, OnFrontview)
	ON_COMMAND(ID_REARVIEW, OnRearview)
	ON_COMMAND(ID_LEFTVIEW, OnLeftview)
	ON_COMMAND(ID_RIGHTVIEW, OnRightview)
	ON_COMMAND(ID_AXONVIEW, OnAxonview)
	ON_COMMAND(ID_FITALL, OnFitall)
	ON_COMMAND(ID_ZOOMWIN, OnZoomwin)
	ON_COMMAND(ID_RESET, OnReset)
	ON_COMMAND(ID_DMODE, OnDisplaymode)
	ON_COMMAND(ID_HLREMOVED, OnHlremoved)
	ON_COMMAND(ID_ANTIAL, OnAntial)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_COORD, OnUpdateCoordViewer)
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_SCREEN_BACKGROUNDCOLOR, OnScreenBackgroundcolor)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView construction/destruction

CStLViewerView::CStLViewerView()
{
	// TODO: add construction code here
	lbutdown = false;
	mbutdown = false;
	rbutdown = false;
	winZoom = false;
	m_bShaded = false;
	antialiased = false;
	m_Pen = new CPen(PS_SOLID, 2, RGB(0,0,0));
}

CStLViewerView::~CStLViewerView()
{
	delete m_Pen;
}

BOOL CStLViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView drawing

void CStLViewerView::OnDraw(CDC* pDC)
{
	CStLViewerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	myView->Draw();
}

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView printing

BOOL CStLViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CStLViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CStLViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView diagnostics

#ifdef _DEBUG
void CStLViewerView::AssertValid() const
{
	CView::AssertValid();
}

void CStLViewerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CStLViewerDoc* CStLViewerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStLViewerDoc)));
	return (CStLViewerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStLViewerView message handlers

void CStLViewerView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
}



void CStLViewerView::OnSize(UINT nType, int cx, int cy) 
{	
	// TODO: Add your message handler code here
	if(myView)
		myView->ReSize(cx, cy);
		CView::OnSize(nType, cx, cy);
}

int CStLViewerView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	//create the OpenGL view with 600x600x600 view volume (ortho)
	myView = new CGLView(this, GetDocument()->GetContext(), 600);
	
	return 0;
}

void CStLViewerView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	// ask window to give us events even if outside the view
    SetCapture();

	//  save the current mouse coordinate in min 
	myXmin=point.x;  myYmin=point.y;
	myXmax=point.x;  myYmax=point.y;

	lbutdown = true;
	lDownPnt = point;
	if(!(nFlags & MK_SHIFT) && !(nFlags & MK_CONTROL) && !winZoom)
	{
		myView->Select(point.x, point.y);
		InvalidateRect(NULL, FALSE);
	}
	if ((nFlags & MK_SHIFT) && !(nFlags & MK_CONTROL) && !winZoom)
	{
		myView->MultiSelect(point.x, point.y);
		InvalidateRect(NULL, FALSE);
	}
	CView::OnLButtonDown(nFlags, point);
}

void CStLViewerView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	lbutdown = false;
	myView->StopZooming();

	if(!(nFlags & MK_SHIFT) && !(nFlags & MK_CONTROL) && !winZoom)
	{
		DrawRectangle(false);
		CRect rect(lDownPnt, point);
		rect.NormalizeRect();
		if(rect.Width() && rect.Height())
			myView->SweepSelect(rect);
		InvalidateRect(NULL, FALSE);
	}

	if(winZoom)
	{
		myXmax=point.x;  myYmax=point.y;
		DrawRectangle(false);
		CRect myZoomRect(myXmin, myYmax, myXmax, myYmin);
		if ((abs(myXmin-myXmax)>1) || (abs(myYmin-myYmax)>1))
		// Test if the zoom window size is not null
		{
			myView->ZoomWindow(myZoomRect);
		}
	}

	winZoom = false;
	ReleaseCapture();

	CView::OnLButtonUp(nFlags, point);
}

void CStLViewerView::OnMButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	mbutdown = true;
	mDownPnt = point;
	CView::OnMButtonDown(nFlags, point);
}

void CStLViewerView::OnMButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	mbutdown = false;
	myView->StopPanning();
	CView::OnMButtonUp(nFlags, point);
}

void CStLViewerView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	rbutdown = true;
	rDownPnt = point;
	CView::OnRButtonDown(nFlags, point);
}

void CStLViewerView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	rbutdown = false;
	myView->StopRotation();
	CView::OnRButtonUp(nFlags, point);
}

void CStLViewerView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(lbutdown && (nFlags & MK_CONTROL))
	{
		myView->ZoomView(lDownPnt, point);
		Invalidate(FALSE);
	}
	if(mbutdown && (nFlags & MK_CONTROL))
	{
		myView->PanView(mDownPnt, point);
		Invalidate(FALSE);
	}
	if(rbutdown && (nFlags & MK_CONTROL))
	{
		myView->RotateView(rDownPnt, point);
		Invalidate(FALSE);
	}
	if(lbutdown && !(nFlags & MK_CONTROL) && !(nFlags & MK_SHIFT))
	{
		myXmax = point.x; myYmax = point.y;	
		DrawRectangle(true,LongDash);
	}

	CPoint3D C;
	myView->ScreenToPoint(point.x, point.y, C);
	coord.Format("X %0.3lf  Y %0.3lf  Z%0.3lf",C.GetX(), C.GetY(), C.GetZ());

	CView::OnMouseMove(nFlags, point);
}

void CStLViewerView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	delete myView;
}


void CStLViewerView::OnTopview() 
{
	// TODO: Add your command handler code here
	myView->TopView();
}

void CStLViewerView::OnBottomview() 
{
	// TODO: Add your command handler code here
	myView->BottomView();
}

void CStLViewerView::OnFrontview() 
{
	// TODO: Add your command handler code here
	myView->FrontView();
}

void CStLViewerView::OnRearview() 
{
	// TODO: Add your command handler code here
	myView->BackView();
}

void CStLViewerView::OnLeftview() 
{
	// TODO: Add your command handler code here
	myView->LeftView();
}

void CStLViewerView::OnRightview() 
{
	// TODO: Add your command handler code here
	myView->RightView();
}


void CStLViewerView::OnAxonview() 
{
	// TODO: Add your command handler code here
	myView->AxonView();
}

void CStLViewerView::OnFitall() 
{
	// TODO: Add your command handler code here
	myView->FitAll();
}

void CStLViewerView::OnZoomwin() 
{
	// TODO: Add your command handler code here
	winZoom = true;
}


void CStLViewerView::OnReset() 
{
	// TODO: Add your command handler code here
	myView->ResetView();
}

void CStLViewerView::OnDisplaymode() 
{
	// TODO: Add your command handler code here
	m_bShaded = !m_bShaded;
	if(m_bShaded)
		myView->SetDisplayMode(GLSHADED);
	else
		myView->SetDisplayMode(GLWIREFRAME);
}

void CStLViewerView::OnHlremoved() 
{
	// TODO: Add your command handler code here
	myView->SetDisplayMode(GLHLREMOVED);
}

void CStLViewerView::OnAntial() 
{
	// TODO: Add your command handler code here
	antialiased = !antialiased;
	myView->SetAntiAliasing(antialiased);
}

void CStLViewerView::OnUpdateCoordViewer(CCmdUI* pCmdUI)
{
	pCmdUI->SetText((LPCTSTR)coord);
}
//
void CStLViewerView::DrawRectangle(const bool  Draw , 
                                        const LineStyle aLineStyle)
{
    static int m_DrawMode=R2_MERGEPENNOT;
    static		int StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
    static		bool m_IsVisible = false;
    static  StoredLineStyle= aLineStyle;

    CPen* aOldPen;
    CClientDC clientDC(this);
    if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
    clientDC.SetROP2(m_DrawMode);

    if ( m_IsVisible ) //  erase at the old position 
    {
     clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
     clientDC.LineTo(StoredMaxX,StoredMaxY); 
	 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
     m_IsVisible = false;
    }

    StoredMinX = min ( myXmin, myXmax );    StoredMinY = min ( myYmin, myYmax );
    StoredMaxX = max ( myXmin, myXmax );    StoredMaxY = max ( myYmin, myYmax);

    if (Draw){ // move : draw
        if (StoredLineStyle != aLineStyle) {
            delete m_Pen;
            if  (aLineStyle ==Solid )
                {m_Pen = new CPen(PS_SOLID, 2, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
            else if (aLineStyle ==Dot )
                {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0));   m_DrawMode = R2_XORPEN;}
            else if (aLineStyle == ShortDash)
                {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0));	m_DrawMode = R2_XORPEN;}
            else if (aLineStyle == LongDash)
                {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0));	m_DrawMode = R2_NOTXORPEN;}
            clientDC.SelectObject(m_Pen);
        }

     clientDC.SetROP2(m_DrawMode);
     clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
     clientDC.LineTo(StoredMaxX,StoredMaxY); 
	 clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
     m_IsVisible = true;
   }

    if (m_Pen) clientDC.SelectObject(aOldPen);
}


void CStLViewerView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	if(GetAsyncKeyState(VK_CONTROL))
		return;
	CRect rcClient;
    GetClientRect (&rcClient);

	int cx = rcClient.Width () / 2;
	int cy = rcClient.Height () / 2;
	CRect rcShape (cx - 45, cy - 45, cx + 45, cy + 45);

	CPoint pos = point;
	ScreenToClient (&pos);

	CGLDisplayContext* ctx = GetDocument()->GetContext();
	if(ctx->HasSelected())
	{
		CMenu menu;
		menu.LoadMenu (IDR_POPUP);
		CMenu* pContextMenu = menu.GetSubMenu (0);
		pContextMenu->InsertMenu(0, MF_BYPOSITION | MF_SEPARATOR, 0, "StL OBJECT");
		pContextMenu->InsertMenu(0, MF_BYPOSITION | MF_STRING | MF_DISABLED, 0, "StL OBJECT");
		SetMenuDefaultItem(pContextMenu->m_hMenu, 0, TRUE);
		      pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
			TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
		return;
	}
	else
	{
		CMenu menu;
		menu.LoadMenu (IDR_POPUP);
		CMenu* pContextMenu = menu.GetSubMenu (1);
		pContextMenu->InsertMenu(0, MF_BYPOSITION | MF_SEPARATOR, 0, "VIEWER");
		pContextMenu->InsertMenu(0, MF_BYPOSITION | MF_STRING | MF_DISABLED, 0, "VIEWER");
		SetMenuDefaultItem(pContextMenu->m_hMenu, 0, TRUE);
		pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
			TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
		return;
	}

	CWnd::OnContextMenu (pWnd, point);
}


void CStLViewerView::OnScreenBackgroundcolor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.rgbResult = RGB(0, 0, 0);
	if(dlg.DoModal()==IDOK)
	{
		COLORREF color = dlg.GetColor();
		GLfloat m_Red = (GLfloat) GetRValue(color);
		GLfloat m_Green = (GLfloat) GetGValue(color);
		GLfloat m_Blue = (GLfloat) GetBValue(color);
		myView->SetBackgroundColor(m_Red/255, m_Green/255, m_Blue/255);
	}
}

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