快速图名编号

2023年09月15日 星期五

在编制图集时有一个小问题,遇到有删减图时,需要各个图进行编号的修改。 基本的程序逻辑是:- 选择需要编号的图,获取既有图页码(编号);- 对图名根据坐标进行编号,并相应做修改。……

​ 在编制图集时有一个小问题,遇到有删减图时,需要各个图进行编号的修改。

基本的程序逻辑是:

  • 选择需要编号的图,获取既有图页码(编号);
  • 对图名根据坐标进行编号,并相应做修改。

一、获取页码

对于第一步,具体代码可以是这样的:

from pyautocad import Autocad,APoint,aDouble,ACAD
A1 = Autocad(create_if_not_exists=True)
aa=A1.doc.Utility


# 读取图名
selSets=A1.doc.SelectionSets 
gg=selSets.add("yema")
gg.SelectOnScreen()
aa=gg.count
i=0
yemaLi =[]
yemaCorLi =[]
for obj in gg:
    if obj.ObjectName=="AcDbAttributeDefinition" and obj.Layer == "页码":
        i+=1
        yemaLi.append(obj)
        yemaCorLi.append([round(obj.InsertionPoint[0],2),round(obj.InsertionPoint[1],2)])
gg.delete()

这里获取图页码时,一般可能有几种情形:

1)页码为单行文字、多行文字,同时具有相同图层

2)页码为属性参照,同时具有相同图层

3)页码在图框的块之中,作为属性参照存在

目前对于上述第三种没法解决,因为采用GetAttributes会报错。

为了方便识别。建议对页码单独采用一个图层,如这里就统一设置为“页码”图层。

二、进行编号

2.1 程序代写

基于上述方法获取了各图页码坐标,对坐标进行排序后即可相应修改页码。这里应用百度推出的对话功能来写一个子程序。即用下面的问话:

二维坐标,是按照y坐标的降序和x坐标的升序对坐标进行排序。同时得到排序后坐标在原排序中的位置。

相应得到程序:

def sort_coordinates_2d_with_index(coordinates):
    # 创建一个包含坐标和索引的列表
    coordinate_with_index = [(coordinate, index) for index, coordinate in enumerate(coordinates)]
    # 对列表进行排序
    coordinate_with_index.sort(key=lambda item: (item[0][1], item[0][0]), reverse=True)
    # 提取排序后的坐标和索引
    sorted_coordinates = [coordinate for coordinate, index in coordinate_with_index]
    sorted_indices = [index for coordinate, index in coordinate_with_index]
    return sorted_coordinates, sorted_indices

# 示例二维坐标列表
coordinates_2d = [(3, 2), (1, 5), (4, 1), (2, 3)]

# 打印排序后的二维坐标和它们在原列表中的位置
sorted_coordinates_2d, sorted_indices = sort_coordinates_2d_with_index(coordinates_2d)
for index, coordinate in enumerate(sorted_coordinates_2d):
    print(f"Coordinate: {coordinate}, Original Position: {sorted_indices[index]}")

不过上面这个程序似乎是按y降序、x降序来的,需要手动修改一个语句,即将:

coordinate_with_index.sort(key=lambda item: (item[0][1], item[0][0]), reverse=True)

修改为:

coordinate_with_index.sort(key=lambda item: (-item[0][1], item[0][0]))

上述实现了从上至下、从左至右的排序。继而稍微调整代码实现对页码的修改。

# 修改页码
sorted_coordinates_2d, sorted_indices = sort_coordinates_2d_with_index(coordinates_2d)
i = 1
for index, coordinate in enumerate(sorted_coordinates_2d):
    yemaLi[sorted_indices[index]].TagString = 'A'+ str(i)
    i+=1

2.2 问题修正

上述程序实际运行时,会遇到一个问题就是每一行各个图页码y坐标可能有略微差别,导致会出现编号与预期有差别。

因此做了一个工作是将相近坐标间的误差给抹平,就是除以一个大数的做法。

sorted_coordinates_2d, sorted_indices = sort_coordinates_2d_with_index(coordinates_2d)
y_min = sorted_coordinates_2d[-1][1]
y_max= sorted_coordinates_2d[0][1]
d_y = y_max-y_min
for index, coordinate in enumerate(sorted_coordinates_2d):    
    print(round((coordinate[1]-y_min)/d_y,1))
    yemaCorLi[sorted_indices[index]][1] = round((coordinate[1]-y_min)/d_y,1)

