class Microstation::Drawing

Constants

Extents
IsHighlighted
Origin

Attributes

app[R]

Public Class Methods

new(app, ole) click to toggle source

Initialize drawing

@param app [<Application>] the app instance @param ole [<WIN32OLIE>] the ole object returned from app.ole_obj

# File lib/microstation/drawing.rb, line 31
def initialize(app, ole)
  @app = app
  @ole_obj = ole
  @find_tagset_instances_called = false
  @app.register_handler('ISaveAsEvents_AfterSaveAs') do |*args|
    @drawing_saved = true
  end

end

Public Instance Methods

activate_model(name) click to toggle source

Active a model @param name [String] a

# File lib/microstation/drawing.rb, line 316
def activate_model(name)
  model = find_model(name)
  model.activate if model
end
active?() click to toggle source

@return [Boolean] is the drawing active?

# File lib/microstation/drawing.rb, line 50
def active?
  ole_obj.IsActive
end
active_model() click to toggle source
# File lib/microstation/drawing.rb, line 202
def active_model
  return nil unless has_active_model?
  Model.new(app,self, app_ole_obj.ActiveModelReference)
end
add_element(line) click to toggle source
# File lib/microstation/drawing.rb, line 419
def add_element(line)
  active_model.add_element(line)
end
basename() click to toggle source

@return [Pathname] the name as Pathname

# File lib/microstation/drawing.rb, line 243
def basename
  Pathname(name)
end
cad_input_queue(&block) click to toggle source

@yield [Microstation::CadInputQueue] yields a cad_input_queue to the block

# File lib/microstation/drawing.rb, line 56
def cad_input_queue(&block)
  @app.cad_input_queue(&block)
end
change_model(name) click to toggle source

activate the model with name param name [String] name the name of the model activate the model found and return the model @return [Model, nil]

# File lib/microstation/drawing.rb, line 362
def change_model(name)
  model = find_model(name)
  return unless model
  model.activate
end
Also aliased as: activate_model
change_text_suffix(reg,offset) click to toggle source
# File lib/microstation/drawing.rb, line 354
def change_text_suffix(reg,offset)
  active_model.change_text_suffix(reg,offset)
end
close() click to toggle source
# File lib/microstation/drawing.rb, line 265
def close
  @drawing_closed = true
  ole_obj.Close rescue nil
  @ole_obj =nil
end
copy(name: nil, dir: nil) click to toggle source

copy the drawing @param [String] name of the file @param [String,Pathname] dir

# File lib/microstation/drawing.rb, line 63
def copy(name: nil, dir: nil)
  if dir.nil?
    lname = name || copy_name
    dir_path = self.dirname
  else
    lname = name || self.name
    dir_path = Pathname(dir)
  end
  copy_path = dir_path + lname
  FileUtils.copy self.path.to_s, copy_path.to_s, verbose: true
end
create_line(p1,p2,el = nil) click to toggle source
# File lib/microstation/drawing.rb, line 597
def create_line(p1,p2,el = nil)
  #  el = find_line_element
  pt1,pt2 = [p1,p2].map{ |p| to_ole_point3d(p) }
  el = WIN32OLE_VARIANT::Nothing unless el
  el = el.ole_obj if el.respond_to? :ole_obj
  begin
    ole = app.ole_obj.CreateLineElement2(el, pt1,pt2)
    return nil unless ole
    app.wrap(ole)
  rescue Exception => ex
    puts ex.message bb
    puts "class: #{el.class}"
    return nil
  end 
end
create_scanner(name,&block) click to toggle source
# File lib/microstation/drawing.rb, line 423
def create_scanner(name,&block)
  app.create_scanner(name,&block)
end
create_tagset_instance_from_tagset_hash(h) click to toggle source
# File lib/microstation/drawing.rb, line 462
def create_tagset_instance_from_tagset_hash(h)
  ts = tagsets[h[:name]]
  ts.create_instance( h[:base_element_id], h[:tags], h[:model])
end
create_tagset_instances(ts_name: nil, base_element_id: nil) click to toggle source
# File lib/microstation/drawing.rb, line 444
def create_tagset_instances(ts_name: nil, base_element_id: nil)
  result = []
  @find_tagset_instances_called = true
  get_tagsets_in_drawing_hash(ts_name: ts_name, base_element_id: base_element_id) do |h|
    result << create_tagset_instance_from_tagset_hash(h)
  end
  result
end
create_tagset_instances_bak() click to toggle source

used

