Box.bas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. Attribute VB_Name = "box"
  2. Public Function Simple_box_five(Optional ByVal l As Double, Optional ByVal w As Double, Optional ByVal h As Double, Optional ByVal b As Double = 15)
  3. Dim sr As New ShapeRange, wing As New ShapeRange, BottomWing As ShapeRange
  4. Dim sh As Shape
  5. l1x = w: l2x = w + l: l3x = 2 * w + l: l4x = 2 * (w + l)
  6. '// 绘制主体上下盖矩形
  7. Set mainRect_aw = ActiveLayer.CreateRectangle(0, 0, w, h)
  8. Set mainRect_al = ActiveLayer.CreateRectangle(0, 0, l, h)
  9. mainRect_al.Move l1x, 0
  10. Set mainRect_bw = ActiveLayer.CreateRectangle(0, 0, w, h)
  11. mainRect_bw.Move l2x, 0
  12. Set mainRect_bl = ActiveLayer.CreateRectangle(0, 0, l, h)
  13. mainRect_bl.Move l3x, 0
  14. Set topRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  15. topRect.Move l1x, h
  16. '// 绘制Box 圆角矩形插口
  17. Set top_RoundRect = ActiveLayer.CreateRectangle(0, 0, l, b, 75, 75)
  18. top_RoundRect.Move l1x, h + w
  19. Set Bond = DrawBond(b, h, l4x, 0)
  20. '// 绘制box 2个翅膀
  21. Set sh = DrawWing(w, (w + b) / 2 - 2)
  22. wing.Add sh.Duplicate(0, h)
  23. wing.Add sh.Duplicate(l2x, h)
  24. wing(2).Flip cdrFlipHorizontal
  25. '// 绘制 Box 底下翅膀 BottomWing
  26. Set BottomWing = DrawBottomWing(l, w, b)
  27. '// 添加到物件组,设置轮廓色 C100
  28. sr.Add mainRect_aw: sr.Add mainRect_al: sr.Add mainRect_bw: sr.Add mainRect_bl
  29. sr.Add topRect: sr.Add Bond: sr.Add top_RoundRect
  30. sr.AddRange BottomWing
  31. sr.AddRange wing: sh.Delete
  32. sr.SetOutlineProperties Color:=CreateCMYKColor(100, 0, 0, 0)
  33. sr.CreateSelection: sr.Group
  34. End Function
  35. Private Function DrawBottomWing(ByVal l As Double, ByVal w As Double, ByVal b As Double) As ShapeRange
  36. Dim sr As New ShapeRange, s As Shape
  37. Dim sp As SubPath, crv(3) As Curve
  38. '// 绘制 Box 底下翅膀 BottomWing
  39. Set crv(1) = Application.CreateCurve(ActiveDocument)
  40. Set sp = crv(1).CreateSubPath(0, 0)
  41. sp.AppendLineSegment w / 2, w * 0.275
  42. sp.AppendLineSegment w / 2, w / 2 - 5
  43. sp.AppendCurveSegment2 w / 2 + 5, w / 2, w / 2, w / 2 - 2.5, w / 2 + 2.5, w / 2
  44. sp.AppendLineSegment w, w / 2
  45. sp.AppendLineSegment w, 0
  46. sp.Closed = True
  47. sr.Add ActiveLayer.CreateCurve(crv(1))
  48. Set crv(2) = Application.CreateCurve(ActiveDocument)
  49. Set sp = crv(2).CreateSubPath(0, 0)
  50. sp.AppendLineSegment w / 2, w * 0.275
  51. sp.AppendLineSegment w / 2 + b - 5, w * 0.275
  52. sp.AppendCurveSegment2 w / 2 + b, w * 0.275 + 5, w / 2 + b - 2.5, w * 0.275, w / 2 + b, w * 0.275 + 2.5
  53. sp.AppendLineSegment w / 2 + b, l - w * 0.275 - 5
  54. sp.AppendCurveSegment2 w / 2 + b - 5, l - w * 0.275, w / 2 + b, l - w * 0.275 - 2.5, w / 2 + b - 2.5, l - w * 0.275
  55. sp.AppendLineSegment w / 2, l - w * 0.275
  56. sp.AppendLineSegment 0, l
  57. sp.Closed = True
  58. sr.Add ActiveLayer.CreateCurve(crv(2))
  59. Set crv(3) = Application.CreateCurve(ActiveDocument)
  60. Set sp = crv(3).CreateSubPath(0, 0)
  61. sp.AppendLineSegment 0, l
  62. sp.AppendLineSegment w / 2 + b, l
  63. sp.AppendLineSegment w / 2 + b, l - w * 0.275 + 5
  64. sp.AppendCurveSegment2 w / 2 + b - 5, l - w * 0.275, w / 2 + b, l - w * 0.275 + 2.5, w / 2 + b - 2.5, l - w * 0.275
  65. sp.AppendLineSegment w / 2, l - w * 0.275
  66. sp.AppendLineSegment w / 2, w * 0.275
  67. sp.AppendLineSegment w / 2 + b - 5, w * 0.275
  68. sp.AppendCurveSegment2 w / 2 + b, w * 0.275 - 5, w / 2 + b - 2.5, w * 0.275, w / 2 + b, w * 0.275 - 2.5
  69. sp.AppendLineSegment w / 2 + b, 0
  70. sp.Closed = True
  71. sr.Add ActiveLayer.CreateCurve(crv(3))
  72. '// 移动到适合的地方
  73. sr(1).Move 0, -w / 2: sr(1).Rotate 180
  74. Set s = sr(1).Duplicate(0, 0): sr.Add s
  75. s.Flip cdrFlipHorizontal: s.Move w + l, 0
  76. sr(2).Rotate -90: sr(3).Rotate -90
  77. sr(2).LeftX = 2 * w + l: sr(3).LeftX = w
  78. sr(2).TopY = 0: sr(3).TopY = 0
  79. Set DrawBottomWing = sr
  80. End Function
  81. Public Function Simple_box_four(Optional ByVal l As Double, Optional ByVal w As Double, Optional ByVal h As Double, Optional ByVal b As Double = 15)
  82. Dim sr As New ShapeRange, wing As New ShapeRange
  83. Dim sh As Shape
  84. l1x = w: l2x = w + l: l3x = 2 * w + l: l4x = 2 * (w + l)
  85. '// 绘制主体上下盖矩形
  86. Set mainRect_aw = ActiveLayer.CreateRectangle(0, 0, w, h)
  87. Set mainRect_al = ActiveLayer.CreateRectangle(0, 0, l, h)
  88. mainRect_al.Move l1x, 0
  89. Set mainRect_bw = ActiveLayer.CreateRectangle(0, 0, w, h)
  90. mainRect_bw.Move l2x, 0
  91. Set mainRect_bl = ActiveLayer.CreateRectangle(0, 0, l, h)
  92. mainRect_bl.Move l3x, 0
  93. Set topRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  94. topRect.Move l1x, h
  95. Set bottomRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  96. bottomRect.Move l3x, -w
  97. '// 绘制Box 圆角矩形插口
  98. Set top_RoundRect = ActiveLayer.CreateRectangle(0, 0, l, b, 50, 50)
  99. top_RoundRect.Move l1x, h + w
  100. Set bottom_RoundRect = ActiveLayer.CreateRectangle(0, 0, l, b, 0, 0, 50, 50)
  101. bottom_RoundRect.Move l3x, -w - b
  102. Set Bond = DrawBond(b, h, l4x, 0)
  103. '// 绘制box 四个翅膀
  104. Set sh = DrawWing(w, (w + b) / 2 - 2)
  105. wing.Add sh.Duplicate(0, h)
  106. wing.Add sh.Duplicate(l2x, h)
  107. wing.Add sh.Duplicate(0, -sh.SizeHeight)
  108. wing.Add sh.Duplicate(l2x, -sh.SizeHeight)
  109. wing(2).Flip cdrFlipHorizontal
  110. wing(3).Rotate 180
  111. wing(4).Flip cdrFlipVertical
  112. '// 添加到物件组,设置轮廓色 C100
  113. sr.Add mainRect_aw: sr.Add mainRect_al: sr.Add mainRect_bw: sr.Add mainRect_bl
  114. sr.Add topRect: sr.Add bottomRect: sr.Add Bond
  115. sr.Add top_RoundRect: sr.Add bottom_RoundRect
  116. sr.AddRange wing: sh.Delete
  117. sr.SetOutlineProperties Color:=CreateCMYKColor(100, 0, 0, 0)
  118. sr.CreateSelection: sr.Group
  119. End Function
  120. Public Function input_box_lwh() As Variant
  121. Dim str, arr, n
  122. str = InputBox("请输入长x宽x高,使用空格 * x 间隔", "盒子长宽高", "100 x 100 x 100 mm") & " "
  123. str = Newline_to_Space(str)
  124. ' 替换 mm x * 换行 TAB 为空格
  125. str = VBA.Replace(str, "mm", " ")
  126. str = VBA.Replace(str, "x", " ")
  127. str = VBA.Replace(str, "X", " ")
  128. str = VBA.Replace(str, "*", " ")
  129. '// 换行转空格 多个空格换成一个空格
  130. str = API.Newline_to_Space(str)
  131. arr = Split(str)
  132. arr(0) = Val(arr(0))
  133. arr(1) = Val(arr(1))
  134. arr(2) = Val(arr(2))
  135. arr(3) = Val(arr(3))
  136. input_box_lwh = arr
  137. End Function
  138. Public Function Simple_box_three(Optional ByVal l As Double, Optional ByVal w As Double, Optional ByVal h As Double, Optional ByVal b As Double = 15)
  139. ActiveDocument.Unit = cdrMillimeter
  140. Dim sr As New ShapeRange, wing As New ShapeRange
  141. Dim sh As Shape
  142. boxL = 2 * l + 2 * w + b: boxH = h
  143. l1x = w: l2x = w + l: l3x = 2 * w + l: l4x = 2 * (w + l)
  144. '// 绘制主体上下盖矩形
  145. Set mainRect = ActiveLayer.CreateRectangle(0, 0, boxL, boxH)
  146. Set topRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  147. topRect.Move l1x, h
  148. Set bottomRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  149. bottomRect.Move l3x, -w
  150. '// 绘制Box 圆角矩形插口
  151. Set top_RoundRect = ActiveLayer.CreateRectangle(0, 0, l, b, 50, 50)
  152. top_RoundRect.Move l1x, h + w
  153. Set bottom_RoundRect = ActiveLayer.CreateRectangle(0, 0, l, b, 0, 0, 50, 50)
  154. bottom_RoundRect.Move l3x, -w - b
  155. '// 绘制box 四个翅膀
  156. Set sh = DrawWing(w, (w + b) / 2 - 2)
  157. wing.Add sh.Duplicate(0, h)
  158. wing.Add sh.Duplicate(l2x, h)
  159. wing.Add sh.Duplicate(0, -sh.SizeHeight)
  160. wing.Add sh.Duplicate(l2x, -sh.SizeHeight)
  161. wing(2).Flip cdrFlipHorizontal
  162. wing(3).Rotate 180
  163. wing(4).Flip cdrFlipVertical
  164. '// 添加到物件组,设置轮廓色 C100
  165. sr.Add mainRect: sr.Add topRect: sr.Add bottomRect
  166. sr.Add top_RoundRect: sr.Add bottom_RoundRect
  167. sr.AddRange wing: sh.Delete
  168. sr.SetOutlineProperties Color:=CreateCMYKColor(100, 0, 0, 0)
  169. '// 绘制尺寸刀痕线
  170. Set sl1 = DrawLine(l1x, 0, l1x, h)
  171. Set sl2 = DrawLine(l2x, 0, l2x, h)
  172. Set sl3 = DrawLine(l3x, 0, l3x, h)
  173. Set sl4 = DrawLine(l4x, 0, l4x, h)
  174. '// 盒子box 群组
  175. sr.Add sl1: sr.Add sl2: sr.Add sl3: sr.Add sl4
  176. sr.CreateSelection: sr.Group
  177. End Function
  178. '// 画一条线,设置轮廓色 M100
  179. Private Function DrawLine(X1, Y1, X2, Y2) As Shape
  180. Set DrawLine = ActiveLayer.CreateLineSegment(X1, Y1, X2, Y2)
  181. DrawLine.Outline.SetProperties Color:=CreateCMYKColor(0, 100, 0, 0)
  182. End Function
  183. Private Function DrawWing(ByVal w As Double, ByVal h As Double) As Shape
  184. Dim sp As SubPath, crv As Curve
  185. Dim x As Double, Y As Double
  186. x = w: Y = h
  187. '// 绘制 Box 翅膀 Wing
  188. Set crv = Application.CreateCurve(ActiveDocument)
  189. Set sp = crv.CreateSubPath(0, 0)
  190. sp.AppendLineSegment 0, 4
  191. sp.AppendLineSegment 2, 6
  192. sp.AppendLineSegment 6, Y - 2.5
  193. sp.AppendCurveSegment2 8.5, Y, 6.2, Y - 1.25, 7, Y
  194. sp.AppendLineSegment x - 2, Y
  195. sp.AppendLineSegment x - 2, 3
  196. sp.AppendLineSegment x, 0
  197. sp.Closed = True
  198. Set DrawWing = ActiveLayer.CreateCurve(crv)
  199. End Function
  200. Private Function DrawBond(ByVal w As Double, ByVal h As Double, ByVal move_x As Double, ByVal move_y As Double) As Shape
  201. Dim sp As SubPath, crv As Curve
  202. Dim x As Double, Y As Double
  203. x = w: Y = h
  204. '// 绘制 Box 粘合边 Bond
  205. Set crv = Application.CreateCurve(ActiveDocument)
  206. Set sp = crv.CreateSubPath(0, 0)
  207. sp.AppendLineSegment 0, Y
  208. sp.AppendLineSegment x, Y - 5
  209. sp.AppendLineSegment x, 5
  210. sp.Closed = True
  211. Set DrawBond = ActiveLayer.CreateCurve(crv)
  212. DrawBond.Move move_x, move_y
  213. End Function
  214. Public Function Simple_box_one()
  215. ActiveDocument.Unit = cdrMillimeter
  216. l = 100: w = 50: h = 70: b = 15
  217. boxL = 2 * l + 2 * w + b
  218. boxH = h
  219. l1x = w
  220. l2x = w + l
  221. l3x = 2 * w + l
  222. l4x = 2 * (w + l)
  223. Set Rect = ActiveLayer.CreateRectangle(0, 0, boxL, boxH)
  224. Set sl1 = DrawLine(l1x, 0, l1x, h)
  225. Set sl2 = DrawLine(l2x, 0, l2x, h)
  226. Set sl3 = DrawLine(l3x, 0, l3x, h)
  227. Set sl4 = DrawLine(l4x, 0, l4x, h)
  228. End Function
  229. Public Function Simple_box_two()
  230. ActiveDocument.Unit = cdrMillimeter
  231. l = 100: w = 50: h = 70: b = 15
  232. boxL = 2 * l + 2 * w + b: boxH = h
  233. l1x = w: l2x = w + l: l3x = 2 * w + l: l4x = 2 * (w + l)
  234. Set mainRect = ActiveLayer.CreateRectangle(0, 0, boxL, boxH)
  235. Set topRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  236. topRect.Move l1x, h
  237. Set bottomRect = ActiveLayer.CreateRectangle(0, 0, l, w)
  238. bottomRect.Move l3x, -w
  239. Set sl1 = DrawLine(l1x, 0, l1x, h)
  240. Set sl2 = DrawLine(l2x, 0, l2x, h)
  241. Set sl3 = DrawLine(l3x, 0, l3x, h)
  242. Set sl4 = DrawLine(l4x, 0, l4x, h)
  243. End Function
  244. Public Function Simple_3Deffect()
  245. Dim sr As ShapeRange ' 定义物件范围
  246. Set sr = ActiveSelectionRange ' 选择3个物件
  247. If sr.Count >= 3 Then
  248. ' // 先上下再左右排序
  249. sr.Sort " @shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
  250. sr(1).Stretch 0.951, 0.525 ' 顶盖物件缩放修正和变形
  251. sr(1).Skew 41.7, 7#
  252. sr(2).Stretch 0.951, 0.937 ' 正面物件缩放修正和变形
  253. sr(2).Skew 0#, 7#
  254. sr(3).Stretch 0.468, 0.937 ' 侧面物件缩放修正和变形
  255. sr(3).Skew 0#, -45#
  256. End If
  257. End Function