您的位置:首页 > 其它

基于Skyline的TerraExplorer6.1.1如何通过二次开发实现折线和多边形对象的手动绘制

2013-01-25 17:41 615 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type = "text/javascript" language = "javascript">
var gPolyObj = null;
function CreateSGObj() {
var obj = $("sgworld");
if (obj == null) {
obj = document.createElement('object');
document.body.appendChild(obj);
obj.name = "sgworld";
obj.id = "sgworld";
obj.classid = "CLSID:3a4f91b0-65a8-11d5-85c1-0001023952c1";
}
return obj;
}
function $(id) {
return window.document.getElementById(id);
}
//********************************************绘制多边形
var gPolylineText = gPolygonText = "Test";
var gDrawPolyClick = null;

function DrawPolyLButtonDown(Flags, X, Y) {
var SGWorld = CreateSGObj();
var CursorCoord = SGWorld.Window.pixelToWorld(X, Y);
if (CursorCoord == null)
return false;

if (gPolyObj == null) {
// We always start with a polyline and change it to Polygon (for area) after the second click)
var myGeometry = SGWorld.Creator.GeometryCreator.CreateLineStringGeometry([CursorCoord.Position.x, CursorCoord.Position.y, 0, CursorCoord.Position.x, CursorCoord.Position.y, 0])
gPolyObj = SGWorld.Creator.createPolyline(myGeometry, SGWorld.Creator.CreateColor(0, 255, 0, 1), 2, 0, gPolylineText);
gPolyObj.LineStyle.Width = 1;
gPolyObj.Geometry.StartEdit();

}
else {
if (gPolyMethod == 2) // Polygon
{
if (gPolyObj.ObjectType == 1) {
// Deleting the temporary line
var x = gPolyObj.Geometry.Points.Item(0).X;
var y = gPolyObj.Geometry.Points.Item(0).Y;
SGWorld.Creator.DeleteObject(gPolyObj.ID);

// Creating the polygon
var myGeometry = SGWorld.Creator.GeometryCreator.CreateLinearRingGeometry([x, y, 0, CursorCoord.Position.x, CursorCoord.Position.y, 0, CursorCoord.Position.x, CursorCoord.Position.y, 0])
gPolyObj = SGWorld.Creator.createPolygon(myGeometry, SGWorld.Creator.CreateColor(0, 255, 0, 1), SGWorld.Creator.CreateColor(0, 255, 0, 0.5), 2, 0, gPolygonText);
gPolyObj.LineStyle.Width = 1;
gPolyObj.Terrain.GroundObject = true;
gPolyObj.Geometry.StartEdit();
}
else {
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).X = CursorCoord.Position.x;
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).Y = CursorCoord.Position.y;
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).Z = 0;
gPolyObj.Geometry.Rings(0).Points.AddPoint(CursorCoord.Position.x, CursorCoord.Position.y, 0);
}
}
else {
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).X = CursorCoord.Position.x;
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).Y = CursorCoord.Position.y;
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).Z = 0;
gPolyObj.Geometry.Points.AddPoint(CursorCoord.Position.x, CursorCoord.Position.y, 0);
}
}
//            if (gDrawPolyClick != null)
//                gDrawPolyClick(gPolyObj.Geometry, gPolyObj.ObjectType);
//            return true;
}
//-----------
// onFrame
//-----------
function DrawPolyOnFrame() {
var SGWorld = CreateSGObj();
if (gPolyObj != null) {
try {
var mouseInfo = SGWorld.Window.GetMouseInfo()
var CursorCoord = SGWorld.Window.pixelToWorld(mouseInfo.X, mouseInfo.Y);
if (CursorCoord == null)
return false;
if (gPolyObj.ObjectType == 2) {
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).X = CursorCoord.Position.x;
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).Y = CursorCoord.Position.y;
gPolyObj.Geometry.Rings(0).Points.Item(gPolyObj.Geometry.Rings(0).Points.count - 1).Z = 0;
}
else {
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).X = CursorCoord.Position.x;
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).Y = CursorCoord.Position.y;
gPolyObj.Geometry.Points.Item(gPolyObj.Geometry.Points.count - 1).Z = 0;
}
}
catch (e) { }
}
}

//-------------
//DrawPolyInputModeChanged
function DrawPolyInputModeChanged(NewMode) {

//            if (NewMode != 1)
//                if (gPolyObj != null)
//                    Reset(0, 1);
}
//-------------
// DrawPolyRButtonUp
function DrawPolyRButtonUp(Flags, X, Y) {
var SGWorld = CreateSGObj();
if (gPolyObj == null || ((gPolyObj.ObjectType == 1 && gPolyObj.Geometry.Points.count <= 2) || (gPolyObj.ObjectType == 2 && gPolyObj.Geometry.Rings(0).Points.count <= 3))) {
//                Reset(0, 0);
return false;
}
if (gPolyObj.ObjectType == 1)
gPolyObj.Geometry.Points.DeletePoint(gPolyObj.Geometry.Points.count - 1);
else
gPolyObj.Geometry.Rings(0).Points.DeletePoint(gPolyObj.Geometry.Rings(0).Points.count - 1);

gPolyObj.Geometry.EndEdit();

gPolyObj = null;
gPolyMethod = null;
return true;
}

function ExCreate(value) {
var SGWorld = CreateSGObj();
if (value == 0) {
gPolyMethod = 1;
SGWorld.AttachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.AttachEvent("OnRButtonUp", DrawPolyRButtonUp);
//SGWorld.AttachEvent("OnFrame", DrawPolyOnFrame);
}
else if (value == 1)
{
gPolyMethod = 2;
SGWorld.AttachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.AttachEvent("OnRButtonUp", DrawPolyRButtonUp);
//SGWorld.AttachEvent("OnFrame", DrawPolyOnFrame);
}
else
{

}
}
</script>
</head>
<body>
<table>
<tr>
<td>
<input id="Button1" type="button" value="线Polyline" onclick="ExCreate(0)" />
</td>
</tr>
<tr>
<td>
<input id="Button2" type="button" value="多边形Polygon" onclick="ExCreate(1)" />
</td>
</tr>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