我正在编写一个脚本,可以帮助我的公司在AutoCAD中启动一个新的设计项目。该脚本应该通过将我们的CAD文件复制到新项目文件夹,重命名文件并根据新名称更新外部引用来自动化项目设置。我现在真的只对python感到满意,并尝试用python解决问题,但成功有限。我将运行该程序,它将适用于某些文件,然后对其他人失败。我将再次运行该程序,它将适用于不同的文件,而其他文件则失败。她是代码的关键部分。我的代码有问题吗?
def repath(filename, project_number):
print('Repathing %s...' %filename)
doc = acad.Documents.Open(filename)
blocks = doc.Database.Blocks # Internally xrefs are just blocks!
xrefs = [item for item in blocks if item.IsXRef]
if xrefs:
for xref in xrefs:
old_path = xref.Path
new_path = old_path.replace('CXXXXX',project_number)
xref.Path = new_path
print('Old path name was %s, new path name is %s.\n' %(old_path, new_path))
try:
doc.Close(True) # Close and save
except: # Something when wrong,
doc.Close(False) # close then report it
raise
answer = messagebox.askokcancel('DWG Path','Create new DWG folder for project {}'.format(project_number) + directory + '?')
if answer:
shutil.copytree(os.path.dirname('Q:/Standards/DWGS/_Obsolete/2018/New Project Template/DWGS/1 - Design'),directory+'/DWGS')
for dirpath, subdirs, files in os.walk(directory+'/DWGS/1 - Design'):
for filename in files:
ext = filename.split('.')[-1]
if ext.lower() =='dwg':
os.chdir(dirpath)
new_name = filename.replace("CXXXXX",project_number)
os.rename(filename,new_name)
files = [
'-C0.01 General Notes.dwg',
'-C1.00-Existing Conditions Plan.dwg',
'-C2.00-Demolition Plan.dwg',
'-C3.00-Site Plan.dwg',
'-C3.20-Signage & Pavement Markings Plan.dwg',
'-C4.00-Erosion Control Plan.dwg',
'-C5.00-Grading & Storm Drainage Plan.dwg',
'-C6.00-Utility Plan.dwg',
'-C9.00-Site Details.dwg',
'-C9.10-Erosion Control Details.dwg',
'- Storm Drainage Details.dwg',
'-C9.30- Utility Details.dwg',
'-L1.00-Planting Plan.dwg',
'-L2.00-Planting & Soils Details.dwg'
]
for dwg in files:
filename = project_number+dwg
drawing = os.path.join(directory+'/DWGS/1 - Design/Sheets/',filename)
try:
repath(drawing,project_number)
except:
print('Unable to repath drawing %s!' %drawing)
root.update()
发布于 2018-09-19 13:56:13
你在做什么模块?
https://stackoverflow.com/questions/-100002692
复制相似问题