1、前言

ArcEngine中,平行线的构造主要使用IConstructCurve接口实现,如果你想在生成平行线的同时对平行线的拐点进行一些特殊处理,那就需要使用IConstructCurve4接口,下面开始介绍。

2、构造一般平行线

构造一般平行线需要使用IConstructCurve接口的ConstructOffset方法,代码如下:

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1.Command
{
    /// <summary>
    /// Summary description for ConstructParallelLinesCommand.
    /// </summary>
    [Guid("639b9a57-d595-4018-85d0-fb5e9350e53c")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication1.Command.ConstructParallelLinesCommand")]
    public sealed class ConstructParallelLinesCommand : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public ConstructParallelLinesCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text
            base.m_caption = "";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            if (m_hookHelper.FocusMap.SelectionCount != 1)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取被选择的要素
            IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
            pEnumFeature.Reset();
            IFeature pFeature = pEnumFeature.Next();
            if (pFeature.Shape is IPoint || pFeature.Shape is IPolygon)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取要素类
            IFeatureLayer pFeatureLayer = m_hookHelper.FocusMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            // 获取原始线段
            IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
            IConstructCurve pConstructCurve = new Polyline() as IConstructCurve;
            pConstructCurve.ConstructOffset(pPolyline, 5);

            // 创建左侧平行线要素
            IFeature pNewFeature = pFeatureClass.CreateFeature();
            pNewFeature.Shape = pConstructCurve as IPolyline;
            pNewFeature.Store();

            // 刷新地图
            m_hookHelper.ActiveView.Refresh();
        }
        #endregion
    }
}

上面代码构造了一条间距为5米的平行线,运行结果如下图所示:

ArcEngine编辑模块——构造平行线的方法_3c


在构造平行线时,如果距离为正数,则在原始线段的右侧构建平行线,如果你想构建原始线段左侧的平行线,那么只需要把距离设置为负数即可:

IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
IConstructCurve pConstructCurve = new Polyline() as IConstructCurve;
pConstructCurve.ConstructOffset(pPolyline, -5);

上面的代码就在原始线段左侧5米处生成了一条平行线,如下图所示:

ArcEngine编辑模块——构造平行线的方法_ide_02


同时生成左右两侧的平行线代码如下:

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1.Command
{
    /// <summary>
    /// Summary description for ConstructParallelLinesCommand.
    /// </summary>
    [Guid("639b9a57-d595-4018-85d0-fb5e9350e53c")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication1.Command.ConstructParallelLinesCommand")]
    public sealed class ConstructParallelLinesCommand : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public ConstructParallelLinesCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text
            base.m_caption = "";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            if (m_hookHelper.FocusMap.SelectionCount != 1)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取被选择的要素
            IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
            pEnumFeature.Reset();
            IFeature pFeature = pEnumFeature.Next();
            if (pFeature.Shape is IPoint || pFeature.Shape is IPolygon)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取要素类
            IFeatureLayer pFeatureLayer = m_hookHelper.FocusMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            // 获取原始线段
            IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
            IConstructCurve pConstructCurve = new Polyline() as IConstructCurve;

            // 构造左侧平行线
            pConstructCurve.ConstructOffset(pPolyline, -5);
            IPolyline pLeftPolyline = pConstructCurve as IPolyline;

            // 创建左侧平行线要素
            IFeature pLeftFeature = pFeatureClass.CreateFeature();
            pLeftFeature.Shape = pLeftPolyline;
            pLeftFeature.Store();

            // 构造右侧平行线
            pConstructCurve.ConstructOffset(pPolyline, 5);
            IPolyline pRightPolyline = pConstructCurve as IPolyline;

            // 创建右侧平行线要素
            IFeature pRightFeature = pFeatureClass.CreateFeature();
            pRightFeature.Shape = pRightPolyline;
            pRightFeature.Store();

            // 刷新地图
            m_hookHelper.ActiveView.Refresh();
        }
        #endregion
    }
}

运行结果如下图所示:

ArcEngine编辑模块——构造平行线的方法_System_03

3、构造特殊平行线

先来看看在ArcMap中构造平行线的过程:

ArcEngine编辑模块——构造平行线的方法_3c_04


可以发现,ArcMap在生成平行线的同时还对拐点进行了处理,第一次生成平行线采用钝角表示拐点,第二次则采用圆角表示拐点,如果你想实现类似的效果,那就需要使用IConstructCurve4接口,下面是生成圆角平行线的代码:

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1.Command
{
    /// <summary>
    /// Summary description for ConstructParallelLinesCommand.
    /// </summary>
    [Guid("639b9a57-d595-4018-85d0-fb5e9350e53c")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication1.Command.ConstructParallelLinesCommand")]
    public sealed class ConstructParallelLinesCommand : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public ConstructParallelLinesCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text
            base.m_caption = "";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            if (m_hookHelper.FocusMap.SelectionCount != 1)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取被选择的要素
            IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
            pEnumFeature.Reset();
            IFeature pFeature = pEnumFeature.Next();
            if (pFeature.Shape is IPoint || pFeature.Shape is IPolygon)
            {
                MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 获取要素类
            IFeatureLayer pFeatureLayer = m_hookHelper.FocusMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            // 获取原始线段
            IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
            IConstructCurve4 pConstructCurve = new Polyline() as IConstructCurve4;

            // 构造左侧平行线
            pConstructCurve.ConstructOffset(pPolyline, -5, 0, esriConstructOffsetEnum.esriConstructOffsetRounded, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, 0);
            IPolyline pLeftPolyline = pConstructCurve as IPolyline;

            // 创建左侧平行线要素
            IFeature pLeftFeature = pFeatureClass.CreateFeature();
            pLeftFeature.Shape = pLeftPolyline;
            pLeftFeature.Store();

            // 构造右侧平行线
            pConstructCurve.ConstructOffset(pPolyline, 5, 0, esriConstructOffsetEnum.esriConstructOffsetRounded, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, 0);
            IPolyline pRightPolyline = pConstructCurve as IPolyline;

            // 创建右侧平行线要素
            IFeature pRightFeature = pFeatureClass.CreateFeature();
            pRightFeature.Shape = pRightPolyline;
            pRightFeature.Store();

            // 刷新地图
            m_hookHelper.ActiveView.Refresh();
        }
        #endregion
    }
}

运行结果如下图所示:

ArcEngine编辑模块——构造平行线的方法_C#_05


如果要构造钝角平行线,则需要修改一下参数:

// 左侧平行线
pConstructCurve.ConstructOffset(pPolyline, -5, 0, esriConstructOffsetEnum.esriConstructOffsetBevelled, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, 1);

// 右侧平行线
pConstructCurve.ConstructOffset(pPolyline, 5, 0, esriConstructOffsetEnum.esriConstructOffsetBevelled, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, 1);

运行结果如下图所示:

ArcEngine编辑模块——构造平行线的方法_C#_06


最后,IConstructCurve4接口也可以构造一般平行线,只需要修改一下参数即可:

// 左侧平行线
pConstructCurve.ConstructOffset(pPolyline, -5, 0, esriConstructOffsetEnum.esriConstructOffsetMitered, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, -5);

// 右侧平行线
pConstructCurve.ConstructOffset(pPolyline, 5, 0, esriConstructOffsetEnum.esriConstructOffsetMitered, esriConstructOffsetSimplifyEnum.esriConstructOffsetFastSimplify, 5);

运行结果如下图所示:

ArcEngine编辑模块——构造平行线的方法_ArcEngine_07