autocad 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop/minitest.yml +2 -0
  3. data/.rubocop/strict.yml +4 -0
  4. data/.rubocop.yml +33 -0
  5. data/.solargraph.yml +22 -0
  6. data/.vscode/launch.json +30 -0
  7. data/CHANGELOG.md +5 -0
  8. data/CODE_OF_CONDUCT.md +132 -0
  9. data/Guardfile +66 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +39 -0
  12. data/Rakefile +10 -0
  13. data/Steepfile +35 -0
  14. data/WASD-D-TDS-C001.dwg +0 -0
  15. data/WASD-D-TDS-C001.pdf +0 -0
  16. data/autocad.log +1 -0
  17. data/autocad_example.rb +26 -0
  18. data/event_handler.log +24 -0
  19. data/examples/example_save_pdf.rb +5 -0
  20. data/exe/autocad +3 -0
  21. data/exe/dgn2pdf +50 -0
  22. data/exe/pw_print +43 -0
  23. data/gemfiles/rubocop.gemfile +6 -0
  24. data/lib/autocad/app.rb +655 -0
  25. data/lib/autocad/arc.rb +29 -0
  26. data/lib/autocad/block.rb +141 -0
  27. data/lib/autocad/block_reference.rb +198 -0
  28. data/lib/autocad/drawing.rb +426 -0
  29. data/lib/autocad/element.rb +454 -0
  30. data/lib/autocad/enumerator.rb +24 -0
  31. data/lib/autocad/errors.rb +37 -0
  32. data/lib/autocad/event_handler.rb +30 -0
  33. data/lib/autocad/filter.rb +168 -0
  34. data/lib/autocad/layer.rb +41 -0
  35. data/lib/autocad/line.rb +55 -0
  36. data/lib/autocad/message_box.rb +95 -0
  37. data/lib/autocad/model.rb +89 -0
  38. data/lib/autocad/mtext.rb +110 -0
  39. data/lib/autocad/paths.rb +29 -0
  40. data/lib/autocad/point3d.rb +143 -0
  41. data/lib/autocad/pviewport.rb +21 -0
  42. data/lib/autocad/selection_filter.rb +180 -0
  43. data/lib/autocad/selection_set.rb +61 -0
  44. data/lib/autocad/selection_set_adapter.rb +146 -0
  45. data/lib/autocad/text.rb +74 -0
  46. data/lib/autocad/version.rb +5 -0
  47. data/lib/autocad.rb +161 -0
  48. data/olegen.rb +341 -0
  49. data/rbs_collection.lock.yaml +188 -0
  50. data/rbs_collection.yaml +19 -0
  51. data/scanner.obj +0 -0
  52. data/sig/generated/autocad/app.rbs +251 -0
  53. data/sig/generated/autocad/arc.rbs +17 -0
  54. data/sig/generated/autocad/block.rbs +63 -0
  55. data/sig/generated/autocad/block_reference.rbs +59 -0
  56. data/sig/generated/autocad/drawing.rbs +158 -0
  57. data/sig/generated/autocad/element.rbs +166 -0
  58. data/sig/generated/autocad/enumerator.rbs +15 -0
  59. data/sig/generated/autocad/errors.rbs +23 -0
  60. data/sig/generated/autocad/event_handler.rbs +14 -0
  61. data/sig/generated/autocad/filter.rbs +60 -0
  62. data/sig/generated/autocad/layer.rbs +19 -0
  63. data/sig/generated/autocad/line.rbs +25 -0
  64. data/sig/generated/autocad/message_box.rbs +81 -0
  65. data/sig/generated/autocad/model.rbs +41 -0
  66. data/sig/generated/autocad/paths.rbs +19 -0
  67. data/sig/generated/autocad/point3d.rbs +66 -0
  68. data/sig/generated/autocad/selection_filter.rbs +50 -0
  69. data/sig/generated/autocad/selection_set.rbs +37 -0
  70. data/sig/generated/autocad/selection_set_adapter.rbs +28 -0
  71. data/sig/generated/autocad/text.rbs +19 -0
  72. data/sig/generated/autocad/text_node.rbs +37 -0
  73. data/sig/generated/autocad/version.rbs +5 -0
  74. data/sig/generated/autocad/viewport.rbs +11 -0
  75. data/sig/generated/autocad.rbs +68 -0
  76. data/sig/prototype/lib/autocad/app.rbs +34 -0
  77. data/sig/prototype/lib/autocad/block.rbs +5 -0
  78. data/sig/prototype/lib/autocad/block_reference.rbs +5 -0
  79. data/sig/prototype/lib/autocad/drawing.rbs +13 -0
  80. data/sig/prototype/lib/autocad/element.rbs +9 -0
  81. data/sig/prototype/lib/autocad/enumerator.rbs +5 -0
  82. data/sig/prototype/lib/autocad/event_handler.rbs +7 -0
  83. data/sig/prototype/lib/autocad/model.rbs +5 -0
  84. data/sig/prototype/lib/autocad/paths.rbs +5 -0
  85. data/sig/prototype/lib/autocad.rbs +3 -0
  86. data/temp/autocad.dwg +0 -0
  87. data/temp/autocad.log +1 -0
  88. data/temp/event_handler.log +0 -0
  89. metadata +147 -0
