📄 view.cpp
字号:
// View.cpp : implementation of the CAddShapeView class
//
#include "stdafx.h"
#include "AddShape.h"
#include "AddShapeDoc.h"
#include "View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAddShapeView
IMPLEMENT_DYNCREATE(CAddShapeView, CFormView)
BEGIN_MESSAGE_MAP(CAddShapeView, CFormView)
//{{AFX_MSG_MAP(CAddShapeView)
ON_WM_SIZE()
ON_COMMAND(ID_MAP_SAVESHAPES, OnMapSaveShapes)
ON_COMMAND(ID_MAP_ADDPOINT, OnMapAddPoint)
ON_COMMAND(ID_MAP_ADDPOLY, OnMapAddPoly)
ON_COMMAND(ID_MAP_ADDLINE, OnMapAddLine)
ON_UPDATE_COMMAND_UI_RANGE(ID_MAP_ADDPOINT, ID_MAP_ADDPOLY, OnUpdateMapTool)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_MAP_ZOOMIN, OnZoomIn)
ON_COMMAND(ID_MAP_ZOOMOUT, OnZoomout)
ON_COMMAND(ID_MAP_PAN, OnPan)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_ADDELLIPSE, OnAddellipse)
ON_COMMAND(ID_ADDRECT, OnAddrect)
ON_COMMAND(ID_FULLEXTENT, OnFullextent)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddShapeView construction/destruction
CAddShapeView::CAddShapeView()
: CFormView(CAddShapeView::IDD),
m_path("untitled.shp"),
m_curTool(ID_MAP_ADDPOLY)
{
//{{AFX_DATA_INIT(CAddShapeView)
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CAddShapeView::~CAddShapeView()
{
ReleasePoints();
ReleaseLines();
ReleasePolygons();
ReleaseRectangles();
ReleaseEllipses();
}
void CAddShapeView::ReleasePoints()
{
for (int i = 0; i < m_points.GetSize(); i++)
{
m_points[i]->ReleaseDispatch();
delete m_points[i];
}
m_points.RemoveAll();
}
void CAddShapeView::ReleaseLines()
{
for (int i = 0; i < m_lines.GetSize(); i++)
{
m_lines[i]->ReleaseDispatch();
delete m_lines[i];
}
m_lines.RemoveAll();
}
void CAddShapeView::ReleasePolygons()
{
for (int i = 0; i < m_polys.GetSize(); i++)
{
m_polys[i]->ReleaseDispatch();
delete m_polys[i];
}
m_polys.RemoveAll();
}
void CAddShapeView::ReleaseRectangles()
{
for (int i = 0; i < m_rectangles.GetSize(); i++)
{
m_rectangles[i]->ReleaseDispatch();
delete m_rectangles[i];
}
m_rectangles.RemoveAll();
}
void CAddShapeView::ReleaseEllipses()
{
for (int i = 0; i < m_ellipses.GetSize(); i++)
{
m_ellipses[i]->ReleaseDispatch();
delete m_ellipses[i];
}
m_ellipses.RemoveAll();
}
void CAddShapeView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAddShapeView)
DDX_Control(pDX, IDC_MAP1, m_map);
//}}AFX_DATA_MAP
}
BOOL CAddShapeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAddShapeView diagnostics
#ifdef _DEBUG
void CAddShapeView::AssertValid() const
{
CFormView::AssertValid();
}
void CAddShapeView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CAddShapeDoc* CAddShapeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAddShapeDoc)));
return (CAddShapeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAddShapeView message handlers
void CAddShapeView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
//
// Add an image to the map and zoom in.
//
// DEFAULT DATADIR PATH MAY NEED TO BE MODIFIED FOR YOUR INSTALLATION.
// SEE MAPHELPER.CPP CDataDir
CDataDir dataDir;
// Try to open the default image layer
try
{
AddImageLayer(m_map, dataDir.GetPath() + "\\washington\\wash.bmp");
CMoRectangle extent(m_map.GetFullExtent());
extent.ScaleRectangle(.1);
m_map.SetExtent(extent);
}
catch (...)
{
::MessageBox(this->GetSafeHwnd(), _T("Default image file not found."), _T("AddShape"), MB_ICONEXCLAMATION);
}
}
void CAddShapeView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
Resize(m_map, cx, cy);
}
void CAddShapeView::SavePoints(CMoDataConnection& conn, CMoTableDesc& tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moPoint, tableDesc, va, va));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
return;
layer.SetGeoDataset(geoDataset);
// Add a record for each pointgon to the GeoDataset
CMoRecordset recs(layer.GetRecords());
CMoFields fields(recs.GetFields());
CString featureName;
for (int i = 0; i < m_points.GetSize(); i++)
{
recs.AddNew();
// "Shape" = m_point
SetValue(fields, TEXT("Shape"), LPDISPATCH(*m_points[i]));
// "Name" = featureName
featureName.Format("%s%d", "point", i);
SetValue(fields, TEXT("Name"), featureName);
// "Area" = m_point.GetArea()
SetValue(fields, TEXT("Area"), 0.0);
// "Perimeter" = m_point.GetPerimeter()
SetValue(fields, TEXT("Perimeter"), 0.0);
recs.Update();
}
// Add new layer to map
CMoLayers layers(m_map.GetLayers());
layers.Add(layer);
ReleasePoints();
}
void CAddShapeView::SaveLines(CMoDataConnection& conn, CMoTableDesc& tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moLine, tableDesc, va ,va));
ASSERT(LPDISPATCH(geoDataset));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
return;
layer.SetGeoDataset(geoDataset);
// Add a record for each line to the GeoDataset
CMoRecordset recs(layer.GetRecords());
CMoFields fields(recs.GetFields());
CString featureName;
for (int i = 0; i < m_lines.GetSize(); i++)
{
recs.AddNew();
// "Shape" = m_line
SetValue(fields, TEXT("Shape"), LPDISPATCH(*m_lines[i]));
// "Name" = featureName
featureName.Format("%s%d", "line", i);
SetValue(fields, TEXT("Name"), featureName);
// "Area" = m_point.GetArea()
SetValue(fields, TEXT("Area"), 0.0);
// "Perimeter" = m_point.GetPerimeter()
SetValue(fields, TEXT("Perimeter"), m_lines[i]->GetLength());
recs.Update();
}
// Add new layer to map
CMoLayers layers(m_map.GetLayers());
layers.Add(layer);
ReleaseLines();
}
void CAddShapeView::SaveRectangles(CMoDataConnection& conn, CMoTableDesc& tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moPolygon, tableDesc, va ,va));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
return;
layer.SetGeoDataset(geoDataset);
// Add a record for each polygon to the GeoDataset
CMoRecordset recs(layer.GetRecords());
CMoFields fields(recs.GetFields());
CString featureName;
// will be writing out the rectangle as a polygon as shape files don't know about Rectangles
// Use Buffer(0,Extent) to create a polygon.
//
// need to set up a Variant for the Extent parameter
CMoRectangle extent(m_map.GetFullExtent());
VariantInit(&va);
va.vt = VT_DISPATCH;
va.pdispVal = extent.m_lpDispatch;
for (int i = 0; i < m_rectangles.GetSize(); i++)
{
recs.AddNew();
CMoRectangle rect(*m_rectangles[i]);
CMoPolygon poly(rect.Buffer(0.0,va));
// "Shape" = poly
SetValue(fields, TEXT("Shape"), LPDISPATCH(poly.m_lpDispatch));
// "Name" = featureName
featureName.Format("%s%d", "Rectangle", i);
SetValue(fields, TEXT("Name"), featureName);
// "Area" = poly.GetArea()
SetValue(fields, TEXT("Area"), poly.GetArea());
// "Perimeter" = poly.GetPerimeter()
SetValue(fields, TEXT("Perimeter"), poly.GetPerimeter());
recs.Update();
}
// Add new layer to map
CMoLayers layers(m_map.GetLayers());
layers.Add(layer);
ReleaseRectangles();
}
void CAddShapeView::SavePolygons(CMoDataConnection& conn, CMoTableDesc& tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moPolygon, tableDesc, va ,va));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
return;
layer.SetGeoDataset(geoDataset);
// Add a record for each polygon to the GeoDataset
CMoRecordset recs(layer.GetRecords());
CMoFields fields(recs.GetFields());
CString featureName;
for (int i = 0; i < m_polys.GetSize(); i++)
{
recs.AddNew();
// "Shape" = m_poly
SetValue(fields, TEXT("Shape"), LPDISPATCH(*m_polys[i]));
// "Name" = featureName
featureName.Format("%s%d", "poly", i);
SetValue(fields, TEXT("Name"), featureName);
// "Area" = m_poly.GetArea()
SetValue(fields, TEXT("Area"), m_polys[i]->GetArea());
// "Perimeter" = m_poly.GetPerimeter()
SetValue(fields, TEXT("Perimeter"), m_polys[i]->GetPerimeter());
recs.Update();
}
// Add new layer to map
CMoLayers layers(m_map.GetLayers());
layers.Add(layer);
ReleasePolygons();
}
void CAddShapeView::SaveEllipses(CMoDataConnection& conn, CMoTableDesc& tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moPolygon, tableDesc, va ,va));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
return;
layer.SetGeoDataset(geoDataset);
// Add a record for each ellipses to the GeoDataset
CMoRecordset recs(layer.GetRecords());
CMoFields fields(recs.GetFields());
CString featureName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -