model = Sketchup.active_model
group_name_pattern = /^[1-9][0-9]?([А-Яа-яA-Za-z]+)[1-9][0-9]?(\s.*)?$/
group_lengths = Hash.new(0.0)
matching_groups = Hash.new { |hash, key| hash[key] = [] }
second_part_lengths = Hash.new(0.0)
scale = 0.0254
def process_group(group, group_name_pattern, group_lengths, matching_groups, second_part_lengths, scale)
group_name = group.name
match = group_name.match(group_name_pattern)
if match
letter = match[1]
edges = group.entities.grep(Sketchup::Edge)
total_length_mm = edges.reduce(0.0) { |sum, edge| sum + edge.length * scale }
group_lengths[letter] += total_length_mm
matching_groups[letter] << group
second_part = match[2].to_s.strip
if !second_part.empty?
second_part_lengths[second_part] += total_length_mm
end
end
group.entities.each do |entity|
if entity.is_a?(Sketchup::Group)
process_group(entity, group_name_pattern, group_lengths, matching_groups, second_part_lengths, scale)
end
end
end
root_groups = model.entities.grep(Sketchup::Group)
root_groups.each do |group|
process_group(group, group_name_pattern, group_lengths, matching_groups, second_part_lengths, scale)
end
group_lengths.each do |letter, total_length_mm|
if total_length_mm > 0.0
total_length_meters = total_length_mm
puts "Трассы с буквами #{letter}, Общая длина линий: #{total_length_meters.round(2)} м"
matching_groups[letter].each do |group|
group_name = group.name
edges = group.entities.grep(Sketchup::Edge)
total_length_mm = edges.reduce(0.0) { |sum, edge| sum + edge.length * scale }
total_length_meters = total_length_mm
puts " #{group_name}, Длина линий: #{total_length_meters.round(2)} м"
end
end
end
second_part_lengths.each do |second_part, total_length_mm|
total_length_meters = total_length_mm
cleaned_second_part = second_part.sub(/^--\s*/, '') # Удаляем два тире в начале второй части имени
puts "Общая длина линий для '#{cleaned_second_part}': #{total_length_meters.round(2)} м"
end