# File lib/microstation/drawing.rb, line 428
def create_tagset_instances_bak()
  result = []
  @find_tagset_instances_called = true
  filtered = if block_given?
                get_tagsets_in_drawing_hash.select(&filter) 
              else
                get_tagsets_in_drawing_hash
              end
  binding.pry
  filtered.map do |h|
    element_id = h[:base_element_id]
    result << tagset.create_instance(element_id, h[:tags], h[model])
  end
  result
end
default_model_reference() click to toggle source

@return [Model] the ole.DefaultModelReference

# File lib/microstation/drawing.rb, line 197
def default_model_reference
  Model.new(app,self,ole_obj.DefaultModelReference)
end
dimensions() click to toggle source

alias_method :keywords , :keywords=x

# File lib/microstation/drawing.rb, line 219
def dimensions
  eval_cexpression("tcb->ndices")
end
dirname() click to toggle source

@return [Pathname] the directory of the file

# File lib/microstation/drawing.rb, line 248
def dirname
  Pathname(ole_obj.Path).expand_path
end
drawing_closed?() click to toggle source
# File lib/microstation/drawing.rb, line 271
def drawing_closed?
  @drawing_closed
end
each_model() { |model| ... } click to toggle source

iterate through each model in the drawing and yield them @yield [Model] model

# File lib/microstation/drawing.rb, line 292
def each_model
  result = []
  ole_obj.Models.each do |el|
    model = model_from_ole(el)
    if block_given?
      yield model
    else
      result << model
    end
  end
  result unless block_given?
end
eval_cexpression(string) click to toggle source
# File lib/microstation/drawing.rb, line 261
def eval_cexpression(string)
  app.eval_cexpression(string)
end
find_by_id(id) click to toggle source

Iterates through all the models and finds the ole_el with id @param [String] -id @return Element

# File lib/microstation/drawing.rb, line 387
def find_by_id(id)
  models.each do |model|
    el = model.find_by_id(id)
    return el if el
  end
  nil
end
find_line_element() click to toggle source
# File lib/microstation/drawing.rb, line 585
def find_line_element
  line = nil
  self.scan_lines do |el|
    line = el if el.microstation_type == MSD::MsdElementTypeLine
  end
  line
end
find_model(name) click to toggle source

Find the model in the drawing @param [String] name - the name of the model @return [Model, nil] the model or nil if not found

# File lib/microstation/drawing.rb, line 324
def find_model(name)
  ole = ole_obj.Models(name)
  model_from_ole(ole)
rescue
  puts "model #{name} not found"
  nil
end
find_tagset_instance_by_name(name) click to toggle source
# File lib/microstation/drawing.rb, line 486
def find_tagset_instance_by_name(name)
  create_tagset_instances(ts_name: name).first
end
find_tagset_instance_by_name_and_id(name,id) click to toggle source
# File lib/microstation/drawing.rb, line 490
def find_tagset_instance_by_name_and_id(name,id)
  create_tagset_instances(ts_name: name, base_element_id: id).first
end
find_tagset_instance_from_hash(ti_array, h_local) click to toggle source
# File lib/microstation/drawing.rb, line 533
def find_tagset_instance_from_hash(ti_array, h_local)
  id = h_local.fetch('microstation_id', :not_found)
  if id == :not_found || id.nil?
    name = ti_array.first.name

    raise MultipleUpdateError, "found #{ti_array.size} instances for tagset #{name}; Need a microstation_id for hash"
  end
  ts = ti_array.find{|ti| ti.microstation_id == id}
end
find_tagset_instances_called?() click to toggle source
# File lib/microstation/drawing.rb, line 41
def find_tagset_instances_called?
  @find_tagset_instances_called
end
find_tagsets() { |ts| ... } click to toggle source
# File lib/microstation/drawing.rb, line 494
def find_tagsets
  results = []
  each_model do |m|
    m.find_tagsets.each do |ts|
      if block_given?
        yield ts
      else
        results << ts
      end
    end

  end
  return results unless block_given?
end
get_matching_text(re,&block) click to toggle source
# File lib/microstation/drawing.rb, line 350
def get_matching_text(re,&block)
  active_model.get_matching_text(re,&block)
end
get_selected_elements() click to toggle source
# File lib/microstation/drawing.rb, line 341
def get_selected_elements
  return [] unless has_active_model?
  active_model.get_selected_elements
end
get_selected_text() click to toggle source
# File lib/microstation/drawing.rb, line 346
def get_selected_text
  active_model.get_selected_text
end
get_tagsets_in_drawing_hash(ts_name: nil, base_element_id: nil) { |tsi| ... } click to toggle source

used

