• Web直接导入导出SHP/CAD实现探讨。


    1、导入SHP/CAD文件

      WEB具有直接美观展现功能,功能实现到可视化最好不要超过3S,那么就要限制导入文件的大小和优化算法了。

    1.1、SHP导入实现思路

      SHP格式开源,Git上随便可以找到读取SHP的源码,读取后一般返回GeoJson或JSON格式的要素,然后解析GeoJson实现可视化,ArcGIS体系或OpenLayers体系。

     1 var jsonf = GeoJsonConverter();//GeoJson转Esri
     2 shp(filename).then(function (data) {
     3     var json = jsonf.toEsri(data);//ESRI要素
     4     window.map.graphics.clear();
     5     var featsarray = [];
     6     for (var j = 0; j < json.features.length; j++) {
     7          var ss = Graphic(json.features[j]);
     8          ss.setSymbol(fillSymbol);
     9          ss.geometry.setSpatialReference(window.map.spatialReference);
    10          window.map.graphics.add(ss);
    11          featsarray.push(ss);
    12     }
    13     var ptcen = featsarray[0].geometry.getExtent().getCenter();
    14     window.map.setScale(4000);
    15     window.map.centerAt(ptcen);
    16  });
    ArcGIS实现加载SHP文件

    1.2、CAD导入实现思路

      CAD格式封闭,只好通过后台服务(AO/AE开发)或GP服务(要素转JSON,貌似ArcGIS10.3及以上才有),原理也是读取CAD文件后将其转为JSON格式,WEB前端解析JSON可视化显示。

    (1)读取CAD:AO/AE开发读取,比较简单;读取后转为JSON可借助类JSONConverterGeometryClass,熟悉JSON结构也可以直接写代码。

    /// <summary>
            /// 提取feature的geometry,并将其转换为json对象
            /// </summary>
            /// <param name="pFeature">要素对象</param>
            /// <returns></returns>
            public static string feature2JsonGeometry(ESRI.ArcGIS.Geodatabase.IFeature pFeature)
            {
                ESRI.ArcGIS.Geometry.IGeometry pGeo = pFeature.Shape;
                //return GeometryToJsonString(pGeo);
                int wkid = pGeo.SpatialReference.FactoryCode;
                ESRI.ArcGIS.Geometry.IPoint pPoint = null;
                ESRI.ArcGIS.Geometry.IPointCollection pPoints = null;
                double x, y;
                StringBuilder sb = new StringBuilder("{");
    
                switch (pGeo.GeometryType)
                {
                    #region Point2Json
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                        pPoint = pGeo as ESRI.ArcGIS.Geometry.IPoint;
                        pPoint.QueryCoords(out x, out y);
                        string json = @"""x"":" + x + @",""y"":" + y + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}";
                        sb.Append(json);
                        break;
                    #endregion
    
                    #region Polyline2Json
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                        pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection;
                        sb.Append(@"""paths"":[[");
                        for (int i = 0; i < pPoints.PointCount; i++)
                        {
                            pPoint = pPoints.get_Point(i);
                            pPoint.QueryCoords(out x, out y);
                            sb.Append("[" + x + "," + y + "],");
                        }
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append("]]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}");
                        break;
    
                    #endregion
    
                    #region Polygon2Json
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                        pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection;
                        sb.Append(@"""rings"":[[");
                        for (int i = 0; i < pPoints.PointCount; i++)
                        {
                            pPoint = pPoints.get_Point(i);
                            pPoint.QueryCoords(out x, out y);
                            sb.Append("[" + x + "," + y + "],");
                        }
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append("]]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}");
    
                        break;
                    #endregion
                }
                sb.Append("}");
                return sb.ToString();
            }
    提取feature的geometry,并将其转换为json对象

    (2)GP服务(要素转JSON),在高版本中工具箱——转换(Conversion)——JSON,执行后发布为GP服务,前端调用GPServer实现读取转为JSON。

    $.ajax({
          type: "post",
          url: window.top.RootPath + "/fastGIS/pro/FastService.asmx/GetCADGraphicContent",
          contentType: "application/json; charset=utf-8",
          data: "{fileName:'" + filename + "'}",
          success: function (result) {
                console.log(result);
                var jsons = result.d.split("|");
                if (jsons) {
                    for(var i=0;i<jsons.length;i++){
                        var geore = JsonUtils.fromJson(JSON.parse(jsons[i]));//ArcGIS JS API函数解析
                        geore.setSpatialReference(window.top.map.spatialReference);
                         switch (geore.type) {
                             case "polygon":
                                  window.top.map.graphics.add(Graphic(geore, fillSymbol));
                                  break;
                             case "polyline":
                                  window.top.map.graphics.add(Graphic(geore, lineSymbol));
                                  break;
                             default:
                                  break;
                          }
                         window.top.map.centerAt(geore.getPoint(0, 0));
                     }
                }
                window.top.map.setScale(4000);
            },
        error: function (err) {
            console.log(err);
        }
    });
    后台服务AJAX实现可视化

    2、导出SHP/CAD文件

      导出是导入的逆向过程,要求从WebGIS的地图上(各种可视化地图)按照需求(比如按区域、按绘制范围)导出对应的图形和属性数据,前后台联合进行。

    2.1 导出SHP文件实现思路

      在地图前台操作,肯定是需要通过绘制范围、选择对象查询符合区域的数据,将该区域的相关对象前台或后台处理;有2种方式,一种直接后台转JSON再输出为SHP文件,另一种就是全部后台运作,通过ArcGIS后台开发保存为SHP文件。

      此处参考博客:【http://blog.csdn.net/gis0911178/article/details/52162477】详细说明SHP文件,开源读取并生成SHP文件。【引用已说明出处,尊重原作】

    2.2 导出CAD文件实现思路

      前面已说明CAD的DWG格式文件封闭,但DXF格式开源,因此可以直接写DXF输出文件;另外一种思路则是直接通过AO/AE开发后台,“导出SHP—转为CAD”这种思路输出DWG文件。

    Geoprocessor gp = new Geoprocessor();
    ExportCAD exCAD = new ExportCAD();//工具箱中的转为CAD工具
    exCAD.Output_File = @fileDir + ".dwg";
    exCAD.Output_Type = "DWG_R2004";
    exCAD.in_features = @fileDir + "\" + fileName + ".shp";
    exCAD.Ignore_FileNames = "False";
    try
     {
          var res = gp.Execute(exCAD, null);
          return "/TemporaryFile/createSHP/" + fileName + ".dwg";
    }
    catch (Exception ee)
    {               
            
    }
    Geoprocessor实现SHP转CAD

    另外写DXF输出文件也提供源码。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Globalization;
    
    using ESRI.ArcGIS.Geometry;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.Carto;
    
    
    
    namespace Fast.FastMan.BUS.GisPro.CommonGIS
    {
        public class ConvertDXFArgs : System.EventArgs
        {
            bool result = false;
            Exception ex = null;
    
            public ConvertDXFArgs()
            {
                result = true;
            }
    
            public ConvertDXFArgs(Exception ex)
            {
                this.ex = ex;
            }
    
            public bool Result
            {
                get { return result; }
            }
    
            public Exception Exception
            {
                get { return ex; }
            }
        }
    
        public class ConvertDXF
        {
            public delegate void OnCompleted(object sender, ConvertDXFArgs e);
            public event OnCompleted completed;
    
            struct ColorDXF
            {
                public byte Red;
                public byte Green;
                public byte Blue;
                public ColorDXF(byte r, byte g, byte b)
                {
                    Red = r;
                    Green = g;
                    Blue = b;
                }
            }
    
            struct TagDXF
            {
                public const string HEADER = "HEADER";
                public const string TABLES = "TABLES";
                public const string ENTITIES = "ENTITIES";
                public const string BLOCKS = "BLOCKS";
            }
    
            string m_Filename = null;
            IFeatureLayer m_FeatureLayer = null;
            TextWriter m_tw = null;
    
            public ConvertDXF(string filename, IFeatureLayer featureLayer)
            {
                m_Filename = filename;
                m_FeatureLayer = featureLayer;
            }
    
            public void Convert()
            {
                if ((m_FeatureLayer == null) || (string.IsNullOrEmpty(m_Filename)))
                {
                    completed(this, new ConvertDXFArgs(new Exception("FeatureLayer is null or Filename is null")));
                }
    
                try
                {
                    m_tw = new StreamWriter(m_Filename);
    
                    IEnvelope envelope = m_FeatureLayer.AreaOfInterest;
                    IFeatureClass featureClass = m_FeatureLayer.FeatureClass;
    
                    Header(envelope);
                    Tables();
                    Blocks();
                    Entities();
    
                    completed(this, new ConvertDXFArgs());
                }
                catch (Exception ex)
                {
                    completed(this, new ConvertDXFArgs(ex));
                }
                finally
                {
                    m_tw.Close();
                    m_tw.Dispose();
                    m_tw = null;
                }
    
            }
    
            /// <summary>
            /// return color nearest Autocad
            /// </summary>
            /// <param name="intR"></param>
            /// <param name="intG"></param>
            /// <param name="intB"></param>
            /// <returns></returns>
            static ColorDXF GetColorDXF(ColorDXF c)
            {
                IDictionary<byte, string> dct = new Dictionary<byte, string>();
                #region Dictionary Color
                //item: colore come valore RGB, key: colore equivalent in AutoCAD
                dct.Add(0, "0|0|0");
                dct.Add(1, "255|0|0");
                dct.Add(2, "255|255|0");
                dct.Add(3, "0|255|0");
                dct.Add(4, "0|255|255");
                dct.Add(5, "0|0|255");
                dct.Add(6, "255|0|255");
                dct.Add(7, "255|255|255");
                dct.Add(8, "65|65|65");
                dct.Add(9, "128|128|128");
                dct.Add(10, "255|0|0");
                dct.Add(11, "255|170|170");
                dct.Add(12, "189|0|0");
                dct.Add(13, "189|126|126");
                dct.Add(14, "129|0|0");
                dct.Add(15, "129|86|86");
                dct.Add(16, "104|0|0");
                dct.Add(17, "104|69|69");
                dct.Add(18, "79|0|0");
                dct.Add(19, "79|53|53");
                dct.Add(20, "255|63|0");
                dct.Add(21, "255|191|170");
                dct.Add(22, "189|46|0");
                dct.Add(23, "189|141|126");
                dct.Add(24, "129|31|0");
                dct.Add(25, "129|96|86");
                dct.Add(26, "104|25|0");
                dct.Add(27, "104|78|69");
                dct.Add(28, "79|19|0");
                dct.Add(29, "79|59|53");
                dct.Add(30, "255|127|0");
                dct.Add(31, "255|212|170");
                dct.Add(32, "189|94|0");
                dct.Add(33, "189|157|126");
                dct.Add(34, "129|64|0");
                dct.Add(35, "129|107|86");
                dct.Add(36, "104|52|0");
                dct.Add(37, "104|86|69");
                dct.Add(38, "79|39|0");
                dct.Add(39, "79|66|53");
                dct.Add(40, "255|191|0");
                dct.Add(41, "255|234|170");
                dct.Add(42, "189|141|0");
                dct.Add(43, "189|173|126");
                dct.Add(44, "129|96|0");
                dct.Add(45, "129|118|86");
                dct.Add(46, "104|78|0");
                dct.Add(47, "104|95|69");
                dct.Add(48, "79|59|0");
                dct.Add(49, "79|73|53");
                dct.Add(50, "255|255|0");
                dct.Add(51, "255|255|170");
                dct.Add(52, "189|189|0");
                dct.Add(53, "189|189|126");
                dct.Add(54, "129|129|0");
                dct.Add(55, "129|129|86");
                dct.Add(56, "104|104|0");
                dct.Add(57, "104|104|69");
                dct.Add(58, "79|79|0");
                dct.Add(59, "79|79|53");
                dct.Add(60, "191|255|0");
                dct.Add(61, "234|255|170");
                dct.Add(62, "141|189|0");
                dct.Add(63, "173|189|126");
                dct.Add(64, "96|129|0");
                dct.Add(65, "118|129|86");
                dct.Add(66, "78|104|0");
                dct.Add(67, "95|104|69");
                dct.Add(68, "59|79|0");
                dct.Add(69, "73|79|53");
                dct.Add(70, "127|255|0");
                dct.Add(71, "212|255|170");
                dct.Add(72, "94|189|0");
                dct.Add(73, "157|189|126");
                dct.Add(74, "64|129|0");
                dct.Add(75, "107|129|86");
                dct.Add(76, "52|104|0");
                dct.Add(77, "86|104|69");
                dct.Add(78, "39|79|0");
                dct.Add(79, "66|79|53");
                dct.Add(80, "63|255|0");
                dct.Add(81, "191|255|170");
                dct.Add(82, "46|189|0");
                dct.Add(83, "141|189|126");
                dct.Add(84, "31|129|0");
                dct.Add(85, "96|129|86");
                dct.Add(86, "25|104|0");
                dct.Add(87, "78|104|69");
                dct.Add(88, "19|79|0");
                dct.Add(89, "59|79|53");
                dct.Add(90, "0|255|0");
                dct.Add(91, "170|255|170");
                dct.Add(92, "0|189|0");
                dct.Add(93, "126|189|126");
                dct.Add(94, "0|129|0");
                dct.Add(95, "86|129|86");
                dct.Add(96, "0|104|0");
                dct.Add(97, "69|104|69");
                dct.Add(98, "0|79|0");
                dct.Add(99, "53|79|53");
                dct.Add(100, "0|255|63");
                dct.Add(101, "170|255|191");
                dct.Add(102, "0|189|46");
                dct.Add(103, "126|189|141");
                dct.Add(104, "0|129|31");
                dct.Add(105, "86|129|96");
                dct.Add(106, "0|104|25");
                dct.Add(107, "69|104|78");
                dct.Add(108, "0|79|19");
                dct.Add(109, "53|79|59");
                dct.Add(110, "0|255|127");
                dct.Add(111, "170|255|212");
                dct.Add(112, "0|189|94");
                dct.Add(113, "126|189|157");
                dct.Add(114, "0|129|64");
                dct.Add(115, "86|129|107");
                dct.Add(116, "0|104|52");
                dct.Add(117, "69|104|86");
                dct.Add(118, "0|79|39");
                dct.Add(119, "53|79|66");
                dct.Add(120, "0|255|191");
                dct.Add(121, "170|255|234");
                dct.Add(122, "0|189|141");
                dct.Add(123, "126|189|173");
                dct.Add(124, "0|129|96");
                dct.Add(125, "86|129|118");
                dct.Add(126, "0|104|78");
                dct.Add(127, "69|104|95");
                dct.Add(128, "0|79|59");
                dct.Add(129, "53|79|73");
                dct.Add(130, "0|255|255");
                dct.Add(131, "170|255|255");
                dct.Add(132, "0|189|189");
                dct.Add(133, "126|189|189");
                dct.Add(134, "0|129|129");
                dct.Add(135, "86|129|129");
                dct.Add(136, "0|104|104");
                dct.Add(137, "69|104|104");
                dct.Add(138, "0|79|79");
                dct.Add(139, "53|79|79");
                dct.Add(140, "0|191|255");
                dct.Add(141, "170|234|255");
                dct.Add(142, "0|141|189");
                dct.Add(143, "126|173|189");
                dct.Add(144, "0|96|129");
                dct.Add(145, "86|118|129");
                dct.Add(146, "0|78|104");
                dct.Add(147, "69|95|104");
                dct.Add(148, "0|59|79");
                dct.Add(149, "53|73|79");
                dct.Add(150, "0|127|255");
                dct.Add(151, "170|212|255");
                dct.Add(152, "0|94|189");
                dct.Add(153, "126|157|189");
                dct.Add(154, "0|64|129");
                dct.Add(155, "86|107|129");
                dct.Add(156, "0|52|104");
                dct.Add(157, "69|86|104");
                dct.Add(158, "0|39|79");
                dct.Add(159, "53|66|79");
                dct.Add(160, "0|63|255");
                dct.Add(161, "170|191|255");
                dct.Add(162, "0|46|189");
                dct.Add(163, "126|141|189");
                dct.Add(164, "0|31|129");
                dct.Add(165, "86|96|129");
                dct.Add(166, "0|25|104");
                dct.Add(167, "69|78|104");
                dct.Add(168, "0|19|79");
                dct.Add(169, "53|59|79");
                dct.Add(170, "0|0|255");
                dct.Add(171, "170|170|255");
                dct.Add(172, "0|0|189");
                dct.Add(173, "126|126|189");
                dct.Add(174, "0|0|129");
                dct.Add(175, "86|86|129");
                dct.Add(176, "0|0|104");
                dct.Add(177, "69|69|104");
                dct.Add(178, "0|0|79");
                dct.Add(179, "53|53|79");
                dct.Add(180, "63|0|255");
                dct.Add(181, "191|170|255");
                dct.Add(182, "46|0|189");
                dct.Add(183, "141|126|189");
                dct.Add(184, "31|0|129");
                dct.Add(185, "96|86|129");
                dct.Add(186, "25|0|104");
                dct.Add(187, "78|69|104");
                dct.Add(188, "19|0|79");
                dct.Add(189, "59|53|79");
                dct.Add(190, "127|0|255");
                dct.Add(191, "212|170|255");
                dct.Add(192, "94|0|189");
                dct.Add(193, "157|126|189");
                dct.Add(194, "64|0|129");
                dct.Add(195, "107|86|129");
                dct.Add(196, "52|0|104");
                dct.Add(197, "86|69|104");
                dct.Add(198, "39|0|79");
                dct.Add(199, "66|53|79");
                dct.Add(200, "191|0|255");
                dct.Add(201, "234|170|255");
                dct.Add(202, "141|0|189");
                dct.Add(203, "173|126|189");
                dct.Add(204, "96|0|129");
                dct.Add(205, "118|86|129");
                dct.Add(206, "78|0|104");
                dct.Add(207, "95|69|104");
                dct.Add(208, "59|0|79");
                dct.Add(209, "73|53|79");
                dct.Add(210, "255|0|255");
                dct.Add(211, "255|170|255");
                dct.Add(212, "189|0|189");
                dct.Add(213, "189|126|189");
                dct.Add(214, "129|0|129");
                dct.Add(215, "129|86|129");
                dct.Add(216, "104|0|104");
                dct.Add(217, "104|69|104");
                dct.Add(218, "79|0|79");
                dct.Add(219, "79|53|79");
                dct.Add(220, "255|0|191");
                dct.Add(221, "255|170|234");
                dct.Add(222, "189|0|141");
                dct.Add(223, "189|126|173");
                dct.Add(224, "129|0|96");
                dct.Add(225, "129|86|118");
                dct.Add(226, "104|0|78");
                dct.Add(227, "104|69|95");
                dct.Add(228, "79|0|59");
                dct.Add(229, "79|53|73");
                dct.Add(230, "255|0|127");
                dct.Add(231, "255|170|212");
                dct.Add(232, "189|0|94");
                dct.Add(233, "189|126|157");
                dct.Add(234, "129|0|64");
                dct.Add(235, "129|86|107");
                dct.Add(236, "104|0|52");
                dct.Add(237, "104|69|86");
                dct.Add(238, "79|0|39");
                dct.Add(239, "79|53|66");
                dct.Add(240, "255|0|63");
                dct.Add(241, "255|170|191");
                dct.Add(242, "189|0|46");
                dct.Add(243, "189|126|141");
                dct.Add(244, "129|0|31");
                dct.Add(245, "129|86|96");
                dct.Add(246, "104|0|25");
                dct.Add(247, "104|69|78");
                dct.Add(248, "79|0|19");
                dct.Add(249, "79|53|59");
                dct.Add(250, "51|51|51");
                dct.Add(251, "80|80|80");
                dct.Add(252, "105|105|105");
                dct.Add(253, "130|130|130");
                dct.Add(254, "190|190|190");
                dct.Add(255, "255|255|255");
    
                #endregion Dictionary Color
    
                string[] sColorCAD;  //tutti possibili colori della collection
                byte bGCAD, bRCAD, bBCAD; //valori RGB di tutti colori
                byte color = 0;
    
                //color nearest of collection
                double dblTemp = Double.MaxValue;
                double dblDistance;
    
                foreach (byte idx in dct.Keys)
                {
                    sColorCAD = dct[idx].Split('|');
                    bRCAD = byte.Parse(sColorCAD[0]);
                    bGCAD = byte.Parse(sColorCAD[1]);
                    bBCAD = byte.Parse(sColorCAD[2]);
    
                    dblDistance = System.Math.Sqrt((bRCAD - c.Red) ^ 2 + (bGCAD - c.Green) ^ 2 + (bBCAD - c.Blue) ^ 2);
    
                    if (dblDistance < dblTemp)
                    {
                        dblTemp = dblDistance;
                        color = idx;
                        if (dblTemp == 0)
                            break;
    
                    }
                }
    
                //color "0" (not defined) -> color "7" (default of AutoCAD)
                if (color == 0)
                    color = 7;
    
                string[] r = dct[color].Split('|');
                return new ColorDXF(byte.Parse(r[0]), byte.Parse(r[1]), byte.Parse(r[2]));
            }
            static IFormatProvider GetFormatProvider()
            {
                NumberFormatInfo nfi = new NumberFormatInfo();
                nfi.CurrencyDecimalSeparator = ".";
                return nfi;
            }
    
            void BeginSection(string Tag)
            {
                m_tw.WriteLine(0);
                m_tw.WriteLine("SECTION");
                m_tw.WriteLine(2);
                m_tw.WriteLine(Tag);
            }
            void EndSection()
            {
                m_tw.WriteLine(0);
                m_tw.WriteLine("ENDSEC");
            }
    
            void EOF()
            {
                m_tw.WriteLine(0);
                m_tw.WriteLine("EOF");
            }
    
            void Point(IPointCollection pPoints, string LayerName, byte? Color)
            {
                for (int i = 0; i < pPoints.PointCount; i++)
                {
                    Point(pPoints.get_Point(i), LayerName, Color);
                }
            }
    
            void Blocks()
            {
                BeginSection(TagDXF.BLOCKS);
                EndSection();
            }
    
            void Tables()
            {
                BeginSection(TagDXF.TABLES);
                EndSection();
            }
    
            void Point(IPoint pPoint, string LayerName, byte? Color)
            {
                m_tw.WriteLine(0);
                m_tw.WriteLine("POINT");
                m_tw.WriteLine(8);
                m_tw.WriteLine(LayerName);
                m_tw.WriteLine(62);
                if (Color != null)
                    m_tw.WriteLine(Color);
    
                m_tw.WriteLine(10);
                m_tw.WriteLine(pPoint.X.ToString(GetFormatProvider()));
                m_tw.WriteLine(20);
                m_tw.WriteLine(pPoint.Y.ToString(GetFormatProvider()));
    
                m_tw.WriteLine(39); //Thickness
                m_tw.WriteLine(3);
            }
    
            void Header(IEnvelope extent)
            {
                IPoint LLExtents = extent.LowerLeft;
                IPoint URExtents = extent.UpperRight;
    
                BeginSection(TagDXF.HEADER);
    
                m_tw.WriteLine(9);
                m_tw.WriteLine("$EXTMIN");
                m_tw.WriteLine(10);
                m_tw.WriteLine(LLExtents.X.ToString(GetFormatProvider()));
                m_tw.WriteLine(20);
                m_tw.WriteLine(LLExtents.Y.ToString(GetFormatProvider()));
                m_tw.WriteLine(30);
                m_tw.WriteLine(0);
                m_tw.WriteLine(9);
                m_tw.WriteLine("$EXTMAX");
                m_tw.WriteLine(10);
                m_tw.WriteLine(URExtents.X.ToString(GetFormatProvider()));
                m_tw.WriteLine(20);
                m_tw.WriteLine(URExtents.Y.ToString(GetFormatProvider()));
                m_tw.WriteLine(30);
                m_tw.WriteLine(0);
    
                EndSection();
            }
    
            void Polyline(IGeometry pShape, string LayerName, byte? Color)
            {
    
                IGeometryCollection geometryCollection;
    
                if (pShape is IGeometryCollection)
                    geometryCollection = pShape as IGeometryCollection;
                else
                {
                    object o = Type.Missing;
                    geometryCollection = new Polygon() as IGeometryCollection;
                    geometryCollection.AddGeometry(pShape, ref o, ref o);
                }
    
                for (int j = 0; j < geometryCollection.GeometryCount; j++)
                {
                    ISegmentCollection pSegColl = geometryCollection.get_Geometry(j) as ISegmentCollection;
    
                    bool bFirstSeg = true;
                    ICircularArc pCA = null;
                    ISegment pSeg = null;
    
                    for (int i = 0; i < pSegColl.SegmentCount; i++)
                    {
                        pSeg = pSegColl.get_Segment(i);
    
    
                        switch (pSeg.GeometryType)
                        {
                            case esriGeometryType.esriGeometryLine:
    
                                if (bFirstSeg)
                                {
                                    m_tw.WriteLine(0);
                                    m_tw.WriteLine("POLYLINE");
    
                                    m_tw.WriteLine(8);
    
                                    m_tw.WriteLine(LayerName);
    
                                    m_tw.WriteLine(66);
                                    m_tw.WriteLine(1);
    
                                    if (Color != null)
                                    {
                                        m_tw.WriteLine(62);
                                        m_tw.WriteLine(Color);
                                    }
                                    m_tw.WriteLine(6);
                                    m_tw.WriteLine("CONTINUOUS");
                                    //from point
                                    m_tw.WriteLine(0);
                                    m_tw.WriteLine("VERTEX");
                                    m_tw.WriteLine(8);
    
                                    m_tw.WriteLine(LayerName);
    
                                    m_tw.WriteLine(10);
                                    m_tw.WriteLine(pSeg.FromPoint.X.ToString(GetFormatProvider()));
                                    m_tw.WriteLine(20);
                                    m_tw.WriteLine(pSeg.FromPoint.Y.ToString(GetFormatProvider()));
    
                                    bFirstSeg = !bFirstSeg;
                                }
    
                                // to point
                                m_tw.WriteLine(0);
                                m_tw.WriteLine("VERTEX");
                                m_tw.WriteLine(8);
                                m_tw.WriteLine(LayerName);
    
                                m_tw.WriteLine(10);
                                m_tw.WriteLine(pSeg.ToPoint.X.ToString(GetFormatProvider()));
                                m_tw.WriteLine(20);
                                m_tw.WriteLine(pSeg.ToPoint.Y.ToString(GetFormatProvider()));
                                if (i == (pSegColl.SegmentCount - 1))
                                {
                                    m_tw.WriteLine(0);
                                    m_tw.WriteLine("SEQEND");
                                    m_tw.WriteLine(8);
                                    m_tw.WriteLine(LayerName);
    
    
                                }
                                break;
    
                            case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryCircularArc:
                                if (!bFirstSeg)
                                {
    
                                    m_tw.WriteLine(0);
                                    m_tw.WriteLine("SEQEND");
                                    m_tw.WriteLine(8);
                                    m_tw.WriteLine(LayerName);
                                }
    
                                pCA = pSeg as ICircularArc;
    
                                m_tw.WriteLine(0);
                                m_tw.WriteLine("ARC");
                                m_tw.WriteLine(8);
                                m_tw.WriteLine(LayerName);
                                m_tw.WriteLine(10);
                                m_tw.WriteLine(pCA.CenterPoint.X.ToString(GetFormatProvider()));
                                m_tw.WriteLine(20);
                                m_tw.WriteLine(pCA.CenterPoint.Y.ToString(GetFormatProvider()));
                                m_tw.WriteLine(40);
                                m_tw.WriteLine(pCA.Radius);
    
                                string sFromAngle = (pCA.FromAngle * 180 / Math.PI).ToString(GetFormatProvider());
                                string sToAngle = (pCA.ToAngle * 180 / Math.PI).ToString(GetFormatProvider());
    
                                m_tw.WriteLine(50);
                                m_tw.WriteLine(pCA.IsCounterClockwise ? sFromAngle : sToAngle);
                                m_tw.WriteLine(51);
                                m_tw.WriteLine(pCA.IsCounterClockwise ? sToAngle : sFromAngle);
    
                                bFirstSeg = true;
                                break;
                        }
                    }
                }
            }
    
            void Polygon(IGeometry pShape, string LayerName, byte? Color)
            {
                IPolygon4 polygon = pShape as IPolygon4;
                IGeometryBag exteriorRings = polygon.ExteriorRingBag;
    
                //For each exterior rings find the interior rings associated with it
                IEnumGeometry exteriorRingsEnum = exteriorRings as IEnumGeometry;
                exteriorRingsEnum.Reset();
                IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
    
                while (currentExteriorRing != null)
                {
                    Polyline(currentExteriorRing, LayerName, Color);
    
                    IGeometryBag interiorRings = polygon.get_InteriorRingBag(currentExteriorRing);
                    IEnumGeometry interiorRingsEnum = interiorRings as IEnumGeometry;
                    interiorRingsEnum.Reset();
                    IRing currentInteriorRing = interiorRingsEnum.Next() as IRing;
                    while (currentInteriorRing != null)
                    {
                        Polyline(currentInteriorRing, LayerName, Color);
                        currentInteriorRing = interiorRingsEnum.Next() as IRing;
                    }
                    currentExteriorRing = exteriorRingsEnum.Next() as IRing;
                }
            }
    
            void Entities()
            {
                BeginSection(TagDXF.ENTITIES);
    
                byte? Color = 7;
                string LayerName = m_FeatureLayer.Name;
    
                IFeatureClass featureClass = m_FeatureLayer.FeatureClass;
                IFeatureCursor featureCursor = featureClass.Search(null, true);
                IFeature feature = featureCursor.NextFeature();
                IGeometry shape = null;
    
                while (feature != null)
                {
                    shape = feature.ShapeCopy;
                    //int? OID = feature.HasOID ? feature.OID : new Nullable<int>();
    
                    switch (shape.GeometryType)
                    {
                        case esriGeometryType.esriGeometryPoint:
                            Point(shape as IPoint, LayerName, Color);
                            break;
                        case esriGeometryType.esriGeometryMultipoint:
                            Point(shape as IPointCollection, LayerName, Color);
                            break;
                        case esriGeometryType.esriGeometryPolyline:
                            Polyline(shape, LayerName, Color);
                            break;
                        case esriGeometryType.esriGeometryPolygon:
                            Polygon(shape, LayerName, Color);
                            break;
                        case esriGeometryType.esriGeometryEnvelope:
                        case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPath:
                        case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny:
                        case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryRing:
                        case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryLine:
                            Polyline(shape, LayerName, Color);
                            break;
                        case esriGeometryType.esriGeometryCircularArc:
                        case esriGeometryType.esriGeometryBezier3Curve:
                        case esriGeometryType.esriGeometryEllipticArc:
                            break;
                    }
                    feature = featureCursor.NextFeature();
                }
                EndSection();
                EOF();
            }
        }
    }
    转为DXF文件

      至此,WEB直接导入导出SHP/CAD如愿实现,共勉。

      【转载请说明出处,尊重知识传播】2017-06-30

  • 相关阅读:
    NVelocity的基本用法
    awk字符串处理
    R中去除为NA的行--转载
    从Github上轻松安装R包—githubinstall包--转载
    志诺维思(北京)基因科技有限公司
    密码子优化--转载
    reshape2 数据操作 数据融合( cast)
    rsync数据同步工具
    R语言中的字符串处理函数
    R中的sub替换函数【转】
  • 原文地址:https://www.cnblogs.com/ajg016/p/WebLoadSHP-CAD.html
Copyright © 2020-2023  润新知