I have a triangulated data of the model, which records the vertex indices of vertices and planes.

I tried to draw this model in Sketchup.

But the code reported an error.

Only add line is ok.

test.txt (119.7 KB)

```
require 'json'
def self.crate_model_by_shape(file)
shape_info = JSON.parse File.read(file)
group = Sketchup.active_model.entities.add_group
# get vertices
points = shape_info['points']
# Avoiding errors
points.map! do |pt_arr|
Geom::Point3d.new(*(pt_arr.map { |i| i * 1000 }))
end
# triangles is vertex index of triangular plane
triangles = shape_info['triangles'].map(&:uniq)
# find more than three points
if triangles.find { |triangle| triangle.length != 3 }
raise 'exist more than three points'
end
# delete three point on one line
delete_count = triangles.length
triangles = triangles.delete_if do |triangle|
p1, p2, p3 = triangle.map { |i| points[i] }
on_line?(p1, p2, p3)
end
puts "delete #{delete_count - triangles.length} triangle"
if triangles.find do |triangle|
p1, p2, p3 = triangle.map { |i| points[i] }
on_line?(p1, p2, p3)
end
raise 'exist three points on line'
end
# Determine the edge line based on the index of the triangle vertex and record the vertex composition of the edge line
triangles.each do |triangle|
triangle.each_with_index { |p2_index, i|
p1_index = triangle[i - 1]
edge_hash[p1_index] ||= []
edge_hash[p2_index] ||= []
next if edge_hash[p1_index][p2_index] # p1 to p2 exist
edge = group.entities.add_line points[p1_index], points[p2_index]
edge_hash[p1_index][p2_index] = edge
edge_hash[p2_index][p1_index] = edge
}
end
# add faces by edges
triangles.each do |triangle|
edges = []
triangle.each_with_index { |p2_index, i|
p1_index = triangle[i - 1]
edges << edge_hash[p1_index][p2_index]
}
group.entities.add_face edges
end
end
Sketchup.active_model.start_operation 'crate_model_by_shape', true
begin
crate_model_by_shape('test.txt')
Sketchup.active_model.commit_operation
rescue => e
puts e.message,e.backtrace
Sketchup.active_model.abort_operation
end
```

Error

```
reference to deleted Group
<main>:35:in `entities'
<main>:35:in `block in crate_model_by_shape'
<main>:29:in `each'
<main>:29:in `crate_model_by_shape'
<main>:41:in `<main>'
SketchUp:in `eval'
```

**Solution, But not found question.**

```
group = Sketchup.active_model.entities.add_group
# add faces by pts
triangles.each do |triangle|
pts = triangle.map { |i| points[i] }
face = group.entities.add_face pts
v1, v2 = pts[1] - pts[0], pts[2] - pts[1]
normal = v1 * v2
face.reverse! unless face.normal.samedirection?(normal)
end
```