# File lib/microstation/drawing.rb, line 473
def get_tagsets_in_drawing_hash(ts_name: nil, base_element_id: nil)
  return to_enum(__callee__, ts_name: ts_name, base_element_id: base_element_id) unless block_given?
  each_model do |m|
    m.get_tagsets_in_model_hash(ts_name: ts_name, base_element_id: base_element_id) do |tsi|
      yield tsi
    end
  end
end
get_text() { |te| ... } click to toggle source
# File lib/microstation/drawing.rb, line 159
def get_text(&block)
  result = []
  scan_text do |te|
    if block_given?
      yield te.to_s
    else
      result << te.to_s
    end
  end
  result
end
has_active_model?() click to toggle source

@return [Boolean] true if drawing has an active model

# File lib/microstation/drawing.rb, line 208
def has_active_model?
  app_ole_obj.HasActiveModelReference
end
model_names() click to toggle source

Returns the model names

@return [Array<String>] the names of the models

# File lib/microstation/drawing.rb, line 374
def model_names
  result = []
  ole_obj.Models.each do |el|
    result << el.name
    el = nil
  end
  result
end
models() click to toggle source
# File lib/microstation/drawing.rb, line 337
def models
  @models ||= each_model
end
modified_date() click to toggle source

@return [Date] date last modified

# File lib/microstation/drawing.rb, line 214
def modified_date
  ole_obj.DateLastSaved
end
multiple_models?() click to toggle source

@return [Boolean] true if drawing has more than 1 model

# File lib/microstation/drawing.rb, line 333
def multiple_models?
  ole_obj.Models.Count > 1
end
name() click to toggle source

@return [String] the name of the drawing

# File lib/microstation/drawing.rb, line 238
def name
  ole_obj.Name
end
new_model(name,template = nil) click to toggle source

Create a new model @param [String] name - the name of the model @param [String] template - the template to use for the model

# File lib/microstation/drawing.rb, line 308
def new_model(name,template = nil)
  template_ole = template ? template.ole_obj : app.ole_obj.ActiveDesignFile.DefaultModelReference
  el = app.ole_obj.ActiveDesignFile.Models.Add(template_ole,name,"Added ")
  m = model_from_ole(el).activate
  m
end
number() click to toggle source
# File lib/microstation/extensions/faa.rb, line 16
def number
  binding.pry
  Drawing::Number.from_string(self.basename)
end
ole_obj() click to toggle source

returns the internal ole_obj

@return [WIN32OLE]

# File lib/microstation/drawing.rb, line 570
def ole_obj
  is_ok = true
  begin
    @ole_obj.Name
  rescue StandardError => e
    is_ok = false
  end

  unless is_ok || drawing_closed?
    binding.pry
  end

  @ole_obj
end
path() click to toggle source

@return [Pathname] the complete path of file

# File lib/microstation/drawing.rb, line 253
def path
  dirname + basename
end
pdf_driver() click to toggle source
# File lib/microstation/drawing.rb, line 281
def pdf_driver
  app.windows_path( (::Microstation.plot_driver_directory + "pdf-bw.plt").to_s)
end
pdf_name(name = nil) click to toggle source
# File lib/microstation/drawing.rb, line 276
def pdf_name(name = nil)
  name = self.name unless name
  return Pathname(name).sub_ext(".pdf")
end
pen_table() click to toggle source
# File lib/microstation/drawing.rb, line 285
def pen_table
  app.windows_path( (::Microstation.plot_driver_directory + 'wmbw.tbl'))
end
read_only?() click to toggle source

@return [Boolean] is the drawing readonly?

# File lib/microstation/drawing.rb, line 124
def read_only?
  active_model_reference.IsReadOnly
end
reset_tagset_instances() click to toggle source
# File lib/microstation/drawing.rb, line 45
def reset_tagset_instances
  @find_tagset_instances_called = false
end
revision_count() click to toggle source
# File lib/microstation/drawing.rb, line 257
def revision_count
  ole_obj.DesignRevisionCount
end
save() click to toggle source

Save the drawing

@return [void]

# File lib/microstation/drawing.rb, line 415
def save
  ole_obj.Save
end
save_as(name,overwrite: false, format: 0) click to toggle source

Save the drawing under a different name

@param [String,Pathname] name name to change to @param [Boolean] overwrite - whether to overwrite if file exists @param [<Type>] format <description>

@return [void]

# File lib/microstation/drawing.rb, line 101
def save_as(name,overwrite: false, format: 0)
  @drawing_saved = false
  path = Pathname(name).expand_path
  wpath = app.windows_path(name)
  begin
    ole_obj.SaveAs(wpath,overwrite, format)
    wait_save_event(10)
    raise "drawing not saved in 10 seconds" unless @drawing_saved
  rescue => e
    binding.pry
  end
