2012年8月24日星期五

my frist project in Arcgis

this my program writed by c# is my prime project used to draw with the map.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
namespace MapcontrolFrm
{
    class Draw
    {
        AxMapControl _axcontrol;
        int isbutton;
        public Draw()
        {
        }
        /// <summary>
        /// Draw类带参数实例化
        /// </summary>
        /// <param name="_axcontrol">MapControle控件</param>
        /// <param name="isbutton">该函数画图确认参数,1表示画点,2表示画线,3表示画矩形,4表示画多边形,5表示画圆,6表示画椭圆</param>
        public Draw(AxMapControl axcontrol, int isbuttons)
        {
            _axcontrol = axcontrol;
            isbutton = isbuttons;
        }
        /// <summary>
        /// 颜色转换,转换成engine用的IRgbColor类型
        /// </summary>
        /// <param name="G">绿色</param>
        /// <param name="R">红色</param>
        /// <param name="B">蓝色</param>
        /// <returns></returns>
        public ESRI.ArcGIS.Display.IRgbColor getRGB(int G, int R, int B)
        {
            ESRI.ArcGIS.Display.IRgbColor pColor = new ESRI.ArcGIS.Display.RgbColor();
            pColor.Red = R;
            pColor.Green = G;
            pColor.Blue = B;
            return pColor;
        }
        /// <summary>
        /// 绘制圆形元素
        /// </summary>
        public void DrawCircle()
        {
            IMap m_Map;
            IActiveView m_ActiveView = _axcontrol.ActiveView;
            IHookHelper m_hookHelper = new HookHelper();
            m_hookHelper.Hook = _axcontrol.Object;
            if (isbutton == 5)
            {
                m_ActiveView = m_hookHelper.ActiveView;
                m_Map = m_hookHelper.FocusMap;
                IScreenDisplay pScreenDisplay = m_ActiveView.ScreenDisplay;
                IRubberBand pRubberCircle = new RubberCircle();
                ISimpleFillSymbol pFillSymbol = new SimpleFillSymbol();
                pFillSymbol.Color = getRGB(255, 255, 0);
                IGeometry pCircle = pRubberCircle.TrackNew(pScreenDisplay, (ISymbol)pFillSymbol) as IGeometry;
                Polygon _Polygon = new Polygon();
                IPolygon pPolygon = _Polygon as IPolygon;    //空的多边形
                ISegmentCollection pSegmentCollection = pPolygon as ISegmentCollection;  //段集合
                ISegment pSegment = pCircle as ISegment;  //将圆赋值给段
                object missing = Type.Missing;  //显示默认值
                pSegmentCollection.AddSegment(pSegment, ref missing, ref missing);  //给空多边形加入圆
                pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;  //填充样式,符号样式
                pFillSymbol.Color = getRGB(0, 255, 255);
                PolygonElement _polygon = new PolygonElement();
                IFillShapeElement pPolygonEle = _polygon as IFillShapeElement;
                pPolygonEle.Symbol = pFillSymbol;
                IElement pEle = pPolygonEle as IElement;
                pEle.Geometry = pPolygon;
                IGraphicsContainer pGraphicsContainer = m_Map as IGraphicsContainer;
                pGraphicsContainer.AddElement(pEle, 0);
                m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            else
            { return; }
        }
        /// <summary>
        /// 绘制矩形元素
        /// </summary>
        public void DrawRectangle()
        {
            IMap m_Map;
            IActiveView m_ActiveView = _axcontrol.ActiveView;
            IHookHelper m_hookHelper = new HookHelper();
            m_hookHelper.Hook = _axcontrol.Object;
            if (isbutton == 3)
            {
                m_ActiveView = m_hookHelper.ActiveView;
                m_Map = m_hookHelper.FocusMap;
                IScreenDisplay pScreenDisplay = m_ActiveView.ScreenDisplay;
                IRubberBand pRubberRectangularPolygon = new RubberRectangularPolygon();
                ISimpleFillSymbol pFillSymbol = new SimpleFillSymbol();
                IPolygon pPolygon = pRubberRectangularPolygon.TrackNew(pScreenDisplay, (ISymbol)pFillSymbol) as IPolygon;
                pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;//填充样式
                pFillSymbol.Color = getRGB(0, 255, 255);//符号颜色
                PolygonElement _polygon = new PolygonElement();
                IFillShapeElement pPolygonEle = _polygon as IFillShapeElement;
                pPolygonEle.Symbol = pFillSymbol;
                IElement pEle = pPolygonEle as IElement;
                pEle.Geometry = pPolygon;
                IGraphicsContainer pGraphicsContainer = m_Map as IGraphicsContainer;
                pGraphicsContainer.AddElement(pEle, 0);
                m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            else
            { return; }
        }
        /// <summary>
        /// 绘制多边形元素
        /// </summary>
        public void DrawPolygon()
        {
            IMap m_Map;
            IActiveView m_ActiveView = _axcontrol.ActiveView;
            IHookHelper m_hookHelper = new HookHelper();
            m_hookHelper.Hook = _axcontrol.Object;
            if (isbutton == 4)
            {
                m_ActiveView = m_hookHelper.ActiveView;
                m_Map = m_hookHelper.FocusMap;
                IScreenDisplay pScreenDisplay = m_ActiveView.ScreenDisplay;
                IRubberBand pRubberPolygon = new RubberPolygon();
                ISimpleFillSymbol pFillSymbol = new SimpleFillSymbol();
                pFillSymbol.Color = getRGB(255, 255, 0);
                IPolygon pPolygon = pRubberPolygon.TrackNew(pScreenDisplay, (ISymbol)pFillSymbol) as IPolygon;
                pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
                pFillSymbol.Color = getRGB(0, 255, 255);
                PolygonElement _polygon = new PolygonElement();
                IFillShapeElement pPolygonEle = _polygon as IFillShapeElement;
                pPolygonEle.Symbol = pFillSymbol;
                IElement pEle = pPolygonEle as IElement;
                pEle.Geometry = pPolygon;
                IGraphicsContainer pGraphicsContainer = m_Map as IGraphicsContainer;
                pGraphicsContainer.AddElement(pEle, 0);
                m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            else
            { return; }
        }
        /// <summary>
        /// //绘制线型元素
        /// </summary>
        public void DrawLine()
        {
            if (isbutton == 2)
            {
                IMap pMap;
                IActiveView pActiveView;
                pMap = _axcontrol.Map;
                IGraphicsContainer pGraphicsContainer;
                pGraphicsContainer = pMap as IGraphicsContainer;
                pActiveView = pMap as IActiveView;
                //定义多义线对象
                IPolyline pPolyline;
                pPolyline = _axcontrol.TrackLine() as IPolyline;
                //定义简单线符号对象
                ISimpleLineSymbol pSimpleLineSym;
                pSimpleLineSym = new SimpleLineSymbol();
                pSimpleLineSym.Style = esriSimpleLineStyle.esriSLSSolid;
                IRgbColor pColor;
                pColor = getRGB(100, 0, 0);
                pSimpleLineSym.Color = pColor;
                pSimpleLineSym.Width = 2;
                //定义线几何对象
                LineElement linesybol = new LineElement();
                ILineElement pLineEle = linesybol as ILineElement;
                pLineEle.Symbol = pSimpleLineSym;
                //定义几何对象
                IElement pEle;
                pEle = pLineEle as IElement;
                //设置元素的几何属性
                pEle.Geometry = pPolyline;
                IGraphicsContainer pGrahpicsContainer; pGrahpicsContainer = pMap as IGraphicsContainer; pGrahpicsContainer.AddElement(pEle, 0);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            else
            { return; }
        }
        /// <summary>
        /// //绘制椭圆形元素
        /// </summary>
        public void DrawOval()
        {
            IActiveView m_ActiveView = _axcontrol.ActiveView;
            IHookHelper m_hookHelper = new HookHelper();
            m_hookHelper.Hook = _axcontrol.Object;
            if (isbutton == 6)
            {
                IEnvelope pEnvelope = new Envelope() as IEnvelope;
                pEnvelope = _axcontrol.TrackRectangle();
                IConstructEllipticArc pConstructEllipticArc = new EllipticArc() as IConstructEllipticArc;
                pConstructEllipticArc.ConstructEnvelope(pEnvelope);

                IEllipticArc pEllipticArc = pConstructEllipticArc as IEllipticArc;  
                ISegment pSegment = pEllipticArc as ISegment;

                ISegmentCollection pSegmentCollection = new Ring() as ISegmentCollection;
                object o = Type.Missing;
                pSegmentCollection.AddSegment(pSegment, ref o, ref o);

                IRing pRing = pSegmentCollection as IRing;
                pRing.Close();

                IGeometryCollection pGeometryColl = new Polygon() as IGeometryCollection;
                pGeometryColl.AddGeometry(pRing, ref o, ref o);

                ISimpleLineSymbol pSimpleLineSym = new SimpleLineSymbol();//填充样式与颜色
                pSimpleLineSym.Color = getRGB(0, 0, 0);              
                pSimpleLineSym.Width = 2;
                ISimpleFillSymbol pSimpleFillSym = new SimpleFillSymbol();
                pSimpleFillSym.Color = getRGB(255, 0, 0);
                pSimpleFillSym.Color.Transparency = 255;
                pSimpleFillSym.Outline = pSimpleLineSym;

                IElement pElement = new EllipseElement();
                pElement.Geometry = pGeometryColl as IGeometry;
                IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
                pFillShapeElement.Symbol = pSimpleFillSym;

                IGraphicsContainer pGC = _axcontrol.ActiveView.GraphicsContainer;
                pGC.AddElement(pElement, 0);
                _axcontrol.Refresh();
            }
        }
        /// <summary>
        /// //绘制点元素
        /// </summary>
        public void DrawMarker(IMapControlEvents2_OnMouseDownEvent e)
        {
            if (isbutton == 1)
            {
                IMap m_map = _axcontrol.Map;
                IActiveView m_activeview = _axcontrol.ActiveView;
                IPoint point = new Point();
                point.PutCoords(e.mapX, e.mapY);
                IMarkerElement maker = new MarkerElement() as IMarkerElement;
                IElement element = maker as IElement;
                ISimpleMarkerSymbol makersyb = new SimpleMarkerSymbol();
                element.Geometry = point;
                makersyb.Color = getRGB(255, 255, 0);
                maker.Symbol = makersyb;
                IGraphicsContainer pGraphicsContainer = m_map as IGraphicsContainer;
                pGraphicsContainer.AddElement(element, 0);
                m_activeview.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }

        }


    }
}


Welcome to your suggestion !



没有评论:

发表评论