这里目前还有个小问题,就是如果只选择了一行图,那就出现d_y=0,就会出现错误,或现d_y≈0,也会出现与预期不符。此时一个比较好的解决方案是,预先设定一个各行图的间距作为d_y

三、程序全文

# 2023/9/12 Tuesday Cloudy
# 本程序用于对图编号快速修改

from pyautocad import Autocad,APoint,aDouble,ACAD
A1 = Autocad(create_if_not_exists=True)
U1=A1.doc.Utility


# 读取坐标
selSets=A1.doc.SelectionSets
for obj in selSets:
    obj.delete()

gg=selSets.add("yema")
gg.SelectOnScreen()
aa=gg.count
i=0
yemaLi =[]
yemaCorLi =[]
for obj in gg:

    # print(obj.ObjectName)
    if (obj.ObjectName=="AcDbText" or obj.ObjectName=="AcDbMText" or obj.ObjectName=="AcDbAttributeDefinition") \
       and obj.Layer == "页码":
        print(obj.TagString)
        print(obj.InsertionPoint )
        i+=1
        yemaLi.append(obj)
        yemaCorLi.append([round(obj.InsertionPoint[0],2),round(obj.InsertionPoint[1],2)])
gg.delete()

U1.prompt('共选择'+str(i)+'个图\n')


def sort_coordinates_2d_with_index(coordinates):
    # 创建一个包含坐标和索引的列表
    coordinate_with_index = [(coordinate, index) for index, coordinate in enumerate(coordinates)]
    # 对列表进行排序
    coordinate_with_index.sort(key=lambda item: (-item[0][1], item[0][0]))
    # 提取排序后的坐标和索引
    sorted_coordinates = [coordinate for coordinate, index in coordinate_with_index]
    sorted_indices = [index for coordinate, index in coordinate_with_index]
    return sorted_coordinates, sorted_indices

# 示例二维坐标列表
coordinates_2d = yemaCorLi



sorted_coordinates_2d, sorted_indices = sort_coordinates_2d_with_index(coordinates_2d)
y_min = sorted_coordinates_2d[-1][1]
y_max= sorted_coordinates_2d[0][1]
d_y = y_max-y_min
for index, coordinate in enumerate(sorted_coordinates_2d):    
    print(round((coordinate[1]-y_min)/d_y,1))
    yemaCorLi[sorted_indices[index]][1] = round((coordinate[1]-y_min)/d_y,1)



# 修改编号
sorted_coordinates_2d, sorted_indices = sort_coordinates_2d_with_index(coordinates_2d)
i = 1
for index, coordinate in enumerate(sorted_coordinates_2d):
    yemaLi[sorted_indices[index]].TagString = 'A'+ str(i)

    U1.prompt('修改第'+str(i)+'一个图\n')
    i+=1
    #print(f"Coordinate: {coordinate}, Original Position: {sorted_indices[index]}")


U1.prompt('修改完成\n')    

精选博客

关于几个长辈的故事

晚上10点妈打电话过来。这么晚接起电话总让人会有些担忧。无非两种事,一是介绍对象,二是亲人去世。是的,八点多爷爷过世了。说起来这是个可以预期的事,毕竟在国庆90虚岁生日后摔了一跤后已经躺床上差不多一个多月了。那会,爷爷和来吃饭的自己的亲妹妹说,大概这是他们俩最后一次见面了。而至此,外公外婆,爷爷奶奶皆已去了另外一个世界。对于爸妈而言是一件更为悲伤的事(如同机器猫里说道:再也没有比他们更大的了。……

继 续 阅 读

北京游

10月4日~10月8日,带着爸妈一道去了北京游玩一趟。很早就准备起来了。主要是因为原单位(绿城设计院)当时有给了6000多的旅游卡,拆开后就剩下半年多有效期了。否则去一趟还是有些贵。不过主要原因还是现在每年都带老爸老妈出去玩(前年杭州、去年上海)今年还是应该去,而且都老了,以后指不定也不太想去这么远的地方了。另外就是让他们体验下坐坐飞机的感觉。……

继 续 阅 读

水平加劲肋和节点域

在处理新型结构体系梁柱连接时,必然会涉及到节点域和水平加劲肋。节点域该如何进行计算?为什么要施加水平加劲肋?加劲肋尺寸又该如何选取?……

继 续 阅 读