#coding=utf-8
import rhinoscriptsyntax as rs
import re
import System.Guid
def process_column_marks():
# 获取目标图层中的所有文本对象
layer_name = "03 STR. DRAFT MEMBER::COLUMN MARK"
text_objects = rs.ObjectsByLayer(layer_name, True)
if not text_objects or len(text_objects) == 0:
print("未在指定图层找到文本对象")
return
# 存储编号及其对应的对象ID
column_marks = {}
object_info = []
# 提取每个文本对象的编号和位置信息
for obj_id in text_objects:
if rs.IsText(obj_id):
text = rs.TextObjectText(obj_id)
# 分割多行文本
lines = text.split('\n')
# 验证文本格式
if len(lines) >= 1:
# 提取第一行的编号 (以C开头的编号)
first_line = lines[0].strip()
if re.match(r'^C[A-Za-z0-9]+', first_line):
# 获取文本对象的位置
pos = rs.TextObjectPoint(obj_id)
# 存储对象信息
object_info.append({
'id': obj_id,
'mark': first_line,
'x': pos.X,
'y': pos.Y,
'text': text
})
# 按从左到右,从下到上排序(先按Y从小到大,相同Y值则按X从小到大)
sorted_objects = sorted(object_info, key=lambda obj: (obj['y'], obj['x']))
# 第一步:先统计重复的编号
mark_counts = {}
for obj in sorted_objects:
mark = obj['mark']
if mark in mark_counts:
mark_counts[mark] += 1
else:
mark_counts[mark] = 1
# 第二步:处理所有重复编号的对象
mark_processed = {}
for obj in sorted_objects:
mark = obj['mark']
# 如果是重复的编号
if mark_counts[mark] > 1:
# 初始化处理计数器
if mark not in mark_processed:
mark_processed[mark] = 0
# 添加后缀 (a, b, c, ...)
suffix = chr(97 + mark_processed[mark]) # 97是ASCII中的'a'
new_mark = mark + suffix
# 更新计数器
mark_processed[mark] += 1
# 更新文本对象
lines = obj['text'].split('\n')
lines[0] = new_mark
new_text = '\n'.join(lines)
rs.TextObjectText(obj['id'], new_text)
print("已将重复标记 %s 更改为 %s" % (mark, new_mark))
print("处理完成,共处理 %d 个文本对象" % len(sorted_objects))
# 运行函数
process_column_marks()