end
save_as_pdf(name: nil , dir: nil) click to toggle source

save the drawing as a pdf file if the name or directory is given it uses those params. If not it uses the drawing name and the drawing directory @param name - the name of the file @param dir - the directory to save the drawing @return [void]

# File lib/microstation/drawing.rb, line 82
def save_as_pdf(name: nil , dir: nil)
  out_name = pdf_path(name: name, dir: dir)
  windows_name = app.windows_path(out_name)
  loop do
    print_pdf(windows_name)
    break if out_name.file?
  end
  puts "saved #{windows_name}"
end
save_tagsets_to_file(name) click to toggle source
# File lib/microstation/drawing.rb, line 467
def save_tagsets_to_file(name)
  require 'json'
  ::File.open(name, 'w'){ |f| f.write get_tagsets_in_drawing_hash.to_a.to_json }
end
scan_all(criteria) { |r| ... } click to toggle source

scans all the drawing models with criteria @param [Scan::Criteria] criteria

calls scan_all_with_block calls scal_all_with_hash

# File lib/microstation/drawing.rb, line 145
def scan_all(criteria,&block)
  return to_enum(__callee__, criteria) unless block_given?
  each_model do |m|
    m.scan_model(criteria) do |r|
      if block_given?
        yield r
      else
        result << r
      end
    end
    
  end
end
scan_cells(&block) click to toggle source

scan all cells in drawing and @yield [Cell] to the block

# File lib/microstation/drawing.rb, line 186
def scan_cells(&block)
  scan_all(cells_criteria,&block)
end
scan_model(criteria, model: default_model_reference, &block) click to toggle source

Scan the drawing. It takes a criteria and an optional model scan the drawing @param [Scan::Criteria] criteria - the criteria @param [Model] @criteria [Scan::Criteria] @yield

# File lib/microstation/drawing.rb, line 134
def scan_model(criteria, model: default_model_reference, &block)
  criteria = criteria || create_scan_criteria
  model = model || default_model_reference
  model.scan_model(criteria,&block)
end
scan_tags(&block) click to toggle source

scan all tags in drawing and @yield [Tag] to the block

# File lib/microstation/drawing.rb, line 180
def scan_tags(&block)
  scan_all(tags_criteria, &block)
end
scan_text(&block) click to toggle source

scan all text and text regions in all models @yield [String] text that is found

# File lib/microstation/drawing.rb, line 174
def scan_text(&block)
  scan_all(text_criteria,&block)
end
scan_text_in_cells(&block) click to toggle source
# File lib/microstation/drawing.rb, line 190
def scan_text_in_cells(&block)
  scan_cells do |c|
    c.text_elements(&block)
  end
end
select_tagset_instances_by_name(name) click to toggle source
# File lib/microstation/drawing.rb, line 482
def select_tagset_instances_by_name(name)
  create_tagset_instances(ts_name: name)
end
tagsets_in_drawing() click to toggle source
# File lib/microstation/drawing.rb, line 453
def tagsets_in_drawing
  @tagset_instances ||= create_tagset_instances
end
tagsets_in_drawing!() click to toggle source
# File lib/microstation/drawing.rb, line 457
def tagsets_in_drawing!
  @tagset_instances = nil
  tagsets_in_drawing
end
three_d?() click to toggle source

@return [Boolean] true if a 3d drawing

# File lib/microstation/drawing.rb, line 233
def three_d?
  dimensions == 3
end
to_ole_point3d(pt) click to toggle source
# File lib/microstation/drawing.rb, line 593
def to_ole_point3d(pt)
  app.to_ole_point3d(pt)
end
to_point(pt) click to toggle source
# File lib/microstation/drawing.rb, line 614
def to_point(pt)
  app.to_point(pt)
end
two_d?() click to toggle source

@return [Boolean] true if a 2d drawing

# File lib/microstation/drawing.rb, line 228
def two_d?
  dimensions == 2
end
update_error() click to toggle source
# File lib/microstation/drawing.rb, line 552
def update_error
  raise ArgumentError, 'Argument must be an array of hashes'
