Box.bas 12 KB

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