Class: Microstation::App

Inherits:
Object
  • Object
show all
Includes:
Wrap
Defined in:
lib/microstation/app.rb,
lib/microstation/scan/criteria.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Wrap

#wrap

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



73
74
75
76
77
78
79
80
# File 'lib/microstation/app.rb', line 73

def initialize(options = {})
  visible = options.fetch(:visible){ true }

  @ole_obj = WIN32OLE.new('MicrostationDGN.Application')
  @windows = Windows::FileSystem.new
  make_visible(visible)
  @scanners = []
end

Instance Attribute Details

#ole_objObject (readonly)

Returns the value of attribute ole_obj.



109
110
111
# File 'lib/microstation/app.rb', line 109

def ole_obj
  @ole_obj
end

#scannersObject (readonly)

Returns the value of attribute scanners.



71
72
73
# File 'lib/microstation/app.rb', line 71

def scanners
  @scanners
end

Class Method Details

.open_drawing(drawing, options = {}, &block) ⇒ Object



47
48
49
50
51
# File 'lib/microstation/app.rb', line 47

def self.open_drawing(drawing,options={},&block)
  self.run(options) do |app|
    app.open_drawing(drawing,options,&block)
  end
end

.run(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/microstation/app.rb', line 37

def self.run(options={})
  begin
    the_app = new(options)

    yield the_app
  ensure
    the_app.quit
  end
end

.with_drawings(*files, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/microstation/app.rb', line 53

def self.with_drawings(*files,&block)
  files = files[0] if files[0].kind_of?  Array
  opts = {:read_only => true}
  self.run do |app|
    files.each do |file|
      puts "opening #{file}.."
      app.open_drawing(file,opts) do |draw|
        block.call draw
      end

    end
  end
end

Instance Method Details

#active_design_fileObject



202
203
204
205
# File 'lib/microstation/app.rb', line 202

def active_design_file
  ole = @ole_obj.ActiveDesignFile rescue nil
  drawing_from_ole(ole) if ole
end

#active_file?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/microstation/app.rb', line 217

def active_file?
  !!active_design_file
end

#active_model_referenceObject



269
270
271
# File 'lib/microstation/app.rb', line 269

def active_model_reference
  @ole_obj.ActiveModelReference rescue nil
end

#active_workspaceObject



150
151
152
# File 'lib/microstation/app.rb', line 150

def active_workspace
  @ole_obj.ActiveWorkspace
end

#base_dirObject



94
95
96
# File 'lib/microstation/app.rb', line 94

def base_dir
  project_dir ? project_dir : Pathname.getwd
end

#cad_input_queueObject



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/microstation/app.rb', line 251

def cad_input_queue
  queue = init_cad_input_queue
  return queue unless block_given?
  begin
    yield queue
  rescue

  ensure
    queue.close
    queue = nil
  end
end

#can_open?(filename) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
267
# File 'lib/microstation/app.rb', line 264

def can_open?(filename)
  ext = File.extname(filename)
  (ext == ".dwg") || (ext == ".dgn")
end

#close_active_drawingObject

alias :current_drawing :active_design_file



213
214
215
# File 'lib/microstation/app.rb', line 213

def close_active_drawing
  active_design_file.close if active_design_file
end

#configurationObject



154
155
156
# File 'lib/microstation/app.rb', line 154

def configuration
  @config ||= Microstation::Configuration.new(self)
end

#create_ole_scan_criteriaObject



13
14
15
# File 'lib/microstation/scan/criteria.rb', line 13

def create_ole_scan_criteria
  ole_obj.CreateObjectInMicroStation("MicroStationDGN.ElementScanCriteria")
end

#create_scanner(&block) ⇒ Object



225
226
227
# File 'lib/microstation/app.rb', line 225

def create_scanner(&block)
  Microstation::Scan::Criteria.create_scanner(self,&block)
end

#current_drawingObject



207
208
209
# File 'lib/microstation/app.rb', line 207

def current_drawing
  active_design_file
end

#determine_seed(seedfile) ⇒ Object



187
188
189
190
# File 'lib/microstation/app.rb', line 187

def determine_seed(seedfile)
  return "seed2d" unless seedfile
  return windows_path( File.expand_path(seedfile))
end

#drawing_from_ole(ole) ⇒ Object



183
184
185
# File 'lib/microstation/app.rb', line 183

def drawing_from_ole(ole)
  Drawing.new(self,ole)
end

#eval_cexpression(string) ⇒ Object



192
193
194
# File 'lib/microstation/app.rb', line 192

def eval_cexpression(string)
  @ole_obj.GetCExpressionValue(string)
end

#file_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/microstation/app.rb', line 221

def file_exists?(file)
  File.file?( File.expand_path(file) )
end

#find_by_id(id) ⇒ Object



229
230
231
232
# File 'lib/microstation/app.rb', line 229

def find_by_id(id)
  model = active_model_reference.GetElementById64(id)
  wrap(model)
end

#load_constantsObject



67
68
69
# File 'lib/microstation/app.rb', line 67

def load_constants
  WIN32OLE.const_load(@ole_obj, MSD) unless MSD.constants.size > 0
end

#make_visible(visible) ⇒ Object



104
105
106
107
# File 'lib/microstation/app.rb', line 104

def make_visible(visible)
  @visible = visible
  @ole_obj.Visible = @visible
end

#new_drawing(filename, seedfile = nil, open = true, &block) ⇒ Object

seedfile A String expression. The name of the seed file. Should not include a path. The default extension is “.dgn”. Typical values are “seed2d” or “seed3d”. open If the open argument is True, CreateDesignFile returns the newly-opened DesignFile object; this is the same value as

ActiveDesignFile. If the Open argument is False, CreateDesignFile returns Nothing.


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/microstation/app.rb', line 167

def new_drawing(filename, seedfile=nil     ,open = true,&block)
  #drawing_name = normalize_name(filename)
  seedfile = determine_seed(seedfile)
  windows_name = windows_path(filename)
  ole = @ole_obj.CreateDesignFile(seedfile, windows_name, open)
  drawing = drawing_from_ole(ole)
  return drawing unless block_given?
  begin
    yield drawing
  rescue
  ensure
    drawing.close
  end

end

#normalize_name(name) ⇒ Object



98
99
100
101
102
# File 'lib/microstation/app.rb', line 98

def normalize_name(name)
  name = Pathname.new(name) unless name.kind_of? Pathname
  name = name.ext('.dgn')  unless name.extname.to_s == /\.(dgn|dwg)$/
  return (base_dir + name).expand_path
end

#open_drawing(filename, options = {}) ⇒ Object

Raises:

  • (FileNotFound)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/microstation/app.rb', line 129

def open_drawing(filename,options = {})
  filename = Pathname(filename)
  raise FileNotFound unless filename.file?
  readonly = options.fetch(:read_only){ false}
  ole = @ole_obj.OpenDesignFile(windows_path(filename), "ReadOnly" => readonly)
  drawing = drawing_from_ole(ole)
  return drawing unless block_given?
  begin
    yield drawing
  rescue
    puts "got error"
  ensure
    drawing.close
  end

end

#project_dirObject



86
87
88
# File 'lib/microstation/app.rb', line 86

def project_dir
  @project_dir
end

#project_dir=(dir) ⇒ Object



90
91
92
# File 'lib/microstation/app.rb', line 90

def project_dir=(dir)
  @project_dir = dir ? Pathname.new(dir) : dir
end

#quitObject



196
197
198
199
200
# File 'lib/microstation/app.rb', line 196

def quit
  active_design_file.close if active_file?
  @scanners.each{|sc| sc.close}
  @ole_obj.quit
end

#scan(criteria = nil) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/microstation/app.rb', line 234

def scan(criteria = nil)
  result = []
  raise 'NoActiveModel' unless active_model_reference
  if criteria
    criteria.resolve
    ee = active_model_reference.Scan(criteria.ole_obj)
  else
    ee = active_model_reference.Scan(nil)
  end
  scan_result = Microstation::Enumerator.new ee
  return scan_result.to_enum unless block_given?
  scan_result.each do |item|
    yield item
  end
  nil
end

#usernameObject



158
159
160
# File 'lib/microstation/app.rb', line 158

def username
  configuration["USERNAME"]
end

#visible?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/microstation/app.rb', line 82

def visible?
  @visible
end

#windows_path(path) ⇒ Object



146
147
148
# File 'lib/microstation/app.rb', line 146

def windows_path(path)
  @windows.windows_path(path)
end

#with_drawing(drawing) ⇒ Object

def method_missing(name,*args,&block)

@ole_obj.send(name,*args,&block)

end



115
116
117
118
119
120
121
# File 'lib/microstation/app.rb', line 115

def with_drawing(drawing)
  begin
    yield drawing
  ensure
    drawing.close
  end
end

#with_template(template) {|template| ... } ⇒ Object

Yields:

  • (template)


123
124
125
126
127
# File 'lib/microstation/app.rb', line 123

def with_template(template)
  template = Template.new(template,self)
  yield template
  template = nil
end