tool.js 11.7 KB

export default {
  attributes: {
    drawPolt: class DrawPolt {
        constructor(arg) {
            this.viewer = arg.viewer;
            this.type = {};
            this._billboardArr = [];
            this._lineArr = [];
            this._gonArr = [];
            this.handler = null;
        }
        get billboardArr() {
            return this._billboardArr;
        }
        get polylineArr() {
            return this._lineArr;
        }
        get polygonArr() {
            return this._gonArr;
        }
        create(e) {
            this.type[e].startCreate();
        }
        clearAll() {
            for (var i = 0; i < this._lineArr.length; i++) {
                var line = this._lineArr[i];
                line.destroy();
            }
            this._lineArr = [];
            for (var j = 0; j < this._gonArr.length; j++) {
                var gon = this._gonArr[j];
                gon.destroy();
            }
            this._gonArr = [];
            for (var k = 0; k < this._billboardArr.length; k++) {
                var billboard = this._billboardArr[k];
                billboard.destroy();
            }
            this._billboardArr = [];
        }
        clearOne() {
            if (!this.handler) {
                this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
            }
            var that = this;
            this.handler.setInputAction(function (evt) {
                var pick = that.viewer.scene.pick(evt.position);
                if (Cesium.defined(pick) && pick.id) {
                    that.viewer.entities.remove(pick.id);
                    that.handler.destroy();
                    that.handler = null;
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        }
    }, 
    polygon: class DrawPolygon {
        constructor(arg) {
            this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
            this.viewer = arg.viewer;
            this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
            this._polygon = null;
            this._polyline = null;
            this._positions = [];
        }
        get polygon() {
            return this._polygon;
        }
        get positions() {
            return this._positions;
        }
        startCreate(func) {
            var $this = this;
            this.handler.setInputAction(function (evt) { //单机开始绘制
                var cartesian = $this.getCatesian3FromPX(evt.position);
                if ($this._positions.length == 0) {
                    $this._positions.push(cartesian.clone());
                }
                $this._positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            this.handler.setInputAction(function (evt) { //移动时绘制面
                if ($this._positions.length < 1) return;
                var cartesian = $this.getCatesian3FromPX(evt.endPosition);
                if ($this._positions.length == 2) {
                    if (!Cesium.defined($this._polyline)) {
                        $this._polyline = $this.createPolyline();
                    }
                }
                if ($this._positions.length == 3) {
                    if ($this._polyline){
                        $this.viewer.entities.remove($this._polyline);
                        $this._polyline = null;
                    }
                    if (!Cesium.defined($this._polygon)) {
                        $this._polygon = $this.createPolygon();
                        $this._polygon.objId = $this.objId;
                    }
                }
                $this._positions.pop();
                $this._positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            this.handler.setInputAction(function (evt) {
                if (!$this._polygon) return;
                var cartesian = $this.getCatesian3FromPX(evt.position);
                $this.handler.destroy();
                $this._positions.pop();
                $this._positions.push(cartesian);
                func && func($this._positions);
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        }
        createPolygon(obj) {
            var $this = this;
            return this.viewer.entities.add({
                polygon: {
                    hierarchy: new Cesium.CallbackProperty(function () {
                        return new Cesium.PolygonHierarchy($this._positions);
                    }, false),
                    // clampToGround: true,
                    // show: true,
                    // fill: false,
                    // material: Cesium.Color.RED,
                    // width: 3,
                    material: new Cesium.Color.fromCssColorString("#FFD700").withAlpha(.2),
                    perPositionHeight: true,
                    outlineColor: Cesium.Color.BLACK,
                    outlineWidth: 3,
                    outline: true
                }
            });
        }
        createPolyline() {
            var $this = this;
            return this.viewer.entities.add({
                polyline: {
                    positions: new Cesium.CallbackProperty(function () {
                        return $this._positions
                    }, false),
                    clampToGround: true,
                    material: Cesium.Color.YELLOW,
                    width: 5
                }
            });
        }
        getCatesian3FromPX(px) {
            var cartesian;
            var ray = this.viewer.camera.getPickRay(px);
            if (!ray) return null;
            cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
            return cartesian;
        }
        destroy() {
            if (this.handler) {
                this.handler.destroy();
                this.handler = null;
            }
            if (this._polygon) {
                this.viewer.entities.remove(this._polygon);
                this._polygon = null;
            }
            if (this._polyline) {
                this.viewer.entities.remove(this._polyline);
                this._polyline = null;
            }
            this._positions = [];
        }
    },
    point: class DrawBillboard {
        constructor(arg) {
            this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
            this.viewer = arg.viewer;
            this._position = null;
            this._billboard = null;
        }
        get billboard() {
            return this._billboard;
        }
        get position() {
            return this._position;
        }
        startCreate() {
            var $this = this;
            this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
            this.handler.setInputAction(evt => { //单机开始绘制
                var cartesian = $this.getCatesian3FromPX(evt.position);
                // if(!Cesium.defined($this._billboard)){
                //     $this._billboard = $this.createBillboard(cartesian);
                //     $this.handler.destroy();
                // }
                this.handler.destroy();
                this.handler = undefined;
                this.createBillboard(cartesian);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        }
        createBillboard(cartesian) {
            var billboard = this.viewer.entities.add({
                position: cartesian,
                point: {
                    color: Cesium.Color.SKYBLUE,
                    pixelSize: 10,
                    outlineColor: Cesium.Color.YELLOW,
                    outlineWidth: 3,
                    disableDepthTestDistance: Number.POSITIVE_INFINITY
                }
            });
            billboard.objId = this.objId;
            return billboard;
        }
        destroy() {
            if (this.handler) {
                this.handler.destroy();
                this.handler = null;
            }
            if (this._billboard) {
                this.viewer.entities.remove(this._billboard);
                this._billboard = null;
            }
            this._position = null;
        }
        getCatesian3FromPX(px) {
            var cartesian;
            var ray = this.viewer.camera.getPickRay(px);
            if (!ray) return null;
            cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
            return cartesian;
        }
    },
    polyline: class DrawPolyline {
        constructor(arg) {
            //设置唯一id 备用
            this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
            this.viewer = arg.viewer;
            //事件
            this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
            this._polyline = null;
            this._positions = [];
        }
        //获取线
        get line(){
            return this._polyline;
        }
        //获取线的坐标数组
        get positions(){
            return this._positions;
        }
        //开始创建
        startCreate() {
            var $this = this;
            this.handler.setInputAction(function (evt) { //单机开始绘制
                //屏幕坐标转地形上坐标
                var cartesian = $this.getCatesian3FromPX(evt.position);
                if ($this._positions.length == 0) {
                    $this._positions.push(cartesian.clone());
                }
                $this._positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            this.handler.setInputAction(function (evt) { //移动时绘制线
                if ($this._positions.length < 1) return;
                var cartesian = $this.getCatesian3FromPX(evt.endPosition);
                if ($this._positions.length == 2) {
                    if (!Cesium.defined($this._polyline)) {
                        $this._polyline = $this.createPolyline();
                    }
                }
                if ($this._polyline) {
                    $this._positions.pop();
                    $this._positions.push(cartesian);
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            this.handler.setInputAction(function (evt) { //单机开始绘制
                if (!$this._polyline) return;
                var cartesian = $this.getCatesian3FromPX(evt.position);
                $this.handler.destroy();
                $this._positions.pop();
                $this._positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        }
        createPolyline(obj) {
            if (!obj) obj = {};
            var $this = this;
            var polyline = this.viewer.entities.add({
                polyline: {
                    //使用cesium的peoperty
                    positions: new Cesium.CallbackProperty(function () {
                        return $this._positions
                    }, false),
                    show: true,
                    material: Cesium.Color.YELLOW,
                    width:5 ,
                    clampToGround:true
                }
            });
            polyline.objId = this.objId;
            return polyline;
        }
        destroy() {
            this.linePointArr = [];
            if (this.handler) {
                this.handler.destroy();
                this.handler = null;
            }
            if (this._polyline) {
                this.viewer.entities.remove(this._polyline);
                this._polyline = null;
            }
            this._positions = [];
        }
        getCatesian3FromPX(px) {
            var cartesian;
            var ray = this.viewer.camera.getPickRay(px);
            if (!ray) return null;
            cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
            return cartesian;
        }
    }
  }
}