@@ -0,0 +1,426 @@
1
+ # rbs_inline: enabled
2
+
3
+ require_relative "event_handler"
4
+ require_relative "enumerator"
5
+ require_relative "model"
6
+ require_relative "selection_set_adapter"
7
+ require "debug"
8
+
9
+ module Autocad
10
+ class Drawing
11
+ include Common
12
+ attr_reader :app
13
+
14
+ def self.from_ole_obj(app, ole) #: Drawing
15
+ new(app, ole)
16
+ end
17
+
18
+ def set_system_variables(names, values)
19
+ atts = names.zip(values).to_h
20
+ atts.each do |k, v|
21
+ ole_obj.SetVariable(k, v)
22
+ end
23
+ end
24
+
25
+ def faa_title_block
26
+ block_reference_enum.find { |b| b.name == "faatitle" }
27
+ end
28
+
29
+ def block_reference_enum
30
+ ss = block_reference_selection_set
31
+ ss.clear
32
+ ss.select
33
+ ss.each
34
+ end
35
+
36
+ def block_reference_selection_set
37
+ @block_reference_selection_set ||= get_block_reference_selection_set
38
+ end
39
+
40
+ def get_block_reference_selection_set
41
+ ss = get_selection_set("block_reference")
42
+ ss ||= create_selection_set("block_reference") do |ss|
43
+ ss.filter do |f|
44
+ f.block_reference
45
+ end
46
+ end
47
+ ss
48
+ end
49
+
50
+ def with_system_variables(names, values, &block)
51
+ atts
52
+ current_values = get_system_variables(names)
53
+ set_system_variables(names, values)
54
+ yield
55
+ ensure
56
+ set_system_variables(names, current_values)
57
+ end
58
+
59
+ def get_system_variables(*atts)
60
+ return [] if atts.empty?
61
+ if atts.first.class == Array
62
+ atts = atts.first
63
+ end
64
+ atts.each_with_object([]) do |k, a|
65
+ a << ole_obj.GetVariable(k)
66
+ a
67
+ end
68
+ end
69
+
70
+ def initialize(app, ole)
71
+ @app = app
72
+ @ole_obj = ole
73
+ @app_event = WIN32OLE_EVENT.new(ole)
74
+ end
75
+
76
+ def event_handler #: EventHandler
77
+ @event_handler ||= default_event_handler
78
+ end
79
+
80
+ def default_event_handler
81
+ handler = EventHandler.new
82
+ @app_event.handler = handler
83
+ handler
84
+ end
85
+
86
+ def app_ole
87
+ app.ole_obj
88
+ end
89
+
90
+ # @rbs return SelectionSetAdapter
91
+ def create_selection_set(name)
92
+ ss = SelectionSet.new(name)
93
+ yield ss if block_given?
94
+ SelectionSetAdapter.new(self, ss)
95
+ end
96
+
97
+ # register an handler
98
+ #
99
+ # @rbs event: String -- event key for handler
100
+ # @rbs &: {() -> void} - handler Proc
101
+ def register_handler(event, &) #:void
102
+ @event_handler.add_handler(event, &) unless event == "OnQuit"
103
+ end
104
+
105
+ def read_only?
106
+ ole_obj.ReadOnly
107
+ end
108
+
109
+ def previously_saved?
110
+ ole_obj.FullFileName != ""
111
+ end
112
+
113
+ def modified?
114
+ ole_obj.Saved == false
115
+ end
116
+
117
+ def save(name: nil, dir: nil) #: void
118
+ return if read_only?
119
+ if previously_saved? && modified?
120
+ ole_obj.Save
121
+ else
122
+ out_name = dwg_path(name: name, dir: dir)
123
+ windows_name = app.windows_path(out_name)
124
+ ole_obj.SaveAs(windows_name)
125
+ end
126
+ puts "saved #{windows_name}"
127
+ end
128
+
129
+ def dwg_path(name: nil, dir: nil)
130
+ name ||= self.name
131
+ dir = Pathname.new(dir || dirname).expand_path
132
+ dir.mkpath unless dir.directory?
133
+ dir + dwg_name(name)
134
+ end
135
+
136
+ def dwg_name(name)
137
+ Pathname.new(name).sub_ext(".dwg")
138
+ end
139
+
140
+ # save the drawing as a pdf file
141
+ # if the name or directory is given it uses
142
+ # those params. If not it uses the drawing name
143
+ # and the drawing directory
144
+ # @rbs name: String? - the name of the file
145
+ # @rbs dir: String? - the directory to save the drawing
146
+ def save_as_pdf(name: nil, dir: nil, model: false) #: void
147
+ out_name = pdf_path(name: name, dir: dir)
148
+ windows_name = app.windows_path(out_name)
149
+ loop do
150
+ print_pdf(windows_name, model:)
151
+ break if out_name.file?
152
+ end
153
+ puts "saved #{windows_name}"
154
+ end
155
+
156
+ def pdf_path(name: nil, dir: nil)
157
+ name ||= self.name
158
+ dir = Pathname.new(dir || dirname).expand_path
159
+ dir.mkpath unless dir.directory?
160
+ dir + pdf_name(name)
161
+ end
162
+
163
+ # Return the pdf name for the drawing.
164
+ #
165
+ # If a name is provided use the name provided otherwise use the drawing name
166
+ #
167
+ # @rbs name: String | nil -- change ext to pdf and return Pathname from
168
+ # the name or drawing name
169
+ def pdf_name(name = nil) #: Pathname
170
+ name ||= self.name
171
+ Pathname.new(name).sub_ext(".pdf")
172
+ end
173
+
174
+ # copy the drawing
175
+ # @rbs name: String | Pathname --name of the file
176
+ # @rbs dir: String|Pathname -- dir
177
+ def copy(name: nil, dir: nil) #: void
178
+ if dir.nil?
179
+ lname = name || copy_name
180
+ dir_path = dirname
181
+ else
182
+ lname = name || self.name
183
+ dir_path = Pathname.new(dir)
184
+ end
185
+ copy_path = dir_path + lname
186
+ FileUtils.copy path.to_s, copy_path.to_s, verbose: true
187
+ end
188
+
189
+ # If you copy the file the name to use
190
+ # @rbs backup_str: String -- the bqckup string to use for copies
191
+ def copy_name(backup_str = ".copy")
192
+ lname = name.dup
193
+ ext = File.extname(lname)
194
+ "#{File.basename(lname, ext)}#{backup_str}#{ext}"
195
+ end
196
+
197
+ def paper_space? #: bool
198
+ ole_obj.ActiveSpace == ACAD::AcPaperSpace
199
+ end
200
+
201
+ def model_space? #: bool
202
+ ole_obj.ActiveSpace == ACAD::AcModelSpace
203
+ end
204
+
205
+ def to_paper_space #: void
206
+ ole_obj.ActiveSpace = ACAD::AcPaperSpace
207
+ end
208
+
209
+ def to_model_space #: void
210
+ ole_obj.ActiveSpace = ACAD::AcModelSpace
211
+ end
212
+
213
+ # @rbs return String -- the name of the drawing
214
+ def name
215
+ ole_obj.Name
216
+ end
217
+
218
+ # @rbs return Pathname -- the name as Pathname
219
+ def basename
220
+ Pathname.new(name)
221
+ end
222
+
223
+ # @rbs return Pathname -- the directory of the file
224
+ def dirname
225
+ Pathname.new(ole_obj.Path).expand_path
226
+ end
227
+
228
+ # @rbs return Pathname -- the complete path of file
229
+ def path
230
+ dirname + basename
231
+ end
232
+
233
+ def print_pdf(print_path, model: false)
234
+ if model
235
+ to_model_space
236
+ else
237
+ to_paper_space
238
+ end
239
+ raise "no plot config" unless pdf_plot_config
240
+ ole_obj.Plot.PlotToFile print_path, pdf_plot_config
241
+ end
242
+
243
+ def pdf_plot_config
244
+ app.plot_configs.find { |p| p =~ /faa.+high/i }
245
+ end
246
+
247
+ def active_layer
248
+ ole_obj.ActiveLayer
249
+ end
250
+
251
+ def active_space
252
+ ole = ole_obj.ActiveSpace
253
+ if ole == ACAD::AcPaperSpace
254
+ PaperSpace.new(ole_obj, app)
255
+ else
256
+ ModelSpace.new(ole_obj, app)
257
+ end
258
+ end
259
+
260
+ # Close the drawing
261
+ # @rbs save: bool -- whether to save the drawing
262
+ def close(save = true)
263
+ @drawing_closed = true
264
+ begin
265
+ ole_obj.Close(save)
266
+ rescue
267
+ nil
268
+ end
269
+ @ole_obj = nil
270
+ end
271
+
272
+ # @rbs name: String -- selection set name to return
273
+ # @rbs return SelectionSet | nil
274
+ def get_selection_set(name)
275
+ ole = get_ole_selection_set(name)
276
+ app.wrap(ole) if ole
277
+ end
278
+
279
+ # @rbs name: String -- selection set name to return
280
+ # @rbs return SelectionSet | nil
281
+ def get_ole_selection_set(name)
282
+ return nil if ole_selection_sets.Count == 0
283
+ begin
284
+ ole_selection_sets.Item(name)
285
+ rescue
286
+ nil
287
+ end
288
+ end
289
+
290
+ def ole_selection_sets
291
+ ole_obj.SelectionSets
292
+ end
293
+
294
+ # @rbs return Enumerator[Block]
295
+ # @rbs &: (Block) -> void
296
+ def blocks
297
+ return to_enum(__callee__) unless block_given?
298
+ ole_obj.Blocks.each { |o| yield app.wrap(o) }
299
+ end
300
+ # return the layers for the drawing
301
+
302
+ # @rbs return Enumerator[Layer]
303
+ # @rbs &: (Layer) -> void
304
+ def layers
305
+ return to_enum(__callee__) unless block_given?
306
+
307
+ ole_obj.Layers.each { |o| yield app.wrap(o) }
308
+ end
309
+
310
+ def linetypes
311
+ return to_enum(__callee__) unless block_given?
312
+ ole_obj.Linetypes.each { |o| yield app.wrap(o) }
313
+ end
314
+
315
+ # @rbs name: String -- layer name to create
316
+ # @rbs return Acad::Layer
317
+ def create_layer(name, color: nil)
318
+ ole_layer = begin
319
+ ole_obj.Layers.Item(name)
320
+ rescue
321
+ nil
322
+ end
323
+ ole_layer ||= ole_obj.Layers.Add(name)
324
+ ole_layer.Color = color if color
325
+ app.wrap(ole_layer)
326
+ end
327
+
328
+ # @rbs name: String -- the name to call new selection set
329
+ # @rbs return Autocad::SelectionSet | nil
330
+ # def create_selection_set(name, filter: nil)
331
+ # ss = get_ole_selection_set(name)
332
+ # ss.Delete if ss
333
+ # ss = ole_selection_sets.Add(name)
334
+ # app.wrap(ss)
335
+ # rescue WIN32OLE::RuntimeError
336
+ # nil
337
+ # end
338
+
339
+ # @rbs message: String -- the String to put in Autocad prompt
340
+ def prompt(message)
341
+ utility.Prompt(message)
342
+ end
343
+
344
+ # @rbs prompt: String -- the string to prompt the user for String
345
+ # @rbs has_spaces: bool -- whether the string returned can contain spaces
346
+ def get_input_string(prompt: "Enter a string", spaces: true)
347
+ utility.GetString(spaces, prompt)
348
+ rescue => ex
349
+ raise Autocad::Error.new("Error getting string input from user #{ex}")
350
+ end
351
+
352
+ # @rbs prompt: String -- the string to prompt the user for Integer
353
+ def get_input_integer(prompt: "Enter a integer")
354
+ utility.GetInteger(prompt)
355
+ rescue => ex
356
+ raise Autocad::Error.new("Error getting integer input from user #{ex}")
357
+ end
358
+
359
+ def get_float(prompt: "Enter a float")
360
+ utility.GetReal(prompt)
361
+ rescue => ex
362
+ raise Autocad::Error.new("Error getting float input from user #{ex}")
363
+ end
364
+
365
+ # In a running Autocad instance, prompts the user for a point.
366
+ # Uses the prompt argument as the prompt string.
367
+ # If base_point is provided, it is used as the base point and a
368
+ # stretched line is drawn from the base point to the returned point.
369
+ # @rbs prompt: String
370
+ # @rbs base_point: Array, Point3d, nil
371
+ # @rbs return [Point3d]
372
+ def get_point(prompt: "Get point", base_point: nil)
373
+ if base_point
374
+ array_pt = base_point.to_ary.map { |x| x.to_f } unless base_point.nil?
375
+ base_point = WIN32OLE_VARIANT.array([3], WIN32OLE::VARIANT::VT_R8)
376
+ base_point[0] = array_pt[0]
377
+ base_point[1] = array_pt[1]
378
+ base_point[2] = array_pt[2]
379
+ end
380
+ pt = utility.GetPoint(base_point, prompt)
381
+ Point3d.new(pt[0], pt[1], pt[2])
382
+ rescue => ex
383
+ raise Autocad::Error.new("Error getting point input from user #{ex}")
384
+ end
385
+
386
+ def get_region
387
+ pt = get_point(prompt: "Specify first corner")
388
+ prompt("X: #{pt.x}, Y: #{pt.y}, Z: #{pt.z}\n")
389
+ pt2 = utility.ole_obj.GetCorner(pt.to_ole, "Specify opposite corner: ")
390
+ [pt, pt2].map { |p| Point3d.new(p[0], p[1], p[2]) }
391
+ end
392
+
393
+ # @rbs return Enumerator[SelectionSet] | void
394
+ # @rbs &: (SelectionSet) -> void
395
+ def selection_sets
396
+ return to_enum(__callee__) unless block_given?
397
+ ole_selection_sets.each { |o| yield app.wrap(o) }
398
+ end
399
+
400
+ def model_space
401
+ ModelSpace.new(ole_obj.ModelSpace, app)
402
+ end
403
+
404
+ def paper_space
405
+ PaperSpace.new(ole_obj.PaperSpace, app)
406
+ end
407
+
408
+ alias_method :model, :model_space
409
+ alias_method :paper, :paper_space
410
+
411
+ def utility
412
+ ole_obj.Utility
413
+ end
414
+
415
+ def ole_obj
416
+ is_ok = true
417
+ begin
418
+ @ole_obj.Name
419
+ rescue
420
+ is_ok = false
421
+ end
422
+ binding.break unless is_ok
423
+ @ole_obj
424
+ end
425
+ end
426
+ end