zoukankan      html  css  js  c++  java
  • Flex 加载dwg

      之前写的几种格式不是专门gis格式,这次来说说加载dwg。首先dwg格式不同于dxf格式,虽然autocad都能加载进去,真正用的比较多的是dwg格式,反正测绘,国土规划部门都是,吐槽下,然而autocad软件也是很贵的。首先为什么dwg格式加载为什么这么麻烦?

    dwg格式随着autocad版本升级,它的文件编码是不一样的,dxf是开源的,dwg是不开源的,只有跟autocad公司合作的企业才可以解析它的,现在我这里写的大部分都是对地图相关的,所以arcgis自然就可以加载。arcgis api就可以解析dwg。

       首先必须先上传dwg文件到服务器目录,服务器打开dwg文件, IFeatureClassContainer接口可以打开dwg,dwg文件有点,线,面,标注,dwg标注这个东西过于复杂,往往转arcgis格式时候会丢失太多,这个必须得用FME软件去搞,这里先处理点,线,面。我的思想是把这些dwg文件存到sde数据库里,当然事先也要建立点,线,面,标注图层,在flex显示的时候可以把这些图层发布,用arcgisdymaiclayer去显示,减少自己对坐标操作,还有地图元素很多小数点要精确到6位小数,后台大量传输这种高精度的坐标不好。

      下面是我写的dwgtool工具类。

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Web;
      5 using ESRI.ArcGIS.Geodatabase;
      6 using ESRI.ArcGIS.DataSourcesFile;
      7 using CheckServices.Class;
      8 using CheckServices.Ext;
      9 using ESRI.ArcGIS.Geometry;
     10 using ESRI.ArcGIS.Carto;
     11 using ESRI.ArcGIS.Display;
     12 using System.Xml;
     13 
     14 namespace CheckServices.query
     15 {
     16     public class DwgTool
     17     {
     18        
     19 
     20         public DwgTool()
     21         {
     22             ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
     23             ESRI.ArcGIS.esriSystem.IAoInitialize aoInitialize = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
     24             aoInitialize.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcInfo);
     25         }
     26         
     27         public  string _guid;
     28         public  string _userID;
     29         public string _withCoordFlag = "false";
     30         public  IEnvelope env = new EnvelopeClass();
     31         public string coord = string.Empty;
     32         public string polygonNum = "0";
     33         public  string CadLoad(string dwgfile,string guid,string userID,string withCoordFlag)
     34         {
     35             _guid = guid;
     36             _userID = userID;
     37             _withCoordFlag = withCoordFlag;
     38             string dwgloadStr = string.Empty;
     39             IFeatureClassContainer pFeaClsContainer = OpenCadFile(dwgfile);
     40             IFeatureClass cadFeatClass = null;
     41             string SDE = "SPA";
     42             for (int i = 0; i < pFeaClsContainer.ClassCount; i++)
     43             {
     44                 cadFeatClass = pFeaClsContainer.get_Class(i);
     45                 if (cadFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation || cadFeatClass.FeatureType == esriFeatureType.esriFTAnnotation)
     46                 {
     47                     CadAnnotationCopy("SDE.DWG_ANNOTAION", SDE, cadFeatClass);
     48                    
     49                 }
     50                 else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPoint)
     51                 {
     52 
     53                     CadLayerCopy("SDE.DWG_POINT", SDE, cadFeatClass);           
     54                 }
     55                 else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPolyline)
     56                 {
     57 
     58                     CadLayerCopy("SDE.DWG_POLYLINE", SDE, cadFeatClass);
     59                   
     60                 }
     61                 else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPolygon)
     62                 {
     63 
     64                     CadLayerCopy("SDE.DWG_POLYGON", SDE, cadFeatClass);
     65                  
     66                 }
     67                 else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryMultiPatch)
     68                 {
     69                     //esriGeometryMultiPatch 不用新增到sde,和polygon是一样的
     70                     continue;
     71                 }   
     72             }
     73 
     74             return createResultXML();
     75         }
     76 
     77         private string createResultXML()
     78         {
     79             XmlDocument doc = new XmlDocument();
     80             XmlElement rootElement = doc.CreateElement("DWGRESULT");
     81 
     82             XmlElement extentElement = doc.CreateElement("EXTENT");
     83             if (env.IsEmpty == false)
     84             {
     85                 extentElement.AppendChild(createXmlNode(doc, "XMin", env.XMin.ToString()));
     86                 extentElement.AppendChild(createXmlNode(doc, "YMin", env.YMin.ToString()));
     87                 extentElement.AppendChild(createXmlNode(doc, "XMax", env.XMax.ToString()));
     88                 extentElement.AppendChild(createXmlNode(doc, "YMax", env.YMax.ToString()));
     89             }
     90             rootElement.AppendChild(extentElement);
     91             rootElement.AppendChild(createXmlNode(doc, "COORD", coord ));
     92             rootElement.AppendChild(createXmlNode(doc, "POLYONNUM", polygonNum));
     93             rootElement.AppendChild(createXmlNode(doc, "GUID", _guid));
     94             rootElement.AppendChild(createXmlNode(doc, "USERID",_userID));
     95             doc.AppendChild(rootElement);
     96             return doc.InnerXml;
     97         }
     98 
     99         private  XmlElement createXmlNode(XmlDocument doc, String elementName, String innerText)
    100         {
    101             XmlElement element = doc.CreateElement(elementName);
    102             element.InnerText = innerText;
    103             return element;
    104         }
    105 
    106 
    107         private  void clearEnv()
    108         {
    109             env = new EnvelopeClass();
    110         }
    111 
    112 
    113         //图层拷贝函数
    114         private  void CadLayerCopy(String layerName, String sde, IFeatureClass cadFeatClass)
    115         {
    116           
    117        
    118             IFeatureClass fc = openFeatureClass(layerName, sde);
    119 
    120             IFeatureCursor cadFeatureCursor = cadFeatClass.Search(null, false);
    121             IFeature pFeature1 = cadFeatureCursor.NextFeature();
    122 
    123             if (pFeature1 == null)
    124                 return;
    125 
    126             IDataset dSet = fc as IDataset;
    127             IWorkspaceEdit pWE = dSet.Workspace as IWorkspaceEdit;
    128             IMultiuserWorkspaceEdit pMWE = pWE as IMultiuserWorkspaceEdit;
    129             if (pMWE.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
    130                 pMWE.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
    131             else
    132                 pWE.StartEditing(false);
    133             pWE.StartEditOperation();
    134 
    135             IFeatureCursor pFtcursor = fc.Insert(true);
    136 
    137             Dictionary<string, bool> drawingDic = getCadDrawingLayer(cadFeatClass);
    138 
    139            
    140           
    141             if(pFeature1.Shape is IPolygon)
    142             {
    143                 coord = ArcGISUtil.PolygonToString(pFeature1.Shape as IPolygon);
    144                 env = pFeature1.Extent;
    145             }
    146             while(pFeature1 != null)
    147             {
    148                 //处理cad非显示图层
    149                 string cadLayerName = getCadLayerName(pFeature1);
    150                 if (drawingDic.ContainsKey(cadLayerName))
    151                 {
    152                     if (drawingDic[cadLayerName] == false)
    153                     {
    154                         pFeature1 = cadFeatureCursor.NextFeature();
    155                         continue;
    156                     }
    157                        
    158                 }
    159                
    160                 //若是polygon 计算env
    161                 setEnv(env,pFeature1);
    162                 //设置polygon个数
    163                 setPolygonNum(pFeature1);
    164 
    165                 //针对返回多坐标的操作必须在setPolygonNum之前
    166                 setCoord(pFeature1);
    167                 //因为是大批量增加数据 用createfeaturebuffer会好些
    168                 IFeatureBuffer pFeatureBuff = fc.CreateFeatureBuffer();
    169                 updateFeature(pFeatureBuff, pFeature1);
    170                 try
    171                 {
    172                     object pfid = pFtcursor.InsertFeature(pFeatureBuff);
    173                 }
    174                 catch (Exception e)
    175                 {
    176                    
    177                 }
    178                 pFeature1 = cadFeatureCursor.NextFeature();
    179             }
    180             pFtcursor.Flush();
    181             pWE.StopEditOperation();
    182             pWE.StopEditing(true);
    183 
    184             ArcGISUtil.FinalReleaseComObject(pFeature1);
    185             ArcGISUtil.FinalReleaseComObject(pWE);
    186             ArcGISUtil.FinalReleaseComObject(fc);
    187 
    188         }
    189 
    190         private void setPolygonNum(IFeature pFeature1)
    191         {
    192             if (pFeature1.Shape is IPolygon && polygonNum == "0")
    193             {
    194                 polygonNum = "1";
    195             }
    196             else if (pFeature1.Shape is IPolygon && polygonNum == "1")
    197             {
    198                 polygonNum = "N";
    199             }
    200         }
    201 
    202         private void setCoord(IFeature pFeature1)
    203         {
    204             if (pFeature1.Shape is IPolygon && _withCoordFlag == "true" && polygonNum == "N")
    205             {
    206                 coord += "*" + ArcGISUtil.PolygonToString(pFeature1.Shape as IPolygon);
    207             }
    208         }
    209 
    210         private  void setEnv(IEnvelope env, IFeature pFeature1)
    211         {
    212             if (pFeature1.Shape is IPolygon)
    213             {
    214                 if (pFeature1.Extent.XMin < env.XMin)
    215                     env.XMin = pFeature1.Extent.XMin;
    216 
    217                 if (pFeature1.Extent.XMax > env.XMax)
    218                     env.XMax = pFeature1.Extent.XMax;
    219 
    220                 if (pFeature1.Extent.YMin < env.YMin)
    221                     env.YMin = pFeature1.Extent.YMin;
    222 
    223                 if (pFeature1.Extent.YMax > env.YMax)
    224                     env.YMax = pFeature1.Extent.YMax;
    225             }
    226         }
    227 
    228         private  string getCadLayerName(IFeature pFeature1)
    229         {
    230             string cadLayer = string.Empty;
    231             int fieldIndex = pFeature1.Fields.FindField("LAYER");
    232             
    233             if (fieldIndex != -1)
    234             {
    235                 cadLayer = pFeature1.get_Value(fieldIndex).ToString();
    236             }
    237             return cadLayer;
    238         }
    239 
    240         //得到cad可见图层
    241 
    242         private  Dictionary<string, bool> getCadDrawingLayer(IFeatureClass cadFeatClass)
    243         {
    244             Dictionary<string, bool> drawingDic = new Dictionary<string, bool>();
    245             IFeatureLayer pFeatLayer = new CadFeatureLayer() as IFeatureLayer;
    246 
    247             pFeatLayer.FeatureClass = cadFeatClass;
    248           
    249             ICadDrawingLayers pCadDwgLayers =(ICadDrawingLayers)pFeatLayer;
    250 
    251             for (int i = 0; i <= pCadDwgLayers.DrawingLayerCount - 1; i++)
    252             {      
    253                drawingDic.Add(pCadDwgLayers.get_DrawingLayerName(i),pCadDwgLayers.get_DrawingLayerVisible(i));     
    254             }
    255             return drawingDic;
    256         }
    257 
    258 
    259         //标注图层不能和几何图层那样加 需用IAnnotationFeature 添加elements实现 ,另外一种ifdographiclayer 我不知道怎么添加自定义属性 作罢
    260         private  void CadAnnotationCopy(String layerName, String sde, IFeatureClass pCadFC)
    261         {
    262 
    263             IFeatureClass pSdeFC = openFeatureClass(layerName, sde);
    264 
    265 
    266             IFeatureCursor pCADFeatureCur = pCadFC.Search(null, false);     
    267             ITextSymbol pTextSymbol = getTextSymbol(pSdeFC);
    268           
    269             ITextElement pTextElement = null;
    270             string pAnnoText;
    271             double pAngle;
    272             int pAnnoTextID;
    273             int pAngleID;
    274             pAnnoTextID = pCadFC.Fields.FindField("Text");
    275             pAngleID = pCadFC.Fields.FindField("txtAngle");
    276 
    277                   
    278             //启动编辑
    279             IDataset dSet = pSdeFC as IDataset;
    280             IWorkspaceEdit pWE = dSet.Workspace as IWorkspaceEdit;
    281             IMultiuserWorkspaceEdit pMWE = pWE as IMultiuserWorkspaceEdit;
    282             if (pMWE.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
    283                 pMWE.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
    284             else
    285                 pWE.StartEditing(false);
    286             pWE.StartEditOperation();
    287 
    288 
    289             IFeature pCADFeature = pCADFeatureCur.NextFeature();
    290             IFeatureCursor pFtcursor = pSdeFC.Insert(true);
    291 
    292             Dictionary<string, bool> drawingDic = getCadDrawingLayer(pCadFC);
    293             while (null != pCADFeature)
    294             {
    295                 string cadLayerName = getCadLayerName(pCADFeature);
    296                 if (drawingDic.ContainsKey(cadLayerName))
    297                 {
    298                     if (drawingDic[cadLayerName] == false)
    299                     {
    300                         pCADFeature = pCADFeatureCur.NextFeature();
    301                         continue;
    302                     }
    303                 }
    304 
    305                 pAnnoText = pCADFeature.get_Value(pAnnoTextID).ToString();
    306                 pAngle = Convert.ToDouble(pCADFeature.get_Value(pAngleID));
    307                 pTextElement = MakeTextElement(pCADFeature, pAnnoText, pAngle, pTextSymbol);
    308             
    309                
    310                 IAnnotationFeature a = pSdeFC.CreateFeatureBuffer() as IAnnotationFeature;
    311                 a.Annotation = pTextElement as IElement;
    312                 upadteFeatureGUIDandUserID(a as IFeature);
    313                 object pfid = pFtcursor.InsertFeature(a as IFeatureBuffer);
    314              
    315                 pCADFeature = pCADFeatureCur.NextFeature();
    316             }
    317             pFtcursor.Flush();
    318             pWE.StopEditOperation();
    319             pWE.StopEditing(true);
    320             ArcGISUtil.FinalReleaseComObject(pSdeFC);
    321             ArcGISUtil.FinalReleaseComObject(pWE);
    322             ArcGISUtil.FinalReleaseComObject(pCadFC);
    323           
    324         }
    325 
    326         //更新图层的GUID和USERID
    327         private  void upadteFeatureGUIDandUserID(IFeature pFeature)
    328         {
    329             int fieldIndex = pFeature.Fields.FindField("GUID");
    330             if (fieldIndex != -1)
    331             {
    332                 pFeature.set_Value(fieldIndex, _guid);
    333             }
    334             int fieldIndex1 = pFeature.Fields.FindField("USERID");
    335             if (fieldIndex1 != -1)
    336             {
    337                 pFeature.set_Value(fieldIndex1, _userID);
    338             }
    339             int fieldIndex2 = pFeature.Fields.FindField("DWGDATE");
    340             if (fieldIndex2 != -1)
    341             {
    342                 DateTime dt = DateTime.Now;
    343                 string date = dt.ToShortDateString().ToString();
    344                 pFeature.set_Value(fieldIndex2, date);
    345             }  
    346         }
    347 
    348         //创建textelecmnt函数
    349         private  ITextElement MakeTextElement(IFeature pFeature, string pAnnoText, double pAngle, ITextSymbol pTextSymbol)
    350         {
    351             IPoint pPoint = new PointClass();
    352             pPoint = pFeature.ShapeCopy as IPoint;
    353             ITextElement pTextElement = new TextElementClass();
    354             pTextElement.Symbol = pTextSymbol;
    355             pTextElement.ScaleText = true;
    356             pTextElement.Text = pAnnoText;
    357             IElement pElement = pTextElement as IElement;
    358             pElement.Geometry = pPoint;
    359             if (pAngle != 0)
    360             {
    361                 ITransform2D pTransform2D = pTextElement as ITransform2D;
    362                 pTransform2D.Rotate(pPoint, pAngle * (System.Math.PI / 180));
    363             }
    364             return pTextElement;
    365         }
    366        
    367 
    368         private  ITextSymbol getTextSymbol(IFeatureClass pFeatureClass)
    369         {
    370             ITextSymbol pTextSymbol = null;
    371             IAnnoClass pAnnoClass = pFeatureClass.Extension as IAnnoClass;
    372             if (pAnnoClass.SymbolCollection != null)
    373             {
    374                 ISymbolCollection pSColl = pAnnoClass.SymbolCollection;
    375                 pSColl.Reset();
    376 
    377                 ISymbolIdentifier pSymID = pSColl.Next();
    378                 while (pSymID != null)
    379                 {
    380                     if (pSymID.Symbol is ITextSymbol)
    381                     {
    382                         pTextSymbol = pSymID.Symbol as ITextSymbol; break;
    383                     }
    384                     pSymID = pSColl.Next();
    385                 }
    386             }
    387             else
    388             {
    389                 IFeatureCursor pFCur = pFeatureClass.Search(null, false);
    390                 IAnnotationFeature pAnnofeat = pFCur.NextFeature() as IAnnotationFeature;
    391                 while (pAnnofeat != null)
    392                 {
    393                     if (pAnnofeat.Annotation is ITextElement)
    394                     {
    395                         pTextSymbol = pAnnofeat.Annotation as ITextSymbol;
    396                     }
    397                     pAnnofeat = pFCur.NextFeature() as IAnnotationFeature;
    398                 }
    399             }
    400             return pTextSymbol;
    401         }
    402 
    403         //图形复制函数
    404         private  Boolean updateFeature(IFeatureBuffer pFeature, IFeature pFeature1)
    405         {
    406             try
    407             {
    408                 for (int i = 0; i < pFeature1.Fields.FieldCount; i++)
    409                 {
    410                     string str = pFeature1.Fields.get_Field(i).Name;
    411                     int fieldIndex = -1;
    412 
    413                     if (str.ToUpper() == "SHAPE")
    414                     {
    415                         IGeometryDef pGeometryDef;
    416                         pGeometryDef = pFeature1.Fields.get_Field(i).GeometryDef as IGeometryDef;
    417 
    418                         IGeometry  geometry = pFeature1.ShapeCopy;
    419                         
    420                         if (pFeature1.ShapeCopy is IPolygon)
    421                         {
    422                             (geometry as ITopologicalOperator).Simplify();
    423                         }
    424                                   
    425                         //dwg z轴有点的情况要解决
    426                         if (pGeometryDef.HasZ)
    427                         {
    428                             IZAware pzaware = (IZAware)geometry;
    429                             pzaware.ZAware = false;
    430 
    431                         }
    432                      
    433                         pFeature.Shape = geometry;
    434                     }
    435                     else
    436                     {
    437 
    438                         fieldIndex = pFeature.Fields.FindField(str.ToUpper());
    439                         if (fieldIndex != -1)
    440                         {
    441                             if (pFeature.Fields.get_Field(fieldIndex).Editable)
    442                             {
    443                                 pFeature.set_Value(fieldIndex, pFeature1.get_Value(i));
    444                             }
    445 
    446                         }
    447                     }
    448                 }
    449                 upadteFeatureGUIDandUserID(pFeature as IFeature);
    450                
    451                 return true;
    452             }
    453             catch (Exception e)
    454             {
    455                 return false;
    456             }
    457         }
    458 
    459        
    460 
    461 
    462         //加载cad文件
    463         private  IFeatureClassContainer OpenCadFile(string fileName)
    464         {
    465             IFeatureClassContainer pFeatClassContainer = null;
    466             IWorkspaceFactory pWorkspaceFactory;
    467             IFeatureWorkspace pFeatureWorkspace;
    468             IFeatureDataset pFeatureDataset;
    469 
    470             try
    471             {
    472 
    473                 //string dicPath = filePath.Substring(0, filePath.LastIndexOf("\"));
    474                 //string fileName = filePath.Split('\')[filePath.Split('\').Length - 1];//"08558.dwg";
    475 
    476                 string dicPath = HttpContext.Current.Server.MapPath(@"Checkservicesupdwg");
    477                 pWorkspaceFactory = new CadWorkspaceFactoryClass();
    478                 pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(dicPath, 0);
    479                 pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
    480                 pFeatClassContainer = (IFeatureClassContainer)pFeatureDataset;
    481 
    482             }
    483             catch (Exception e)
    484             {
    485 
    486             }
    487 
    488             return pFeatClassContainer;
    489         }
    490 
    491         private  IFeatureClass openFeatureClass(String layerName, String sde)
    492         {
    493             IFeatureClass fc = null;
    494             try
    495             {
    496                 fc = SdeConnectManager.getFeatureClass(layerName, sde);
    497             }
    498             catch
    499             {
    500                 return null;
    501             }
    502             return fc;
    503         }
    504 
    505 
    506 
    507     }
    508 
    509     
    510 }

    gtool处理的工具类

  • 相关阅读:
    Go语言入门
    简述cookies 和 session
    Linux inode 理解
    BZOJ 1012 最大数maxnumber
    BZOJ 1087 互不侵犯king
    CSS从大图中抠取小图完整教程(background-position应用)
    javascript中i++与++i
    脱离文档流分析
    在Windows上以zip压缩包方式安装mysql
    centos7 python2.7下安装paramiko模块
  • 原文地址:https://www.cnblogs.com/haibalai/p/5029832.html
Copyright © 2011-2022 走看看