在Skyline的TerraExplorer Pro软件中,用户可以轻松实现几何对象的手动绘制;但在实际的项目中,为了满足不同行业的需求,我们更多情况需要定制开发一些几何对象。这里,通过Skyline提供的开发接口实现圆形对象的绘制,希望能够扩展大家的一些实现思路。
<!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 StartCal() {
var SGWorld = new CreateSGObj();
SGWorld.AttachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.AttachEvent("OnFrame", DrawPolyOnFrame);
SGWorld.Window.SetInputMode(1);
}
function EndCal() {
var SGWorld = new CreateSGObj();
SGWorld.DetachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.DetachEvent("OnFrame", DrawPolyOnFrame);
SGWorld.Window.SetInputMode(0);
SGWorld = null;
gPolyObj = null;
}
function CreateTempGroup(groupname) {
var SGWorld = new CreateSGObj();
var gid = SGWorld.ProjectTree.FindItem(groupname);
if (gid > 0) {
}
else {
gid = SGWorld.ProjectTree.CreateLockedGroup(groupname, 0);
}
return gid;
}
function DelTemp(groupname) {
var SGWorld = new CreateSGObj();
var gid = SGWorld.ProjectTree.FindItem(groupname);
if (gid > 0) {
SGWorld.ProjectTree.DeleteItem(gid);
}
}
var gPolyText = "圆";
//********************************************绘制Circle
function DrawPolyLButtonDown(Flags, X, Y) {
try {
var SGWorld = CreateSGObj();
var CursorCoord = SGWorld.Window.pixelToWorld(X, Y);
if (CursorCoord == null)
return false;
if (gPolyObj == null) {
var gid = CreateTempGroup("标绘");
var pos = SGWorld.Creator.CreatePosition(CursorCoord.Position.X, CursorCoord.Position.Y, 0, 2, 0, 0, 0, 0);
gPolyObj = SGWorld.Creator.CreateCircle(pos, 1, SGWorld.Creator.CreateColor(255, 0, 0, 1), SGWorld.Creator.CreateColor(255, 0, 0, 1), gid, gPolyText);
gPolyObj.LineStyle.Width = 1;
}
else {
EndCal();
}
}
catch (e)
{ }
}
//-----------
// 跟随鼠标位置移动,改变圆半径大小
//-----------
function DrawPolyOnFrame() {
try {
var SGWorld = CreateSGObj();
if (gPolyObj != null) {
var mouseInfo = SGWorld.Window.GetMouseInfo();
var CursorCoord = SGWorld.Window.pixelToWorld(mouseInfo.X, mouseInfo.Y);
if (CursorCoord == null)
return false;
var dr = gPolyObj.Position.DistanceTo(CursorCoord.Position);
gPolyObj.Radius = dr;
}
}
catch (e) { }
}
/*
功能: 创建sgworld对象
备注: 赵贺 2011.04.01.
*/
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:3a4f91b1-65a8-11d5-85c1-0001023952c1";
}
return obj;
}
function $(id) {
return window.document.getElementById(id);
}
</script>
</head>
<body onunload="DelTemp('标绘')">
<table style="margin: 0px; border: 0px;">
<tr>
<td colspan="4">
<input id="Button6" type="button" value="绘制" onclick="StartCal()" />
<input id="Button1" type="button" value="清除" onclick="DelTemp('标绘')" />
</td>
</tr>
</table>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>手绘圆</title>
<script type = "text/javascript" language = "javascript">
var gPolyObj = null;
function StartCal() {
var SGWorld = new CreateSGObj();
SGWorld.AttachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.AttachEvent("OnFrame", DrawPolyOnFrame);
SGWorld.Window.SetInputMode(1);
}
function EndCal() {
var SGWorld = new CreateSGObj();
SGWorld.DetachEvent("OnLButtonDown", DrawPolyLButtonDown);
SGWorld.DetachEvent("OnFrame", DrawPolyOnFrame);
SGWorld.Window.SetInputMode(0);
SGWorld = null;
gPolyObj = null;
}
function CreateTempGroup(groupname) {
var SGWorld = new CreateSGObj();
var gid = SGWorld.ProjectTree.FindItem(groupname);
if (gid > 0) {
}
else {
gid = SGWorld.ProjectTree.CreateLockedGroup(groupname, 0);
}
return gid;
}
function DelTemp(groupname) {
var SGWorld = new CreateSGObj();
var gid = SGWorld.ProjectTree.FindItem(groupname);
if (gid > 0) {
SGWorld.ProjectTree.DeleteItem(gid);
}
}
var gPolyText = "圆";
//********************************************绘制Circle
function DrawPolyLButtonDown(Flags, X, Y) {
try {
var SGWorld = CreateSGObj();
var CursorCoord = SGWorld.Window.pixelToWorld(X, Y);
if (CursorCoord == null)
return false;
if (gPolyObj == null) {
var gid = CreateTempGroup("标绘");
var pos = SGWorld.Creator.CreatePosition(CursorCoord.Position.X, CursorCoord.Position.Y, 0, 2, 0, 0, 0, 0);
gPolyObj = SGWorld.Creator.CreateCircle(pos, 1, SGWorld.Creator.CreateColor(255, 0, 0, 1), SGWorld.Creator.CreateColor(255, 0, 0, 1), gid, gPolyText);
gPolyObj.LineStyle.Width = 1;
}
else {
EndCal();
}
}
catch (e)
{ }
}
//-----------
// 跟随鼠标位置移动,改变圆半径大小
//-----------
function DrawPolyOnFrame() {
try {
var SGWorld = CreateSGObj();
if (gPolyObj != null) {
var mouseInfo = SGWorld.Window.GetMouseInfo();
var CursorCoord = SGWorld.Window.pixelToWorld(mouseInfo.X, mouseInfo.Y);
if (CursorCoord == null)
return false;
var dr = gPolyObj.Position.DistanceTo(CursorCoord.Position);
gPolyObj.Radius = dr;
}
}
catch (e) { }
}
/*
功能: 创建sgworld对象
备注: 赵贺 2011.04.01.
*/
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:3a4f91b1-65a8-11d5-85c1-0001023952c1";
}
return obj;
}
function $(id) {
return window.document.getElementById(id);
}
</script>
</head>
<body onunload="DelTemp('标绘')">
<table style="margin: 0px; border: 0px;">
<tr>
<td colspan="4">
<input id="Button6" type="button" value="绘制" onclick="StartCal()" />
<input id="Button1" type="button" value="清除" onclick="DelTemp('标绘')" />
</td>
</tr>
</table>
</body>
</html>