FajarHidaa commited on
Commit
9b70e85
·
verified ·
1 Parent(s): ed5b4c7

Update Banner/Hitung_banner_yang_dicetak.txt

Browse files
Files changed (1) hide show
  1. Banner/Hitung_banner_yang_dicetak.txt +233 -132
Banner/Hitung_banner_yang_dicetak.txt CHANGED
@@ -2,218 +2,325 @@ Option Explicit
2
 
3
  Public Sub AmbilMultiTeksSpesifik()
4
 
5
- On Error GoTo ErrorHandler
 
 
6
 
7
- Dim doc As Document
8
  Dim shp As Shape
9
- Dim txtShape As Shape
10
- Dim rectShape As Shape
11
- Dim bestRect As Shape
12
 
13
- Dim teksCocok As Collection
14
- Dim daftarRect As Collection
 
15
 
16
- Dim txt As String
 
17
  Dim laporan As String
18
- Dim jumlahCocok As Long
19
- Dim i As Long
20
-
21
- Dim bestArea As Double
22
- Dim area As Double
23
- Dim w As Double
24
- Dim h As Double
25
-
26
- If ActiveDocument Is Nothing Then
27
- MsgBox "Tidak ada dokumen aktif.", vbExclamation
28
- Exit Sub
29
- End If
30
 
31
- Set doc = ActiveDocument
 
 
 
 
 
 
 
32
 
33
  If ActiveSelection.Shapes.Count = 0 Then
34
- MsgBox "Pilih objek teks atau grup yang berisi teks.", _
35
- vbExclamation, "Tidak Ada Seleksi"
36
  Exit Sub
37
  End If
38
 
39
- Set teksCocok = New Collection
40
- Set daftarRect = New Collection
 
41
 
42
- ' 1. Ambil teks yang cocok dari seleksi,
43
- ' termasuk teks di dalam grup bertingkat.
44
  For Each shp In ActiveSelection.Shapes
45
- KumpulkanTeksCocok shp, teksCocok
46
  Next shp
47
 
48
- If teksCocok.Count = 0 Then
49
- MsgBox "Tidak ditemukan teks yang mengandung " & _
50
- "'x' atau 'X' DAN '//'.", _
51
- vbInformation, "Tidak Ada Hasil"
 
 
 
 
 
 
 
 
 
52
  Exit Sub
53
  End If
54
 
55
- ' 2. Kumpulkan semua Rectangle pada halaman aktif.
56
- ' Termasuk Rectangle di dalam grup.
57
- For Each shp In ActivePage.Shapes
58
- KumpulkanRectangle shp, daftarRect
59
- Next shp
60
 
61
- laporan = "LAPORAN UKURAN RECTANGLE BANNER" & vbCrLf & _
62
- String(42, "-") & vbCrLf & vbCrLf
63
 
64
- jumlahCocok = 0
 
65
 
66
- ' 3. Cari Rectangle terkecil yang mencakup teks.
67
- For Each txtShape In teksCocok
68
 
69
- txt = RapikanTeks(txtShape.Text.Story.Text)
70
 
71
- Set bestRect = Nothing
72
- bestArea = 1E+100
73
 
74
- For Each rectShape In daftarRect
 
 
75
 
76
- If RectangleMencakupTeks(rectShape, txtShape) Then
77
 
78
- area = rectShape.SizeWidth * _
79
- rectShape.SizeHeight
 
 
 
 
 
 
 
 
 
 
80
 
81
- If area < bestArea Then
82
- bestArea = area
83
- Set bestRect = rectShape
84
  End If
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  End If
87
 
88
- Next rectShape
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- jumlahCocok = jumlahCocok + 1
91
 
