爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 8393|回复: 3

[vbscript] 用自动化脚本给张贴图加个色标

[复制链接]

新浪微博达人勋

发表于 2017-9-15 09:59:51 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
在 Surfer 中,分类张贴图有图例选项,但是普通的张贴图是没有图例选项的。
这很好理解,非分类的张贴图,都使用同一个符号,哪里有图例可言?

但是张贴图可以根据某列数据按比例显示符号大小,还可以用 clr 色谱文件按数据值进行着色,那加个色标也是可以的,效果如下:

普通张贴图加图例

普通张贴图加图例



可惜的是,目前只做到按 clr 节点提取色标,不能完美匹配张贴图。

代码如下:
  1. '=========================================================================
  2. '脚本名称: CustomPostLegend.bas
  3. '运行环境: Golden software Surfer 13.x + Scripter.exe
  4. '作者: holz [AT] live.com
  5. '日期: 2017.9.14
  6. '功能: 在张贴图的下方生成对应的图例。
  7. '用法: 用 Surfer 附带的 Scripter.exe 程序(一般在 Surfer 安装目录下的 Scripter
  8. '       文件夹中,或者“开始 -> 程序 -> Golden Software Surfer 13 -> Scripter”)
  9. '       打开本脚本。
  10. '       运行 Surfer,打开或创建一个张贴图,选中要生成自定义图例的图层,
  11. '       回到 Scripter 按 F5 运行脚本即可。
  12. '=========================================================================

  13. Option Explicit
  14. Sub Main
  15.     Dim gsApp As IApplication2
  16.     Dim plot As IPlotDocument

  17.     Debug.Clear
  18.     Debug.Print "======== " & Date & " " & Time & " ========"
  19.     On Error Resume Next
  20.     Set gsApp = GetObject(,"Surfer.Application")
  21.     If Err.Number <> 0 Then
  22.         MsgBox "未发现运行中的 Surfer," & vbCrLf & _
  23.             "请先运行 Surfer,打开或创建一个张贴图。", _
  24.             vbCritical + vbOkOnly, "程序终止"
  25.         End
  26.     End If
  27.     on error goto 0

  28.     If gsApp.Documents.Count < 1 Then
  29.         MsgBox "未发现图形文档。" & vbCrLf & _
  30.             "请先打开或创建一个张贴图。", _
  31.             vbCritical + vbOkOnly, "程序终止"
  32.         End
  33.     End If

  34.     If gsApp.ActiveDocument.Type <> srfDocPlot Then
  35.         MsgBox "活动文档不是图形文档。" & vbCrLf & _
  36.             "请先打开或创建一个张贴图,并设为活动文档。", _
  37.             vbCritical + vbOkOnly, "程序终止"
  38.         End
  39.     End If
  40.     Set plot = gsApp.ActiveDocument

  41.     If plot.Selection.Count <> 1 Then
  42.         MsgBox "未选中对象或选中了多个对象。" & vbCrLf & _
  43.             "每次只能选中一个张贴图。", _
  44.             vbCritical + vbOkOnly, "程序终止"
  45.         End
  46.     End If

  47.     Dim layerName As String
  48.     layerName = plot.Selection.Item(1).Name
  49.     layerName = Right(layerName, Len(layerName)-InStr(layerName," "))
  50.     Debug.Print "选中的图层名称:" & layerName
  51.     If plot.Selection.Item(1).Type <> srfShapeMapFrame Then
  52.         MsgBox "未选中张贴图对象。" & vbCrLf & _
  53.             "请选择一个张贴图对象。", vbCritical + vbOkOnly, "程序终止"
  54.         End
  55.     End If

  56.     Dim MapFrame As IMapFrame2
  57.     Dim postLayer As Object
  58.     Dim x0 As Double, y0 As Double, w0 As Double
  59.     Dim x As Double
  60.     Dim nNodes As Integer
  61.     Dim txt() As IText, sym() As IRectangle
  62.     Dim txtStr As String
  63.     Dim customLegend As IComposite2
  64.     Dim i As Integer

  65.     Set MapFrame = plot.Selection.Item(1)
  66.     With MapFrame
  67.         x0 = .Left + 1.0
  68.         y0 = .Top - .Height - 1.0
  69.     End With
  70.     x = x0  : w0 = 0
  71.     plot.Selection.DeselectAll

  72.     For Each postLayer In MapFrame.Overlays
  73.         If postLayer.Type = srfShapePostmap And postLayer.Name = layerName Then
  74.             If postLayer.SymbolColorCol > 0 Then
  75.               With postLayer.SymbolColorMap
  76.                 nNodes = .ColorNodes.Count
  77.                 w0 = (MapFrame.Width - 2.0) / nNodes
  78.                 ReDim txt(nNodes), sym(nNodes)
  79.                 For i = 1 To nNodes
  80.                     Set sym(i) = plot.Shapes.AddRectangle(x, y0, x+w0, y0+0.2)
  81.                     sym(i).LineColor = srfColorBlack
  82.                     sym(i).Fill.Pattern = "Solid"
  83.                     sym(i).Fill.ForeColorRGBA.Red = .ColorNodes.Item(i).ColorRGBA.Red
  84.                     sym(i).Fill.ForeColorRGBA.Green = .ColorNodes.Item(i).ColorRGBA.Green
  85.                     sym(i).Fill.ForeColorRGBA.Blue = .ColorNodes.Item(i).ColorRGBA.Blue
  86.                     sym(i).Name = "节点" & i & "图例"
  87.                     sym(i).Select

  88.                     txtStr = Str(Round(.PosToDat(.ColorNodes.Item(i).Position),1))
  89.                     Set txt(i) = plot.Shapes.AddText(x+0.5*w0, y0-0.5, txtStr)
  90.                     txt(i).Font.VAlign = srfTAVCenter
  91.                     txt(i).Font.HAlign = srfTACenter
  92.                     txt(i).Font.Size = 12
  93.                     txt(i).Name = "节点" & i & "说明"
  94.                     txt(i).Select

  95.                     x = x + w0
  96.                 Next i
  97.               End With
  98.               Set customLegend = plot.Selection.Combine
  99.               customLegend.Name = "自定义图例"
  100.               customLegend.Deselect
  101.             End If    'postLayer.SymbolColorCol > 0
  102.         End If    'srfShapePostmap And layerName
  103.     Next    'postLayer
  104. End Sub
复制代码


评分

参与人数 2金钱 +30 贡献 +7 收起 理由
大老鹰 + 10 + 2 谢谢H大
chengxf + 20 + 5 赞一个!

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-9-18 19:17:26 | 显示全部楼层
很是花哨的感觉哦
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2017-9-19 04:57:52 | 显示全部楼层
charleszhou 发表于 2017-9-18 19:17
很是花哨的感觉哦

是吧,我也觉得没有什么实际意义。

不过人生也不能要求事事都有意义。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2021-2-8 13:29:21 | 显示全部楼层
{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表