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工具类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using CheckServices.Class;
using CheckServices.Ext;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using System.Xml; namespace CheckServices.query
{
public class DwgTool
{ public DwgTool()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
ESRI.ArcGIS.esriSystem.IAoInitialize aoInitialize = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
aoInitialize.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcInfo);
} public string _guid;
public string _userID;
public string _withCoordFlag = "false";
public IEnvelope env = new EnvelopeClass();
public string coord = string.Empty;
public string polygonNum = "";
public string CadLoad(string dwgfile,string guid,string userID,string withCoordFlag)
{
_guid = guid;
_userID = userID;
_withCoordFlag = withCoordFlag;
string dwgloadStr = string.Empty;
IFeatureClassContainer pFeaClsContainer = OpenCadFile(dwgfile);
IFeatureClass cadFeatClass = null;
string SDE = "SPA";
for (int i = ; i < pFeaClsContainer.ClassCount; i++)
{
cadFeatClass = pFeaClsContainer.get_Class(i);
if (cadFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation || cadFeatClass.FeatureType == esriFeatureType.esriFTAnnotation)
{
CadAnnotationCopy("SDE.DWG_ANNOTAION", SDE, cadFeatClass); }
else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPoint)
{ CadLayerCopy("SDE.DWG_POINT", SDE, cadFeatClass);
}
else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{ CadLayerCopy("SDE.DWG_POLYLINE", SDE, cadFeatClass); }
else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{ CadLayerCopy("SDE.DWG_POLYGON", SDE, cadFeatClass); }
else if (cadFeatClass.ShapeType == esriGeometryType.esriGeometryMultiPatch)
{
//esriGeometryMultiPatch 不用新增到sde,和polygon是一样的
continue;
}
} return createResultXML();
} private string createResultXML()
{
XmlDocument doc = new XmlDocument();
XmlElement rootElement = doc.CreateElement("DWGRESULT"); XmlElement extentElement = doc.CreateElement("EXTENT");
if (env.IsEmpty == false)
{
extentElement.AppendChild(createXmlNode(doc, "XMin", env.XMin.ToString()));
extentElement.AppendChild(createXmlNode(doc, "YMin", env.YMin.ToString()));
extentElement.AppendChild(createXmlNode(doc, "XMax", env.XMax.ToString()));
extentElement.AppendChild(createXmlNode(doc, "YMax", env.YMax.ToString()));
}
rootElement.AppendChild(extentElement);
rootElement.AppendChild(createXmlNode(doc, "COORD", coord ));
rootElement.AppendChild(createXmlNode(doc, "POLYONNUM", polygonNum));
rootElement.AppendChild(createXmlNode(doc, "GUID", _guid));
rootElement.AppendChild(createXmlNode(doc, "USERID",_userID));
doc.AppendChild(rootElement);
return doc.InnerXml;
} private XmlElement createXmlNode(XmlDocument doc, String elementName, String innerText)
{
XmlElement element = doc.CreateElement(elementName);
element.InnerText = innerText;
return element;
} private void clearEnv()
{
env = new EnvelopeClass();
} //图层拷贝函数
private void CadLayerCopy(String layerName, String sde, IFeatureClass cadFeatClass)
{ IFeatureClass fc = openFeatureClass(layerName, sde); IFeatureCursor cadFeatureCursor = cadFeatClass.Search(null, false);
IFeature pFeature1 = cadFeatureCursor.NextFeature(); if (pFeature1 == null)
return; IDataset dSet = fc as IDataset;
IWorkspaceEdit pWE = dSet.Workspace as IWorkspaceEdit;
IMultiuserWorkspaceEdit pMWE = pWE as IMultiuserWorkspaceEdit;
if (pMWE.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
pMWE.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
else
pWE.StartEditing(false);
pWE.StartEditOperation(); IFeatureCursor pFtcursor = fc.Insert(true); Dictionary<string, bool> drawingDic = getCadDrawingLayer(cadFeatClass); if(pFeature1.Shape is IPolygon)
{
coord = ArcGISUtil.PolygonToString(pFeature1.Shape as IPolygon);
env = pFeature1.Extent;
}
while(pFeature1 != null)
{
//处理cad非显示图层
string cadLayerName = getCadLayerName(pFeature1);
if (drawingDic.ContainsKey(cadLayerName))
{
if (drawingDic[cadLayerName] == false)
{
pFeature1 = cadFeatureCursor.NextFeature();
continue;
} } //若是polygon 计算env
setEnv(env,pFeature1);
//设置polygon个数
setPolygonNum(pFeature1); //针对返回多坐标的操作必须在setPolygonNum之前
setCoord(pFeature1);
//因为是大批量增加数据 用createfeaturebuffer会好些
IFeatureBuffer pFeatureBuff = fc.CreateFeatureBuffer();
updateFeature(pFeatureBuff, pFeature1);
try
{
object pfid = pFtcursor.InsertFeature(pFeatureBuff);
}
catch (Exception e)
{ }
pFeature1 = cadFeatureCursor.NextFeature();
}
pFtcursor.Flush();
pWE.StopEditOperation();
pWE.StopEditing(true); ArcGISUtil.FinalReleaseComObject(pFeature1);
ArcGISUtil.FinalReleaseComObject(pWE);
ArcGISUtil.FinalReleaseComObject(fc); } private void setPolygonNum(IFeature pFeature1)
{
if (pFeature1.Shape is IPolygon && polygonNum == "")
{
polygonNum = "";
}
else if (pFeature1.Shape is IPolygon && polygonNum == "")
{
polygonNum = "N";
}
} private void setCoord(IFeature pFeature1)
{
if (pFeature1.Shape is IPolygon && _withCoordFlag == "true" && polygonNum == "N")
{
coord += "*" + ArcGISUtil.PolygonToString(pFeature1.Shape as IPolygon);
}
} private void setEnv(IEnvelope env, IFeature pFeature1)
{
if (pFeature1.Shape is IPolygon)
{
if (pFeature1.Extent.XMin < env.XMin)
env.XMin = pFeature1.Extent.XMin; if (pFeature1.Extent.XMax > env.XMax)
env.XMax = pFeature1.Extent.XMax; if (pFeature1.Extent.YMin < env.YMin)
env.YMin = pFeature1.Extent.YMin; if (pFeature1.Extent.YMax > env.YMax)
env.YMax = pFeature1.Extent.YMax;
}
} private string getCadLayerName(IFeature pFeature1)
{
string cadLayer = string.Empty;
int fieldIndex = pFeature1.Fields.FindField("LAYER"); if (fieldIndex != -)
{
cadLayer = pFeature1.get_Value(fieldIndex).ToString();
}
return cadLayer;
} //得到cad可见图层 private Dictionary<string, bool> getCadDrawingLayer(IFeatureClass cadFeatClass)
{
Dictionary<string, bool> drawingDic = new Dictionary<string, bool>();
IFeatureLayer pFeatLayer = new CadFeatureLayer() as IFeatureLayer; pFeatLayer.FeatureClass = cadFeatClass; ICadDrawingLayers pCadDwgLayers =(ICadDrawingLayers)pFeatLayer; for (int i = ; i <= pCadDwgLayers.DrawingLayerCount - ; i++)
{
drawingDic.Add(pCadDwgLayers.get_DrawingLayerName(i),pCadDwgLayers.get_DrawingLayerVisible(i));
}
return drawingDic;
} //标注图层不能和几何图层那样加 需用IAnnotationFeature 添加elements实现 ,另外一种ifdographiclayer 我不知道怎么添加自定义属性 作罢
private void CadAnnotationCopy(String layerName, String sde, IFeatureClass pCadFC)
{ IFeatureClass pSdeFC = openFeatureClass(layerName, sde); IFeatureCursor pCADFeatureCur = pCadFC.Search(null, false);
ITextSymbol pTextSymbol = getTextSymbol(pSdeFC); ITextElement pTextElement = null;
string pAnnoText;
double pAngle;
int pAnnoTextID;
int pAngleID;
pAnnoTextID = pCadFC.Fields.FindField("Text");
pAngleID = pCadFC.Fields.FindField("txtAngle"); //启动编辑
IDataset dSet = pSdeFC as IDataset;
IWorkspaceEdit pWE = dSet.Workspace as IWorkspaceEdit;
IMultiuserWorkspaceEdit pMWE = pWE as IMultiuserWorkspaceEdit;
if (pMWE.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
pMWE.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
else
pWE.StartEditing(false);
pWE.StartEditOperation(); IFeature pCADFeature = pCADFeatureCur.NextFeature();
IFeatureCursor pFtcursor = pSdeFC.Insert(true); Dictionary<string, bool> drawingDic = getCadDrawingLayer(pCadFC);
while (null != pCADFeature)
{
string cadLayerName = getCadLayerName(pCADFeature);
if (drawingDic.ContainsKey(cadLayerName))
{
if (drawingDic[cadLayerName] == false)
{
pCADFeature = pCADFeatureCur.NextFeature();
continue;
}
} pAnnoText = pCADFeature.get_Value(pAnnoTextID).ToString();
pAngle = Convert.ToDouble(pCADFeature.get_Value(pAngleID));
pTextElement = MakeTextElement(pCADFeature, pAnnoText, pAngle, pTextSymbol); IAnnotationFeature a = pSdeFC.CreateFeatureBuffer() as IAnnotationFeature;
a.Annotation = pTextElement as IElement;
upadteFeatureGUIDandUserID(a as IFeature);
object pfid = pFtcursor.InsertFeature(a as IFeatureBuffer); pCADFeature = pCADFeatureCur.NextFeature();
}
pFtcursor.Flush();
pWE.StopEditOperation();
pWE.StopEditing(true);
ArcGISUtil.FinalReleaseComObject(pSdeFC);
ArcGISUtil.FinalReleaseComObject(pWE);
ArcGISUtil.FinalReleaseComObject(pCadFC); } //更新图层的GUID和USERID
private void upadteFeatureGUIDandUserID(IFeature pFeature)
{
int fieldIndex = pFeature.Fields.FindField("GUID");
if (fieldIndex != -)
{
pFeature.set_Value(fieldIndex, _guid);
}
int fieldIndex1 = pFeature.Fields.FindField("USERID");
if (fieldIndex1 != -)
{
pFeature.set_Value(fieldIndex1, _userID);
}
int fieldIndex2 = pFeature.Fields.FindField("DWGDATE");
if (fieldIndex2 != -)
{
DateTime dt = DateTime.Now;
string date = dt.ToShortDateString().ToString();
pFeature.set_Value(fieldIndex2, date);
}
} //创建textelecmnt函数
private ITextElement MakeTextElement(IFeature pFeature, string pAnnoText, double pAngle, ITextSymbol pTextSymbol)
{
IPoint pPoint = new PointClass();
pPoint = pFeature.ShapeCopy as IPoint;
ITextElement pTextElement = new TextElementClass();
pTextElement.Symbol = pTextSymbol;
pTextElement.ScaleText = true;
pTextElement.Text = pAnnoText;
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
if (pAngle != )
{
ITransform2D pTransform2D = pTextElement as ITransform2D;
pTransform2D.Rotate(pPoint, pAngle * (System.Math.PI / ));
}
return pTextElement;
} private ITextSymbol getTextSymbol(IFeatureClass pFeatureClass)
{
ITextSymbol pTextSymbol = null;
IAnnoClass pAnnoClass = pFeatureClass.Extension as IAnnoClass;
if (pAnnoClass.SymbolCollection != null)
{
ISymbolCollection pSColl = pAnnoClass.SymbolCollection;
pSColl.Reset(); ISymbolIdentifier pSymID = pSColl.Next();
while (pSymID != null)
{
if (pSymID.Symbol is ITextSymbol)
{
pTextSymbol = pSymID.Symbol as ITextSymbol; break;
}
pSymID = pSColl.Next();
}
}
else
{
IFeatureCursor pFCur = pFeatureClass.Search(null, false);
IAnnotationFeature pAnnofeat = pFCur.NextFeature() as IAnnotationFeature;
while (pAnnofeat != null)
{
if (pAnnofeat.Annotation is ITextElement)
{
pTextSymbol = pAnnofeat.Annotation as ITextSymbol;
}
pAnnofeat = pFCur.NextFeature() as IAnnotationFeature;
}
}
return pTextSymbol;
} //图形复制函数
private Boolean updateFeature(IFeatureBuffer pFeature, IFeature pFeature1)
{
try
{
for (int i = ; i < pFeature1.Fields.FieldCount; i++)
{
string str = pFeature1.Fields.get_Field(i).Name;
int fieldIndex = -; if (str.ToUpper() == "SHAPE")
{
IGeometryDef pGeometryDef;
pGeometryDef = pFeature1.Fields.get_Field(i).GeometryDef as IGeometryDef; IGeometry geometry = pFeature1.ShapeCopy; if (pFeature1.ShapeCopy is IPolygon)
{
(geometry as ITopologicalOperator).Simplify();
} //dwg z轴有点的情况要解决
if (pGeometryDef.HasZ)
{
IZAware pzaware = (IZAware)geometry;
pzaware.ZAware = false; } pFeature.Shape = geometry;
}
else
{ fieldIndex = pFeature.Fields.FindField(str.ToUpper());
if (fieldIndex != -)
{
if (pFeature.Fields.get_Field(fieldIndex).Editable)
{
pFeature.set_Value(fieldIndex, pFeature1.get_Value(i));
} }
}
}
upadteFeatureGUIDandUserID(pFeature as IFeature); return true;
}
catch (Exception e)
{
return false;
}
} //加载cad文件
private IFeatureClassContainer OpenCadFile(string fileName)
{
IFeatureClassContainer pFeatClassContainer = null;
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureDataset pFeatureDataset; try
{ //string dicPath = filePath.Substring(0, filePath.LastIndexOf("\\"));
//string fileName = filePath.Split('\\')[filePath.Split('\\').Length - 1];//"08558.dwg"; string dicPath = HttpContext.Current.Server.MapPath(@"\Checkservices\updwg\");
pWorkspaceFactory = new CadWorkspaceFactoryClass();
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(dicPath, );
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
pFeatClassContainer = (IFeatureClassContainer)pFeatureDataset; }
catch (Exception e)
{ } return pFeatClassContainer;
} private IFeatureClass openFeatureClass(String layerName, String sde)
{
IFeatureClass fc = null;
try
{
fc = SdeConnectManager.getFeatureClass(layerName, sde);
}
catch
{
return null;
}
return fc;
} } }
gtool处理的工具类
Flex 加载dwg的更多相关文章
- Flex 加载 wmf,svg
最近做gis系统发现要在flex加载wmf图片.我记得flash的loader只能是png,gis,jpg.wmf格式居然是window出的,flash居然不支持我也是醉了,没办法,只能后台转格式,首 ...
- Flex 加载pdf
如果想要在flex加载pdf,虽然pdf格式是开源的,但是自己去解析太麻烦了,pdf还要分页之类的,现在网上各种文档上传可以在线看很多都是pdf,当然也有word,excel之类,其实很多都是转了sw ...
- Flex 加载dxf
因为已经写过加载dwg了,dxf应该不陌生,dxf是个开源格式,所以加载比较简单这里直接附上as的代码,但是真正使用场景还是比较少,dwg文件比较多 package widgetscadastre.S ...
- Flex加载google地图、百度地图以及天地图作底图
一 Flex加载Google地图作底图 (1)帮助类GoogleLayer.as /* * 根据输入的地图类型加载Google地图(by chenyuming) */ package Layers ...
- Silverlight客户端加载DWG图纸方案
前段时间一直再研究怎么才能在Silverlight客户端加载 DWG图纸,ArcGIS API For Silverlight中可以加载Shp文件,但是不能加载DWG,最后想出了一个方法步骤如下: 1 ...
- 如何实现通过Leaflet加载dwg格式的CAD图
前言 在前面介绍了通过openlayers加载dwg格式的CAD图并与互联网地图叠加,openlayers功能很全面,但同时也很庞大,入门比较难,适合于大中型项目中.而在中小型项目中,一般用开源的 ...
- Flex 加载tiff
gis系统常常要加载tiff,因为好多土地证书,各种文件都是扫描件,如果你是用as来写的前台,怎么加载呢,顺便说下用插件AlternaTIFF也是可以得不过浏览器加载这么多插件是不太好的. 首先TIF ...
- 通过openlayers加载dwg格式的CAD图并与互联网地图叠加
Openlayers介绍 Openlayers是一个基于Javacript开发,免费.开源的前端地图开发库,使用它,可以很容易的开发出WebGIS系统.目前Openlayers支持地图瓦片.矢量数 ...
- Flex 加载shp
至于gis格式比较常见的shp是开源的,网上开源的as代码也很多 这个支持的shp算比较好的 源码在这边http://files.cnblogs.com/files/haibalai/shp.rar, ...
随机推荐
- mariadb配置主从同步遇到的问题
一:ERROR: No query specified 解决方案: \G后面不能再加分号;,因为\G在功能上等同于;,如果加了分号,那么就是;;(2个分号),SQL语法错误 二:主从同步不成功 Sla ...
- python之守护进程
主进程创建子进程,然后将该进程设置成守护自己的进程,守护进程就好比崇祯皇帝身边的老太监,崇祯皇帝已死老太监就跟着殉葬了. 关于守护进程需要强调两点: 其一:守护进程会在主进程代码执行结束后就终止 其二 ...
- [BZOJ5248][2018九省联考]一双木棋
题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=5248 Solution 我们首先考虑放棋子的操作 发现它一定放棋子的部分是一个联通块 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- window下eclipse安装python插件
1.安装python环境 python安装包下载地址:https://www.python.org/downloads/windows/ 2.在eclipse中在线安装PyDev插件 启动Eclips ...
- C 标准库 - ctype.h之iscntrl 使用
iscntrl int iscntrl ( int c ); Check if character is a control character 检查给定字符是否为控制字符,即编码 0x00-0x1F ...
- mysql 索引使用策略及优化
索引使用策略及优化 MySQL的优化主要分为结构优化(Scheme optimization)和查询优化(Query optimization).本章讨论的高性能索引策略主要属于结构优化范畴.本章的内 ...
- SQL Serever学习17——数据库的分析和设计
数据库的分析和设计 设计数据库确定一个合适的数据模型,满足3个要求: 符合用户需求,包含用户所需的所有数据 能被数据库管理系统实现,如sqlserver,oracle,db2 具有比较高质量,容易理解 ...
- Layui 好用的弹出框
layui的下载地址: http://www.layui.com/ 需要引用layui里面的css跟js layui自带jquery var $ = layui.$ 一个直接弹出另一个窗体的弹出框 w ...
- centos 网络很慢且无法远程登陆的解决办法
安装了centOS,但是发现网速实在是卡得几乎不能上网,连百度都打不开 后来想到偶然记得有一次看过一段话,说到关闭ipv6,测试来一下,果然有效,关闭来ipv6打开网速飞快. 关闭方法,在/etc/m ...