这篇文章将为大家详细讲解有关在TE二次开发中如何使用API进行Polygon的动态标绘,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
在TE的二次开发中,使用API进行Polygon的动态标绘,在CS程序中,Polyon自相交情况下,程序会抛出异常,而在bs网页的开发中,则不会出现这个错误,不过,CS开发动态画Polygon也有一种解决方式,能保证画的过程中不出现异常,下边分享一下详细的实现代码。
定义TE二次开发中的鼠标左键、右键、实时事件自己进行添加,代码中主要是各个事件的详细实现,详细的CS代码如下。
//鼠标左键事件
protected bool sgworld_OnLButtonUp(int Flags, int X, int Y)
{
try
{
var ptw = sgworld.Window.PixelToWorld(X, Y, WorldPointType.WPT_TERRAIN);
//为达到实时动态效果,先画一条线,因为面至少要3个点
if (tPolyline == null)
{
double[] pointarray = { ptw.Position.X, ptw.Position.Y, ptw.Position.Altitude, ptw.Position.X, ptw.Position.Y, ptw.Position.Altitude + 0.1 };
ILineString cPolygonGeometry = sgworld.Creator.GeometryCreator.CreateLineStringGeometry(pointarray);
tPolyline = sgworld.Creator.CreatePolyline(cPolygonGeometry, sgworld.Creator.CreateColor(lineColor.R, lineColor.G, lineColor.B, lineColor.A), AltitudeTypeCode.ATC_TERRAIN_RELATIVE, string.Empty, "tempPolyline");
//开始编辑线
tPolyline.Geometry.StartEdit();
}
else
{
if (tPolygon == null)
{
//获取线的坐标,准备画polygon
var _temGeo = tPolyline.Geometry as ILineString;
dynamic _temPoint = _temGeo.Points[0];
double[] arrr = new double[] { _temPoint.X, _temPoint.Y, _temPoint.Z, ptw.Position.X, ptw.Position.Y, ptw.Position.Altitude, ptw.Position.X, ptw.Position.Y, ptw.Position.Altitude + 0.1 };
var myGeometry = sgworld.Creator.GeometryCreator.CreateLinearRingGeometry(arrr);
tPolyline.Geometry.EndEdit();
sgworld.Creator.DeleteObject(tPolyline.ID);
//画polygon
tPolygon = sgworld.Creator.CreatePolygon(myGeometry, sgworld.Creator.CreateColor(lineColor.R, lineColor.G, lineColor.B, lineColor.A), sgworld.Creator.CreateColor(fillColor.R, fillColor.G, fillColor.B, fillColor.A), AltitudeTypeCode.ATC_ON_TERRAIN, groupid, "Polygon");
//设置polygon属性,编辑polygon
tPolygon.FillStyle.Color.SetAlpha(fOpacity);
tPolygon.Geometry.StartEdit();
}
else
{
//编辑polyon的坐标
polygon = tPolygon.Geometry as IPolygon;
if (polygon != null)
{
IRings1 = polygon.ExteriorRing as ILinearRing;
IPoint Ipint = IRings1.Points[IRings1.Points.Count - 1] as IPoint;
Ipint.X = ptw.Position.X;
Ipint.Y = ptw.Position.Y;
Ipint.Z = ptw.Position.Altitude;
IRings1.Points.AddPoint(ptw.Position.X, ptw.Position.Y, ptw.Position.Altitude);
}
}
}
}
catch (Exception ex1)
{
throw new Exception(ex1.Message);
}
return false;
}
//实时渲染事件
protected void sgworld_OnFrame()
{
try
{
//获得鼠标的信息,转换空间坐标信息
var mouseinfo = sgworld.Window.GetMouseInfo();
var ptwtwo = sgworld.Window.PixelToWorld(mouseinfo.X, mouseinfo.Y);
if (tPolygon != null )
{
polygon = tPolygon.Geometry as IPolygon;//强制转换
if (polygon != null)
{
//编辑polygon中的坐标信息
IRings1 = polygon.ExteriorRing as ILinearRing;
var Ipint = IRings1.Points[IRings1.Points.Count - 1] as IPoint;
Ipint.X = ptwtwo.Position.X;
Ipint.Y = ptwtwo.Position.Y;
Ipint.Z = ptwtwo.Position.Altitude;
}
}
else if (tPolyline != null)
{
//编辑Polyline中的坐标信息
var tLineStr = tPolyline.Geometry as ILineString;
var Ipint = tLineStr.Points[tLineStr.Points.Count - 1] as IPoint as IPoint;
Ipint.X = ptwtwo.Position.X;
Ipint.Y = ptwtwo.Position.Y;
Ipint.Z = ptwtwo.Position.Altitude;
}
}
catch (Exception ex2)
{
throw new Exception(ex2.Message);
}
}
//右键结束事件
protected bool sgworld_OnRButtonUp(int Flags, int X, int Y)
{
if (tPolygon != null)
{
tPolyline = null;
polygon = tPolygon.Geometry as IPolygon;
if (polygon != null)
{
IRings1 = polygon.ExteriorRing as ILinearRing;
//结束编辑,坐标点不够3个的,删除对象
if (IRings1.Points.Count > 3)
{
IRings1.Points.DeletePoint(IRings1.Points.Count - 1);
tPolygon.Geometry.EndEdit();
polygon = null;
}
else
{
sgworld.Creator.DeleteObject(tPolygon.ID);
tPolygon = null;
}
}
}
return false;
}
关于“在TE二次开发中如何使用API进行Polygon的动态标绘”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。