-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodbx_Michael_Puckett.lsp
563 lines (488 loc) · 16.3 KB
/
odbx_Michael_Puckett.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
(defun _OdbxReport ( filelist reportname overwrite foo / handle document result )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; In short this function is a convenience wrapper. For a given
;; filelist, reportname, overwrite toggle and arbitrary function
;; 'Foo' it will, if possible, cycle through all of the drawings in
;; filelist and apply function 'Foo to said drawings.
;;
;; Arguments:
;;
;; filelist : A list of path qualified files.
;;
;; reportname : A path qualified file which will be opened for
;; output, writing over any original contents if
;; overwrite argument is non nil, otherwise
;; appending new data to existing.
;;
;; overwrite : As noted, indicates whther the report is
;; overwritten or appended.
;;
;; foo : Arbitrary function which must have this
;; signature:
;;
;; (defun foo ( docname document handle ) ... )
;;
;; Where:
;;
;; docname : A string, the name of the
;; document.
;;
;; document : An objectdbx document.
;;
;; handle : A file handle opened for
;; output.
;;
;; For example, 'Foo' might be as simple as:
;;
;; (lambda ( docname document handle )
;; (princ
;; (strcat
;; docname
;; " has "
;; (itoa
;; (vlax-get
;; (vlax-get
;; document
;; 'RegisteredApplications
;; )
;; 'Count
;; )
;; )
;; " appid(s).\n"
;; )
;; handle
;; )
;; )
;;
;; What 'Foo' actually does with the objectdbx document or the
;; handle this function cares not, its sole responsibility is to:
;;
;; 1. Open the report file for output, either overwrite or
;; append.
;;
;; 2. Cycle through all files:
;;
;; 2.1 Open the file via objectdbx.
;;
;; 2.2 Pass document etc. to function 'Foo'.
;;
;; 2.3 Close the objectdbx file.
;;
;; 3. Close the report file.
;;
;; reporting any errors thrown trying to open the report file,
;; opening files via objectdbx, or unhandled errors thrown by
;; function 'Foo'. Typically 'Foo' will interogate the objectdbx
;; document and write data to the handle.
;;
;; "But" you say, "it seems redundant, passing the document name
;; when one could just use (vla-get-name document)".
;;
;; Ahhhh, but if a temporary copy of the document is created by
;; the (_OdbxOpen ...) function the name reported will be wrong. In
;; other words, if 'Foo' needs to report the original document name
;; it should use the docname argument rather than querying the
;; document for its name, ergo the docname requirement. If the
;; actual document name is desired you can use the afore mentioned
;; vla-get-name function. If necessary you can use the supplied
;; function _OdbxIsTempCopy function to determine if the document
;; is not the original.
;;
;; What's the point of all this again?
;;
;; Typically function 'Foo' will interogate the document and write
;; information to the data file represented by the handle. Could be
;; block attribute data, could be some other rivoting data like
;; AutoPlant component IDs, who knows. This function provides easy
;; access to previous authored functions, like the _OdbxOpen and
;; _OdbxClose functions to make easy the frequent exercise of using
;; ObjectDBX to interogate multiple files. No point in penning the
;; same scafolding again and again.
;;
;;==================================================================
(cond
( (setq handle (open reportname (if overwrite "w" "a")))
(foreach docname filelist
(cond
( (setq document (_OdbxOpen docname t))
(if
(vl-catch-all-error-p
(setq result
(vl-catch-all-apply
'(lambda ( )
(foo
docname
document
handle
)
)
)
)
)
(princ
(strcat
"(Foo docname document handle)"
" threw this error: <"
(vl-catch-all-error-message result)
">\n"
)
)
)
(_OdbxClose document)
)
( (princ
(strcat
"Could not open <"
docname
"> with ObjectDBX.\n"
)
)
)
)
)
(close handle)
)
( (princ
(strcat
"Could not open <"
reportname
"> for output.\n"
)
)
)
)
)
(defun c:Example ( )
(_OdbxReport
;; filelist
'(
"x:\\SomePath\\SomeName1.dwg"
"x:\\SomePath\\SomeName2.dwg"
"x:\\SomePath\\SomeName3.dwg"
)
;; reportname
"c:\\MyReportName.txt"
;; overwrite
t
;; foo
(lambda ( docname document handle )
(princ
(strcat
docname
" has "
(itoa
(vlax-get
(vlax-get
document
'RegisteredApplications
)
'Count
)
)
" appid(s).\n"
)
handle
)
)
)
(princ)
)
(defun _OdbxOpen ( drawingname tempflag / foo exists odbxdoc tempfile result )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; Open the document via ObjectDBX if possible (trap any errors).
;;
;; If the file cannot be opened directly AND the tempflag is non
;; nil try to open a temporary copy (flagging it as a copy).
;;
;;==================================================================
(cond
( (and
(defun foo ( odbxdoc drawingname )
(null
(vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ( )
(vla-open
odbxdoc
drawingname
)
)
)
)
)
)
(setq exists (_Exists drawingname))
(setq odbxdoc (_OdbxGetInterface))
(foo odbxdoc drawingname)
)
;; File opened no problem.
(setq result odbxdoc)
)
(
(and
exists
odbxdoc
;; Assumption: File exists in a read-only directory,
;; (can't make associated dwl file) try make and use
;; a temporary copy.
(setq tempfile (_MakeTempFileCopy drawingname))
(foo odbxdoc tempfile)
)
;; Flag the file as being a temp by adding a main dict
;; entry named "OdbxTempCopy".
;;
;; See companion functions --
;;
;; _OdbxIsTempCopy
;;
;; _OdbxDeleteIfTempCopy
(vl-catch-all-apply
'(lambda ( )
(vla-add
(vla-get-dictionaries odbxdoc)
"OdbxTempCopy"
)
)
)
(setq result odbxdoc)
)
( odbxdoc
(vlax-release-object odbxdoc)
)
)
result
)
(defun _OdbxClose ( document / filename )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; Close the objectdbx document. If it was a temporary copy made by
;; _OdbxOpen try to delete it.
;;
;; See companion functions --
;;
;; _OdbxOpen
;;
;; _OdbxIsTempCopy
;;
;;==================================================================
(if (_OdbxIsTempCopy document)
(setq filename (vla-get-name document))
)
(vl-catch-all-apply
'(lambda ( )
(vlax-release-object document)
)
)
(if filename
(vl-catch-all-apply
'(lambda ( )
(vl-file-delete filename)
)
)
)
(princ)
)
(defun _OdbxGetInterface ( / version )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; If possible create an instance of the ObjectDBX interface.
;;
;; If I recall correctly Luis Esquivel helped me refine this way
;; back in the day on the Autodesk forums. Others too have
;; contributed to the collective objectdbx knowledge, folks like
;; Tony Tanzillo, who was the first to publically share any info
;; about said topic and who we are all indebted to.
;;
;;==================================================================
(if (_OdbxGetCLSID)
(vla-getinterfaceobject
(vlax-get-acad-object)
(apply 'strcat
(cons "ObjectDBX.AxDbDocument"
(if (/= 15 (setq version (atoi (getvar "acadver"))))
(list "." (itoa version))
)
)
)
)
)
)
(defun _OdbxGetCLSID ( / key version dll )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; Get the ObjectDBX CLSID so callers may attemp to create an
;; objectdbx interface. If necessary register the dll (was required
;; for early versions of AutoCAD, like 2000 IIRC.
;;
;; If I recall correctly Luis Esquivel helped me refine this way
;; back in the day on the Autodesk forums. Others too have
;; contributed to the collective objectdbx knowledge, folks like
;; Tony Tanzillo, who was the first to publically share any info
;; about said topic and who we are all indebted to.
;;
;;==================================================================
(cond
( (vl-registry-read
(setq key
(strcat "HKEY_CLASSES_ROOT\\ObjectDBX.AxDbDocument"
(if (eq 15 (setq version (atoi (getvar "acadver"))))
"\\CLSID"
(strcat "." (itoa version) "\\CLSID")
)
)
)
)
)
( (and
(setq dll
(findfile
(strcat
"AxDb"
(itoa version)
".dll"
)
)
)
(_RegSvr32 dll)
)
(vl-registry-read key)
)
)
)
(defun _OdbxIsTempCopy ( document / result )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; Determine if the document is a temporary copy that had been made
;; via the _OdbxOpen function.
;;
;; See companion functions --
;;
;; _OdbxOpen
;;
;; _OdbxClose
;;
;;==================================================================
(vl-catch-all-apply
'(lambda ( )
(setq result
(not
(null
(vla-item
(vla-get-dictionaries document)
"OdbxTempCopy"
)
)
)
)
)
)
result
)
(defun _RegSvr32 ( dll / cmd path match )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; If possible register the specified dll.
;;
;; If I recall correctly Luis Esquivel helped me refine this way
;; back in the day on the Autodesk forums.
;;
;;==================================================================
(vl-some
'(lambda (x) (startapp x (strcat "\"" dll "\" /s")))
(list
(setq cmd "regsvr32.exe")
(strcat
(setq path (getenv "WinDir"))
"\\System\\"
cmd
)
(strcat path "\\System32\\" cmd)
)
)
(setq match
(strcat
(strcase (vl-filename-base dll))
"`.*"
)
)
(vl-some
'(lambda (x) (wcmatch (strcase x) match))
(vl-registry-descendents "HKEY_CLASSES_ROOT")
)
)
(defun _MakeTempFileCopy ( filename / result )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; If possible make a copy of the specified file in the temp
;; folder.
;;
;;==================================================================
(if
(and
(findfile filename)
(vl-file-copy
filename
(setq result
(vl-filename-mktemp
nil
nil
(vl-filename-base filename)
)
)
nil
)
)
result
)
)
(defun _Exists ( filename / handle )
;;==================================================================
;;
;; Written by Michael Puckett
;;
;;------------------------------------------------------------------
;;
;; Return t if the file exists. Note that this differs from
;; findfile function which will search the AutoCAD support path.
;; This function merely reports whether a file exists as expressed.
;;
;;==================================================================
(and
(setq handle (open filename "r"))
(null (close handle))
)
)