线面采集操作消息体封装与解译

项目:基于GIS操作语义的协作制图系统

需求:将线面封装,写入和读取XML

1 修订XML结构

<Project ID="">
    <CooperationMap ID="">
        <Layer Name="" Type="">
            <esriGeometryType ID="" Operation="">
                <Point></Point>
                <Point></Point>
            </esriGeometryType>
        </Layer>
    </CooperationMap>
</Project>

 增添了PointCollectionID属性,作为几何图形唯一识别,每次消息体再客户端解译后会再次生成新的XML,使用PointCollection作为XML文件命名(目前使用时间命名)。

2 几何图像的绘制采集

使用 INewPolygonFeedback 和 INewLineFeedback 接口获取,目前实在 axMapControl 的鼠标交互事件内写的,暂未封装成 Command 和 Tool ,然后再转成PointCollection。

3 封装与解译

3.1 封装

去除 IEngineEditEvents_Event 草图完成事件,这个事件需要鼠标焦点离开编辑中的几何图形才生效。

public void WriteXML(string absolutePath)
{
    IPoint pnt = PointCollection.get_Point(0);
    //创建节点
    XElement project = new XElement("Project",
        new XAttribute("ID", this.ProjectID));
    XElement cooperationMap = new XElement("CooperationMap",
        new XAttribute("ID", this.CooperationMapID));
    XElement layer = new XElement("Layer",
        new XAttribute("Name", this.LayerName),
        new XAttribute("Type", this.LayerType));
    XElement geometry = new XElement(LayerType.ToString(),
            new XAttribute("ID", this.PointCollectionID),
            new XAttribute("Operation", this.OperationEnum));
    for (int i = 0; i < PointCollection.PointCount; i++)
    {
        pnt = PointCollection.get_Point(i);
        XElement point = new XElement("Point", pnt.X, " ", pnt.Y);
        geometry.Add(point);
    }
    //添加节点
    project.Add(cooperationMap);
    cooperationMap.Add(layer);
    layer.Add(geometry);
    //保存
    project.Save(absolutePath);
}

3.2 解译

读取XML的PointCollection的实例化要和几何图形类型(esriGeometryType)要对应;草图工具 IEngineEditSketch 的点的添加为 pEnginEditSketch.AddPoint(point, true); ,线面则是将PointCollection赋值给 pEnginEditSketch.Geometry ,然后调用 pEnginEditSketch.FinishSketch(); 

public void ReadXML(string absolutePath)
{
    //加载文档
    XDocument document = XDocument.Load(absolutePath);
    //获取参数
    XElement project = document.Root;
    ProjectID = project.Attribute("ID").Value;
    XElement cooperationMap = project.Element("CooperationMap");
    CooperationMapID = cooperationMap.Attribute("ID").Value;
    XElement layer = cooperationMap.Element("Layer");
    LayerName = layer.Attribute("Name").Value;
    LayerType = (esriGeometryType)Enum.Parse(typeof(esriGeometryType),
        layer.Attribute("Type").Value);
    XElement geometry = layer.Element(LayerType.ToString());
    PointCollectionID = geometry.Attribute("ID").Value;
    OperationEnum = (OperationEnum)Enum.Parse(typeof(OperationEnum),
        geometry.Attribute("Operation").Value);
    //获取点集
    string[] sPoint;
    IPoint point = new PointClass();
    PointCollection = point as IPointCollection;
    if (LayerType == esriGeometryType.esriGeometryPoint)
        PointCollection = new MultipointClass();
    else if (LayerType == esriGeometryType.esriGeometryPolyline)
        PointCollection = new PolylineClass();
    else
        PointCollection = new PolygonClass();
    IEnumerable<XElement> pointcollection = geometry.Elements();
    foreach (XElement pnt in pointcollection)
    {
        sPoint = pnt.Value.Split(' ');
        point.X = double.Parse(sPoint[0]);
        point.Y = double.Parse(sPoint[1]);
        this.PointCollection.AddPoint(point);
    }
}
原文地址:https://www.cnblogs.com/liuwenzhen/p/12779775.html