明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 442|回复: 5

批量保存框选的图形为块,有没有大佬教教我

[复制链接]
发表于 2023-6-10 01:34 | 显示全部楼层 |阅读模式
本帖最后由 314611967 于 2023-6-10 02:08 编辑

# 能识别的图形为,直线,圆,单行文字,填充,
# 导入win32com库,用于与AutoCAD进行交互
import pythoncom
import win32com.client
from win32com.client import VARIANT
# 获取当前活动的AutoCAD文档
acad = win32com.client.Dispatch("AutoCAD.Application")  # 通过应用程序接口获取AutoCAD应用程序
doc = acad.ActiveDocument  # 获取当前活动的文档

# 获取文档中的模型空间
mspace = doc.ModelSpace  # 获取当前文档的模型空间,用于存放图形对象

# 定义一个函数,用于将三个坐标值转换为AutoCAD中的点对象
def vtpnt(x, y, z):
    return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [x, y, z])
def point(x, y, z=0):
    return win32com.client.VARIANT(win32com.client.pythoncom.VT_ARRAY | win32com.client.pythoncom.VT_R8, (x, y, z))
# 创建第一个选择集,用于存放用户选择的图形
selection_set_name1 = "MySelectionSet1"
if doc.SelectionSets.Count > 0:
    for sel_set in doc.SelectionSets:
        if sel_set.Name == selection_set_name1:
            sel_set.Delete()
            break
selection_set1 = doc.SelectionSets.Add(selection_set_name1)
# 创建第二个选择集
selection_set_name2 = "MySelectionSet2"
if doc.SelectionSets.Count > 0:
    for sel_set in doc.SelectionSets:
        if sel_set.Name == selection_set_name2:
            sel_set.Delete()
            break
