Skip to content

Instantly share code, notes, and snippets.

@likaci
Created November 1, 2013 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save likaci/7265886 to your computer and use it in GitHub Desktop.
Save likaci/7265886 to your computer and use it in GitHub Desktop.
axMapControl
/*
ArcGIS Engine提供了保存Mxd文件的类MapDoment。但在具体保存MXD文件过程中有下面几种情况:
1 直接使用IMapDocument接口的Open方法来打开MXD文件,编辑过后进行保存。
2 使用Engine中带的OpenDocument方法来打开MXD文件,然后编辑过之后要进行保存。
3 使用自己写的添加数据的工具直接添加数据,也就是说一开始没有MXD文件,在编辑完之后需要把当前的地图保存为一个MXD文件。
过程描述
解决方法:
首先这三种情况都可以使用Engine中自带的SaveDoument的工具进行保存
1 这种情况比较简单,直接使用IMapDocument的save或者saveas的方法来进行保存就可以,可以在帮助中查到例子。
2 这种情况可使用下面的代码进行保存,
*/
IMxdContents pMxdC;
pMxdC = axMapControl1.Map as IMxdContents ;
IMapDocument pMapDocument = new MapDocumentClass();
pMapDocument.Open (axMapControl1.DocumentFilename,"");
IActiveView pActiveView = axMapControl1.Map as IActiveView ;
pMapDocument.ReplaceContents (pMxdC);
pMapDocument.SaveAs ("d:\aa2.mxd",true,true);
//3 这种情况使用的代码稍微有点不同:
IMxdContents pMxdC;
pMxdC = axMapControl1.Map as IMxdContents ;
IMapDocument pMapDocument = new MapDocumentClass ();
pMapDocument.New ("d:\aa3.mxd");
IActiveView pActiveView = axMapControl1.Map as IActiveView ;
pMapDocument.ReplaceContents (pMxdC);
pMapDocument.Save (true,true);
//---------------------------------------------------
public void ReplaceContents (IMxdContents pObject);保存修改;
public void Save ( bool bUseRelativePaths,bool bCreateThumnbail);
public void SaveAs (string sDocument,bool bUseRelativePaths,bool bCreateThumnbail);
IMxdContents接口:Provides access to members to pass data into and out off a MXD map document file. Coclasses that implement this interface can limited the implementation to one property if required.
1 新建一个Mxd空白文档:
string path=@"f:\TEST\untitled.mxd";
MapDocument pMapDocument = new MapDocumentClass();
pMapDocument.New(path);
pMapDocument.Open(path, "");
axMapControl1.Map = pMapDocument.get_Map(0);
2 在打开一个Mxd文档进行修改、编辑再保存在原文件里面:
IMxdContents pMxdC;
pMxdC = axMapControl1.Map as IMxdContents;
IMapDocument pMapDocument = new MapDocumentClass();
pMapDocument.Open(axMapControl1.DocumentFilename, "");
IActiveView pActiveView = axMapControl1.Map as IActiveView;
pMapDocument.ReplaceContents(pMxdC);
pMapDocument.Save(true, true);
3 自定义加载各种数据,也就是一开始就没有Mxd文档,之后进行修改、编辑,把当前地图保存为一个Mxd文档
SaveFileDialog Sfd = new SaveFileDialog();
Sfd.Title = "另存文档";
Sfd.Filter = "(*.mxd)|*.mxd";
Sfd.ShowDialog();
string path = Sfd.FileName;
IMxdContents pMxdC;
pMxdC = axMapControl1.Map as IMxdContents;
IMapDocument pMapDocument = new MapDocumentClass();
pMapDocument.New(path);
IActiveView pActiveView = axMapControl1.Map as IActiveView;
pMapDocument.ReplaceContents(pMxdC);
pMapDocument.Save(true, true);
private void btnCreatAddShp_Click(object sender, EventArgs e)
{
DataOperator dataOperator = new DataOperator(axMapControl1.Map);
IFeatureClass featureClass = dataOperator.CreatShapefile("c:\\", "ShapefileWorkspace", "ShapefileSample");
if (featureClass == null)
{
MessageBox.Show("创建失败");
return;c
}
bool bRes = dataOperator.AddFeatureClassToMap(featureClass, "ObservationStation");
return;
}
1 private void btnLoadMxd_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开MXD文件";
ofd.Filter = "mxd|*.mxd";
if (ofd.ShowDialog()==DialogResult.OK)
{
string filePath = ofd.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;
//
axMapControl1.Extent = axMapControl1.FullExtent;
}
else
MessageBox.Show("不是有效的Mxd");
}
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
IGeometry geometry = this.axMapControl1.TrackLine();
//drawMapShape(geometry);
drawMapText(geometry);
}
private void drawMapShape(IGeometry geometry)
{
IRgbColor color = new RgbColor();
color.Red = 255;
object symbol = null;
if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline || geometry.GeometryType == esriGeometryType.esriGeometryLine)
{
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbol();
simpleLineSymbol.Color = color;
simpleLineSymbol.Width = 5;
symbol = simpleLineSymbol;
}
else
{
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
simpleFillSymbol.Color = color;
symbol = simpleFillSymbol;
}
axMapControl1.DrawShape(geometry, ref symbol);
}
private void drawMapText(IGeometry geometry)
{
IRgbColor color = new RgbColor();
color.Blue = 255;
ITextSymbol tSymbol = new TextSymbol();
tSymbol.Color = color;
object symbol = tSymbol;
this.axMapControl1.DrawText(geometry,"测试",ref symbol);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment