1
Surpriseplus
2022-10-14 6d536dea3cec157947947aa075bebde4305f29b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var DrawPolygon = {
    SG: null,
 
    polygon: null,
 
    callback: null,
 
    isDrawing: false,
 
    isEditing: false,
 
    groupName: "图形绘制",
 
    polygonName: "_polygon_",
 
    // 开始
    start: function (callback) {
        try {
            if (this.isDrawing) {
                return;
            }
 
            this.polygon = null;
            this.isDrawing = true;
            this.isEditing = false;
            this.callback = callback;
            this.SG.Window.SetInputMode(1);
            //this.SG.Application.CPUSaveMode = true;
 
            this.addEvent();
        } catch (e) {
            console.error(e);
        }
    },
 
    // 获取组ID
    getGid: function () {
        var gid = this.SG.ProjectTree.FindItem(this.groupName);
        if (gid) return gid;
 
        return this.SG.ProjectTree.CreateGroup(this.groupName);
    },
 
    // 删除组
    delGroup:function(){
        var gid = this.SG.ProjectTree.FindItem(this.groupName);
        if (gid) {
            this.SG.ProjectTree.DeleteItem(gid);
        }
    },
 
    // 添加事件
    addEvent: function () {
        this.SG.AttachEvent("OnLButtonDown", this.onLButtonDown);
        this.SG.AttachEvent("OnRButtonDown", this.onRButtonDown);
        this.SG.AttachEvent("OnFrame", this.onFrame);
    },
 
    // 移除事件
    rmEvent: function () {
        this.SG.DetachEvent("OnLButtonDown", this.onLButtonDown);
        this.SG.DetachEvent("OnRButtonDown", this.onRButtonDown);
        this.SG.DetachEvent("OnFrame", this.onFrame);
    },
 
    // 左键按下事件
    onLButtonDown: function (Flags, X, Y) {
        var _this = DrawPolygon;
        try {
            if (X < 0 || Y < 0 || _this.isEditing) {
                return;
            }
 
            var wpi = _this.SG.Window.PixelToWorld(X, Y, -1); // 0-Terrain
            wpi.Position.ChangeAltitudeType(3); // 0-RELATIVE,3-ABSOLUTE
            if (wpi.Position.X == 0 || wpi.Position.Y == 0) {
                return;
            }
 
            _this.isEditing = true;
            if (_this.polygon) {
                _this.polygon.Geometry.ExteriorRing.Points.AddPoint(wpi.Position.X, wpi.Position.Y, wpi.Position.Altitude);
            } else {
                _this.polygon = _this.createPolygon(wpi);
                _this.polygon.Geometry.StartEdit();
            }
            _this.isEditing = false;
        } catch (e) {
            _this.isEditing = false;
            console.error(e);
        }
 
        return true;
    },
 
    // 创建多边形
    createPolygon: function (wpi) {
        var arr = [
          wpi.Position.X,
          wpi.Position.Y,
          wpi.Position.Altitude,
          wpi.Position.X + 0.0000001,
          wpi.Position.Y + 0.0000001,
          wpi.Position.Altitude,
          wpi.Position.X + 0.0000002,
          wpi.Position.Y + 0.0000002,
          wpi.Position.Altitude
        ];
 
        var gid = this.SG.ProjectTree.FindItem(this.groupName);
        if (!gid) {
            gid = this.SG.ProjectTree.CreateLockedGroup(this.groupName, this.SG.ProjectTree.RootID);
        }
 
        var id = this.SG.ProjectTree.FindItem(this.groupName + "\\" + this.polygonName);
        if (id) {
            this.SG.ProjectTree.DeleteItem(id);
        }
 
        var fillColor = this.SG.Creator.CreateColor(255, 0, 0, 51);
        var ring = this.SG.Creator.GeometryCreator.CreateLinearRingGeometry(arr);
        var geometry = this.SG.Creator.GeometryCreator.CreatePolygonGeometry(ring, null);
        var polygon = this.SG.Creator.CreatePolygon(geometry, "#ff0000", fillColor, 3, gid, this.polygonName);//2-ON_TERRAIN, 3-ABSOLUTE, 1-PIVOT_RELATIVE, 0-TERRAIN_RELATIVE
 
        return polygon;
    },
 
    // 右键按下事件
    onRButtonDown: function (Flags, X, Y) {
        try {
            DrawPolygon.end();
        } catch (e) {
            console.error(e);
        }
 
        return true;
    },
 
    // 帧事件
    onFrame: function () {
        try {
            var _this = DrawPolygon;
            if (_this.polygon == null || _this.isEditing) {
                return;
            }
 
            var mi = _this.SG.Window.GetMouseInfo();
            if (mi.X < 0 || mi.Y < 0) {
                return;
            }
 
            var wpi = _this.SG.Window.PixelToWorld(mi.X, mi.Y, -1); // 0-Terrain
            wpi.Position.ChangeAltitudeType(3);
            if (wpi.Position.X == 0 || wpi.Position.Y == 0) {
                return;
            }
 
            var p = _this.polygon.Geometry.ExteriorRing.Points.Item(_this.polygon.Geometry.ExteriorRing.Points.Count - 1);
            p.X = wpi.Position.X;
            p.Y = wpi.Position.Y;
            p.Z = wpi.Position.Altitude;
        } catch (e) {
            console.error(e);
        }
    },
 
    // 结束
    end: function () {
        this.rmEvent();
        if (this.polygon) {
            if (this.polygon.Geometry.ExteriorRing.Points.Count < 5) {
                this.polygon.Geometry.EndEdit();
                this.SG.ProjectTree.DeleteItem(this.polygon.ID);
                this.polygon = null;
            } else {
                this.polygon.Geometry.ExteriorRing.Points.DeletePoint(1);
                this.polygon.Geometry.ExteriorRing.Points.DeletePoint(this.polygon.Geometry.ExteriorRing.Points.Count - 1);
                this.polygon.Geometry.EndEdit();
            }
        }
 
        //this.SG.ProjectTree.EndEdit();
        this.SG.Window.SetInputMode(0);
        this.isEditing = false;
        this.isDrawing = false;
 
        if (this.callback) {
            this.callback(this.polygon);
        }
    }
};