请教一个关于mxd保存的思路
Arcengine中,现需要将加载到axpagelayoucontrol里的MXD文件保存成.mdb格式的数据文件。请问如何操作?如给点关键代码感激不仅。我用的是C#语言。
[解决办法]
用Geoprocessor 完全可以解决.
[解决办法]
这个实现起来很简单。
第一步:创建MDB空间数据库
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
//strFileName为全路径例如c:\\test.mdb
IWorkspaceName workspaceName = workspaceFactory.Create(System.IO.Path.GetDirectoryName(strFileName), System.IO.Path.GetFileName(strFileName), null, 0);
ESRI.ArcGIS.esriSystem.IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
IWorkspace targetWorkspace = (IWorkspace)name.Open();
第二步:实现利用已知FeatureClass创建新的FeatureClass对象。
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace )targetWorkspace ;
//pFeatureClass为已知的FeatureClass
IFeatureClass newFeatureClass = featureWorksapce.CreateFeatureClass(dataset.Name, pFeatureClass.Fields, null, null, esriFeatureType.esriFTSimple, featureClass.ShapeFieldName, "");
第三步:实现FeatureClass到FeatureClass的数据装载
IFeatureCursor featureCursor = sourceFeatureClass.Search(null, true);
IFeature feature = featureCursor.NextFeature();
IFeatureCursor newFeatureCursor = targetFeatureClass.Insert(true);
while (feature != null)
{
IFeatureBuffer newFeatureBuffer = targetFeatureClass.CreateFeatureBuffer();
//赋属性
CopyBaseAttribute(feature,newFeatureBuffer);
if (featureCount % 1000== 0)
{
//每千条提交一次数据
newFeatureCursor.Flush();
feature = featureCursor.NextFeature();
}
/// <summary>
/// 完全拷贝源要素属性,不拷贝二进制字段
/// </summary>
/// <param name="pSourceFeature">IFeature类型,源要素</param>
/// <param name="pTargerFeatureBuffer">IFeatureBuffer要素,目标要素</param>
/// <returns>bool类型,成功返回true,失败返回false</returns>
private bool CopyBaseAttribute(IFeature pSourceFeature, ref IFeatureBuffer pTargerFeatureBuffer)
{
if (pSourceFeature == null || pTargerFeatureBuffer == null)
{
return false;
}
IField ipField;
IFields ipFields = pSourceFeature.Fields;
IFields ipTargetFields = pTargerFeatureBuffer.Fields;
if (ipFields == null || ipTargetFields == null)
{
return false;
}
int lFieldsCount = ipFields.FieldCount;
for (int lIndex = 0; lIndex < lFieldsCount; lIndex++)
{
ipField = ipFields.get_Field(lIndex);
esriFieldType eType = ipField.Type;
if (((int)eType) < 6) //基本属性
{
string strFieldName = ipField.Name;
if (!CanWriteValue(strFieldName))
{
continue;
}
int lFieldIndex = -1;
lFieldIndex = ipTargetFields.FindField(strFieldName);
if (lFieldIndex != -1)
{
pTargerFeatureBuffer.set_Value(lFieldIndex, pSourceFeature.get_Value(lIndex));
}
else
{
//TRACE("没有找到对应属性项!");
}
}
}
return true;
}
/// <summary>
/// 判断属性字段是否可写,因为有些字段是关键字段,用户不能自己写
/// </summary>
/// <param name="strFieldName">string类型,字段名称</param>
/// <returns>bool类型,能写返回true,反之返回false</returns>
private bool CanWriteValue(string strFieldName)
{
string strText = strFieldName.ToUpper();
if (strText == "FEATUREID" || strText == "OBJECTID" || strText == "FID" ||
strText == "SHAPE_LENGTH" || strText == "SHAPE_LEN" || strText == "SHAPE_AREA")
{
return false;
}
return true;
}
第四步:通过地图窗口获取图层,循环将数据写入mdb
IMap ipMap;
IUID ipUID;
IEnumLayer ipEnumLayer;
ipUID = new UIDClass();
ipMap = this.mapView.MapControl.Map;
//仅获取FeatuerLayer图层
UID id = new UIDClass();
id.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
ipEnumLayer = ipMap.get_Layers(id, true);
ILayer ipLayer;
ipLayer = ipEnumLayer.Next();
while (ipLayer != null)
{
IFeatureLayer pFeatureLayer = ipLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//将FeatureClass转入mdb
......
ipLayer = ipEnumLayer.Next();
}