selection_set2 = doc.SelectionSets.Add(selection_set_name2)
# 创建一个空列表,用于存储满足条件的实体
items_to_add = []
# 让用户在AutoCAD中手动选择图形,将选择的图形添加到选择集中
selection_set1.SelectOnScreen()
# 遍历选择集中的每个图形,根据图形类型将满足条件的实体添加到 select_rect 选择集中
for i in range(selection_set1.Count):
    item = selection_set1.Item(i)  # 获取选择集中的第i个图形
    item_type = item.ObjectName  # 获取图形的类型
    item_layer = item.Layer  # 获取图形所在的图层

    # 检查实体类型是否为多段线,且图层为101
    if item_type == "AcDbPolyline" and item_layer == "101":
        # 使用 GetBoundingBox 方法获取最小点和最大点
        minrecbounding = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
        maxrecbounding = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
        recbound = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
        recbound = item.GetBoundingBox(minrecbounding, maxrecbounding)
        minrecbounding = recbound[0]
        maxrecbounding = recbound[1]
        recminX = minrecbounding[0]
        recminY = minrecbounding[1]
        recmaxX = maxrecbounding[0]
        recmaxY = maxrecbounding[1]
        # 计算绝对值
        recminX_abs = abs(recminX)
        recminY_abs = abs(recminY)
        recmaxX_abs = abs(recmaxX)
        recmaxY_abs = abs(recmaxY)
        # 创建一个新的块,用于存放复制的图形
        block_name = "11111"
        blocks = doc.Blocks
        block_origin = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (minrecbounding[0],minrecbounding[1], 0))
        # 检查是否已经存在具有相同名称的块
        try:
            existing_block = doc.Blocks.Item(block_name)
            # 如果存在具有相同名称的块,则删除该块
            existing_block.Delete()
        except Exception:  # 在找不到具有相同名称的块时,Item方法会引发异常
            pass

        # 添加新块
        block = doc.Blocks.Add(block_origin, block_name)
        which_block = doc.blocks.Item("11111")
        for i in range(mspace.Count):
            obj_ent = mspace.Item(i)
            minbounding= win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
            maxbounding= win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
            bounding = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [0, 0, 0])
            bounding = obj_ent.GetBoundingBox(minbounding, maxbounding)
            minbounding = bounding[0]
            maxbounding = bounding[1]
            minX = minbounding[0]
            minY = minbounding[1]
            maxX = maxbounding[0]
            maxY = maxbounding[1]
            # 计算绝对值
            minX_abs = abs(minX)
            minY_abs = abs(minY)
            maxX_abs = abs(maxX)
            maxY_abs = abs(maxY)

            if minX_abs>recminX_abs and maxX_abs<recmaxX_abs and minY_abs>recminY_abs and maxY_abs<recmaxY_abs:
                if obj_ent.ObjectName == "AcDbText":

                    text_content = obj_ent.TextString
                    centerPoint = vtpnt(0, 0, 0)
                    Height = obj_ent.Height
                    centerPoint = obj_ent.InsertionPoint
                    point = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [centerPoint[0], centerPoint[1], centerPoint[2]])
                    which_block.AddText(text_content, point, Height)

                    # 检查文本内容中是否包含"HG/T"字符串
                    if "HG/T" in text_content:
                        qqqq = "1"
                    else:
                        # 获取单行文本的内容
                        endblockname = obj_ent
                if obj_ent.ObjectName == "AcDbLine":
                    start_point = vtpnt(0, 0, 0)
                    end_point = vtpnt(0, 0, 0)
                    start_point = obj_ent.startpoint
                    end_point = obj_ent.endpoint
                    startpoint1 = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [start_point[0], start_point[1], start_point[2]])
                    endpoint1 = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [end_point[0],end_point[1],end_point[2]])
                    doc.blocks.Item("11111").AddLine(startpoint1, endpoint1)
                if obj_ent.ObjectName == "AcDbCircle":# 对象是圆,执行相应操作
                    Center1 = vtpnt(0, 0, 0)
                    # Radius1 = vtpnt(0, 0, 0)
                    Center1 = obj_ent.Center
                    Radius1 = obj_ent.Radius
                    Center1 = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [Center1[0], Center1[1], Center1[2]])
                    # startpoint1 = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [Center1[0], Center1[1], Center1[2]])
                    doc.blocks.Item("11111").AddCircle(Center1, Radius1)
                if obj_ent.ObjectName == "AcDbHatch": # 对象是填充,执行相应操作
                    # 获取填充实体的属性
                    pattern_type = obj_ent.PatternType
                    pattern_name = obj_ent.PatternName
                    pattern_angle = obj_ent.PatternAngle
                    pattern_scale = obj_ent.PatternScale
                    associativity = True
                    # 获取填充实体的边界
                    boundary_loops = []
                    for i in range(obj_ent.NumberOfLoops):
                        loop = obj_ent.GetLoopAt(i)
                        boundary_obj = []
                        for j in range(len(loop)):
                            obj = loop[j]
                            boundary_obj.append(obj)
                        boundary_loops.append(boundary_obj)

                    new_hatch = doc.blocks.Item("11111").AddHatch(pattern_type, pattern_name, associativity)

                    # 添加边界到新的填充实体
                    for boundary_obj in boundary_loops:
                        # boundary_obj列表转换为PyIDispatch对象列表
                        py_idispatch_obj_list = []
                        for obj in boundary_obj:
                            py_idispatch_obj = win32com.client.Dispatch(obj)  # 使用实际的对象
                            py_idispatch_obj_list.append(py_idispatch_obj)

                        # PyIDispatch对象列表转换为COM安全数组
                        variant_array = VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, py_idispatch_obj_list)
                        # 将转换后的对象添加到新建的填充实体的外部循环中
                        new_hatch.AppendOuterLoop(variant_array)
                        new_hatch.Evaluate()

        # 检查是否已经存在具有相同名称的块
        try:
           existing_block = doc.Blocks.Item(endblockname.TextString)    # 如果存在具有相同名称的块,则删除该块
           existing_block.Delete()
        except Exception:  # 在找不到具有相同名称的块时,Item方法会引发异常
         pass
        doc.blocks.Item("11111").Name = endblockname.TextString

        block_Insertion = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [minrecbounding[0], minrecbounding[1], 0])

        block_reference = doc.ModelSpace.InsertBlock(block_Insertion, endblockname.TextString, 1, 1, 1, 0)


        del block_origin

doc.Regen(True)  # 更新文档,使改动生效

发表于 2023-6-11 12:14 | 显示全部楼层
这是啥高级语言吖
发表于 2023-6-12 17:13 | 显示全部楼层
LPACMQ 发表于 2023-6-11 12:14
这是啥高级语言吖

头像好好看
发表于 2023-6-12 17:24 | 显示全部楼层
看着挺高级
 楼主| 发表于 2023-6-14 00:43 | 显示全部楼层
LPACMQ 发表于 2023-6-11 12:14
这是啥高级语言吖

python啦 ,无聊玩玩
 楼主| 发表于 2023-6-14 00:45 | 显示全部楼层

一般般啦随便找的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-3 06:39 , Processed in 0.494076 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表