注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

程序员驿站

淘宝店:http://shop106888457.taobao.com

 
 
 

日志

 
 

Silverlight客户端加载DWG图纸方案  

2013-09-23 08:40:30|  分类: sl&gis |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

前段时间一直再研究怎么才能在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左右的文件是没问题的。

  评论这张
 
阅读(795)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018