ArcGIS API for JavaScript之text转换成CAD(.dwg)文件

最近根据用户的需求写了这个功能。

需求:先在web端编辑文本标注,然后转换成CAD文件。

 用户想将注记发布成要素服务,然后再转换成CAD(.dwg)文件使用。但是注记是不支持发布成要素服务的,下面是我想到的实现思路(如有其它方案,欢迎补充!):

  1. 定义一个TextSymbol,写入内容;
  2. 将TextSymbol存入到Graphic里面;
  3. 将Export to CAD工具发布成GP服务;
  4. 通过GP服务转换成CAD文件。

实现步骤:

一、定义一个TextSymbol,写入内容。(由于测试,我点击地图后将点击的X坐标写入到TextSymbol,后期根据需求可能也会修改。)

 var color = new Color("#0a162c");
 var textSymbol = new TextSymbol(evt.mapPoint.x).setColor(color);;
 textSymbol.font.setSize("14pt");
 textSymbol.font.setFamily("arial");

二、将TextSymbol存入到Graphic里面,并在地图显示。

var labelPoint = new Point(evt.mapPoint.x, evt.mapPoint.y, new SpatialReference({ wkid: map.spatialReference }));
labelGraphic = new Graphic(labelPoint, textSymbol);
map.graphics.add(labelGraphic);

三、将Export to CAD工具发布成GP服务。
 可以直接将该工具运行,然后在结果窗口中共享成GP服务,这样生成的结果会存放到arcgisjobs文件下对应ID文件夹内。如果想自定义数据存放位置操作如下:

 将Export to CAD工具放到模型(moudle)中,然后新建一个为currentTime的变量,类型为任意值。参数填写如下图:

这里写图片描述
这里写图片描述
这里写图片描述

四、通过GP服务转换成CAD文件。

 var gp = new Geoprocessor("https://sms.esrichina.com/serv ... 6quot;);
          var in_features = new FeatureSet();
          in_features.fields = ;
          in_features.features.push(labelGraphic);

          var Output_Type = "DWG_R2013";
          //var Output_File = "E:/demo/ExportCAD";  //如果自定义路径,需要该参数
          var param = {
            //currentTime: Output_File,
            Output_Type: Output_Type,
            Input_Features: [in_features]

          };
          gp.submitJob(param, Result);

        });
          function Result(res){
            console.log(res);
            if(res.jobStatus = "esriJobSucceeded")
            {
              alert("已生成");
            }else{
              alert("执行工具失败");
            }
          }

全部代码:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>

    <link rel="stylesheet" href="https://sms.esrichina.com/arcg ... gt%3B
    <script type="text/javascript" src="https://sms.esrichina.com/arcg ... ot%3B ></script>
    <style>
      html, body, #map {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #button{
      position: fixed;
      top: 10px;
      right: 30px;
      border-radius: 4px;
      height: 30px;
      width: 80px;
      }
    </style>
     <script>
      var map;

      require([
        "esri/map", 
        "esri/Color",
        "esri/symbols/TextSymbol",
        "esri/layers/LabelClass",
        "esri/geometry/Point",
        "esri/SpatialReference",
        "esri/graphic",
        "esri/tasks/Geoprocessor",
        "esri/tasks/FeatureSet",

        "dojo/dom", "dojo/on",
        "dojo/domReady!"
        ], function(Map, Color,TextSymbol, LabelClass, Point, SpatialReference, Graphic, Geoprocessor, FeatureSet, dom, on) {
        map = new Map("map", {
          basemap: "osm", 
          center: [112.886936,34.807108], 
          zoom: 6
        });
        var labelGraphic;
        map.on("click", function(evt){
          var color = new Color("#0a162c");
          var textSymbol = new TextSymbol(evt.mapPoint.x).setColor(color);;
          textSymbol.font.setSize("14pt");
          textSymbol.font.setFamily("arial");

          var labelPoint = new Point(evt.mapPoint.x, evt.mapPoint.y, new SpatialReference({ wkid: map.spatialReference }));
          labelGraphic = new Graphic(labelPoint, textSymbol);
          map.graphics.add(labelGraphic);
        });


        var converButton = dom.byId("button")
          on(converButton, "click", function(evt){
          var gp = new Geoprocessor("https://sms.esrichina.com/serv ... 6quot;);
          var in_features = new FeatureSet();
          in_features.fields = ;
          in_features.features.push(labelGraphic);

          var Output_Type = "DWG_R2013";
          //var Output_File = "E:/demo/ExportCAD"; //如果自定义路径,需要该参数
          var param = {
           // currentTime: Output_File,
            Output_Type: Output_Type,
            Input_Features: [in_features]

          };
          gp.submitJob(param, Result);

        });
          function Result(res){
            console.log(res);
            if(res.jobStatus = "esriJobSucceeded")
            {
              alert("已生成");
            }else{
              alert("执行工具失败");
            }
          }

      });
    </script>
  </head>

  <body>
    <div id="map"></div>
    <button id="button" >转换</button>
  </body>
</html>

这里写图片描述
P.S:虽然自定义了路径,但是还是生成到安装server的服务器上,所以,自己写个代码将服务器上的文件保存到本地吧。

猜你喜欢

转载自blog.csdn.net/smss007/article/details/79742501