I wrote a samll feature, it can automatically create dimension between entities
when you move these groups, and run the code again, these dimension can renew.
Actually, It doesn’t work very well. I want to achieve the feature like this:
Here’s the code, Is anyone interested in that?
# 首先建立数个组,并命名为g1,g2,g3…
# First, you have to make some groups, and name it as g1,g2,g3…
# 将一个数组两两配对,生成一个两两组合的数组
# This is function, the purpose is to choice two elements from any given list
# and match them without repeat.
def per(list)
nl = Array.new
list.each do |i|
temp = list - [i]
temp.each do |j|
nl << [j,i]
end
end
nl.each do |k|
a,b = k[0],k[1]
k[0],k[1] = b,a
nl.uniq!
end
end
# 尝试删除最后生成的尺寸标注,以便更新
# Try to delete generated dimension_linear in the end
# for renew these dimension
begin
dim_list.each {|i| i.erase!}
rescue
puts 'deleted'
end
# 接收开始创建的groups
# receive the groups that have made at beginning
group_list = [g1,g2,g3,g4,g5]
model = Sketchup.active_model
entities = model.active_entities
# 找到各组的中心点
# find the each center point of the groups
cp_list = Array.new
group_list.each do |g|
cp_list << g.bounds.center
end
# 新建一个列表,存储即将生成的连线
# new array for storing lines
line_list = Array.new
# 将中心点数组两两配对,生成连线
# match those point for drawing lines
new_cp_list = per(cp_list)
new_cp_list.each do |cp|
if cp[0] != cp[1]
line = entities.add_line cp[0],cp[1]
# 生成连线列表
# drawing line
line_list << line
end
end
# 再将连线数组两两配对
# match these lines
new_line_list = per(line_list)
# 储藏两线之间的夹角
# get the angle between two lines
angle_between_lines = Array.new
new_line_list.each do |l|
vector1 = l[0].line[1]
vector2 = l[1].line[1]
angle = vector1.angle_between vector2
# you can also get a degree
# degrees = Sketchup.format_angle(angle)
if angle < 0.1
angle_between_lines << l
end
end
# 筛选出夹角过小的两条线
# filter those lines which angle is too small
# 比较两条线的长度,保留短的
# then keep the shorter one
eList = Array.new
angle_between_lines.each do |i|
l1 = i[0].length
l2 = i[1].length
if l1 > l2
eList << i[0]
else
eList << i[1]
end
end
eList.uniq!
eList.each {|i| i.erase!}
# 剩下的线的列表
# now, we got the final lines
rList = new_line_list.flatten - eList
rList.uniq!
# 给line加上尺寸标注
# add dimension on these lines
dim_list = Array.new
rList.each do |l|
dim = entities.add_dimension_linear l.start.position, l.end.position, [0, 0, 20]
dim_list << dim
end
# erase lines and keep the dimensions
# job down
# 删除剩下的所有的线
rList.each {|i| i.erase!}