Module: TT::Entities

Defined in:
TT_Lib2/entities.rb

Overview

Collection of Entities methods.

Since:

  • 2.0.0

Class Method Summary collapse

Class Method Details

.bounds(entities) ⇒ Geom::BoundingBox

Returns a boundingbox for all the given entities.

Parameters:

  • entities (Enumerable)

Returns:

  • (Geom::BoundingBox)

Since:

  • 2.1.0



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

def self.bounds(entities)
  bb = Geom::BoundingBox.new
  for e in entities
    next unless e.respond_to?(:bounds)
    bb.add( e.bounds )
  end
  return bb
end

.count_unique_entities(context, options = {}) ⇒ Integer

Counts all unique Sketchup::Entities collections in the given context, including sub-entities.

Parameters:

  • context (Enumerable)
  • options (Hash) (defaults to: {})

Returns:

  • (Integer)

Since:

  • 2.5.0



41
42
43
44
45
46
# File 'TT_Lib2/entities.rb', line 41

def self.count_unique_entities( context, options={} )
  c = 0
  entities = nil # Init variables for speed
  self.each_entities( context, options={} ) { |entities| c = c.next  }
  c
end

.count_unique_entity(context, options = {}) ⇒ Integer

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

Parameters:

  • context (Enumerable)
  • options (Hash) (defaults to: {})

Returns:

  • (Integer)

Since:

  • 2.5.0



56
57
58
59
60
61
62
63
# File 'TT_Lib2/entities.rb', line 56

def self.count_unique_entity( context, options={} )
  c = context.length
  entities = nil # Init variables for speed
  self.each_entities( context, options={} ) { |entities|
    c += entities.length
  }
  c
end

.each_entities(context, processed_definitions = {}, options = {}) {|entities| ... } ⇒ Integer

Yields each unique Entities collection recursivly.

TT::Entities.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::Entities.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:

  • context (Enumerable)
  • processed_definitions (Hash) (defaults to: {})

    Hash index of processed entities.

  • options (Hash) (defaults to: {})

Yields:

  • (entities)

Yield Parameters:

  • entities (Enumerable|Sketchup::Entities)

Returns:

  • (Integer)

    Returns

Since:

  • 2.5.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'TT_Lib2/entities.rb', line 95

def self.each_entities( context, processed_definitions={}, options={}, &block )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  c = 0
  result = yield( context )
  c += result if result.is_a?( Numeric )
  # Process Groups and ComponentInstances
  for e in context.to_a
    next unless e.valid? && TT::Instance.is?( e )
    d = TT::Instance.definition( e )
    if processed_definitions[d].nil?
      processed_definitions[d] = true
      next if skip_locked && options[:locked].key?(d)
      result = self.each_entities( d.entities, processed_definitions, options, &block )
      c += result if result.is_a?( Numeric )
    end
  end
  c
end

.each_entity(entities, processed_definitions = {}, options = {}, &block) ⇒ Object

Yields each entity recursivly.

Since:

  • 2.0.0



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'TT_Lib2/entities.rb', line 116

def self.each_entity( entities, processed_definitions={}, options={}, &block )
  skip_locked = options[:locked] && options[:locked].is_a?( Hash )
  c = 0
  for e in entities.to_a
    next if skip_locked && e.respond_to?( :locked? ) && e.locked?
    c = c.next if yield( e )
    # Process Groups and ComponentInstances
    next unless e.valid? && TT::Instance.is?( e )
    d = TT::Instance.definition( e )
    if processed_definitions[d].nil?
      processed_definitions[d] = true
      next if skip_locked && options[:locked].key?(d)
      c += self.each_entity( d.entities, processed_definitions, options, &block )
    end
  end # for
  c
end

.positions(entities, parent_transformation = Geom::Transformation.new) ⇒ Array<Geom::Point3d>

Collects the 3d positions of the vertices in the given entities collection. Processes child Groups and Components recursivly.

Parameters:

  • entities (Enumerable)
  • parent_transformation (Geom::Transformation) (defaults to: Geom::Transformation.new)

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.0.0



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

def self.positions(entities, parent_transformation = Geom::Transformation.new)
  pts = []
  vertices = []
  for e in entities
    if e.respond_to?(:vertices)
      vertices << e.vertices
    elsif TT::Instance.is?( e )
      d = TT::Instance.definition(e)
      t = parent_transformation * e.transformation
      sub_pts = self.positions(d.entities, t)
      pts.concat( sub_pts )
    end
  end # for
  vertices.flatten!
  vertices.uniq!
  pts.concat( vertices.map { |v| v.position.transform( parent_transformation ) } )
  pts
end