1
1

Box.bas 12 KB

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