ClipbRectangle.bas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Attribute VB_Name = "ClipbRectangle"
  2. '// This is free and unencumbered software released into the public domain.
  3. '// For more information, please refer to https://github.com/hongwenjun
  4. '// Attribute VB_Name = "剪贴板尺寸建立矩形" Clipboard Size Build Rectangle 2023.6.11
  5. Type Coordinate
  6. X As Double
  7. Y As Double
  8. End Type
  9. Public O_O As Coordinate
  10. Sub Start()
  11. '// 坐标原点
  12. O_O.X = 0: O_O.Y = 0
  13. Dim ost As ShapeRange
  14. Set ost = ActiveSelectionRange
  15. O_O.X = ost.LeftX
  16. O_O.Y = ost.BottomY - 50 '选择物件 下移动 50mm
  17. '// 建立矩形 Width x Height 单位 mm
  18. Dim Str, arr, n
  19. Str = API.GetClipBoardString
  20. ' 替换 mm x * 换行 TAB 为空格
  21. Str = VBA.Replace(Str, "m", " ")
  22. Str = VBA.Replace(Str, "x", " ")
  23. Str = VBA.Replace(Str, "X", " ")
  24. Str = VBA.Replace(Str, "*", " ")
  25. Str = VBA.Replace(Str, vbNewLine, " ")
  26. Do While InStr(Str, " ") '多个空格换成一个空格
  27. Str = VBA.Replace(Str, " ", " ")
  28. Loop
  29. arr = Split(Str)
  30. ActiveDocument.BeginCommandGroup '一步撤消'
  31. Dim X As Double
  32. Dim Y As Double
  33. For n = LBound(arr) To UBound(arr) - 1 Step 2
  34. ' MsgBox arr(n)
  35. X = Val(arr(n))
  36. Y = Val(arr(n + 1))
  37. If X > 0 And Y > 0 Then
  38. Rectangle X, Y
  39. O_O.X = O_O.X + X + 30
  40. End If
  41. Next
  42. ActiveDocument.EndCommandGroup
  43. End Sub
  44. '// 建立矩形 Width x Height 单位 mm
  45. Private Function Rectangle(Width As Double, Height As Double)
  46. ActiveDocument.Unit = cdrMillimeter
  47. Dim size As Shape
  48. Dim d As Document
  49. Dim s1 As Shape
  50. '// 建立矩形 Width x Height 单位 mm
  51. Set s1 = ActiveLayer.CreateRectangle(O_O.X, O_O.Y, O_O.X + Width, O_O.Y - Height)
  52. '// 填充颜色无,轮廓颜色 K100,线条粗细0.3mm
  53. s1.Fill.ApplyNoFill
  54. s1.Outline.SetProperties 0.3, OutlineStyles(0), CreateCMYKColor(0, 100, 0, 0), ArrowHeads(0), ArrowHeads(0), cdrFalse, cdrFalse, cdrOutlineButtLineCaps, cdrOutlineMiterLineJoin, 0#, 100, MiterLimit:=5#
  55. sw = s1.SizeWidth
  56. sh = s1.SizeHeight
  57. text = Trim(Str(sw)) + "x" + Trim(Str(sh)) + "mm"
  58. Set d = ActiveDocument
  59. Set size = d.ActiveLayer.CreateArtisticText(O_O.X + sw / 2 - 25, O_O.Y + 10, text, Font:="Tahoma") '// O_O.y + 10 标注尺寸上移 10mm
  60. size.Fill.UniformColor.CMYKAssign 0, 100, 100, 0
  61. End Function
  62. ' 测试矩形变形
  63. Private Function setRectangle(Width As Double, Height As Double)
  64. Dim s1 As Shape
  65. Set s1 = ActiveSelection
  66. ActiveDocument.Unit = cdrMillimeter
  67. '// 物件中心基准, 先把宽度设定为
  68. ActiveDocument.ReferencePoint = cdrCenter
  69. s1.SetSize Height, Height
  70. '// 物件旋转 30度,轮廓线1mm ,轮廓颜色 M100Y100
  71. s1.Rotate 30#
  72. s1.Outline.SetProperties 1#
  73. s1.Outline.SetProperties Color:=CreateCMYKColor(0, 100, 100, 0)
  74. End Function
  75. '// 获得选择物件大小信息
  76. Sub get_all_size()
  77. ActiveDocument.Unit = cdrMillimeter
  78. Set fs = CreateObject("Scripting.FileSystemObject")
  79. Set f = fs.CreateTextFile("R:\size.txt", True)
  80. Dim sh As Shape, shs As Shapes
  81. Set shs = ActiveSelection.Shapes
  82. Dim s As String
  83. For Each sh In shs
  84. size = Trim(Str(Int(sh.SizeWidth + 0.5))) + "x" + Trim(Str(Int(sh.SizeHeight + 0.5))) + "mm"
  85. f.WriteLine (size)
  86. s = s + size + vbNewLine
  87. Next sh
  88. f.Close
  89. MsgBox "输出物件尺寸信息到文件" & "R:\size.txt" & vbNewLine & s
  90. API.WriteClipBoard s
  91. End Sub