MirrorParalleHorizon.bas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Attribute VB_Name = "MirrorParalleHorizon"
  2. '// 两个端点的坐标,为(x1,y1)和(x2,y2) 那么其角度a的tan值: tana=(y2-y1)/(x2-x1)
  3. '// 所以计算arctan(y2-y1)/(x2-x1), 得到其角度值a
  4. '// VB中用atn(), 返回值是弧度,需要 乘以 PI /180
  5. Private Function lineangle(x1, y1, x2, y2) As Double
  6. pi = 4 * VBA.Atn(1) '// 计算圆周率
  7. If x2 = x1 Then
  8. lineangle = 90: Exit Function
  9. End If
  10. lineangle = VBA.Atn((y2 - y1) / (x2 - x1)) / pi * 180
  11. End Function
  12. '// 角度转平
  13. Public Function Angle_to_Horizon()
  14. On Error GoTo ErrorHandler
  15. API.BeginOpt
  16. Set sr = ActiveSelectionRange
  17. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  18. If nr.Count = 2 Then
  19. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  20. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  21. a = lineangle(x1, y1, x2, y2): sr.Rotate -a
  22. sr.LastShape.Delete '// 删除参考线
  23. End If
  24. ErrorHandler:
  25. API.EndOpt
  26. End Function
  27. '// 自动旋转角度
  28. Public Function Auto_Rotation_Angle()
  29. On Error GoTo ErrorHandler
  30. API.BeginOpt
  31. ' ActiveDocument.ReferencePoint = cdrCenter
  32. Set sr = ActiveSelectionRange
  33. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  34. If nr.Count = 2 Then
  35. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  36. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  37. a = lineangle(x1, y1, x2, y2): sr.Rotate 90 + a
  38. sr.LastShape.Delete '// 删除参考线
  39. End If
  40. ErrorHandler:
  41. API.EndOpt
  42. End Function
  43. '// 交换对象
  44. Public Function Exchange_Object()
  45. Set sr = ActiveSelectionRange
  46. If sr.Count = 2 Then
  47. x = sr.LastShape.CenterX: y = sr.LastShape.CenterY
  48. sr.LastShape.CenterX = sr.FirstShape.CenterX: sr.LastShape.CenterY = sr.FirstShape.CenterY
  49. sr.FirstShape.CenterX = x: sr.FirstShape.CenterY = y
  50. End If
  51. End Function
  52. '// 标记镜像参考线
  53. Public Function Set_Guides_Name()
  54. On Error GoTo ErrorHandler
  55. API.BeginOpt
  56. Dim sr As ShapeRange, s As Shape
  57. Set sr = ActiveSelectionRange
  58. For Each s In sr
  59. s.name = "MirrorGuides"
  60. Next s
  61. '// 感谢李总捐赠,定置透明度70%
  62. With ActiveSelection.Transparency
  63. .ApplyUniformTransparency 70
  64. ' .AppliedTo = cdrApplyToFillAndOutline
  65. ' .MergeMode = cdrMergeNormal
  66. End With
  67. ErrorHandler:
  68. API.EndOpt
  69. End Function
  70. '// 参考线镜像
  71. Public Function Mirror_ByGuide()
  72. On Error GoTo ErrorHandler
  73. API.BeginOpt
  74. Dim sr As ShapeRange, gds As ShapeRange
  75. Set sr = ActiveSelectionRange
  76. Set gds = sr.Shapes.FindShapes(Query:="@name ='MirrorGuides'")
  77. If gds.Count > 0 Then
  78. '// sr.RemoveRange gds
  79. Set nr = gds(1).DisplayCurve.Nodes.All
  80. Else
  81. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  82. '// sr.Remove sr.Count
  83. End If
  84. If nr.Count >= 2 Then
  85. byshape = False
  86. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  87. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  88. a = lineangle(x1, y1, x2, y2) '// 参考线和水平的夹角 a
  89. ang = 90 - a '// 镜像的旋转角度
  90. Set s = sr.Group
  91. With s
  92. Set s_copy = .Duplicate '// 复制物件保留,然后按 x1,y1 点 旋转
  93. .RotationCenterX = x1
  94. .RotationCenterY = y1
  95. .Rotate ang
  96. If Not byshape Then
  97. lx = .LeftX
  98. .Stretch -1#, 1# '// 通过拉伸完成镜像
  99. .LeftX = lx
  100. .Move (x1 - .LeftX) * 2 - .SizeWidth, 0
  101. .RotationCenterX = x1 '// 之前因为镜像,旋转中心点反了,重置回来
  102. .RotationCenterY = y1
  103. .Rotate -ang
  104. End If
  105. .RotationCenterX = .CenterX '// 重置回旋转中心点为物件中心
  106. .RotationCenterY = .CenterY
  107. .Ungroup
  108. s_copy.Ungroup
  109. End With
  110. End If
  111. ErrorHandler:
  112. API.EndOpt
  113. End Function
  114. '// 物件建立平行线
  115. Public Function Create_Parallel_Lines(space As Double)
  116. On Error GoTo ErrorHandler
  117. API.BeginOpt
  118. Dim sr As ShapeRange
  119. Set sr = ActiveSelectionRange
  120. sr.CreateParallelCurves 1, space
  121. ErrorHandler:
  122. API.EndOpt
  123. End Function