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左右的文件是没问题的。

研究了好久的时间才找到这个不怎么完美的方法,希望有好方法的童鞋能够贡献共享,感激不尽

不喜勿喷

上一篇:ArcGIS API For Silverlight使用在线地图的多种方法总结


下一篇:Action访问Servlet API