欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#+MO实现一个道路编辑软件(刚开始)

程序员文章站 2023-11-09 12:54:46
//********************************************************** //******主窗口程序 //*********...
//**********************************************************

//******主窗口程序

//********************************************************

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace irnd_dpt
{
    public partial class frmmain : form
    {
        irnd_dpt.layervariable publayervariable = new layervariable ();
        public frmmain()
        {
            initializecomponent();
        }
        //添加图层
        private void tlbaddlayer_click(object sender, eventargs e)
        {
            irnd_dpt.openfile addfile = new openfile();
            addfile.openshapefiles(cd1,axmap1 );

            object refmap = (object)this.axmap1;
            bool reftrue = true;
            short refshort = 0;
            axlegend1.setmapsource(ref refmap);
            axlegend1.showalllegend();
            axlegend1.loadlegend(ref reftrue);
            axlegend1.set_active(ref refshort, true);
            axmap1.refresh();
            axlegend1.refresh();

            //addfile(cd1, axmap1);
        }
        //删除图层
        private void toolstripbutton4_click(object sender, eventargs e)
        {
            if (publayervariable.maplayerindex >= 0)
            {
                for (int i = 0; i <= axmap1.layers.count - 1; i++)
                {
                    axmap1.layers.remove(publayervariable.maplayerindex);
                    break;
                }
                bool reftrue = true;
                axmap1.refresh();
                axlegend1.loadlegend(ref reftrue);
            }

        }
        private void axlegend1_aftersetlayervisible(object sender,axsamplelegendcontrol.__legend_aftersetlayervisibleevent e)
        {
            axmap1.refresh();            
        }

        private void axlegend1_layerdblclick(object sender, axsamplelegendcontrol.__legend_layerdblclickevent e)
        {

        }

        private void axlegend1_mousedownevent(object sender, axsamplelegendcontrol.__legend_mousedownevent e)
        {

            if (e.index >= 0)
            {
                mapobjects2.maplayer layer= (mapobjects2.maplayer )axmap1.layers.item(e.index );
                publayervariable.maplayername = layer.name;
                publayervariable.maplayerindex = e.index;                
            }
            axmap1.trackinglayer.refresh(true, axmap1.extent);
        }
        //放大
        private void tlb_zoomin_click(object sender, eventargs e)
        {
            axmap1.mousepointer = mapobjects2.mousepointerconstants.mozoomin;
        }
        //缩小
        private void tlb_zoomout_click(object sender, eventargs e)
        {
            axmap1.mousepointer = mapobjects2.mousepointerconstants.mozoomout;
        }
        //漫游
        private void tlb_pan_click(object sender, eventargs e)
        {
            axmap1.mousepointer = mapobjects2.mousepointerconstants.mopan;
        }
        //全图
        private void tbl_full_click(object sender, eventargs e)
        {
            axmap1.extent = axmap1.fullextent;
            axmap1.mousepointer = mapobjects2.mousepointerconstants.moarrow;
        }
        //逐渐放大
        private void tbl_smallin_click(object sender, eventargs e)
        {
            mapobjects2.rectangle r = axmap1.extent;
            r.scalerectangle(0.9);
            axmap1.extent = r;
        }
        //逐渐缩小
        private void tbl_smallout_click(object sender, eventargs e)
        {
            mapobjects2.rectangle r = axmap1.extent;
            r.scalerectangle(1.1);
            axmap1.extent = r;
        }
        //选择查询
        private void tbl_identify_click(object sender, eventargs e)
        {
            axmap1.mousepointer = mapobjects2.mousepointerconstants.moidentify;
        }
        //属性浏览
        private void toolstripbutton2_click(object sender, eventargs e)
        {
            if (publayervariable.maplayerindex >= 0 & publayervariable.maplayername != null)
            {
                mapobjects2.maplayer lyr = (mapobjects2.maplayer )axmap1.layers.item(publayervariable.maplayername);
                irnd_dpt.frmbrowseattr frmbrowset = new frmbrowseattr();                
                frmbrowset.inilistview(lyr);
                frmbrowset.showdialog(this); 
            }
        }
        //地图响应事件
        private void axmap1_mousedownevent(object sends, axmapobjects2._dmapevents_mousedownevent e)
        {
            mapobjects2.rectangle rect;
            mapobjects2.point curp;
            mapobjects2.maplayer lyr;
            mapobjects2.recordset rest;

            switch (axmap1.mousepointer)
            {
                //放大                    
                case mapobjects2.mousepointerconstants.mozoomin:
                {                    
                    rect = axmap1.trackrectangle();
                    if (rect.width == 0 || rect.height == 0)
                    {
                        rect = axmap1.extent;
                        rect.scalerectangle(0.5);
                    }
                    axmap1.extent = rect;
                    break;
                }
                //缩小
                case mapobjects2.mousepointerconstants.mozoomout:
                {
                    mapobjects2.rectangle tempr;
                    tempr = axmap1.extent;
                    rect = axmap1.trackrectangle();
                    double newsr;
                    if (rect.width != 0 || rect.height != 0)
                    {
                        if (axmap1.extent.width / rect.width  > axmap1.extent.height / rect.height)
                        {
                            newsr = axmap1.extent.height / rect.height;
                        }
                        else
                        {
                            newsr = axmap1.extent.width / rect.width;
                        }
                        tempr.scalerectangle(newsr);
                    }
                    else 
                    {
                        tempr.scalerectangle(2.0);
                    }
                    axmap1.extent = tempr;
                    break;
                }
                //漫游
                case mapobjects2.mousepointerconstants.mopan:
                {
                    axmap1.pan();
                    break;
                }
                //选择查询
            case mapobjects2.mousepointerconstants.moidentify:
                {
                    if (publayervariable.maplayerindex >= 0 && publayervariable.maplayername != "")
                    {
                        rect = axmap1.trackrectangle();
                        lyr = (mapobjects2.maplayer)axmap1.layers.item(publayervariable.maplayerindex);
                        if (rect.width == 0)
                        {
                            curp = axmap1.tomappoint(e.x, e.y);
                            rest = lyr.searchbydistance(curp, (double)axmap1.tomapdistance((float)screen.primaryscreen.workingarea.x * 5), "");
                        }
                        else
                        {
                            rest = lyr.searchshape(rect, mapobjects2.searchmethodconstants.moareaintersect, "");
                        }
                        if (rest.eof!=true)
                        {
                            axmap1.flashshape(rest.fields.item("shape").value, 2);
                            irnd_dpt.frmidentify functionclass = new frmidentify();
                            ////functionclass.close();  
                            functionclass.currecordset(rest);
                            functionclass.initvfeat(rest, lyr.name);
                            functionclass.inilvwattr(rest);
                            functionclass.show(this);
                        }
                    }
                    break;
                }
            }
        }


        ////////
        ///////////////////////////////////////////////////////////////////////////////////////////////
    }
}

