forked from juuli/ParallelFDTD
-
Notifications
You must be signed in to change notification settings - Fork 5
/
geom2json.rb
197 lines (169 loc) · 4.56 KB
/
geom2json.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# JSON Exporter for Google Sketchup
# Jukka Saarelma 2014
# Aalto University
#
# Exporter is heavily influenced by
# VTK Exporter for Google Sketchup
# By Alex Southern
#
require 'sketchup.rb'
def jsonExport
mod = Sketchup.active_model
ent = mod.entities
sel = mod.selection
layers = mod.layers
$num_l = layers.length
model_filename = File.basename(mod.path)
if sel.empty?
ans = UI.messagebox("No objects selected. Export entire model?", MB_YESNOCANCEL)
if( ans == 6 )
$model = mod.entities
else
$model = sel
end
else
$model = Sketchup.active_model.selection
end
$vrtx = []
$vrtx_len = 0
$numPolys = 0
$numUsedPts = 0
if ($model.length > 0)
file_type="json"
out_name = UI.savepanel( file_type+" file location", "." , "#{File.basename(mod.path).split(".")[0]}." +file_type )
$mesh_file = File.new( out_name , "w" )
model_name = model_filename.split(".")[0]
$mesh_file.puts("{")
jsonExtractData
jsonWritePoints
jsonWritePolygons
jsonWriteLayers
$mesh_file.puts("}")
$mesh_file.close
end
end # End main
def jsonExtractData
$numPolys = 0
$numUsedPts = 0
$model.each do |entity|
if (entity.typename == "Face")
# Each Face is made up of triangular polygons, keep a count of how many polygons
$numPolys += entity.mesh.count_polygons
entity.vertices.each do |vertex|
$numUsedPts += 1
if ($vrtx_len == 0)
# Add First Vertex to Vrtx List
$vrtx[$vrtx_len] = vertex.position.to_a
$vrtx_len += 1
else
# Check if current vertex already exists in $vrtx list
found = 0
(0..$vrtx_len).each { |i|
if ($vrtx[i] == vertex.position.to_a)
found = 1
end
}
# If vertex is unique then add it to the $vrtx list
if (found == 0)
$vrtx[$vrtx_len] = vertex.position.to_a
$vrtx_len += 1
end
end
end
end
end
end # End function
def jsonWritePoints
$mesh_file.puts("\t\"vertices\": [")
(0..$vrtx_len-1).each { |i|
$mesh_file.print("\t")
(0..$vrtx[i].length-1).each {|j|
v = $vrtx[i][j]*0.0254
$mesh_file.print(v)
if( (i != $vrtx_len-1) or (j != 2))
$mesh_file.print(",")
end
}
$mesh_file.puts(" ")
}
$mesh_file.puts("\t],")
end # End function
def jsonWritePolygons
# Calculate number of triangles
tri_count = 0
count = 0
$mesh_file.puts("\t\"indices\": [")
$model.each do |entity|
if (entity.typename == "Face")
mesh = entity.mesh
num = mesh.count_polygons
(1..num).each { |i|
count += 1
$mesh_file.print("\t")
p = mesh.polygon_at(i)
p1 = mesh.point_at(p[0].abs)
p2 = mesh.point_at(p[1].abs)
p3 = mesh.point_at(p[2].abs)
i1 = $vrtx.index(p1.to_a)
i2 = $vrtx.index(p2.to_a)
i3 = $vrtx.index(p3.to_a)
str = i1.to_s+", "+i2.to_s+", "+i3.to_s
$mesh_file.print(str)
if(count != $numPolys)
$mesh_file.puts(",")
end
}
end # Face if
end # Entity loop
$mesh_file.puts("\n\t],")
end # End function
def jsonWriteLayers
m = $model.model;
l = []
l_num = 0;
flag = 0;
$mesh_file.puts("\t\"layers_of_triangles\": [")
count = 0
$model.each do |entity|
if(entity.typename == "Face")
flag = 0
layername = entity.layer.name
(0..l_num).each {|i|
if (l[i] == layername && l_num >0)
flag = 1
end
}
if (flag == 0)
l_num+=1
l[l_num-1] = layername
end
mesh = entity.mesh
mesh.polygons.each do |poly|
count += 1
$mesh_file.print("\t\"")
$mesh_file.print(layername)
$mesh_file.print("\"")
if(count < $numPolys)
$mesh_file.print(",")
end
$mesh_file.print("\n")
end
end # End face if
end # End entity for
$mesh_file.puts("\t],")
$mesh_file.puts("\t\"layer_names\": [")
(0..l_num-1).each{|i|
$mesh_file.print("\t\"")
$mesh_file.print(l[i].to_s)
$mesh_file.print("\"")
if(i < l_num-1)
$mesh_file.print(",")
end
$mesh_file.print("\n")
}
$mesh_file.puts("\t]")
end # End function
if (not file_loaded?("geom2json.rb"))
UI.menu("Tools").add_item("Export to JSON") {jsonExport}
end
file_loaded("geom2json.rb")