class Microstation::Drawing
Constants
- Extents
- IsHighlighted
- Origin
Attributes
Public Class Methods
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
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
@return [Boolean] is the drawing active?
# File lib/microstation/drawing.rb, line 50 def active? ole_obj.IsActive end
# 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
# File lib/microstation/drawing.rb, line 419 def add_element(line) active_model.add_element(line) end
@return [Pathname] the name as Pathname
# File lib/microstation/drawing.rb, line 243 def basename Pathname(name) end
@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
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
# File lib/microstation/drawing.rb, line 354 def change_text_suffix(reg,offset) active_model.change_text_suffix(reg,offset) end
# File lib/microstation/drawing.rb, line 265 def close @drawing_closed = true ole_obj.Close rescue nil @ole_obj =nil end
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
# 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
# File lib/microstation/drawing.rb, line 423 def create_scanner(name,&block) app.create_scanner(name,&block) end
@return [Model] the ole.DefaultModelReference
# File lib/microstation/drawing.rb, line 197 def default_model_reference Model.new(app,self,ole_obj.DefaultModelReference) end
alias_method :keywords , :keywords=x
# File lib/microstation/drawing.rb, line 219 def dimensions eval_cexpression("tcb->ndices") end
@return [Pathname] the directory of the file
# File lib/microstation/drawing.rb, line 248 def dirname Pathname(ole_obj.Path).expand_path end
# File lib/microstation/drawing.rb, line 271 def drawing_closed? @drawing_closed end
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
# File lib/microstation/drawing.rb, line 261 def eval_cexpression(string) app.eval_cexpression(string) end
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
# 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 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
# File lib/microstation/drawing.rb, line 350 def get_matching_text(re,&block) active_model.get_matching_text(re,&block) end
# File lib/microstation/drawing.rb, line 341 def get_selected_elements return [] unless has_active_model? active_model.get_selected_elements end
# File lib/microstation/drawing.rb, line 346 def get_selected_text active_model.get_selected_text end
# 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
@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
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
# File lib/microstation/drawing.rb, line 337 def models @models ||= each_model end
@return [Date] date last modified
# File lib/microstation/drawing.rb, line 214 def modified_date ole_obj.DateLastSaved end
@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
@return [String] the name of the drawing
# File lib/microstation/drawing.rb, line 238 def name ole_obj.Name end
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
# File lib/microstation/extensions/faa.rb, line 16 def number binding.pry Drawing::Number.from_string(self.basename) end
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
@return [Pathname] the complete path of file
# File lib/microstation/drawing.rb, line 253 def path dirname + basename end
# File lib/microstation/drawing.rb, line 281 def pdf_driver app.windows_path( (::Microstation.plot_driver_directory + "pdf-bw.plt").to_s) end
# File lib/microstation/drawing.rb, line 276 def pdf_name(name = nil) name = self.name unless name return Pathname(name).sub_ext(".pdf") end
# File lib/microstation/drawing.rb, line 285 def pen_table app.windows_path( (::Microstation.plot_driver_directory + 'wmbw.tbl')) end
@return [Boolean] is the drawing readonly?
# File lib/microstation/drawing.rb, line 124 def read_only? active_model_reference.IsReadOnly end
# File lib/microstation/drawing.rb, line 257 def revision_count ole_obj.DesignRevisionCount end
Save the drawing
@return [void]
# File lib/microstation/drawing.rb, line 415 def save ole_obj.Save end
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 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
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 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
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 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
# File lib/microstation/drawing.rb, line 190 def scan_text_in_cells(&block) scan_cells do |c| c.text_elements(&block) end end
@return [Boolean] true if a 3d drawing
# File lib/microstation/drawing.rb, line 233 def three_d? dimensions == 3 end
# File lib/microstation/drawing.rb, line 593 def to_ole_point3d(pt) app.to_ole_point3d(pt) end
# File lib/microstation/drawing.rb, line 614 def to_point(pt) app.to_point(pt) end
@return [Boolean] true if a 2d drawing
# File lib/microstation/drawing.rb, line 228 def two_d? dimensions == 2 end
# File lib/microstation/drawing.rb, line 552 def update_error raise ArgumentError, 'Argument must be an array of hashes' end
# 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
# 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
# File lib/microstation/drawing.rb, line 674 def app_ole_obj @app_ole_obj ||= app.ole_obj end
# 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
# 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
# File lib/microstation/drawing.rb, line 649 def model_from_ole(ole) Model.new(app,self,ole) end
# 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
# File lib/microstation/drawing.rb, line 682 def ole_classes @ole_classes ||= typelib.ole_classes end
# File lib/microstation/drawing.rb, line 670 def ole_element_klass @element_class ||= ole_classes.find{|c| c.name == '_Element'} end
# File lib/microstation/drawing.rb, line 666 def ole_line_element_klass @line_element ||= ole_classes.find{|c| c.name == '_LineElement'} end
# 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
# File lib/microstation/drawing.rb, line 629 def print_pdf(windows_path) cad_input_queue do |q| q << "Print Driver #{pdf_driver}" q << "Print Papername ANSI D" q << "Print BOUNDARY FIT ALL" q << "Print ATTRIBUTES BORDER OUTLINE OFF" q << "Print Attributes Fenceboundary Off" q << "Print pentable attach #{pen_table}" q << "Print colormode monochrome" q << "Print Execute #{windows_path}" end end
# File lib/microstation/drawing.rb, line 678 def typelib @typelib ||= app_ole_obj.ole_typelib end