//*************************************************

//********属性查询

//************************************************

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace irnd_dpt
{
    public partial class frmidentify : form
    {
        mapobjects2.recordset mrs;        

        public frmidentify()
        {
            initializecomponent();
        }
        private void frmidentify_load(object sender, system.eventargs e)
        {
            lvwattr.view = view.details; 
            lvwattr.columns.add("字段",50,horizontalalignment.left );
            lvwattr.columns.add ("值",50,horizontalalignment.center );

        }

        //


        private void tvfeat_nodemouseclick(object sender, system.windows.forms.treenodemouseclickeventargs e)
        {
            string id;
            listviewitem item;

            //if (e.node.parent == null) { return; }
            lvwattr.items.clear();
            id = e.node.text;
            mrs.movefirst();
            while (mrs.eof != true)
            {
                if (id == mrs.fields.item("featureid").valueasstring)
                {
                    for (short fld = 1; fld < mrs.tabledesc.fieldcount; fld++)
                    {                        
                        item = lvwattr.items.add(mrs.tabledesc.get_fieldname(fld));
                        item.subitems.add(mrs.fields.item(item.text).valueasstring);
                    }
                    lvwattr.refresh();   
                    break;
                }
                mrs.movenext();
            }     
        }

        public void currecordset(mapobjects2.recordset vnewvalue)
        {
            mrs = vnewvalue;
        }
        public void inilvwattr(mapobjects2.recordset rst)
        {
            treenode n=null ;
            listviewitem item;
            string id;

            lvwattr.items.clear();
            if (tvfeat.nodes.count >= 2)
            {
                int i = 0;
                foreach (treenode tn in tvfeat.nodes)
                {                    
                    i=i+1;
                    if (i == 2) 
                    { 
                        n = tn;
                        break;
                    }
                }              

                if (n.text != null)
                {
                    rst.movefirst();                    
                    id = rst.fields.item("featureid").valueasstring;                    
                    while (rst.eof != true)
                    {
                        if (id == n.text)
                        {
                            for (short fld = 1; fld < rst.tabledesc.fieldcount; fld++)
                            {
                                item = lvwattr.items.add(rst.tabledesc.get_fieldname(fld));
                                item.subitems.add(rst.fields.item(item.text).valueasstring);
                            }
                            lvwattr.refresh();
                            break;
                        }
                    }
                }
            }

        }
        //将选择的记录集加入到树型控件中进行显示
        public void initvfeat(mapobjects2.recordset mors_in, string lyrname_in)
        {
            treenode n;            
            string id;
            if (mors_in.eof != true)
            {
                mors_in.movefirst();
                tvfeat.nodes.clear();
                n = tvfeat.nodes.add(lyrname_in);
                n=n.parent;
                while (mors_in.eof != true)
                {             
                    id = mors_in.fields.item("featureid").valueasstring;                    
                    n= tvfeat.nodes.add(id);
                    mors_in.movenext(); 
                }                  
            }
        }


        ///////zuihou////////////////////////////////////////////////////////////////////////////
    }
}

//*************************************************************

//************加载图层函数

//*************************************************************

using system;
using system.collections.generic;
using system.text;
using system.windows.forms;