92
- laporan = laporan & jumlahCocok & _
93
- ". Teks: """ & txt & """" & vbCrLf
94
 
95
- If Not bestRect Is Nothing Then
 
 
 
96
 
97
- w = bestRect.SizeWidth
98
- h = bestRect.SizeHeight
99
 
100
- laporan = laporan & _
101
- " Ukuran Rectangle: " & _
102
- Format$(w, "0.00") & " x " & _
103
- Format$(h, "0.00") & " " & _
104
- DapatkanSatuanString(doc.Unit) & vbCrLf
105
 
106
- Else
 
 
 
 
 
 
 
107
 
108
- laporan = laporan & _
109
- " Rectangle pembungkus: TIDAK DITEMUKAN" & _
110
- vbCrLf
111
 
112
  End If
113
 
114
- laporan = laporan & vbCrLf
115
 
116
- Next txtShape
 
117
 
118
- laporan = laporan & String(42, "-") & vbCrLf & _
119
- "Total teks cocok: " & jumlahCocok
120
 
121
- MsgBox laporan, vbInformation, _
122
- "Selesai - " & jumlahCocok & " Teks Diperiksa"
 
 
 
123
 
124
- Exit Sub
 
125
 
126
- ErrorHandler:
 
 
 
 
 
127
 
128
- MsgBox "Terjadi kesalahan macro." & vbCrLf & _
129
- "Error " & Err.Number & ": " & _
130
- Err.Description, vbCritical, "Error"
 
 
 
 
131
 
132
  End Sub
133
 
134
 
135
- Private Sub KumpulkanTeksCocok( _
136
- ByVal shp As Shape, _
137
- ByRef hasil As Collection)
138
 
139
  Dim child As Shape
140
- Dim txt As String
141
 
142
  If shp.Type = cdrGroupShape Then
143
-
144
  For Each child In shp.Shapes
145
- KumpulkanTeksCocok child, hasil
146
  Next child
 
 
 
 
 
147
 
148
- Exit Sub
149
 
150
- End If
151
 
152
- If shp.Type <> cdrTextShape Then Exit Sub
 
153
 
154
- On Error GoTo Selesai
 
155
 
156
- txt = RapikanTeks(shp.Text.Story.Text)
 
157
 
158
- If InStr(1, txt, "x", vbTextCompare) > 0 And _
159
- InStr(1, txt, "//", vbBinaryCompare) > 0 Then
 
 
 
160
 
161
- hasil.Add shp
162
 
163
- End If
164
 
165
- Selesai:
166
 
167
- End Sub
 
168
 
 
 
169
 
170
- Private Sub KumpulkanRectangle( _
171
- ByVal shp As Shape, _
172
- ByRef hasil As Collection)
 
 
173
 
174
- Dim child As Shape
175
 
176
- If shp.Type = cdrGroupShape Then
177
 
178
- For Each child In shp.Shapes
179
- KumpulkanRectangle child, hasil
180
- Next child
 
181
 
182
- Exit Sub
 
183
 
184
- End If
185
 
186
- If shp.Type = cdrRectangleShape Then
187
- hasil.Add shp
188
- End If
189
 
190
- End Sub
 
 
 
 
191
 
 
 
192
 
193
- Private Function RectangleMencakupTeks( _
194
- ByVal rect As Shape, _
195
- ByVal txt As Shape) As Boolean
 
196
 
197
- Const TOLERANSI As Double = 0.01
 
 
198
 
199
- RectangleMencakupTeks = _
200
- (rect.LeftX <= txt.LeftX + TOLERANSI) And _
201
- (rect.RightX >= txt.RightX - TOLERANSI) And _
202
- (rect.BottomY <= txt.BottomY + TOLERANSI) And _
203
- (rect.TopY >= txt.TopY - TOLERANSI)
204
 
205
  End Function
206
 
207
 
208
- Private Function RapikanTeks( _
209
- ByVal txt As String) As String
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
- txt = Replace(txt, vbCrLf, " ")
212
- txt = Replace(txt, vbCr, " ")
213
- txt = Replace(txt, vbLf, " ")
214
- txt = Replace(txt, vbTab, " ")
215
 
216
- RapikanTeks = Trim$(txt)
217
 
218
  End Function
219
 
@@ -222,22 +329,16 @@ Private Function DapatkanSatuanString( _
222
  ByVal satuan As cdrUnit) As String
223
 
224
  Select Case satuan
225
-
226
  Case cdrMillimeter
227
  DapatkanSatuanString = "mm"
228
-
229
  Case cdrCentimeter
230
  DapatkanSatuanString = "cm"
231
-
232
  Case cdrInch
233
  DapatkanSatuanString = "inch"
234
-
235
  Case cdrPixel
236
  DapatkanSatuanString = "px"
237
-
238
  Case Else
239
  DapatkanSatuanString = "unit"
240
-
241
  End Select
242
 
243
  End Function
 
2
 
3
  Public Sub AmbilMultiTeksSpesifik()
4
 
5
+ Dim semuaShape As Collection
6
+ Dim semuaRect As Collection
7
+ Dim rectValid As Collection
8
 
 
9
  Dim shp As Shape
10
+ Dim rect As Shape
11
+ Dim kandidat As Shape
 
12
 
13
+ Dim i As Long, j As Long
14
+ Dim pemilik As Long
15
+ Dim jumlahBanner As Long
16
 
17
+ Dim teks As String
18
+ Dim teksBanner() As String
19
  Dim laporan As String
20
+ Dim satuan As String
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ Dim minMargin As Double
23
+ Dim maxMargin As Double
24
+ Dim minX As Double, maxX As Double
25
+ Dim minY As Double, maxY As Double
26
+ Dim marginKiri As Double, marginKanan As Double
27
+ Dim marginAtas As Double, marginBawah As Double
28
+ Dim luas As Double, luasTerbaik As Double
29
+ Dim adaObjek As Boolean
30
 
31
  If ActiveSelection.Shapes.Count = 0 Then
32
+ MsgBox "Pilih semua objek banner terlebih dahulu.", vbExclamation
 
33
  Exit Sub
34
  End If
35
 
36
+ Set semuaShape = New Collection
37
+ Set semuaRect = New Collection
38
+ Set rectValid = New Collection
39
 
40
+ ' Kumpulkan objek daun; grup ditelusuri hingga isinya.
 
41
  For Each shp In ActiveSelection.Shapes
42
+ KumpulkanShapeRekursif shp, semuaShape
43
  Next shp
44
 
45
+ ' Kumpulkan Rectangle yang unik.
46
+ For i = 1 To semuaShape.Count
47
+ Set shp = semuaShape(i)
48
+
49
+ If shp.Type = cdrRectangleShape Then
50
+ If Not KoleksiBerisiShape(semuaRect, shp) Then
51
+ semuaRect.Add shp
52
+ End If
53
+ End If
54
+ Next i
55
+
56
+ If semuaRect.Count = 0 Then
57
+ MsgBox "Tidak ditemukan Rectangle dalam seleksi.", vbExclamation
58
  Exit Sub
59
  End If
60
 
61
+ ' Konversi jarak dari cm ke satuan dokumen.
62
+ minMargin = CmKeSatuanDokumen(2.5)
63
+ maxMargin = CmKeSatuanDokumen(10)
 
 
64
 
65
+ ' Validasi setiap Rectangle berdasarkan jarak ke desain.
66
+ For i = 1 To semuaRect.Count
67
 
68
+ Set rect = semuaRect(i)
69
+ adaObjek = False
70
 
71
+ minX = 0: maxX = 0
72
+ minY = 0: maxY = 0
73
 
74
+ For j = 1 To semuaShape.Count
75
 
76
+ Set shp = semuaShape(j)
 
77
 
78
+ ' Gunakan objek desain, bukan Rectangle atau grup.
79
+ If shp.Type <> cdrRectangleShape _
80
+ And shp.Type <> cdrGroupShape Then
81
 
82
+ If RectangleMencakupShape(rect, shp) Then
83
 
84
+ If Not adaObjek Then
85
+ minX = shp.LeftX
86
+ maxX = shp.RightX
87
+ minY = shp.BottomY
88
+ maxY = shp.TopY
89
+ adaObjek = True
90
+ Else
91
+ If shp.LeftX < minX Then minX = shp.LeftX
92
+ If shp.RightX > maxX Then maxX = shp.RightX
93
+ If shp.BottomY < minY Then minY = shp.BottomY
94
+ If shp.TopY > maxY Then maxY = shp.TopY
95
+ End If
96
 
 
 
 
97
  End If
98
+ End If
99
+
100
+ Next j
101
+
102
+ If adaObjek Then
103
+
104
+ marginKiri = minX - rect.LeftX
105
+ marginKanan = rect.RightX - maxX
106
+ marginAtas = rect.TopY - maxY
107
+ marginBawah = minY - rect.BottomY
108
+
109
+ If MarginValid(marginKiri, minMargin, maxMargin) _
110
+ And MarginValid(marginKanan, minMargin, maxMargin) _
111
+ And MarginValid(marginAtas, minMargin, maxMargin) _
112
+ And MarginValid(marginBawah, minMargin, maxMargin) Then
113
+
114
+ rectValid.Add rect
115
 
116
  End If
117
 
118
+ End If
119
+
120
+ Next i
121
+
122
+ If rectValid.Count = 0 Then
123
+ MsgBox "Tidak ada Rectangle yang memenuhi jarak 2,5–10 cm." & _
124
+ vbCrLf & _
125
+ "Periksa seleksi, satuan dokumen, dan jarak pembatas.", _
126
+ vbExclamation, "Pembatas Tidak Ditemukan"
127
+ Exit Sub
128
+ End If
129
+
130
+ ReDim teksBanner(1 To rectValid.Count)
131
+
132
+ ' Alokasikan setiap teks penanda ke satu Rectangle valid.
133
+ For i = 1 To semuaShape.Count
134
+
135
+ Set shp = semuaShape(i)
136
+
137
+ If shp.Type = cdrTextShape Then
138
+
139
+ teks = NormalisasiTeks(AmbilIsiTeks(shp))
140
+
141
+ If InStr(1, teks, "x", vbTextCompare) > 0 _
142
+ And InStr(1, teks, "//", vbBinaryCompare) > 0 Then
143
+
144
+ pemilik = 0
145
+ luasTerbaik = 0
146
+
147
+ For j = 1 To rectValid.Count
148
+
149
+ Set kandidat = rectValid(j)
150
 
151
+ If RectangleMencakupShape(kandidat, shp) Then
152
 
153
+ luas = Abs(kandidat.SizeWidth * _
154
+ kandidat.SizeHeight)
155
 
156
+ If pemilik = 0 Or luas < luasTerbaik Then
157
+ pemilik = j
158
+ luasTerbaik = luas
159
+ End If
160
 
161
+ End If
 
162
 
163
+ Next j
 
 
 
 
164
 
165
+ If pemilik > 0 Then
166
+ If Len(teksBanner(pemilik)) = 0 Then
167
+ teksBanner(pemilik) = teks
168
+ Else
169
+ teksBanner(pemilik) = teksBanner(pemilik) & _
170
+ " | " & teks
171
+ End If
172
+ End If
173
 
174
+ End If
 
 
175
 
176
  End If
177
 
178
+ Next i
179
 
180
+ ' Laporan.
181
+ satuan = DapatkanSatuanString(ActiveDocument.Unit)
182
 
183
+ laporan = "LAPORAN UKURAN BANNER" & vbCrLf & _
184
+ String(40, "-") & vbCrLf
185
 
186
+ jumlahBanner = rectValid.Count
187
+
188
+ For i = 1 To rectValid.Count
189
+
190
+ Set rect = rectValid(i)
191
 
192
+ teks = Trim$(teksBanner(i))
193
+ If Len(teks) = 0 Then teks = "unknown"
194
 
195
+ laporan = laporan & _
196
+ i & ". Banner: " & teks & vbCrLf & _
197
+ " Lebar : " & Format$(rect.SizeWidth, "0.00") & _
198
+ " " & satuan & vbCrLf & _
199
+ " Tinggi : " & Format$(rect.SizeHeight, "0.00") & _
200
+ " " & satuan & vbCrLf & vbCrLf
201
 
202
+ Next i
203
+
204
+ laporan = laporan & String(40, "-") & vbCrLf & _
205
+ "TOTAL BANNER: " & jumlahBanner
206
+
207
+ MsgBox laporan, vbInformation, _
208
+ "Selesai - " & jumlahBanner & " Banner"
209
 
210
  End Sub
211
 
212
 
213
+ Private Sub KumpulkanShapeRekursif( _
214
+ ByVal shp As Shape, ByRef daftar As Collection)
 
215
 
216
  Dim child As Shape
 
217
 
218
  If shp.Type = cdrGroupShape Then
 
219
  For Each child In shp.Shapes
220
+ KumpulkanShapeRekursif child, daftar
221
  Next child
222
+ Else
223
+ If Not KoleksiBerisiShape(daftar, shp) Then
224
+ daftar.Add shp
225
+ End If
226
+ End If
227
 
228
+ End Sub
229
 
 
230
 
231
+ Private Function KoleksiBerisiShape( _
232
+ ByVal daftar As Collection, ByVal cari As Shape) As Boolean
233
 
234
+ Dim i As Long
235
+ Dim item As Shape
236
 
237
+ For i = 1 To daftar.Count
238
+ Set item = daftar(i)
239
 
240
+ If item Is cari Then
241
+ KoleksiBerisiShape = True
242
+ Exit Function
243
+ End If
244
+ Next i
245
 
246
+ KoleksiBerisiShape = False
247
 
248
+ End Function
249
 
 
250
 
251
+ Private Function RectangleMencakupShape( _
252
+ ByVal rect As Shape, ByVal shp As Shape) As Boolean
253
 
254
+ Dim tol As Double
255
+ tol = 0.001
256
 
257
+ RectangleMencakupShape = _
258
+ (rect.LeftX <= shp.LeftX + tol) And _
259
+ (rect.RightX >= shp.RightX - tol) And _
260
+ (rect.TopY >= shp.TopY - tol) And _
261
+ (rect.BottomY <= shp.BottomY + tol)
262
 
263
+ End Function
264
 
 
265
 
266
+ Private Function MarginValid( _
267
+ ByVal margin As Double, _
268
+ ByVal minimum As Double, _
269
+ ByVal maksimum As Double) As Boolean
270
 
271
+ MarginValid = (margin >= minimum - 0.001) _
272
+ And (margin <= maksimum + 0.001)
273
 
274
+ End Function
275
 
 
 
 
276
 
277
+ Private Function AmbilIsiTeks(ByVal shp As Shape) As String
278
+
279
+ On Error GoTo Gagal
280
+ AmbilIsiTeks = shp.Text.Story.Text
281
+ Exit Function
282
 
283
+ Gagal:
284
+ AmbilIsiTeks = ""
285
 
286
+ End Function
287
+
288
+
289
+ Private Function NormalisasiTeks(ByVal teks As String) As String
290
 
291
+ teks = Replace(teks, vbCr, " ")
292
+ teks = Replace(teks, vbLf, " ")
293
+ teks = Replace(teks, vbTab, " ")
294
 
295
+ NormalisasiTeks = Trim$(teks)
 
 
 
 
296
 
297
  End Function
298
 
299
 
300
+ Private Function CmKeSatuanDokumen(ByVal cm As Double) As Double
301
+
302
+ Select Case ActiveDocument.Unit
303
+
304
+ Case cdrMillimeter
305
+ CmKeSatuanDokumen = cm * 10#
306
+
307
+ Case cdrCentimeter
308
+ CmKeSatuanDokumen = cm
309
+
310
+ Case cdrInch
311
+ CmKeSatuanDokumen = cm / 2.54
312
+
313
+ Case cdrPixel
314
+ Err.Raise vbObjectError + 1001, _
315
+ "CmKeSatuanDokumen", _
316
+ "Satuan pixel belum didukung untuk jarak fisik cm."
317
 
318
+ Case Else
319
+ Err.Raise vbObjectError + 1002, _
320
+ "CmKeSatuanDokumen", _
321
+ "Satuan dokumen belum didukung. Gunakan mm, cm, atau inch."
322
 
323
+ End Select
324
 
325
  End Function
326
 
 
329
  ByVal satuan As cdrUnit) As String
330
 
331
  Select Case satuan
 
332
  Case cdrMillimeter
333
  DapatkanSatuanString = "mm"
 
334
  Case cdrCentimeter
335
  DapatkanSatuanString = "cm"
 
336
  Case cdrInch
337
  DapatkanSatuanString = "inch"
 
338
  Case cdrPixel
339
  DapatkanSatuanString = "px"
 
340
  Case Else
341
  DapatkanSatuanString = "unit"
 
342
  End Select
343
 
344
  End Function