■ 파이썬_01
Recipe 440498: Win32com - Batch repathing of Autocad Xrefs
________________________________________
This script asks for a base directory and then changes all xrefs in all drawings in all subdirectories so that they use relative paths. To use it just copy it somewhere in your target directory structure and run.
Python
# Relative-refs.pyw
"""A short python script for repathing xrefs in Autocad."""
Discussion
Where I work we had been using a poorly designed directory structure, with absolute paths to all our referance files. This caused troubles when files were moved, because they retain referances to things they shouldn't, and it prevented us from shipping our drawings in a meaningful directory structure. Being a Python programmer I decided to try and write a script to rectify the situation.
The relative path used is hard coded into the script and is specific to the directory structure at my company. It wouldn't be too difficult to also ask the user for the directory containing the referance files, but I had no reason to. My company's standards require the folder structure to be identical for every project.
Autocad does come with a separate 'referance manager' application for doing things like this. It is slower when working with many files though and is more complicated to use.
Recipe 440498: Win32com - Batch repathing of Autocad Xrefs
________________________________________
This script asks for a base directory and then changes all xrefs in all drawings in all subdirectories so that they use relative paths. To use it just copy it somewhere in your target directory structure and run.
Python
# Relative-refs.pyw
"""A short python script for repathing xrefs in Autocad."""
import win32com.client,os, os.path, tkFileDialog from Tkinter import * from tkMessageBox import askokcancel from time import sleep # Get a COM object for Autocad acad = win32com.client.Dispatch("AutoCAD.Application") def repath(filename): 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 = os.path.join('..\\x-ref\\',os.path.basename(old_path)) 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 class Logger: """A filelike object that prints its input on the screen.""" def __init__(self, logfile=None): """Takes one argument, a file like object for logging.""" print 'Starting logger...' if not logfile: self.logfile = open('relative-refs.log','w') else: self.logfile = logfile sys.stderr = self # Super cheap logging facility... sys.stdout = self # Just redirect output to a file. print 'Logger running...' def write(self, line): sys.__stdout__.write(line) self.logfile.write(line) def close(self): """The close method restores stdout and stderr to normal.""" self.logfile.close() sys.stderr = sys.__stderr__ sys.stdout = sys.__stdout__ class Tktextfile: """A file like interface to the Tk text widget.""" def __init__(self, root): """Create a scrollable text widget to be written to.""" self.root = root self.text = Text(root,width=40,height=20) self.text.pack(side=LEFT, expand=True, fill=BOTH) scrollbar = Scrollbar(root) scrollbar.pack(side=RIGHT,fill=Y) self.text.configure(yscrollcommand=scrollbar.set) scrollbar.config(command=self.text.yview) self.text.focus() def write(self, line): """Write method for file like widget.""" self.text.insert(INSERT, line) self.text.see(END) def close(self): """Fake close method.""" pass if __name__ == '__main__': if acad.Visible: acad.Visible = False root = Tk() text = Tktextfile(root) logger = Logger(text) dir = tkFileDialog.askdirectory() answer = askokcancel('RePath','Re path all dwg files in ' + dir + '?') if answer: for dirpath, subdirs, files in os.walk(dir): for name in files: ext = name.split('.')[-1] or '' # We want dwg files which are not in the x-ref directory if ext.lower() == 'dwg' and 'x-ref' not in dirpath.lower(): drawing = os.path.join(dirpath, name) try: repath(drawing) except: print 'Unable to repath drawing %s!' %drawing root.update() acad.Visible = True
Discussion
Where I work we had been using a poorly designed directory structure, with absolute paths to all our referance files. This caused troubles when files were moved, because they retain referances to things they shouldn't, and it prevented us from shipping our drawings in a meaningful directory structure. Being a Python programmer I decided to try and write a script to rectify the situation.
The relative path used is hard coded into the script and is specific to the directory structure at my company. It wouldn't be too difficult to also ask the user for the directory containing the referance files, but I had no reason to. My company's standards require the folder structure to be identical for every project.
Autocad does come with a separate 'referance manager' application for doing things like this. It is slower when working with many files though and is more complicated to use.
댓글
댓글 쓰기