快速图名编号

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')    

精选博客

抗震与滞回曲线

在抗震里,经常有几个概念:变形能力、滞回曲线、耗能等等,然而似乎也没有一个地方把这几个东西讲的很清楚。而那些专家们也经常云里来雾里去,不把这些以浅显的方式讲个明白。如同本科专业课里每个老师都会说以概率理论为基础的极限状态设计方法会有其他老师讲的,自己就不讲了,然后就没有一个老师把这个问题讲清楚乃至于讲过。这里就谈谈自己的理解和一些困惑。……

继 续 阅 读

markdown格式转化

本文主要讲如何应用python将markdown转化为html语句以及相关需要注意的事项……

继 续 阅 读

底层倾覆力矩的问题

抗规中对于框架-剪力墙结构中基于底层框架倾覆力矩占总倾覆力矩的比值进行分类设计。并在条文说明中给出了计算底层框架倾覆力矩的公式,但这个公式是否合理?本文给出相关推导并进行适当讨论……

继 续 阅 读