Silverlight客户端加载DWG图纸方案
前段时间一直再研究怎么才能在Silverlight客户端加载 DWG图纸,ArcGIS API For Silverlight中可以加载Shp文件,但是不能加载DWG,最后想出了一个方法步骤如下:
1、上传DWG文件到服务器。
2、用WCF调用AO的东西读取DWG文件,然后将DWG文件的要素转化成JSON格式。
3、发布WCF服务。
4、在Silverlight中调用WCF服务,然后显示将Json数据转化成Geometry,显示在地图上。
以下是部分的核心代码:
DWG要素转换成JSON:
public string DWGFileToJSON(string pathStr, string fileName)
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
IAoInitialize aoInit = new AoInitializeClass();
esriLicenseStatus licenseStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
{
return "ERROR:AO初始化错误,请确保服务器上的ArcGIS服务已开启";
}
else
{
return DWGToJSONStr(pathStr, fileName);
}
}
/// <summary>
/// 提取feature的geometry,并将其转换为json对象
/// </summary>
/// <param name="pFeature">要素对象</param>
/// <returns></returns>
private string feature2JsonGeometry(ESRI.ArcGIS.Geodatabase.IFeature pFeature)
{
ESRI.ArcGIS.Geometry.IGeometry pGeo = pFeature.Shape;
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);
//sb.Append(@"""point"":" + 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();
}
/// <summary>
/// 将DWG文件转换成JSON
/// </summary>
/// <param name="path">DWG所在文件夹</param>
/// <param name="fileName">DWG文件名</param>
/// <returns></returns>
private string DWGToJSONStr(string path, string fileName)
{
StringBuilder sb = new StringBuilder();
IWorkspaceFactory Fact = new CadWorkspaceFactoryClass();
IFeatureWorkspace workSpace = Fact.OpenFromFile(path, 0) as IFeatureWorkspace;
//加载面图层
IFeatureClass polygonClass = workSpace.OpenFeatureClass(fileName + ":polygon");
//加载线图层
IFeatureClass polylineClass = workSpace.OpenFeatureClass(fileName + ":polyline");
//加载点图层
IFeatureClass pointClass = workSpace.OpenFeatureClass(fileName + ":point");
//加载注记图层
IFeatureClass annotationClass = workSpace.OpenFeatureClass(fileName + ":annotation");
//面
IFeatureCursor featureCursor = polygonClass.Search(null, false);
IFeature feature = featureCursor.NextFeature();
while (feature != null)
{
sb.Append(feature2JsonGeometry(feature) + "|");
feature = featureCursor.NextFeature();
}
//线
featureCursor = polylineClass.Search(null, false);
feature = featureCursor.NextFeature();
while (feature != null)
{
sb.Append(feature2JsonGeometry(feature) + "|");
feature = featureCursor.NextFeature();
}
//点
featureCursor = pointClass.Search(null, false);
feature = featureCursor.NextFeature();
while (feature != null)
{
sb.Append(feature2JsonGeometry(feature) + "|");
feature = featureCursor.NextFeature();
}
//注记
featureCursor = annotationClass.Search(null, false);
feature = featureCursor.NextFeature();
while (feature != null)
{
sb.Append(feature2JsonGeometry(feature) + "|");
feature = featureCursor.NextFeature();
}
return sb.ToString().TrimEnd('|');
}
Silverlight客户端读取JSON并渲染到地图上:
Geometry extentGeometry = null;//用来放大到DWG图纸区域的Geometry
GraphicsLayer graphicsLayer = null;//用来显示JSON要素的图层
public MainPage()
{
InitializeComponent();
//加载地图
ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer();
tiledLayer.Url = App.MapURL;//地图地址
MyMap.Layers.Add(tiledLayer);
//加载graphicsLayer
graphicsLayer = new GraphicsLayer();
graphicsLayer.Opacity = 0.5;
MyMap.Layers.Add(graphicsLayer);
}
private void btnAddJson_Click(object sender, RoutedEventArgs e)
{
DWGToJSONClient client = new DWGToJSONClient();
client.DWGFileToJSONCompleted+=new EventHandler<DWGFileToJSONCompletedEventArgs>(client_DWGFileToJSONCompleted);
client.DWGFileToJSONAsync(App.DwgFolderPath, App.DwgFileName);//传入DWG文件夹的路径 DWG文件名
}
void client_DWGFileToJSONCompleted(object sender, DWGFileToJSONCompletedEventArgs e)
{
string str = e.Result;
string[] jsonStr = str.Split('|');
Graphic graphic = null;
Geometry geometry = null;
foreach (string s in jsonStr)
{
geometry = Geometry.FromJson(s);
graphic = new Graphic();
if (geometry is MapPoint)
{
graphic.Symbol = LayoutRoot.Resources["RedMarkerSymbol"] as SimpleMarkerSymbol;//样式,定义在前台页面
}
else if (geometry is Polyline)
{
graphic.Symbol = LayoutRoot.Resources["RedLineSymbol"] as SimpleLineSymbol;
}
else if (geometry is Polygon)
{
extentGeometry = geometry;
graphic.Symbol = LayoutRoot.Resources["RedFillSymbol"] as SimpleFillSymbol;
}
else if (geometry is Envelope)
{
graphic.Symbol = LayoutRoot.Resources["RedFillSymbol"] as SimpleFillSymbol;
}
if (graphic.Symbol != null)
{
graphic.Geometry = geometry;
graphicsLayer.Graphics.Add(graphic);
}
}
if (extentGeometry != null)
{
ZommTo(extentGeometry);
}
}
/// <summary>
/// 放大到Geometry
/// </summary>
/// <param name="geometry"></param>
private void ZommTo(Geometry geometry)
{
ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = geometry.Extent;
double expandPercentage = 30;
double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);
ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
selectedFeatureExtent.XMin - (widthExpand / 2),
selectedFeatureExtent.YMin - (heightExpand / 2),
selectedFeatureExtent.XMax + (widthExpand / 2),
selectedFeatureExtent.YMax + (heightExpand / 2));
MyMap.ZoomTo(displayExtent);
}
这里包含了所有的核心代码,如果哪位童鞋有不懂的,可以来讨论。
不过这个有一个很严重的问题:如果你的DWG图纸数据量很大超过1M的dwg图纸基本上是会加载失败的,一般300K左右的文件是没问题的。
研究了好久的时间才找到这个不怎么完美的方法,希望有好方法的童鞋能够贡献共享,感激不尽
不喜勿喷
Silverlight客户端加载DWG图纸方案的更多相关文章
- C#开发BIMFACE系列53 WinForm程序中使用CefSharp加载模型图纸1 简单应用
BIMFACE二次开发系列目录 [已更新最新开发文章,点击查看详细] 在我的博客<C#开发BIMFACE系列52 CS客户端集成BIMFACE应用的技术方案>中介绍了多种集成BIM ...
- ArcGIS API for Silverlight中加载Google地形图(瓦片图)
原文:ArcGIS API for Silverlight中加载Google地形图(瓦片图) 在做水利.气象.土地等行业中,若能使用到Google的地形图那是再合适不过了,下面就介绍如何在ArcGIS ...
- 如何实现通过Leaflet加载dwg格式的CAD图
前言 在前面介绍了通过openlayers加载dwg格式的CAD图并与互联网地图叠加,openlayers功能很全面,但同时也很庞大,入门比较难,适合于大中型项目中.而在中小型项目中,一般用开源的 ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...
- Android 高清加载巨图方案, 拒绝压缩图片
源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...
- Flex 加载dwg
之前写的几种格式不是专门gis格式,这次来说说加载dwg.首先dwg格式不同于dxf格式,虽然autocad都能加载进去,真正用的比较多的是dwg格式,反正测绘,国土规划部门都是,吐槽下,然而auto ...
- Silverlight实用窍门系列:2.Silverlight动态加载外部XML指定地址的WebService---(动态加载外部XML文件中指定的WebService地址)【附带实例源码】
接上节所讲的,Silverlight可以加载外部的XML文件里面的内容,那么我们可不可以在外部XML里面配置一个WebService地址,并且以此加载这个地址来动态加载WebService呢?这样子就 ...
- 【Silverlight】Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps
[Silverlight]Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps 上个月微软必应地图(Bing Maps) ...
- Silverlight中以客户端加载另一项目客户端(先登录后加载的一般实现)
近几日,本人在对一个老的Silverlight的GIS项目进行维护,发现该项目的实现是先把所有的资源加载至客户端,其中包括登录部分和业务实现部分,我就在想可不可以把登录部分和业务实现部分开来.如果用户 ...
随机推荐
- Aop第一节
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- 关于Winform控件调用插入点(光标)的用法
我们自定义控件中可能会有一些光标的使用,比如插入文字和图片提示,下面是调用WIN32 API的光标用法 Winform控件调用插入点的用法 // 导入处理光标的 Windows 32 位 API // ...
- DVWA之文件包含(File inclusion)
daFile inclusion的意思就是文件包含,当服务器开启了allow_url_include选项时,就可以通过PHP的某些特征函数 include,require和include_once,r ...
- win7下如何解决协议适配器错误问题
数据库为oracle 11g,在cmd中使用sqlplus命令出现了“协议适配器错误”. 原因分析:oracle相关服务没有启动. 解决办法如下: step1:进入服务页面. 方法一:cmd → se ...
- Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- saltstack-day1
一.远程执行命令 1.指定一个ipv4地址或者一个子网 salt -S 172.16.7.19 test.ping salt -S test.ping 2. 正则表达式 salt -E "j ...
- idea报错:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configu
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more tha ...
- Python 字典dict 集合set
字典dict Python内置字典,通过key-value进行存储,字典是无序的,拓展hash names = ['Michael', 'Bob', 'Tracy'] scores = [95, 75 ...
- Python基础篇 -- 字典
字典 dict. 以 {} 表示, 每一项用逗号隔开, 内部元素用 key: value的形式来保存数据 例子: dict.{"JJ":"林俊杰"," ...
- Python基础篇 -- 运算符和编码
运算符 记熟 ! ! ! 2**1=2 2**2=4 2**3=8 2**4=16 2**5=32 2**6=64 2**7=128 2**8=256 2**9=512 2**10=1024 运算符 ...