一、制作mxd模板文件
尽可能在模板图层中标注要素,这样在模板图层中插入要素后,标注可以随之自动显示。
二、打开真实数据要素类
打开真实要素类,获取该要素类和空间参考,以及标注需要的字段索引。
三、打开模板图层要素类
打开模板图层要素类,获取该要素类,并修改其空间参考为真实数据的空间参考,获取标注需要的字段索引。
四、打开模板图mxd文件
打开mxd文件,并修改数据框的空间参考。(2-4步看AE开发笔记(一)空间参考坐标系)
五、初始化模板图层(清空数据源)
//清空模板图中源数据要素类中的要素
ITable m1Table = pFeatureclass as ITable;
m1Table.DeleteSearchedRows(null);
六、判断真实要素类和模板图层要素类是否有ZM值
int pShapeIndex= pzdFeaclass.Fields.FindField("Shape");//真实数据要素类
IGeometryDef pGeometryDef= pFeaclass.Fields.get_Field(pShapeIndex).GeometryDef as IGeometryDef;
int mShapeIndex = mFeatureclass.Fields.FindField("Shape");//模板图层要素类
IGeometryDef mGeometryDef=mFeatureclass.Fields.get_Field(mShapeIndex ).GeometryDef as IGeometryDef;
bool HAS_ZM= false;
if (pGeometryDef.HasZ == mGeometryDef.HasZ && pGeometryDef.HasM ==mGeometryDef.HasM)
{
HAS_ZM= true;
}
七、将真实数据插入模板图层要素类中
IQueryFilter sQueryFilter = new QueryFilterClass();
sQueryFilter.WhereClause = "DM = '" + dm + "'";
IFeatureCursor pfeatureCursor = pFeaclass.Search(sQueryFilter, false);//真实数据要素类
IFeature pfeature = null;
IFeatureBuffer mFeatureBuffer = mFeatureclass.CreateFeatureBuffer();//模板图层要素类
IFeatureCursor mFeatureCursor = mFeatureclass.Insert(true);
while((pfeature=pfeatureCursor.NextFeature())!=null)
{
string value= pfeature.get_Value(pindex).ToString();
if (HAS_ZM == true)
{
pFeatureBuffer.Shape = pfeature.Shape;
}
else
{
SetZMValue(mFeatureBuffer as IFeature, pfeature.Shape);
}
mFeatureBuffer.set_Value(mindex, value);
mFeatureCursor.InsertFeature(mFeatureBuffer);
}
mFeatureCursor.Flush();
Marshal.ReleaseComObject(pfeatureCursor);
Marshal.ReleaseComObject(mFeatureBuffer);
Marshal.ReleaseComObject(mFeatureCursor);
设置ZM值:
//设置Z、M值
private void SetZValue(IFeature pF, IGeometry pGeo)
{
int index;
index = pF.Fields.FindField("Shape");
IGeometryDef pGeometryDef;
pGeometryDef = pF.Fields.get_Field(index).GeometryDef as IGeometryDef;
if (pGeometryDef.HasZ)
{
IZAware pZAware = (IZAware)pGeo;
pZAware.ZAware = true;
IPoint point = (IPoint)pGeo;
point.Z = 0;
}
else
{
IZAware pZAware = (IZAware)pGeo;
pZAware.ZAware = false;
}
if (pGeometryDef.HasM)
{
IMAware pMAware = (IMAware)pGeo;
pMAware.MAware = true;
}
else
{
IMAware pMAware = (IMAware)pGeo;
pMAware.MAware = false;
}
pF.Shape = pGeo;
}
八、设置比例尺,将数据视图和布局视图联动
//将布局联动到数据上
IObjectCopy objCopy = new ObjectCopyClass();
object copyFromMap = pActiveViewfw.FocusMap;
object copyMap = objCopy.Copy(copyFromMap);
object copyToMap = this.axMapControl1.Map;
objCopy.Overwrite(copyMap, ref copyToMap);
//设置比例尺
IEnvelope pEnvelope = pfeature.Extent;
this.axMapControl1.Extent = pEnvelope;
this.axMapControl1.ActiveView.Refresh();
pActiveViewfw.Extent = pEnvelope;
pActiveViewfw.Extent = pEnvelope;
this.axMapControl1.Map.MapScale = 500;
//将数据联动到布局上
objCopy = new ObjectCopyClass();
copyFromMap = this.axMapControl1.Map;
copyMap = objCopy.Copy(copyFromMap);
copyToMap = pActiveViewfw.FocusMap;
objCopy.Overwrite(copyMap, ref copyToMap);
//缩放至
ICommand ZoomIn = new ControlsPageZoomWholePageCommand();
ZoomIn.OnCreate(axPageLayoutControl1.Object);
axPageLayoutControl1.CurrentTool = ZoomIn as ITool;
ZoomIn.OnClick();
pActiveViewfw.Refresh();
九、制图输出
tagRECT grect = new tagRECT
{
left = grect.top = 0,
right = 2244,//制图输出的宽,单位像素
bottom = 3175//制图输出的高,单位像素
};
IEnvelope menvelope = new EnvelopeClass();
menvelope.PutCoords(0, 0, 2244, 3175);
this.axMapControl1.Refresh();
string pdfpath ="D://测试图.PDF";
ExportPageLayout(pActiveView, menvelope, grect, pdfpath, 300);
/// <summary>
/// 制图输出
/// </summary>
/// <param name="activeView"></param>
/// <param name="env"></param>
/// <param name="exportRect"></param>
/// <param name="strOutPath">输出路径</param>
/// <param name="resolution">分辨率</param>
private void ExportPageLayout(IActiveView activeView, IEnvelope env, tagRECT exportRect, string strOutPath, double resolution)
{
try
{
if ((activeView != null) && !string.IsNullOrEmpty(strOutPath))
{
IExport export = null;
if (strOutPath.ToUpper().EndsWith(".JPG"))
{
export = new ExportJPEGClass();
}
else if (strOutPath.ToUpper().EndsWith(".TIF"))
{
export = new ExportTIFFClass();
}
else if (strOutPath.ToUpper().EndsWith(".BMP"))
{
export = new ExportBMPClass();
}
else if (strOutPath.ToUpper().EndsWith(".EMF"))
{
export = new ExportEMFClass();
}
else if (strOutPath.ToUpper().EndsWith(".PNG"))
{
export = new ExportPNGClass();
}
else if (strOutPath.ToUpper().EndsWith(".GIF"))
{
export = new ExportGIFClass();
}
else if (strOutPath.ToUpper().EndsWith(".PDF"))
{
export = new ExportPDFClass();
}
export.ExportFileName = strOutPath;
export.Resolution = resolution;
export.PixelBounds = env;
Int32 hDC = export.StartExporting();
//System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
//sw.Start();
activeView.Output(hDC, (Int16)export.Resolution, ref exportRect, null, null);
//sw.Stop();
//Console.WriteLine("执行时间 {0}", sw.Elapsed.TotalMilliseconds);
export.FinishExporting();
export.Cleanup();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(export);
}
}
catch (Exception ex)
{
MessageBox.Show("导出失败!");
}
}
关于像素和厘米的转换,使用在线转换工具:https://www.gaitubao.com/tools/pixel2cm.html