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); } } };