-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoor_builder.rb
More file actions
610 lines (496 loc) · 22.1 KB
/
door_builder.rb
File metadata and controls
610 lines (496 loc) · 22.1 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# 门构建模块:处理门的创建和优化
module DoorBuilder
# 统一创建所有门(核心优化:滞后创建门,确保墙体已存在)
def self.create_all_doors(door_data_list, parent_group)
puts "开始创建门..."
door_count = 0
door_data_list.each do |door_item|
begin
door_data = door_item[:door_data]
wall_data = door_item[:wall_data]
current_parent = door_item[:parent_group]
# 墙体门处理
if wall_data
wall_id = wall_data["id"] || wall_data["name"] || "未知墙体"
puts "处理墙体门 (墙体ID: #{wall_id}, 门ID: #{door_data['id'] || '未知'})"
# 找到对应的墙体组
wall_group = Utils.find_wall_group(current_parent, wall_id)
if wall_group
# 创建门开口
create_door_opening(wall_group, wall_data, door_data, parent_group)
door_count += 1
else
puts "警告: 未找到墙体 '#{wall_id}',跳过此门"
end
else
# 独立门处理
puts "处理独立门 (ID: #{door_data['id'] || '未知'})"
import_independent_door(door_data, parent_group)
door_count += 1
end
rescue => e
door_id = door_data["id"] || "未知"
error_msg = "警告: 创建门时出错 (ID: #{door_id}): #{Utils.ensure_utf8(e.message)}"
puts Utils.ensure_utf8(error_msg)
end
end
puts "门创建完成,共创建 #{door_count} 个门"
end
# 在墙体上创建门开口(支持厚度为0的墙体)
def self.create_door_opening(wall_group, wall_data, door_data, parent_group)
dimensions = door_data["size"] || []
# 验证门尺寸数据
if dimensions.size < 2
puts "警告: 门尺寸数据不足,跳过 (ID: #{door_data['id'] || '未知'})"
return
end
# 提取门的起点和终点
start_point = Utils.validate_and_create_point(dimensions[0])
end_point = Utils.validate_and_create_point(dimensions[1])
if !start_point || !end_point
puts "警告: 门坐标无效,跳过 (ID: #{door_data['id'] || '未知'})"
return
end
# 提取门的高度 - 毫米转米
height = Utils.parse_number(door_data["height"]) * 0.001
height = 2.0 if height <= 0
# 墙体厚度 - 毫米转米
wall_thickness = Utils.parse_number(wall_data["thickness"]) * 0.001
puts "在墙体上创建门开口: 起点=#{start_point}, 终点=#{end_point}, 高度=#{height}m, 墙体厚度=#{wall_thickness}m"
# 计算墙体的方向
wall_start = Utils.validate_and_create_point(wall_data["start"])
wall_end = Utils.validate_and_create_point(wall_data["end"])
if !wall_start || !wall_end
puts "警告: 墙体坐标无效,无法创建门开口"
return
end
# 计算墙体方向向量
wall_vector = wall_end - wall_start
if wall_vector.length < 0.001
puts "警告: 墙体向量长度过小,无法创建门开口"
return
end
# 根据墙体厚度处理不同情况
if wall_thickness <= 0.001
# 厚度为0的墙体处理
create_door_on_zero_thickness_wall(wall_group, wall_data, door_data, start_point, end_point, height)
else
# 正常厚度墙体处理
create_door_on_normal_wall(wall_group, wall_data, door_data, start_point, end_point, height)
end
end
# 在厚度为0的墙体上创建门
def self.create_door_on_zero_thickness_wall(wall_group, wall_data, door_data, start_point, end_point, height)
wall_entities = wall_group.entities
# 计算墙体的方向
wall_start = Utils.validate_and_create_point(wall_data["start"])
wall_end = Utils.validate_and_create_point(wall_data["end"])
if !wall_start || !wall_end
puts "警告: 墙体坐标无效,无法在零厚度墙体上创建门"
return
end
# 计算墙体方向向量
wall_vector = wall_end - wall_start
# 检查墙体向量有效性
wall_direction = nil
if wall_vector.length >= 0.001
wall_direction = wall_vector.normalize
else
# 如果墙体向量无效,尝试从门的起点和终点推断方向
door_vector = end_point - start_point
# 检查门向量有效性
if door_vector.length >= 0.001
wall_direction = door_vector.normalize
puts "警告: 墙体向量无效,使用门的方向代替"
else
# 如果门的向量也无效,使用默认方向(水平向右)
wall_direction = Geom::Vector3d.new(1, 0, 0)
puts "警告: 墙体向量和门的向量都无效,使用默认方向"
end
end
# 计算门的中心点
door_center = Geom::Point3d.new(
(start_point.x + end_point.x) / 2,
(start_point.y + end_point.y) / 2,
0
)
# 确保门宽度有效
door_width = (end_point - start_point).length
# 检查门宽度是否合理(门不应该超过10米宽,允许更大的门)
max_reasonable_width = 10.0 # 10米,允许更大的门
if door_width < 0.001
puts "警告: 门的宽度#{door_width}米过小,使用默认值0.9米"
door_width = 0.9
# 当宽度无效时,基于墙体方向计算门的起点和终点
half_width = door_width / 2
start_point = door_center - wall_direction * half_width
end_point = door_center + wall_direction * half_width
puts "重新计算门边界: 中心点=#{door_center}, 起点=#{start_point}, 终点=#{end_point}"
elsif door_width > max_reasonable_width
puts "警告: 门的宽度#{door_width}米过大,但继续使用原始尺寸"
puts "门宽度: #{door_width}米,位置: 起点=#{start_point}, 终点=#{end_point}"
else
puts "门宽度正常: #{door_width}米"
end
# 计算墙体法线方向(垂直于墙体方向和向上方向)
wall_normal = wall_direction.cross(Geom::Vector3d.new(0, 0, 1)).normalize
# 确保法线向量有效
if wall_normal.length < 0.001
wall_normal = Geom::Vector3d.new(0, 1, 0) # 默认垂直于X轴
puts "警告: 法线向量计算失败,使用默认法线方向"
end
# 计算门的四个角点 - 确保门在正确的高度
door_points = [
start_point,
end_point,
end_point + Geom::Vector3d.new(0, 0, height),
start_point + Geom::Vector3d.new(0, 0, height)
]
# 找到墙体的面(单面墙只有一个面)
wall_face = wall_entities.grep(Sketchup::Face).find { |f|
normal = f.normal
(normal.z.abs < 0.001) && !f.deleted?
}
if !wall_face
puts "警告: 未找到墙体面,无法创建门开口"
return
end
# 创建门组
door_group = wall_group.entities.add_group
door_group.name = "Door-#{door_data['id'] || 'unknown'}"
# 在门的位置创建一个新面
begin
door_face = door_group.entities.add_face(door_points)
if door_face
# 设置门的材质
door_face.material = [200, 200, 200] # 浅灰色
puts "在厚度为0的墙体上创建门成功,门点: #{door_points.inspect}"
else
puts "警告: 创建门的面失败,点可能共线或无效"
puts " 门点: #{door_points.inspect}"
end
rescue Exception => e
puts "警告: 创建门时出错: #{e.message}"
puts " 门点: #{door_points.inspect}"
end
end
# 在正常厚度墙体上创建门 - 修复与更新后墙体结构的兼容性
def self.create_door_on_normal_wall(wall_group, wall_data, door_data, start_point, end_point, height)
model = Sketchup.active_model
model.start_operation("创建墙体门", true)
# 获取墙体坐标
wall_start = Utils.validate_and_create_point(wall_data["start"])
wall_end = Utils.validate_and_create_point(wall_data["end"])
unless wall_start && wall_end && start_point && end_point
puts "[DEBUG] wall_start: #{wall_start.inspect}, wall_end: #{wall_end.inspect}, start_point: #{start_point.inspect}, end_point: #{end_point.inspect}"
puts "警告: 墙体或门的坐标无效,无法创建门"
model.abort_operation
return
end
puts "=== 智能门洞生成 ==="
puts "墙体起点: #{wall_start.inspect}"
puts "墙体终点: #{wall_end.inspect}"
puts "门起点: #{start_point.inspect}"
puts "门终点: #{end_point.inspect}"
# 计算门宽度
door_vector = end_point - start_point
door_width = door_vector.length
# 检查门宽度是否合理
max_reasonable_width = 10.0 # 10米,允许更大的门
if door_width < 0.001
puts "警告: 门的宽度#{door_width}米过小,使用默认值0.9米"
door_width = 0.9
# 计算门的中心点
door_center = Geom::Point3d.new(
(start_point.x + end_point.x) / 2,
(start_point.y + end_point.y) / 2,
(start_point.z + end_point.z) / 2
)
# 基于墙体方向重新计算门的起点和终点
wall_vector = wall_end - wall_start
wall_direction = wall_vector.normalize
half_width = door_width / 2
start_point = door_center - wall_direction * half_width
end_point = door_center + wall_direction * half_width
puts "重新计算门边界: 中心点=#{door_center}, 起点=#{start_point}, 终点=#{end_point}"
elsif door_width > max_reasonable_width
puts "警告: 门的宽度#{door_width}米过大,但继续使用原始尺寸"
puts "门宽度: #{door_width}米"
else
puts "门宽度正常: #{door_width}米"
end
# 智能投影:将门坐标投影到墙体上
puts "\n=== 坐标投影阶段 ==="
projected_start = project_point_to_wall(start_point, wall_start, wall_end)
projected_end = project_point_to_wall(end_point, wall_start, wall_end)
puts "投影后门起点: #{projected_start.inspect}"
puts "投影后门终点: #{projected_end.inspect}"
# 获取墙体厚度(从已生成的墙体几何中提取)
wall_thickness = extract_wall_thickness(wall_group, wall_data)
puts "提取的墙体厚度: #{wall_thickness}英寸"
# 计算门洞地面四点坐标 - 修复与更新后墙体结构的兼容性
puts "\n=== 门洞四点计算 ==="
ground_points = calculate_door_ground_points_fixed(projected_start, projected_end, wall_thickness, wall_start, wall_end, wall_group)
puts "门洞地面四点:"
ground_points.each_with_index do |point, i|
puts " 点#{i+1}: #{point.inspect}"
end
# 创建门洞面
wall_entities = wall_group.entities
door_base_face = wall_entities.add_face(ground_points)
if door_base_face
puts "门洞面创建成功"
# 直接沿Z轴正方向挖洞
puts "开始沿Z轴正方向挖洞,高度: #{height}米"
door_base_face.pushpull(height / 0.0254)
puts "门洞生成完成!"
puts "门高度: #{height}米"
puts "门洞深度: #{wall_thickness * 0.0254}米"
else
puts "警告: 门洞面创建失败"
puts " 地面四点: #{ground_points.inspect}"
end
model.commit_operation
rescue => e
model.abort_operation if model
puts "创建门失败: #{Utils.ensure_utf8(e.message)}"
puts "错误详情: #{e.backtrace.join("\n")}"
end
# 将点投影到墙体上 - 使用简化的方法,参考类结构版
def self.project_point_to_wall(point, wall_start, wall_end)
wall_vector = wall_end - wall_start
wall_length = wall_vector.length
if wall_length < 0.001
puts "警告: 墙体长度过小,无法投影"
return point
end
# 计算点到墙体的投影
wall_direction = wall_vector.normalize
# 验证向量有效性
unless wall_direction && wall_direction.is_a?(Geom::Vector3d)
puts " 警告: 墙体方向向量无效,无法投影"
return point
end
point_to_wall_start = point - wall_start
# 计算投影参数 t (0 <= t <= 1 表示在墙体上)
t = point_to_wall_start.dot(wall_direction) / wall_length
# 限制投影点在墙体范围内
t = [0.0, [t, 1.0].min].max
# 计算投影点 - 使用简化的方法
projection_distance = t * wall_length
projection_vector = wall_direction.clone
projection_vector.length = projection_distance
projected_point = wall_start + projection_vector
puts " 原始点: #{point.inspect}"
puts " 投影参数 t: #{t}"
puts " 投影距离: #{projection_distance}"
puts " 投影点: #{projected_point.inspect}"
projected_point
end
# 从已生成的墙体几何中提取厚度
def self.extract_wall_thickness(wall_group, wall_data)
# 首先尝试从wall_data中获取厚度
thickness_mm = Utils.parse_number(wall_data["thickness"] || 200)
thickness_m = thickness_mm * 0.001
thickness_inches = thickness_m / 0.0254
puts " 从数据获取厚度: #{thickness_mm}mm = #{thickness_m}m = #{thickness_inches}英寸"
# 如果厚度为0或无效,使用默认值
if thickness_inches <= 0.001
thickness_inches = 7.874 # 200mm = 7.874英寸
puts " 使用默认厚度: #{thickness_inches}英寸"
end
thickness_inches
end
# 计算门洞地面四点坐标 - 从实际墙体几何中提取两面坐标
def self.calculate_door_ground_points_fixed(start_point, end_point, wall_thickness, wall_start, wall_end, wall_group = nil)
# 从实际生成的墙体几何中提取两个面的坐标,而不是用数学计算估算
puts " 从实际墙体几何中提取两面坐标"
# 尝试从墙体组中提取实际的两面坐标
left_face_coord, right_face_coord = extract_wall_face_coordinates_from_geometry(wall_start, wall_end, wall_thickness, wall_group)
if left_face_coord && right_face_coord
puts " 成功提取墙体两面坐标: 左面=#{left_face_coord}, 右面=#{right_face_coord}"
# 判断墙体是平行于x轴还是y轴
if (wall_start.y - wall_end.y).abs < 0.001
# 墙体平行于x轴(y坐标相同)
puts " 墙体平行于x轴"
# 门洞四点(逆时针顺序)
ground_points = [
Geom::Point3d.new(start_point.x, left_face_coord, 0), # 点1:左面起点
Geom::Point3d.new(start_point.x, right_face_coord, 0), # 点2:右面起点
Geom::Point3d.new(end_point.x, right_face_coord, 0), # 点3:右面终点
Geom::Point3d.new(end_point.x, left_face_coord, 0) # 点4:左面终点
]
elsif (wall_start.x - wall_end.x).abs < 0.001
# 墙体平行于y轴(x坐标相同)
puts " 墙体平行于y轴"
# 门洞四点(逆时针顺序)
ground_points = [
Geom::Point3d.new(left_face_coord, start_point.y, 0), # 点1:左面起点
Geom::Point3d.new(right_face_coord, start_point.y, 0), # 点2:右面起点
Geom::Point3d.new(right_face_coord, end_point.y, 0), # 点3:右面终点
Geom::Point3d.new(left_face_coord, end_point.y, 0) # 点4:左面终点
]
else
puts " 警告: 墙体不是轴对齐的,使用简化的门洞"
return [start_point, end_point, end_point, start_point]
end
else
puts " 警告: 无法提取墙体两面坐标,使用简化的门洞"
return [start_point, end_point, end_point, start_point]
end
puts " 门洞四点:"
ground_points.each_with_index do |point, i|
puts " 点#{i+1}: #{point.inspect}"
end
ground_points
end
# 从实际墙体几何中提取两个面的坐标
def self.extract_wall_face_coordinates_from_geometry(wall_start, wall_end, wall_thickness, wall_group = nil)
# 首先尝试从墙体组中提取实际的两面坐标
if wall_group
puts " 尝试从墙体组中提取实际两面坐标..."
actual_coords = extract_coordinates_from_wall_group(wall_group, wall_start, wall_end)
if actual_coords
puts " 成功从墙体组提取坐标: #{actual_coords.inspect}"
return actual_coords
else
puts " 无法从墙体组提取坐标,使用计算值"
end
end
# 如果无法从墙体组提取,则使用计算值
puts " 使用计算值作为两面坐标"
# wall_thickness是英寸单位,需要转换为米
thickness_m = wall_thickness * 0.0254 # 英寸转米
# 判断墙体是平行于x轴还是y轴
if (wall_start.y - wall_end.y).abs < 0.001
# 墙体平行于x轴(y坐标相同)
# 墙体的两个面应该垂直于x轴方向
# 左面:y坐标减小(向负y方向)
# 右面:y坐标增加(向正y方向)
left_y = wall_start.y - thickness_m / 2 # 左面y坐标
right_y = wall_start.y + thickness_m / 2 # 右面y坐标
puts " 墙体平行于x轴,两面y坐标: 左面=#{left_y.round(6)}, 右面=#{right_y.round(6)}"
puts " 墙体中心y坐标: #{wall_start.y.round(6)}, 厚度: #{thickness_m.round(6)}m"
return left_y, right_y
elsif (wall_start.x - wall_end.x).abs < 0.001
# 墙体平行于y轴(x坐标相同)
# 墙体的两个面应该垂直于y轴方向
# 左面:x坐标减小(向负x方向)
# 右面:x坐标增加(向正x方向)
left_x = wall_start.x - thickness_m / 2 # 左面x坐标
right_x = wall_start.x + thickness_m / 2 # 右面x坐标
puts " 墙体平行于y轴,两面x坐标: 左面=#{left_x.round(6)}, 右面=#{right_x.round(6)}"
puts " 墙体中心x坐标: #{wall_start.x.round(6)}, 厚度: #{thickness_m.round(6)}m"
return left_x, right_x
else
puts " 警告: 墙体不是轴对齐的"
return nil, nil
end
end
# 从墙体组中提取两面坐标
def self.extract_coordinates_from_wall_group(wall_group, wall_start, wall_end)
begin
# 获取墙体组中的所有面
faces = wall_group.entities.grep(Sketchup::Face).select { |f| !f.deleted? }
if faces.empty?
puts " 墙体组中没有找到面"
return nil
end
puts " 找到 #{faces.length} 个面"
# 判断墙体是平行于x轴还是y轴
if (wall_start.y - wall_end.y).abs < 0.001
# 墙体平行于x轴,提取y坐标
y_coords = faces.flat_map do |face|
face.vertices.map { |v| v.position.y }
end.uniq.sort
if y_coords.length >= 2
left_y = y_coords.first
right_y = y_coords.last
puts " 从面提取的y坐标: #{y_coords.inspect}"
puts " 两面y坐标: 左面=#{left_y.round(6)}, 右面=#{right_y.round(6)}"
return left_y, right_y
end
elsif (wall_start.x - wall_end.x).abs < 0.001
# 墙体平行于y轴,提取x坐标
x_coords = faces.flat_map do |face|
face.vertices.map { |v| v.position.x }
end.uniq.sort
if x_coords.length >= 2
left_x = x_coords.first
right_x = x_coords.last
puts " 从面提取的x坐标: #{x_coords.inspect}"
puts " 两面x坐标: 左面=#{left_x.round(6)}, 右面=#{right_x.round(6)}"
return left_x, right_x
end
end
puts " 无法从面中提取有效的两面坐标"
return nil
rescue => e
puts " 从墙体组提取坐标时出错: #{e.message}"
return nil
end
end
# 将点投影到指定线段上
def self.project_point_to_line(point, line_start, line_end)
line_vector = line_end - line_start
line_length = line_vector.length
if line_length < 0.001
return line_start # 线段长度过小时返回起点
end
# 计算点到线段的投影
line_direction = line_vector.normalize
point_to_line_start = point - line_start
# 计算投影参数 t (0 <= t <= 1 表示在线段上)
t = point_to_line_start.dot(line_direction) / line_length
# 限制投影点在线段范围内
t = [0.0, [t, 1.0].min].max
# 计算投影点 - 修复向量乘法问题
# 确保 line_direction 是有效的向量,并且使用正确的向量运算
if line_direction && line_direction.is_a?(Geom::Vector3d)
projected_point = line_start + line_direction * (t * line_length)
else
# 如果向量无效,使用原始点
puts " 警告: 线段方向向量无效,使用原始点"
projected_point = point
end
puts " 投影详情: 原始点=#{point.inspect}, 投影参数t=#{t.round(6)}, 投影点=#{projected_point.inspect}"
projected_point
end
# 导入独立门(非墙体上的门)
def self.import_independent_door(door_data, parent_group)
position = Utils.validate_and_create_point(door_data["position"])
if !position
puts "警告: 独立门位置无效,跳过 (ID: #{door_data['id'] || '未知'})"
return
end
# 提取门的尺寸
size = door_data["size"] || []
width = Utils.parse_number(size[0] || 1.0)
height = Utils.parse_number(size[1] || 2.0)
depth = Utils.parse_number(size[2] || 0.1)
# 确保尺寸有效
width = 1.0 if width <= 0
height = 2.0 if height <= 0
depth = 0.1 if depth <= 0
# 创建门组
door_group = parent_group.entities.add_group
door_group.name = door_data["name"] || "独立门"
# 创建门的四个角点
points = [
position,
position + Geom::Vector3d.new(width, 0, 0),
position + Geom::Vector3d.new(width, 0, height),
position + Geom::Vector3d.new(0, 0, height)
]
# 应用旋转(如果有)
orientation = Utils.parse_number(door_data["orientation"] || 0.0)
if orientation != 0
rotation = Geom::Transformation.rotation(position, Geom::Vector3d.new(0, 0, 1), orientation * Math::PI / 180)
points.map! { |p| p.transform(rotation) }
end
# 创建门的面并拉伸
door_face = door_group.entities.add_face(points)
door_face.pushpull(depth) if door_face
puts "创建独立门成功: #{door_group.name}"
end
end