Reputation: 1
I am trying to use pyautocad to copy a table from one autocad drawing and paste it in another. Its important that I retain the same table style and location as well. Afterwards the script populates the table.
I've tried using both the COPY and COPYBASE methods (below) but I am still being prompted to manually select the objects and insertion points. Does anyone know the proper way to do this?
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')
Upvotes: 0
Views: 126
Reputation: 1
I was able to answer the question by utilizing the comments on this question: Autocad using pyautocad / comtypes to copy objects from one drawing to another
Working code below:
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.")
Upvotes: 0