前段时间一直再研究怎么才能在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图纸方案的更多相关文章

  1. C#开发BIMFACE系列53 WinForm程序中使用CefSharp加载模型图纸1 简单应用

    BIMFACE二次开发系列目录     [已更新最新开发文章,点击查看详细] 在我的博客<C#开发BIMFACE系列52 CS客户端集成BIMFACE应用的技术方案>中介绍了多种集成BIM ...

  2. ArcGIS API for Silverlight中加载Google地形图(瓦片图)

    原文:ArcGIS API for Silverlight中加载Google地形图(瓦片图) 在做水利.气象.土地等行业中,若能使用到Google的地形图那是再合适不过了,下面就介绍如何在ArcGIS ...

  3. 如何实现通过Leaflet加载dwg格式的CAD图

    前言 ​ 在前面介绍了通过openlayers加载dwg格式的CAD图并与互联网地图叠加,openlayers功能很全面,但同时也很庞大,入门比较难,适合于大中型项目中.而在中小型项目中,一般用开源的 ...

  4. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  5. Android 高清加载巨图方案, 拒绝压缩图片

    源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...

  6. Flex 加载dwg

    之前写的几种格式不是专门gis格式,这次来说说加载dwg.首先dwg格式不同于dxf格式,虽然autocad都能加载进去,真正用的比较多的是dwg格式,反正测绘,国土规划部门都是,吐槽下,然而auto ...

  7. Silverlight实用窍门系列:2.Silverlight动态加载外部XML指定地址的WebService---(动态加载外部XML文件中指定的WebService地址)【附带实例源码】

    接上节所讲的,Silverlight可以加载外部的XML文件里面的内容,那么我们可不可以在外部XML里面配置一个WebService地址,并且以此加载这个地址来动态加载WebService呢?这样子就 ...

  8. 【Silverlight】Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps

    [Silverlight]Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps 上个月微软必应地图(Bing Maps) ...

  9. Silverlight中以客户端加载另一项目客户端(先登录后加载的一般实现)

    近几日,本人在对一个老的Silverlight的GIS项目进行维护,发现该项目的实现是先把所有的资源加载至客户端,其中包括登录部分和业务实现部分,我就在想可不可以把登录部分和业务实现部分开来.如果用户 ...

随机推荐

  1. Mysql之Union用法

    在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后 ...

  2. setTimeout的核心原理和巧用

    你所不了解的setTimeout 发表于 2015年11月23日 by 愚人码头 被浏览 14,756 次 分享到: 0 小编推荐:掘金是一个高质量的技术社区,从 ECMAScript 6 到 Vue ...

  3. deepin15.2无线网无法使用

    原文链接:https://bbs.deepin.org/forum.php?mod=viewthread&tid=40276&highlight=%E6%97%A0%E7%BA%BF% ...

  4. Ubuntu启动项

    原文地址:http://blog.163.com/yangshuai126%40126/blog/static/1734262652010928101641555/ Ubuntu开机之后会执行/etc ...

  5. div+css 布局经验 - 最简单的 = 最不变形的(原创技巧)

    站酷几年了 一直饱受其恩泽 尤为感激 一直想奉献些什么 但是苦于水平 苦于奔波 今天静下心来 为大家奉献下 自己的div+css 经验 ,以下观点只代表 深海个人立场 希望为初学者提供一条" ...

  6. Javafinal变量

    class Test02 {     public static void main(String args[]){         final int x;         x = 100; //  ...

  7. 微信程序开发系列教程(三)使用微信API给微信用户发文本消息

    这个系列的第二篇教程,介绍的实际是被动方式给微信用户发文本消息,即微信用户关注您的公众号时,微信平台将这个关注事件通过一个HTTP post发送到您的微信消息服务器上.您对这个post请求做了应答(格 ...

  8. mvc工作总结

    MVC的页面跳转方式(放在一般类): filterContext.Result = new RedirectResult("controller/action"); filterC ...

  9. 关于img

    为img添加属性max-width min-height之类的属性可以对图片溢出部分实行自动裁剪功能 非常方便!!!!!!!!!(仅适用于那些原始图片大于max-width,max-height的图片 ...

  10. kafka启动报错&问题解决

    kafka启动报错&问题解决 一早上班,就收到运维同事通知说有一台物理机宕机,导致虚拟机挂了.只得重启kafka服务器. 1.启动 启动zookeeper bin/zkServer.sh st ...