前段时间一直再研究怎么才能在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. Spring-打印机案例

    1.导包 <!--beans--><dependency> <groupId>org.springframework</groupId> <art ...

  2. MySQL57修改root密碼

    之前在電腦里安裝了MySQL57之后,一直沒用,卻忘記了root密碼, 在網上找了一些資料修改root密碼,卻一直出錯.直到試到這個: 用管理員權限打開CMD CD C:\Program Files\ ...

  3. gin实现spring boot url拦截器

    1.定义中间件 func middle(c *gin.Context) { fmt.Println("我是中间件") c.Next() } 2.对要拦截的路由进行分组并引入中间件 ...

  4. oracle 换行回车符

    工作中碰到这样一种情况,做一个data patch,将表中的某个字段的内容copy到另一个字段,添加时若目标字段有值,需要换行处理. 首先,oracle中的回车符是chr(13),换行符是chr(10 ...

  5. 洛谷 P1474 货币系统 Money Systems

    P1474 货币系统 Money Systems !! 不是noip2018的那道题. 简单的多重背包的变式. #include <iostream> #include <cstdi ...

  6. Android 面试总结~~~

    一.面试中的问题 通过这几天的面试,总结了自己在面试过程中问到的问题,部分问题已经给出了答案,还有部分问题,还未有时间整理出来. ListView出现闪图.图片错乱原因解决方案 函数式编程 (Lamb ...

  7. Azure 8 月新公布

    Azure 8 月新发布:Cosmos DB 每分钟请求单位,存储的托管磁盘及促销,高级和标准磁盘存储大尺寸磁盘,高级磁盘存储小尺寸磁盘. Azure Cosmos DB:每分钟请求单位为您降低成本, ...

  8. Grid Infrastructure 启动的五大问题 (文档 ID 1526147.1)

    适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 和更高版本本文档所含信息适用于所有平台 用途 本文档的目的是总结可能阻止 Grid In ...

  9. Hdoj—1789

    //大意理解 先排序 最早交的里面选最大值 扫描完了加没写的 排序后 应该是早交的和扣分多的在前 用结构体吧/*#include<stdio.h>#include<stdio.h&g ...

  10. java 正则表达式如何提取中文的问题

    String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...