Class: TT::SimpleTask

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/simpletask.rb

Overview

Wrapper class for enumerating a collection with UI feedback and optional operation wrapping.

task = TT::SimpleTask.new( 'HelloWorld', model.entities, true )
task.run { |entity|
  someWork( entity )
}

Since:

  • 2.5.0

Instance Method Summary collapse

Constructor Details

#initialize(task_name, collection, model = nil, decimals_places = 1) ⇒ SimpleTask

Returns a new instance of SimpleTask

Parameters:

  • task_name (String)
  • collection (Enumerable)
  • model (Nil||Sketchup::Model) (defaults to: nil)
  • decimals_places (Integer) (defaults to: 1)

Since:

  • 2.5.0



37
38
39
40
41
42
43
44
45
46
47
48
# File 'TT_Lib2/simpletask.rb', line 37

def initialize( task_name, collection, model=nil, decimals_places=1 )
  unless collection.is_a?( Enumerable )
    raise ArgumentError, 'collection argument must be Enumerable'
  end
  if model && !model.is_a?( Sketchup::Model )
    raise ArgumentError, 'model argument must be Sketchup::Model or nil'
  end
  @task_name = task_name
  @collection = collection
  @model = model
  @decimals_places = decimals_places
end

Instance Method Details

#runNil

Returns:

  • (Nil)

Since:

  • 2.5.0



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
# File 'TT_Lib2/simpletask.rb', line 52

def run
  if @model
    TT::Model.start_operation( @task_name, @model )
  end
  
  progress = TT::Progressbar.new( @collection, @task_name, @decimals_places )
  for item in @collection
    yield( item )
    progress.next
  end
  
  if @model
    @model.commit_operation
  end
  Sketchup.status_text = "#{@task_name} complete! (#{progress.elapsed_time(true)})"
  nil
rescue => e
  if @model
    @model.abort_operation
  end
  #raise e
  puts e.message
  puts e.backtrace.join("\n")
  #UI.messagebox("#{e.message}\n\n#{e.backtrace.join("\n")}", MB_MULTILINE)
end