zhaoxing
2021-11-24 cad2eaeffbc51c9b8e8a67a56c59124270d656ee
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
(function (window, mars3d) {
  //mm转为px
  var ratio = 0;
  var div = document.createElement("div");
  div.style.width = "1cm";
  div.id = "puc";
  document.body.appendChild(div);
 
  var w = getComputedStyle(div, null).width;
  ratio = w.substr(0, w.length - 2) / 10;
  div.parentNode.removeChild(div);
 
  //创建widget类,需要继承BaseWidget
  class MyWidget extends mars3d.widget.BaseWidget {
    //外部资源配置
    get resources() {
      return ["view.css"];
    }
 
    //弹窗配置
    get view() {
      return { type: "append", url: "view.html" };
    }
 
    //初始化[仅执行1次]
    create() {
      this.$mapParentDiv = $("#centerDiv");
    }
    winCreateOK(opt, result) {
      var that = this;
      $("#btn_print_expimg").click(function () {
        that.expImg();
      });
      $("#btn_print_start").click(function () {
        that.printview();
      });
      $("#btn_print_type").change(function () {
        var val = $(this).val();
        that.changeSize(val);
      });
      $("#btn_print_close").click(function () {
        that.disableBase();
      });
    }
    //激活插件
    activate() {
      //隐藏div
      $(".no-print-view").hide();
      $(".mars3d-control").hide();
      $(".cesium-viewer-toolbar").hide();
      $(".cesium-viewer-fullscreenContainer").hide();
 
      $("body").css({
        "overflow-x": "auto",
        "overflow-y": "auto",
        background: "#cccccc",
      });
      this.$mapParentDiv.css({
        position: "absolute",
      });
      this.changeSize();
    }
    //释放插件
    disable() {
      //还原显示div
      $(".no-print-view").show();
      $(".mars3d-control").show();
      $(".cesium-viewer-toolbar").show();
      $(".cesium-viewer-fullscreenContainer").show();
 
      $("body").css({
        "overflow-x": "hidden",
        "overflow-y": "hidden",
        background: "#000",
      });
      this.$mapParentDiv.css({
        position: "",
        width: "100%",
        height: "100%",
        transform: "scale(1.0)",
      });
    }
    changeSize(type) {
      this._lasttype = type;
 
      var width;
      var height;
      var isZongxiang;
      switch (
        type //单位为:mm毫米
      ) {
        default:
        case "A4H":
          width = 297;
          height = 210;
          break;
        case "A4Z":
          width = 210;
          height = 297;
          isZongxiang = true;
          break;
        case "A3H":
          width = 420;
          height = 297;
          break;
        case "A3Z":
          width = 297;
          height = 420;
          isZongxiang = true;
          break;
      }
 
      //減去边框
      if (isZongxiang) {
        width -= 10;
        height -= 10;
      } else {
        width -= 10;
        height -= 15;
      }
 
      var clientWidth = document.body.clientWidth;
      var clientHeight = document.body.clientHeight;
      var divWidth = this.mm2px(width);
      var divHeight = this.mm2px(height);
 
      let scale = Math.min(clientWidth / divWidth, clientHeight / divHeight);
      let x = 0,
        y = 0;
      if (scale > 1) {
        scale = 1;
      } else {
        x = -(divWidth - clientWidth) / 2;
        y = -(divHeight - clientHeight) / 2;
      }
 
      this.$mapParentDiv.css({
        width: width + "mm",
        height: height + "mm",
        transform: "matrix(" + scale + ",0,0," + scale + "," + x + "," + y + ")",
      });
    }
 
    mm2px(mm) {
      return Math.round(mm * ratio);
    }
 
    expImg() {
      haoutil.loading.show();
      this.map.expImage();
      haoutil.loading.hide();
    }
    printview() {
      // window.print()
 
      this.map.expImage({
        download: false,
        callback: (base64, size) => {
          this.printImage(base64);
        },
      });
    }
    printImage(base64) {
      var iframe = document.createElement("IFRAME");
      var doc = null;
      iframe.setAttribute("style", "position:absolute;width:0px;height:0px;left:-500px;top:-500px;");
      document.body.appendChild(iframe);
      doc = iframe.contentWindow.document;
      doc.write(`<div><img src="${base64}" style="margin:0" /></div>`);
      doc.close();
 
      iframe.focus();
      iframe.contentWindow.focus();
      setTimeout(() => {
        iframe.contentWindow.print();
        document.body.removeChild(iframe);
      }, 500);
    }
  }
 
  //注册到widget管理器中。
  mars3d.widget.bindClass(MyWidget);
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d);