Module: TT::Model

Defined in:
TT_Lib2/model.rb

Overview

Collection of Model methods.

Since:

  • 2.0.0

Class Method Summary collapse

Class Method Details

.count_unique_entities(model, only_used_definitions = true, options = {}) ⇒ Integer

Counts all unique Sketchup::Entities collections in the given model, excluding Image definitions.

Parameters:

  • model (Sketchup::Model)
  • only_used_definitions (Boolean) (defaults to: true)
  • options (Hash) (defaults to: {})

Returns:

  • (Integer)

Since:

  • 2.5.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'TT_Lib2/model.rb', line 41

def self.count_unique_entities( model, only_used_definitions=true, options={} )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  if only_used_definitions
    model.definitions.reject { |d|
      d.image? ||
      d.count_instances == 0 ||
      (skip_locked && options[:locked].key?(d))
    }.length + 1
  else
    model.definitions.reject { |d|
      d.image? ||
      (skip_locked && options[:locked].key?(d))
    }.length + 1
  end
end

.count_unique_entity(model, only_used_definitions = true, options = {}) ⇒ Integer

Counts all unique entities in the given model, including sub-entities.

Parameters:

  • model (Sketchup::Model)
  • only_used_definitions (Boolean) (defaults to: true)
  • options (Hash) (defaults to: {})

Returns:

  • (Integer)

Since:

  • 2.5.0



66
67
68
69
70
71
72
73
74
75
76
# File 'TT_Lib2/model.rb', line 66

def self.count_unique_entity( model, only_used_definitions=true, options={} )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  c = model.entities.length
  for d in model.definitions
    next if d.image?
    next if only_used_definitions && d.count_instances == 0
    next if skip_locked && options[:locked].key?(d)
    c += d.entities.count
  end
  c
end

.each_entities(model, only_used_definitions = true, options = {}) {|entities| ... } ⇒ Integer

Yields each unique Entities collection recursivly.

TT::Model.each_entities { |entities|
  processEntities( entities )
}

If a number is returned to the processing block it will be used to add up a total when each_entities returns.

TT::Model.each_entities { |entities|
  c = 0
  for e in entities
    c += 1 if e.is_a?( SketchUp::Edge )
  end
  c
}

This example will return the total number of edges processed. Use to keep statistic for the iteration.

Parameters:

  • model (Sketchup::Model)
  • only_used_definitions (Boolean) (defaults to: true)
  • options (Hash) (defaults to: {})

Yields:

  • (entities)

Yield Parameters:

  • entities (Enumerable|Sketchup::Entities)

Returns:

  • (Integer)

    Returns

Since:

  • 2.5.0



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'TT_Lib2/model.rb', line 108

def self.each_entities( model, only_used_definitions=true, options={} )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  c = 0
  result = yield( model.entities )
  c += result if result.is_a?( Numeric )
  for d in model.definitions
    next if d.image?
    next if only_used_definitions && d.count_instances == 0
    next if skip_locked && options[:locked].key?(d)
    result += yield( d.entities )
    c += result if result.is_a?( Numeric )
  end
  c
end

.each_entity(model, only_used_definitions = true, options = {}) {|entity| ... } ⇒ Integer

Yields all the entities in the model. Note that it does not traverse the model hierarchy.

When only_used_definitions is true, only entities that is used in the model is yielded. When only_used_definitions is false, also the entiites in unused definitions are yielded.

Parameters:

  • model (Sketchup::Model)
  • only_used_definitions (Boolean) (defaults to: true)
  • options (Hash) (defaults to: {})

Yields:

  • (entity)

Yield Parameters:

  • entity (Sketchup::Entity)

Returns:

  • (Integer)

    Number of entities which returned non-false.

Since:

  • 2.5.0



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'TT_Lib2/model.rb', line 140

def self.each_entity( model, only_used_definitions=true, options={} )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  # c = c.next is faster than c += 1
  # http://forums.sketchucation.com/viewtopic.php?f=180&t=25305&p=305810#p305810
  c = 0
  for e in model.entities.to_a
    next if skip_locked && e.respond_to?( :locked? ) && e.locked?
    c = c.next if yield( e )
  end
  for d in model.definitions
    next if d.image?
    next if only_used_definitions && d.count_instances == 0
    next if skip_locked && options[:locked].key?(d)
    for e in d.entities.to_a
      next if skip_locked && e.respond_to?( :locked? ) && e.locked?
      c = c.next if yield( e )
    end
  end
  c
end

.start_operation(name, model = Sketchup.active_model) ⇒ Boolean

Version safe model.start_operation wrapper.

Parameters:

  • name (String)
  • model (Sketchup::Model) (defaults to: Sketchup.active_model)
    • defaults to active model.

Returns:

  • (Boolean)

Since:

  • 2.0.0



23
24
25
26
27
28
29
# File 'TT_Lib2/model.rb', line 23

def self.start_operation(name, model = Sketchup.active_model)
  if TT::SketchUp.version[0] >= 7
    model.start_operation(name, true)
  else
    model.start_operation(name)
  end
end