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

<!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>

 

posted @ 2013-10-09 20:19  宇你同行  阅读(142)  评论(0)    收藏  举报