Code:
;---###Scripted by David Baldacchino 08/10/2007, with lots of help from the Discussion Forum at AutoHotKey.com. Thanks guys!!
;---###Modified by David Kingham 09/16/2007
i = 0
Loop, *central.rvt, 0, 0 ; Don't include folders (0) or Recurse into subfolders (0)
;---#### The script is designed to be placed in the location of the central file. First we're going to loop through
;---#### all files in the folder to check if there is only one central file. If there are more files that match the
;---#### searched pattern, the script will issue a dialog, open the folder and terminate execution.
{
i := i+1
CENTRALFILE = %A_LoopFileName%
}
if i > 1
{
; This will execute if more than one central exists
MsgBox, 16, Problem encountered!!, %i% Central Files were found! Please make sure there is only one Central File in the folder.
Goto, LABEL1
}
Else if i = 0
{
; This will execute if no central is found
MsgBox, 16, Problem encountered!!, No Central File was found!
Goto, LABEL1
}
Goto, LABEL2
LABEL1: ;---------------------------Activate or open the central file folder
IfWinExist %A_WorkingDir%
{
WinActivate
ExitApp
}
Else
{
Run %A_WorkingDir%
ExitApp
}
LABEL2: ;---------------------------Check discipline
DSCPLN := SubStr(CENTRALFILE,-14,1) ; Pick the 14th character to determine the discipline
If DSCPLN = A
{
DISCIPLINE = Architecture
Goto, LABEL3
}
If DSCPLN = S
{
DISCIPLINE = Structure
Goto, LABEL3
}
If DSCPLN = M
{
DISCIPLINE = MEP
Goto, LABEL3
}
If DSCPLN = E
{
DISCIPLINE = MEP
Goto, LABEL3
}
If DSCPLN = P
{
DISCIPLINE = MEP
Goto, LABEL3
}
else
{
MsgBox, 16, Undefined Discipline!!, The discipline designator ("%DSCPLN%") in the filename is incorrect.`n Please rename the file correctly and try again.
Goto, LABEL1
}
LABEL3: ;---------------------------Create Local File
;---#### Create local file folder in case it doesn't exist. Delete the backup copy of the local file. Rename the existing local file so it becomes the new backup.
;---#### Then copy the central file to this folder and rename it to reflect the user's username, thus becoming the new local file.
StringTrimRight, PARTFILE, CENTRALFILE, 11
DESTINATION = C:\Revit Local Files\%PARTFILE%
FileCreateDir, C:\Revit Local Files\Archive
LOCALFILE = %PARTFILE%%username%.rvt ; and replace with "username.rvt"
FileRecycle, C:\Revit Local Files\Archive\%PARTFILE% ; If "_BAK" file exists, delete it and keep in recycle bin
FileMoveDir, %DESTINATION%, C:\Revit Local Files\Archive\%PARTFILE%, 2 ; Rename the local (if exists) by appending "_BAK" to the filename
FileCreateDir, %DESTINATION%
FileCopy, %CENTRALFILE%, %DESTINATION%, 0 ; Do not overwrite existing files (0). Copy central to destination...
FileMove, %DESTINATION%\%CENTRALFILE%, %DESTINATION%\%LOCALFILE% ; and rename copied central to appropriate local filename.
;---#### Now start the correct flavor of Revit, but first check if it is installed and if it is already running, to prevent a new session from opening.
;---#### Remove the user's username from Revit.ini so Revit uses the Username System Variable. Then open the new local file.
AppPath = %A_ProgramFiles%\Revit %Discipline% 2008\Program\
IfNotExist, %AppPath%
{
MsgBox, 20, Application not installed!!, The local file "%LOCALFILE%" has been created and would typically be opened with Revit %Discipline% 2008.`nThis application is not installed on your computer. Do you want to use another discipline's Revit application?
IfMsgBox No, ExitApp
IfMsgBox Yes
{
DISCIPLINE = Architecture
AppPath = %A_ProgramFiles%\Revit %Discipline% 2008\Program\
IfNotExist, %AppPath%
{
DISCIPLINE = Structure
}
AppPath = %A_ProgramFiles%\Revit %Discipline% 2008\Program\
IfNotExist, %AppPath%
{
DISCIPLINE = MEP
}
AppPath = %A_ProgramFiles%\Revit %Discipline% 2008\Program\
IfNotExist, %AppPath%
{
MsgBox, 16, Application not found!!, No version of Revit is installed on this computer. Please contact IT for assistance.
ExitApp
}
}
}
IniWrite, %DESTINATION%, %AppPath%\Revit.ini, Directories, ProjectPath ;
{
; Open Revit Discipline 2008 and open the file
Run %AppPath%Revit.exe
WinWait, Revit Architecture 2008 - [Project1 - Drafting View: Start],
IfWinNotActive, Revit Architecture 2008 - [Project1 - Drafting View: Start], , WinActivate, Revit Architecture 2008 - [Project1 - Drafting View: Start],
WinWaitActive, Revit Architecture 2008 - [Project1 - Drafting View: Start],
Send, {ALTDOWN}fo{ALTUP}
WinWait, Open,
IfWinNotActive, Open, , WinActivate, Open,
WinWaitActive, Open,
Send, %LOCALFILE% {TAB}{ALTDOWN}w{ALTUP}{DOWN}{ALTDOWN}o{ALTUP}{ALTDOWN}o{ALTUP}
}
#Persistent
SetTimer, MsgBoxCheck, 1000
MsgBoxCheck:
If WinExist("Revit", "This Central File has been copied or moved from", "ahk_class #32770")
{
WinClose
ExitApp
}
Return