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, ...
随机推荐
- python全栈开发_day14_常见语法糖,递归和匿名函数
一:常见语法糖 1)三元函数(三目函数) a=1 if 3>2 else 2 print(a) #得到返回值:1 2)列表字典推导式 lis=[("a",1),(" ...
- 基于Allwinner的Audio子系统分析(Android-5.1)
前言 一直想总结下Audio子系统的博客,但是各种原因(主要还是自己懒>_<),一直拖到现在才开始重新整理,期间看过H8(Android-4.4),T3(Android-4.4),A64( ...
- remote link Centos6.6 Horrible Slow
客户端win7 , 本地直连,secureCRT连接Centos6.6 速度巨慢,FTP tool almost cannot link in. 即使用cmd ftp 也是反应30s以上.
- 3Q大战现高潮,360 推出Android "3Q" IM即时通讯,岁末年初3Q大战惊现高潮
岁末年初3Q大战惊现高潮,360震撼推出Android "3Q" IM即时通讯 看过了QQ和360斗争的开端高潮,当然现在还不能说这场斗争已经结束,在我看来这次的事件未 ...
- python并发学习总结
目录 一.理解操作系统 二.任务类型 三.Socket模块 四.一个简单的C/S程序 五.使用阻塞IO实现并发 方案一:阻塞IO+多进程 方案二:阻塞IO+多线程 阻塞IO模型的思考和总结 六.使用非 ...
- SPSS学习系列之SPSS Statistics的使用介绍
不多说,直接上干货! 首先,在自己电脑找到软件. 大家根据自己的需求,我这里是双击IBM SPSS Statistics 24 打开后,如下的界面 以上就是SPSS的初步一个界面. 欢迎大家,加入我的 ...
- Installing Vim 8.0 on Ubuntu 16.04 and Linux Mint 18
sudo add-apt-repository ppa:jonathonf/vim sudo apt update sudo apt install vim uninstall sudo apt re ...
- CentOS6.4安装OpenSSL
1.下载 wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz 2.解压 tar zxf openssl-1.0.2h.tar.gz cd ...
- WPF Binding(四种模式)
在使用Binding类的时候有4中绑定模式可以选择 BindingMode TwoWay 导致对源属性或目标属性的更改可自动更新对方.此绑定类型适用于可编辑窗体或其他完全交互式 UI 方案. OneW ...
- hibernate 中addScalar的用法与作用
作用: 1.提高性能 2.指定要返回哪几个字段,为指定的不返回(主要用于select *查询全部) 3.也可指定返回字段的具体类型 常用于自定义本地sql中 如: StringBuffer sql=n ...