end
update_tagset(name,h_local={}) click to toggle source
# File lib/microstation/drawing.rb, line 510
   def update_tagset(name,h_local={})
  tset_instances = select_tagset_instances_by_name(name)
  case tset_instances.size
  when 0
    raise 'no tagset found'
  when 1
    ts = tset_instances.first
    ts.update(h_local)
  else
    if (h_local.class == Array && h_local.all?{|l| l.class == Hash})
      h_local.each do |update_hash|
        ts = find_tagset_instance_from_hash(tset_instances, update_hash)
        ts.update(update_hash)
      end
    elsif h_local.class == Hash
      ts = find_tagset_instance_from_hash(tset_instances, h_local)
      ts.update(h_local)
    else
      raise ArgumentError, "don't know how to update tagset"
    end
  end
end
update_tagsets(ts_arg) click to toggle source
# File lib/microstation/drawing.rb, line 543
def update_tagsets(ts_arg)
  return if ts_arg == []
  return if ts_arg == {}
  ts_array = [ts_arg] if ts_arg.class == Hash
  ts_array.each do |hash_pair|
    update_tagset(hash_pair.keys[0], hash_pair.values[0])
  end
end
update_tagsets_from_template_structure(ts_arg) click to toggle source
# File lib/microstation/drawing.rb, line 556
def update_tagsets_from_template_structure(ts_arg)
  ts_arg.each do |h|
    inst_array = h['instances']
    inst_array.each do |ts_hash|
      update_tagset(ts_hash['tagset_name'], ts_hash['attributes'])
    end
  end
end
wait_save_event(secs, interval = 0.5) click to toggle source
# File lib/microstation/drawing.rb, line 114
def wait_save_event(secs, interval = 0.5)
  elapsed = 0
  while(!@drawing_saved && elapsed <= 5)
    elapsed += interval
    sleep(interval)
    WIN32OLE_EVENT.message_loop
  end
end
zoom_to_element(target, n_view) click to toggle source
# File lib/microstation/drawing.rb, line 395
def zoom_to_element(target, n_view)
  return nil unless target.graphical?
  zoom = 4
  range = target.Range
  oview = app_ole_obj.ActiveDesignFile.Views.Item(n_view)
  range_diff = app_ole_obj.Point3dSubtract(range.High, range.Low)
  extent = app_ole_obj.Point3dScale(range_diff, zoom)
  
  oview_origin = app_ole_obj.Point3dSubtract(range.Low, app_ole_obj.Point3dScale(extent,0.5))
  oview.Origin = oview_origin
  oview.Extents = extent
  oview.Redraw
  target.IsHighlighted = true
end

Protected Instance Methods

app_ole_obj() click to toggle source
# File lib/microstation/drawing.rb, line 674
def app_ole_obj
  @app_ole_obj ||= app.ole_obj
end
copy_name(backup_str = '.copy') click to toggle source
# File lib/microstation/drawing.rb, line 653
def copy_name(backup_str = '.copy')
  lname = self.name.dup
  ext = File.extname(lname)
  name = "#{File.basename(lname, ext)}#{backup_str}#{ext}"
end
get_model_for_scan(model = nil) click to toggle source
# File lib/microstation/drawing.rb, line 643
def get_model_for_scan(model = nil)
  model = find_model(model) if model.is_a? String
  model ||= active_model || default_model_reference
  model
end
model_from_ole(ole) click to toggle source
# File lib/microstation/drawing.rb, line 649
def model_from_ole(ole)
  Model.new(app,self,ole)
end
normalize_update_hash(h) click to toggle source
# File lib/microstation/drawing.rb, line 621
def normalize_update_hash(h)
  h = h.to_h if h.respond_to?(:to_h)
  if h.kind_of? Hash
    h = h.map_keys{|k| k.to_s}
  end
  h
end
ole_classes() click to toggle source
# File lib/microstation/drawing.rb, line 682
def ole_classes
  @ole_classes ||= typelib.ole_classes
end
ole_element_klass() click to toggle source
# File lib/microstation/drawing.rb, line 670
def ole_element_klass
  @element_class ||= ole_classes.find{|c| c.name == '_Element'}
end
ole_line_element_klass() click to toggle source
# File lib/microstation/drawing.rb, line 666
def ole_line_element_klass
  @line_element ||= ole_classes.find{|c| c.name == '_LineElement'}
end
pdf_path(name: nil, dir: nil) click to toggle source
# File lib/microstation/drawing.rb, line 659
def pdf_path(name: nil, dir: nil)
  name = name || self.name
  dir = Pathname(dir || self.dirname).expand_path
  dir.mkpath unless dir.directory?
  dir + pdf_name(name)
end
print_pdf(windows_path) click to toggle source
typelib() click to toggle source
# File lib/microstation/drawing.rb, line 678
def typelib
  @typelib  ||= app_ole_obj.ole_typelib
end