快速图名编号

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

精选博客

高斯积分相关

对于一般的积分表达式,是无法求出其定积分形式的。对于有限元问题而言,主要就是单元刚度矩阵的问题。(当然对于梁单元而言,是可以给出单元刚度矩阵显式表达式的)。因此得用到数值积分方式。而这其中,如果积分具有如下形式,则可以应用高斯积分。……

继 续 阅 读

基于ezdxf的DXF编写

本文主要用于讲采用python的ezdxf模块进行DXF格式的CAD创建。主要讲清楚了具体对象结构和使用流程。在此基础上结合相关帮助文件可以进行较为复杂的图形绘制了。……

继 续 阅 读

观海卫建城

前言:一直说慈溪东西两边方言差距大,说起这原因大概很有意思。我想本身这就是一种错误。也还不能称之为美丽。观城反正是肉眼可见的迅速没落了,所以历史很有意思,几个决定就可能改变了。慢慢把搜到的一些资料整理开来写一写。偶然翻到一本《慈谿县志》(这里叢即丛),是对岸64年(公元1975年)所出版的《中国方志叢书》里的其中一册。而其实际上是雍正八年(公元1730年)《慈谿志》的影印版。……

继 续 阅 读