ArcGIS Server 9.3 ADF开发 CAD文件本地动态加载到GraphicsLayer中总结

  最近一段时间在研究这个cad数据(dwg)的本地化加载,折腾了很久终于取得了点小成就,现总结下自己的经验算是抛砖引玉吧,如果哪位大神有更好的办法请联系我,我还是个很小很小的小菜,希望得到你们的帮助。

  其实网上有一些加载办法,但是完全是ao的方法,会直接加载到服务器端,这样一旦加载进去后除非重启服务器或者重启服务否则加进去的临时cad图层就不会消失,会影响到其他用户的用户体验,所以我用了这个笨办法,来添加临时cad图层不过效率是低了一点。

  以下是主要的思路:

  1.先要读取建立工作空间,读取cad文件,用的是ao方法: 

    //CAD数据的读取
            List<ESRI.ArcGIS.Carto.ILayer> layerList = new List<ESRI.ArcGIS.Carto.ILayer>();
            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactoryClass();
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(@"D:\data", 0) as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
            ESRI.ArcGIS.Geodatabase.IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("需测量合图40坐标.dwg");
            ESRI.ArcGIS.Geodatabase.IFeatureClassContainer pFeatClassContainer = pFeatureDataset as ESRI.ArcGIS.Geodatabase.IFeatureClassContainer;

            for (int i = 0; i < pFeatClassContainer.ClassCount; i++)
            {
                ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer;
                ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass = pFeatClassContainer.get_Class(i);
                if (pFeatClass.FeatureType == ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTCoverageAnnotation) pFeatureLayer = new ESRI.ArcGIS.Carto.CadAnnotationLayerClass();
                else pFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();

                pFeatureLayer.Name = pFeatClass.AliasName;
                pFeatureLayer.FeatureClass = pFeatClass;
                layerList.Add(pFeatureLayer as ESRI.ArcGIS.Carto.ILayer);
            }

   2.对应数据的转换,包括 点、线、面、注记(路径我没做):  

           #region point的转换
            List<ESRI.ArcGIS.ADF.Web.Geometry.Point> adfpoints = new List<ESRI.ArcGIS.ADF.Web.Geometry.Point>();
            ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayerPiont = (ESRI.ArcGIS.Carto.IFeatureLayer)layerList[0];
            ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatureCursorPoint = pFeatureLayerPiont.FeatureClass.Search(null, true);
            ESRI.ArcGIS.Geodatabase.IFeature pFeaturePiont = null;
            pFeaturePiont = pFeatureCursorPoint.NextFeature();
            while (pFeaturePiont != null)
            {
                ESRI.ArcGIS.Geometry.IGeometry pGeometry = pFeaturePiont.Shape as ESRI.ArcGIS.Geometry.IGeometry;
                ESRI.ArcGIS.Geometry.IPoint pPoint = (ESRI.ArcGIS.Geometry.IPoint)pGeometry;
                ESRI.ArcGIS.ADF.Web.Geometry.Point adf_point = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPoint(pPoint);
                adfpoints.Add(adf_point);
                pFeaturePiont = pFeatureCursorPoint.NextFeature();
            }
            #endregion

            #region polyline的转换
            List<ESRI.ArcGIS.ADF.Web.Geometry.Polyline> adfpolylines = new List<ESRI.ArcGIS.ADF.Web.Geometry.Polyline>();
            ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayerLine = (ESRI.ArcGIS.Carto.IFeatureLayer)layerList[1];
            ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatureCursorLine = pFeatureLayerLine.FeatureClass.Search(null, true);
            ESRI.ArcGIS.Geodatabase.IFeature pFeatureLine = null;
            pFeatureLine = pFeatureCursorLine.NextFeature();
            while (pFeatureLine != null)
            {
                ESRI.ArcGIS.Geometry.IGeometry pGeometry = pFeatureLine.Shape as ESRI.ArcGIS.Geometry.IGeometry;
                ESRI.ArcGIS.Geometry.IPolyline pPolyline = (ESRI.ArcGIS.Geometry.IPolyline)pGeometry;
                ESRI.ArcGIS.Geometry.IPointCollection com_pointcollection = (ESRI.ArcGIS.Geometry.IPointCollection)pPolyline;
                ESRI.ArcGIS.ADF.Web.Geometry.Point[] adf_points = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection(com_pointcollection);

                ESRI.ArcGIS.ADF.Web.Geometry.PointCollection adf_pointcollection = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();
                for (int i = 0; i < adf_points.Length - 1; i++)
                {
                    adf_pointcollection.Add(adf_points[i]);
                }
                ESRI.ArcGIS.ADF.Web.Geometry.Path adf_path = new ESRI.ArcGIS.ADF.Web.Geometry.Path();
                adf_path.Points = adf_pointcollection;
                ESRI.ArcGIS.ADF.Web.Geometry.PathCollection adf_pathcollection = new ESRI.ArcGIS.ADF.Web.Geometry.PathCollection();
                adf_pathcollection.Add(adf_path);
                ESRI.ArcGIS.ADF.Web.Geometry.Polyline valuePolyLine = new ESRI.ArcGIS.ADF.Web.Geometry.Polyline();
                valuePolyLine.Paths = adf_pathcollection;
                adfpolylines.Add(valuePolyLine);
                pFeatureLine = pFeatureCursorLine.NextFeature();
            }
            #endregion

            #region polygon的转换
            List<ESRI.ArcGIS.ADF.Web.Geometry.Polygon> adfpolys = new List<ESRI.ArcGIS.ADF.Web.Geometry.Polygon>();
            ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayerPoly = (ESRI.ArcGIS.Carto.IFeatureLayer)layerList[2];
            ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatureCursorPoly = pFeatureLayerPoly.FeatureClass.Search(null, true);
            ESRI.ArcGIS.Geodatabase.IFeature pFeaturePoly = null;

            pFeaturePoly = pFeatureCursorPoly.NextFeature();
            while (pFeaturePoly != null)
            {
                ESRI.ArcGIS.Geometry.IGeometry pGeometry = pFeaturePoly.Shape as ESRI.ArcGIS.Geometry.IGeometry;
                ESRI.ArcGIS.Geometry.IPolygon pPolygon = (ESRI.ArcGIS.Geometry.IPolygon)pGeometry;
                ESRI.ArcGIS.Geometry.IPointCollection com_pointcollection = (ESRI.ArcGIS.Geometry.IPointCollection)pPolygon;
                ESRI.ArcGIS.ADF.Web.Geometry.Point[] adf_points = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection(com_pointcollection);

                //将得到的点集再组成polygon存入polygon泛型中
                ESRI.ArcGIS.ADF.Web.Geometry.PointCollection adf_pointcollection = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();
                for (int i = 0; i < adf_points.Length - 1; i++)
                {
                    adf_pointcollection.Add(adf_points[i]);
                }
                ESRI.ArcGIS.ADF.Web.Geometry.Ring adf_ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
                adf_ring.Points = adf_pointcollection;
                ESRI.ArcGIS.ADF.Web.Geometry.RingCollection adf_ringcollection = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection();
                adf_ringcollection.Add(adf_ring);
                ESRI.ArcGIS.ADF.Web.Geometry.Polygon valuePoly = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
                valuePoly.Rings = adf_ringcollection;
                adfpolys.Add(valuePoly);
                pFeaturePoly = pFeatureCursorPoly.NextFeature();
            }
            #endregion

            #region 注记的转换
            List<ESRI.ArcGIS.ADF.Web.Geometry.Point> adfannopnts = new List<ESRI.ArcGIS.ADF.Web.Geometry.Point>();
            List<string> annos = new List<string>();
            ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayerAnno = (ESRI.ArcGIS.Carto.IFeatureLayer)layerList[4];
            //ESRI.ArcGIS.Carto.IAnnotateFeature2 pAnnotateFeature = (ESRI.ArcGIS.Carto.IAnnotateFeature2)layerList[4];
            //ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatureCursorAnno = pFeatureLayerAnno.FeatureClass.Search(null, true);
            ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatureCursorAnno = pFeatureLayerAnno.FeatureClass.Search(null, true);
            ESRI.ArcGIS.Geodatabase.IFeature pFeatureAnno = null;
            pFeatureAnno = pFeatureCursorAnno.NextFeature();
            while (pFeatureAnno != null)
            {
                string temp = pFeatureAnno.get_Value(39).ToString();
                annos.Add(temp);
                ESRI.ArcGIS.Geometry.IGeometry pGeometry = pFeatureAnno.get_Value(1) as ESRI.ArcGIS.Geometry.IGeometry;
                ESRI.ArcGIS.Geometry.IPoint pAnno = (ESRI.ArcGIS.Geometry.IPoint)pGeometry;
                ESRI.ArcGIS.ADF.Web.Geometry.Point adf_anno = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPoint(pAnno);
                adfannopnts.Add(adf_anno);
                pFeatureAnno = pFeatureCursorAnno.NextFeature();
            }
            #endregion

  3.添加到graphicslayer中:  

 System.Collections.IEnumerable gfc = Map1.GetFunctionalities();
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource resource = null;
            foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gfunc in gfc)
            {
                if (gfunc.Resource.Name == "ImportToLayer")
                {
                    resource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
                }
            }
            if (resource == null) return;
            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;
            foreach (System.Data.DataTable dt in resource.Graphics.Tables)
            {
                if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
                {
                    glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
                    break;
                }
            }
            if (glayer == null)
            {
                glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
                glayer.TableName = "导入的CAD数据";
                resource.Graphics.Tables.Add(glayer);
            }

            //polyline的显示元素添加
            for (int i = 0; i < adfpolylines.Count - 2; i++)  //最后两条线是cad的边框,故不纳入
            {
                ESRI.ArcGIS.ADF.Web.Geometry.Polyline line = adfpolylines[i];
                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(line, System.Drawing.Color.White);
                ge.Symbol.Transparency = 20;
                glayer.Add(ge);
            }
            //polygon的显示元素添加
            for (int i = 0; i < adfpolys.Count - 2; i++) //此处不适合用foreach了,因为最后两个图斑是cad的边界图框不是我们所要的图斑故用for循环避免之
            {
                ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = adfpolys[i];
                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Red);
                ge.Symbol.Transparency = 50;
                glayer.Add(ge);
            }
            //annotation的显示元素添加
            for (int i = 0; i < adfannopnts.Count; i++)
            {
                ESRI.ArcGIS.ADF.Web.Geometry.Point point = adfannopnts[i];    //此处泛型的点为需要标注的某一点要素的X,Y坐标值
                ESRI.ArcGIS.ADF.Web.Display.Symbol.TextMarkerSymbol pMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.TextMarkerSymbol();
                pMarkerSymbol.Text = annos[i];     //此处的string泛型元素为标注要素的动态信息字符串
                pMarkerSymbol.Font.Size = 20;
                pMarkerSymbol.Transparency = 0;
                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(point, pMarkerSymbol);
                glayer.Add(ge);
            }

显示效果:

posted @ 2013-06-28 10:54  thsgar  阅读(424)  评论(1编辑  收藏  举报