Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c8c94184df | |||
| 62c436131c | |||
| ac3579de46 | |||
| e7a72b25d7 | |||
| 6d155d987a | |||
| e4a78e4c6e | |||
| 35c819dab0 |
@@ -6,6 +6,7 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
if can_convert_to_native(obj)
|
||||
convert_to_native(obj, Sketchup.active_model.entities)
|
||||
elsif obj.is_a?(Hash) && obj.key?("speckle_type")
|
||||
puts(">>> Found #{obj["speckle_type"]}: #{obj["id"]}")
|
||||
props = obj.keys.filter_map { |key| key unless key.start_with?("_") }
|
||||
props.each { |prop| traverse_commit_object(obj[prop]) }
|
||||
elsif obj.is_a?(Hash)
|
||||
@@ -24,6 +25,7 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
"Objects.Geometry.Line",
|
||||
"Objects.Geometry.Polyline",
|
||||
"Objects.Geometry.Mesh",
|
||||
"Objects.Geometry.Brep",
|
||||
"Objects.Other.BlockInstance",
|
||||
"Objects.Other.BlockDefinition",
|
||||
"Objects.Other.RenderMaterial"
|
||||
@@ -36,6 +38,7 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
when "Objects.Other.BlockInstance" then component_instance_to_native(obj, entities)
|
||||
when "Objects.Other.BlockDefinition" then component_definition_to_native(obj)
|
||||
when "Objects.Geometry.Mesh" then mesh_to_native(obj, entities)
|
||||
when "Objects.Geometry.Brep" then mesh_to_native(obj["displayMesh"], entities)
|
||||
else
|
||||
nil
|
||||
end
|
||||
@@ -53,6 +56,7 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
if line.key?("value")
|
||||
values = line["value"]
|
||||
points = values.each_slice(3).to_a.map { |pt| point_to_native(pt[0], pt[1], pt[2], line["units"]) }
|
||||
points.push(points[0]) if line["closed"]
|
||||
entities.add_edges(*points)
|
||||
else
|
||||
start_pt = point_to_native(line["start"]["x"], line["start"]["y"], line["start"]["z"], line["units"])
|
||||
@@ -69,18 +73,6 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
Geom::Point3d.new(length_to_native(x, units), length_to_native(y, units), length_to_native(z, units))
|
||||
end
|
||||
|
||||
def component_definition_to_native(block_def)
|
||||
definition = Sketchup.active_model.definitions[block_def["name"]]
|
||||
return definition if definition&.guid == block_def["applicationId"]
|
||||
|
||||
definition&.entities&.clear!
|
||||
definition ||= Sketchup.active_model.definitions.add(block_def["name"])
|
||||
block_def["geometry"].each { |obj| convert_to_native(obj, definition.entities) }
|
||||
puts("definition finished: #{block_def["name"]} (#{block_def["id"]})")
|
||||
puts(" entity count: #{definition.entities.count}")
|
||||
definition
|
||||
end
|
||||
|
||||
def mesh_to_native(mesh, entities)
|
||||
native_mesh = Geom::PolygonMesh.new(mesh["vertices"].count / 3)
|
||||
points = []
|
||||
@@ -90,7 +82,8 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
faces = mesh["faces"]
|
||||
while faces.count.positive?
|
||||
num_pts = faces.shift
|
||||
num_pts += 3 if num_pts < 3 # 0 -> 3, 1 -> 4 to preserve backwards compatibility
|
||||
# 0 -> 3, 1 -> 4 to preserve backwards compatibility
|
||||
num_pts += 3 if num_pts < 3
|
||||
indices = faces.shift(num_pts)
|
||||
native_mesh.add_polygon(indices.map { |index| points[index] })
|
||||
end
|
||||
@@ -99,11 +92,25 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
native_mesh
|
||||
end
|
||||
|
||||
def component_definition_to_native(block_def)
|
||||
definition = Sketchup.active_model.definitions[block_def["name"]]
|
||||
return definition if definition && (definition.name == block_def["name"] || definition.guid == block_def["applicationId"])
|
||||
|
||||
definition&.entities&.clear!
|
||||
definition ||= Sketchup.active_model.definitions.add(block_def["name"])
|
||||
block_def["geometry"].each { |obj| convert_to_native(obj, definition.entities) }
|
||||
puts("definition finished: #{block_def["name"]} (#{block_def["id"]})")
|
||||
puts(" entity count: #{definition.entities.count}")
|
||||
definition
|
||||
end
|
||||
|
||||
def component_instance_to_native(block, entities)
|
||||
is_group = block.key?("is_sketchup_group") && block["is_sketchup_group"]
|
||||
# is_group = block.key?("is_sketchup_group") && block["is_sketchup_group"]
|
||||
# something about this conversion is freaking out if nested block geo is a group
|
||||
# so this is set to false always until I can figure this out
|
||||
is_group = false
|
||||
|
||||
definition = component_definition_to_native(block["blockDefinition"])
|
||||
# return unless definition.entities.count.positive?
|
||||
|
||||
transform = transform_to_native(block["transform"], block["units"])
|
||||
instance =
|
||||
@@ -112,11 +119,7 @@ module SpeckleSystems::SpeckleConnector::ToNative
|
||||
else
|
||||
entities.add_instance(definition, transform)
|
||||
end
|
||||
puts("Failed to create instance for speckle object #{block["id"]}") if instance.nil?
|
||||
if instance.nil?
|
||||
p(definition.name)
|
||||
p(definition.entities.to_a)
|
||||
end
|
||||
puts("Failed to create instance for speckle block instance #{block["id"]}") if instance.nil?
|
||||
instance.transformation = transform if is_group
|
||||
instance.material = material_to_native(block["renderMaterial"])
|
||||
instance
|
||||
|
||||
@@ -64,37 +64,18 @@ module SpeckleSystems::SpeckleConnector::ToSpeckle
|
||||
# convert material
|
||||
mat_id = face.material.nil? ? "none" : face.material.entityID
|
||||
mat_groups[mat_id] = initialise_group_mesh(face, component_def.bounds) unless mat_groups.key?(mat_id)
|
||||
|
||||
# add points and texture coordinates
|
||||
mesh = face.mesh(1)
|
||||
mat_groups[mat_id]["@(31250)vertices"].push(*points_to_array(mesh))
|
||||
mat_groups[mat_id]["@(31250)textureCoordinates"].push(*uvs_to_array(mesh))
|
||||
points = mesh.points
|
||||
polys =
|
||||
mesh.polygons.map do |poly|
|
||||
poly.map { |coord| points[coord.abs - 1] }
|
||||
end
|
||||
polys.each do |poly|
|
||||
mat_groups[mat_id]["@(62500)faces"].push(
|
||||
case poly.count
|
||||
when 3 then 0 # tris
|
||||
when 4 then 1 # polys
|
||||
else
|
||||
poly.count # ngons
|
||||
end
|
||||
)
|
||||
poly.each do |pt|
|
||||
index = mat_groups[mat_id][:pt_log][pt.to_s]
|
||||
unless index
|
||||
mat_groups[mat_id]["@(31250)vertices"].push(
|
||||
length_to_speckle(pt[0]),
|
||||
length_to_speckle(pt[1]),
|
||||
length_to_speckle(pt[2])
|
||||
)
|
||||
index = mat_groups[mat_id][:pt_log][pt.to_s] = mat_groups[mat_id][:pt_log].count
|
||||
end
|
||||
mat_groups[mat_id]["@(62500)faces"].push(index)
|
||||
end
|
||||
end
|
||||
|
||||
# add faces
|
||||
mat_groups[mat_id]["@(62500)faces"].push(*faces_to_array(mesh, mat_groups[mat_id][:pt_count]))
|
||||
mat_groups[mat_id][:pt_count] += mesh.points.count
|
||||
end
|
||||
mat_groups.values.map { |group| group.delete(:pt_log) }
|
||||
|
||||
mat_groups.values.map { |group| group.delete(:pt_count) }
|
||||
mat_groups.values
|
||||
end
|
||||
|
||||
@@ -128,7 +109,7 @@ module SpeckleSystems::SpeckleConnector::ToSpeckle
|
||||
"@(31250)vertices" => [],
|
||||
"@(62500)faces" => [],
|
||||
"@(31250)textureCoordinates" => [],
|
||||
pt_log: {},
|
||||
pt_count: -1,
|
||||
renderMaterial: face.material.nil? ? nil : material_to_speckle(face.material)
|
||||
}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user