lines.bas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Attribute VB_Name = "lines"
  2. '// This is free and unencumbered software released into the public domain.
  3. '// For more information, please refer to https://github.com/hongwenjun
  4. Sub start()
  5. LinesForm.Show 0
  6. End Sub
  7. Public Function Nodes_DrawLines()
  8. Dim sr As ShapeRange, sr_tmp As New ShapeRange, sr_lines As New ShapeRange
  9. Dim s As Shape, sh As Shape
  10. Dim nr As NodeRange
  11. Set sr = ActiveSelectionRange
  12. If sr.Count = 0 Then Exit Function
  13. For Each sh In sr
  14. Set nr = sh.Curve.Selection
  15. If nr.Count > 0 Then
  16. For Each n In nr
  17. Set s = ActiveLayer.CreateEllipse2(n.PositionX, n.PositionY, 0.5, 0.5)
  18. sr_tmp.Add s
  19. Next n
  20. End If
  21. Next sh
  22. '// 没有选择节点的情况,使用物件中心划线
  23. If sr_tmp.Count < 2 And sr.Count > 1 Then
  24. Set Line = DrawLine(sr(1), sr(2))
  25. sr_lines.Add Line
  26. End If
  27. #If VBA7 Then
  28. sr_tmp.Sort "@shape1.left < @shape2.left"
  29. #Else
  30. Set sr_tmp = X4_Sort_ShapeRange(sr_tmp, stlx)
  31. #End If
  32. '// 使用 Count 遍历 shaperange 这种情况方便点
  33. For i = 1 To sr_tmp.Count - 1
  34. Set Line = DrawLine(sr_tmp(i), sr_tmp(i + 1))
  35. sr_lines.Add Line
  36. Next
  37. sr_tmp.Delete
  38. sr_lines.CreateSelection
  39. End Function
  40. Public Function Draw_Multiple_Lines(hv As cdrAlignType)
  41. Dim sr As ShapeRange, sr_lines As New ShapeRange
  42. Set sr = ActiveSelectionRange
  43. If sr.Count < 2 Then Exit Function
  44. #If VBA7 Then
  45. If hv = cdrAlignVCenter Then
  46. '// 从左到右排序
  47. sr.Sort "@shape1.left < @shape2.left"
  48. ElseIf hv = cdrAlignHCenter Then
  49. '// 从上到下排序
  50. sr.Sort "@shape1.top < @shape2.top"
  51. End If
  52. #Else
  53. '// X4_Sort_ShapeRange for CorelDRAW X4
  54. If hv = cdrAlignVCenter Then
  55. Set sr = X4_Sort_ShapeRange(sr, stlx)
  56. ElseIf hv = cdrAlignHCenter Then
  57. Set sr = X4_Sort_ShapeRange(sr, stty)
  58. End If
  59. #End If
  60. For i = 1 To sr.Count - 1 Step 2
  61. Set Line = DrawLine(sr(i), sr(i + 1))
  62. sr_lines.Add Line
  63. Next
  64. sr_lines.CreateSelection
  65. End Function
  66. Public Function FirtLineTool()
  67. Dim sr As ShapeRange
  68. Set sr = ActiveSelectionRange
  69. If sr.Count > 1 Then
  70. Set Line = DrawLine(sr(1), sr(2))
  71. End If
  72. End Function
  73. Private Function DrawLine(ByVal s1 As Shape, ByVal s2 As Shape) As Shape
  74. '// 创建线段方法在图层上的指定位置创建由单个线段组成的曲线。
  75. Set DrawLine = ActiveLayer.CreateLineSegment(s1.CenterX, s1.CenterY, s2.CenterX, s2.CenterY)
  76. End Function
  77. Private Sub Test()
  78. ActiveDocument.Unit = cdrMillimeter
  79. Set Rect = ActiveLayer.CreateRectangle(0, 0, 30, 30)
  80. Set ell = ActiveLayer.CreateEllipse2(50, 50, 10, 10)
  81. Set Line = DrawLine(Rect, ell)
  82. End Sub