如何使用 python 复制和粘贴 AutoCAD 表格

问题描述 投票:0回答:1

我正在尝试使用 pyautocad 从一张 AutoCAD 绘图中复制表格并将其粘贴到另一张绘图中。重要的是我也保留相同的桌子样式和位置。然后脚本填充表。

我已尝试使用 COPY 和 COPYBASE 方法(如下),但仍然提示我手动选择对象和插入点。有谁知道正确的方法吗?

import os, time
import pyautocad

def copy_table(source_file, dest_file):
    acad = pyautocad.Autocad(create_if_not_exists=True)
    os.startfile(source_file)
    time.sleep(5)
    source_doc = acad.ActiveDocument

    table = None
    for obj in acad.iter_objects('AcDbTable'):
        if obj.ObjectName == "AcDbTable":
            table = obj
            break

    if not table:
        print("Table not found in the source drawing.")
        return

    insertion_point = (1.15, 24.1162)

    try:
        acad.Application.SendCommand('ENTSEL\n')
        acad.Application.SendCommand(f'{table.Handle}\n')
        acad.Application.SendCommand(f'COPY\n')

        # I've also tried this but still got prompted to select the table
        source_doc.SendCommand(f"COPYBASE \n {table.Handle}\n {insertion_point[0]}\n {insertion_point[1]}\n")
        
        print("table selected")
    except Exception as e:
        print(e)

    os.startfile(dest_file)
    time.sleep(5)
    dest_doc = acad.ActiveDocument

    # this part works
    dest_doc.SendCommand('_PASTECLIP\n' + str(insertion_point[0] + 0) + ',' + str(insertion_point[1] + 0) + '\n')

python lisp pywin32 autocad
1个回答
0
投票

我能够通过利用这个问题的评论来回答这个问题:Autocad使用pyautocad / comtypes将对象从一个图形复制到另一个图形

工作代码如下:

import os, time
import pyautocad

def copy_table(source_file, dest_file):
    acad = pyautocad.Autocad(create_if_not_exists=True)
    os.startfile(source_file)
    time.sleep(5)
    source_doc = acad.ActiveDocument

    table = None
    for obj in acad.iter_objects('AcDbTable'):
        if obj.ObjectName == "AcDbTable":
            table = obj
            break

    if not table:
        print("Table not found in the source drawing.")
        return


    handle_string = 'COPYBASE\n'
    handle_string += '0,0,0\n'  
    handle_string += '(handent "' + table.Handle + '")\n'
    handle_string += '\n'

    try:
        source_doc.SendCommand(handle_string)
        
        print("table selected")
    except Exception as e:
        print(e)

    os.startfile(dest_file)
    time.sleep(5)
    dest_doc = acad.ActiveDocument

    # this part works
    dest_doc.SendCommand('_PASTECLIP\n' + '0,0,0\n')

    # Save changes and close documents
    source_doc.Close()
    dest_doc.Save()
    dest_doc.Close()

    print("Table copied successfully.")
© www.soinside.com 2019 - 2024. All rights reserved.