使用字典和排序计算行列.bas 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. Private Type Coordinate
  2. x As Double
  3. y As Double
  4. End Type
  5. Sub 计算行列() ' 字典使用计算行列
  6. ActiveDocument.Unit = cdrMillimeter
  7. Set xdict = CreateObject("Scripting.dictionary")
  8. Set ydict = CreateObject("Scripting.dictionary")
  9. Dim dot As Coordinate, Offset As Coordinate
  10. Dim s As Shape, ssr As ShapeRange
  11. Set ssr = ActiveSelectionRange
  12. ' 当前选择物件的范围边界
  13. set_lx = ssr.LeftX: set_rx = ssr.RightX
  14. set_by = ssr.BottomY: set_ty = ssr.TopY
  15. ssr(1).GetSize Offset.x, Offset.y
  16. ' 当前选择物件 ShapeRange 初步排序
  17. ssr.Sort " @shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
  18. For Each s In ssr
  19. dot.x = s.CenterX: dot.y = s.CenterY
  20. If xdict.Exists(Int(dot.x)) = False Then xdict.Add Int(dot.x), dot.x
  21. If ydict.Exists(Int(dot.y)) = False Then ydict.Add Int(dot.y), dot.y
  22. Next s
  23. ' MsgBox "字典使用计算行列:" & xdict.Count & ydict.Count
  24. Dim cnt As Long: cnt = 1
  25. ' 遍历字典,输出
  26. Dim key As Variant
  27. For Each key In xdict.keys
  28. dot.x = xdict(key)
  29. puts dot.x, set_by - Offset.y / 2, cnt
  30. cnt = cnt + 1
  31. Next key
  32. cnt = 1
  33. For Each key In ydict.keys
  34. dot.y = ydict(key)
  35. puts set_lx - Offset.x / 2, dot.y, cnt
  36. cnt = cnt + 1
  37. Next key
  38. End Sub
  39. Private Sub puts(x, y, n)
  40. Dim st As String
  41. st = str(n)
  42. Set s = ActiveLayer.CreateArtisticText(0, 0, st)
  43. s.CenterX = x: s.CenterY = y
  44. End Sub