namespace irnd_dpt
{
    class openfile
    {   
        //功能说明:
        //    从公共对话框中提取文件名,然后调用不同的子模块处理不同的文件加入图层的问题(支持打开多个文件)
        public void  openshapefiles(openfiledialog comopenfile,axmapobjects2.axmap map)
        {
            try
            {
                string strshape, strcov, strcad, strvpf, strimage, strmilimage;


                //将过滤器设置为可支持的文件
                strshape = "esri shapefiles(*.shp)|*.shp";
                strcov = "esri coverages(*.adf,*.tat,*.pat,*.rat)|aat.adf;pat.adf;nat.adf;txt.adf;*.tat;*.pat;*.rat";
                strcad = "autocad file (*.dwg,*.dxf)|*.dwg;*.dxf";
                strimage ="all image(*.bmp,*.dib,*.tif,*.jpg,*.jff,*.bil,*.bip,*.bsq,*.gis,*.lan,*.rlc,*.sid,*.sun,*.rs,*.ras,*.svf,*.img,*.gif)|*.bmp,*.dib,*.tif,*.jpg,*.jff,*.bil,*.bip,*.bsq,*.gis,*.lan,*.rlc,*.sid,*.sun,*.rs,*.ras,*.svf,*.img,*.gif";

                comopenfile.checkfileexists = true;
                //设置过滤
                comopenfile.filter = strshape + "|" + strcov + "|" + strcad + "|" + strimage ;
                comopenfile.title = "添加图层";
                //允许选择多个文件,并允许长文件名
                comopenfile.multiselect = true;
                if (comopenfile.showdialog() == dialogresult.ok)
                {

                    foreach (string strfilename in comopenfile.filenames)
                    {
                        string spath = strfilename.substring(0, strfilename.lastindexof("\\") + 1);
                        string sfile = strfilename.substring(strfilename.lastindexof("\\") + 1, strfilename.length - strfilename.lastindexof("\\") - 1);
                        string nametype = sfile.substring(sfile.lastindexof(".") + 1, sfile.length - sfile.lastindexof(".") - 1);
                        open_file(spath, sfile, nametype, map);
                    }
                }
            }
            catch
            {
                messagebox.show("图层添加有错!");
            }         
        }
        //功能说明:
        //    测试文件类型,一便调用不同的子模块.处理不同图层加入的问题
        private void open_file(string path,string filename,string filetype,axmapobjects2.axmap mapobject)
        {
            string layername = filename.substring(0, filename.lastindexof("."));
            switch (filetype)
            {
                case "shp":
                    shpadd(path, filename, mapobject, layername, true);
                    break;

            }
        }
        //功能说明:
        //添加shape格式的文件
        public void   shpadd(string databasepath, string filename,axmapobjects2.axmap mapobject, string layername, bool hide)
        { 
            mapobjects2.dataconnection dcon=new mapobjects2.dataconnection() ;
           // mapobjects2.geodataset gset=new mapobjects2.geodataset ();
            mapobjects2.maplayer newlayer = new mapobjects2.maplayer();     
            long i=0;

            try
            {
                dcon.database = databasepath;
                if (dcon.connect())
                {
                    mapobjects2.geodataset gset = dcon.findgeodataset(filename);
                    if (gset == null)
                    {

                        return ;
                    }
                    else
                    { 
                        //查看当前是否有重复的图层名,以确保所有的图层名都是唯一的
                        if (findmaplayername(mapobject,layername)==true )
                        {
                            string tempstr = layername;
                            while (findmaplayername(mapobject, tempstr + "-" + i)==true )
                            {
                                i++;
                            }
                            layername = layername + "-" + i;
                        }
                        newlayer.geodataset = gset;

                        if (hide)
                        {
                            newlayer.visible = true;
                        }
                        else
                        {
                            newlayer.visible = false; 
                        }
                        //使用默认颜色和样式设置图层
                        switch (newlayer.shapetype)
                        { 
                            case mapobjects2.shapetypeconstants.moshapetypepoint :
                                newlayer.symbol.color=11513775;
                                newlayer.symbol.size=5;
                                break;
                            case mapobjects2.shapetypeconstants .moshapetypeline :
                                newlayer.symbol.color = 11513775;
                                newlayer.symbol.size = 1;
                                break;
                            case mapobjects2.shapetypeconstants.moshapetypepolygon :
                                newlayer.symbol.symboltype = 0;
                                newlayer.symbol.style = 8;
                                newlayer.symbol.color = 12566463;
                                newlayer.symbol.outline = true;
                                newlayer.symbol.outlinecolor = (int)mapobjects2.colorconstants.moblack;
                                break;
                        }
                        mapobject.layers.add (newlayer);
                        newlayer.name = layername;
                        newlayer.tag=databasepath + filename ;                        

                    }

                }
            }
            catch
            { 
               // messagebox("加载图层出错!");
            }
        }
        //功能说明:
        //    查找map中的图层axmapobjects2.axmap mapobject,string layername 
        public bool findmaplayername(axmapobjects2.axmap mapobject, string layername)
        {    

            for (int i=0;i<mapobject.layers.count ;i++)
            {
                if (mapobject.layers.item(i).tostring() ==layername )
                {
                    return true;

                }
            }     
           return false ;

        }
    }
}