CryFron,
This is "consulting level" code, but what the heck. I have taken working code and commented things out to make it more basic. I haven't tested the commented version, but it should be very close to functional. The guts to this code is the call to SessionUtil.CopyObjectFileFromVaultPermission. Almost all of the code below is either setting up this call or setting up debugging.
This is code is being called after CATParts, CATProducts, and CATDrawings are checked in. If you use it just for CATDrawings, it should work fine.
Hope this helps.
The PLMDatabase.bs script should already be called in the "After Check In" Hook. I know it is in Design Express. If it isn't there on your system you will need to create a full script and add it to the hook. The only thing missing here from that script are the original function and the setting up of SmSession.
In PLMDatabase.bs script
STEP 1
' At the top of the script add the following "Global" variables. Bad programming,
' but I find it useful to be able to switch from Testing Environment to production
' environment in the part of the script that is visible (or close it visiable) when
' I open the script in an editor.
'NOTE - this is the path that CGR,CGM, PDF files are copied to. Modify as necessary
' DO NOT ADD A TRAILING SLASH
' Used for testing
Const strDest_Path As String = "C:\temp"
' You shouldn't need the extra work path. I had to do the renaming in a separate folder
'Const strWork_Path As String = "C:\temp1"
' Used for production - comment out the previous Const and uncomment this when you
'deploy. Obviously tailor this value to the location you want your files to go.
' Const strDest_Path As String = "\\ProdServer\Smart_team_files\newfiles"
' Const strWork_Path As String = "\\ProdServer\Smart_team_files\Temp"
' This constant helps me turn the debugging screen off and on.
' Note PRINT statements will go into this window if the window is open
' They are ignored if the window doesn't exist. Not as neat as a debugger,
' but better than 300 msgboxs.
Const DEBUG As String = "YES" 'One of these values "YES" '"NO"
STEP 2.
up with the rest of the Declare lines add this one
Declare Function CopyFileFromVault(cFileName As String, SmSession As SmApplic.SmSession, FirstRec As Object ) As Integer
STEP 3.
Find the After_CheckInDocument function - the following line
Function PLMDB_AfterCheckInDocuments(ApplHndl As Long,SelectOp As String,FirstPar As Long,SecondPar As Long,ThirdPar As Long ) As Integer
Find this line
PLMDB_AfterCheckInDocuments = PLMDB.AfterCheckInDocuments (Session, RecListIn)
After that line add this. Obviously I'm not using the tmpString for anything right now.
Dim ok1 As Integer
Dim tmpString As String
ok1 = CopyFileFromVault(tmpString, Session, RecListIn)
' Your choice on how you hanlde debugging/ error messages
' if ok1 <> Err_None then
' msgbox "Copy File From Vault failed"
' end if
STEP 4. Insert this code at the bottom of the PLM_Database.bs file
Function CopyFileFromVault(cFileName As String, SmSession As SmApplic.SmSession, FirstRec As Object ) As Integer
' NOTE cFileName is not being used right now
Dim FirstPar As Long
Dim SecondRec As Object
Dim ThirdRec As Object
Dim FirstWorkObject As SmApplic.ISmObject
Dim SecondWorkObject As SmApplic.ISmObject
Dim Vault As SmApplic.ISmObject ' vault as object
'Dim Vault As Object ' vault as object
Dim VaultType As Integer ' type of vault
Dim SourceVault As Long ' source vault object id
Dim SourceDirectory As String 'source directory in vault
Dim SourceFileName As String 'source file name
Dim DestinationDirectory As String 'destination directory in vault
Dim DestinationFileName As String 'destination file name
Dim SessionUtil As SmUtil.SmSessionUtil 'main SMARTEAM service object
Dim Result As Long ' object id for refresh
Dim FileMode As Integer 'file mode in destination directory
Dim finalFile As String
' Set the Function value. If everything runs okay, set it back to err_none
CopyFileFromVault = Err_Gen
' DEBUG was set globally
If DEBUG = "YES" Then
ViewPort.Open "Debug window",3000,3000,500,500
ViewPort.Clear ' Comment this line out if you have several things that run
' one right after the other. The view window then contains the
' message from all of the runs.
' now put Print statements whereever you want
Print "Starting Program = After Check-in"
' I like to see what is actually in the FirstRec. Sometimes I'm surprised
' either by what is there or what is NOT there.
FirstRec.PrintToFile "In PLMDB Check In", "C:\rec_checkin_copy2vault.txt"
End If
Set FirstWorkObject = SmSession.ObjectStore.ObjectFromData(FirstRec.GetRecord(0),true)
Set SessionUtil = SmSession.GetService("SmUtil.SmSessionUtil"
' Vault type
VaultType = VltInWork ' Checked In vault. There is a different value for the
' Released vault
' Get Vault for WorkObject according to specific vault type
Set Vault = SessionUtil.GetVaultForObject(VaultType, FirstWorkObject)
' Get Vault object id - for local vault NULL object id
If Vault Is Nothing Then
SourceVault = NULL_OBJ_ID
' msgbox "Aborint Copy file failed - Could not get Vault ID"
' exit function
Else
SourceVault = Vault.ObjectId
End If
' Set all parameters
SourceDirectory = FirstWorkObject.Data.ValueAsString(NM_DIRECTORY)
' DestinationDirectory = SecondWorkObject.Data.ValueAsString(NM_DIRECTORY)
' NOTE: The following variable Is Set As a Global variable so that it can be modified
' at the top of the script
' DestinationDirectory = strWork_Path
DestinationDirectory = strDest_Path
' On Error Resume Next
' mkdir DestinationDirectory
' On Error GoTo 0
Print "Destination Directory: " + DestinationDirectory
SourceFileName = FirstWorkObject.Data.ValueAsString(NM_FILE_NAME)
Dim strId As String
strId = FirstWorkObject.Data.ValueAsString("TDM_ID"
Print "TDM_ID = " + strId
Print "Source File Name: " + SourceFileName
' msgbox "SourceFileName = " + SourceFileName
DestinationFileName = SourceFileName
FileMode = modReadWrite 'file mode in destination directory
' You don't need this. It allowed renaming to file to include the user who checked
' the CATDrawing in
' Dim User As String
' On Error Resume Next
' Set CATIA = GetObject(,"CATIA.Application"
'User = CATIA.SystemService.Environ("LOGNAME"
' If (User = "" Then
' User = CATIA.SystemService.Environ("USERNAME"
' End If
'Print "User set to " + User
' HERE is the guts of the function
err.clear
SessionUtil.CopyObjectFileFromVaultPermission FirstWorkObject, DestinationFileName, DestinationDirectory, FileMode
If err.number <> 0 Then
msgbox "Copy of PDF file " + DestinationFileName + " to " + DestinationDirectory + " failed!"
Print "Copy of PDF file " + DestinationFileName + " to " + DestinationDirectory + " failed!"
End If
' Dim strRev As String
' strRev = FirstWorkObject.Data.ValueAsString("REVISION"
' Dim StrCmt As String
' Dim StrCadId As String
' Dim nameLen As Integer
' nameLen = inStr(DestinationFileName,"_"
' StrCmt = FirstWorkObject.Data.ValueAsString("CN_COMMENTS"
' If inStr(uCase(DestinationFileName),".CATPART" > 0 Then'
' strCmt = FindComment(strId, "CATIA Part" ' ;As String
' strCadId = FindCadId(strId, "CATIA Part" ' ;As String
' End If
' If inStr(uCase(DestinationFileName), ".CATPRODUCT" > 0 Then
' strCmt = FindComment(strId, "CATIA Product" ' ;As String
' strCadId = FindCadId(strId, "CATIA Product" ' ;As String
' End If
' If inStr(uCase(DestinationFileName), ".CATDRAWING" > 0 Then
' strCmt = FindComment(strId, "CATIA Drawing" ' ;As String
' strCadId = FindCadId(strId, "CATIA Drawing" ' ;As String
' End If
' Print "Comment = " + strCmt
' Print "CAD ID = " + strCadId
' If ucase(strCadId) = "MISSING" Then
' strCadId = "Error: " +Mid(DestinationFileName,1,nameLen)
' End If
' Print "Revision: " + strRev
Dim strFile As String
Dim strFileOut As String
' This is the CAD folder and file name we just copied out of the vault
strFile =DestinationDirectory + "\" + DestinationFileName
If inStr(uCase(DestinationFileName),".CATPART" > 0 Or inStr(uCase(DestinationFileName), ".CATPRODUCT" > 0 Then
Print "CATPart/CATProduct Delete extra file: " + strFile
' This erases the CATPart or CATProduct itself
kill strFile
'GHP Note: set this to your desired file Name
' You can comment more of this out if the vault name is okay as the finale name
' by default it will contain the vault name which has the part number and
' and objectId number and an objectType number separated by underscores.
strFileOut = DestinationFileName + ".cgr" 'strCadId+"_"+strRev+"_"+User + ".cgr"
' finalFile = strDest_Path + "\" + strFileOut
' strFileOut =DestinationDirectory + "\" + strFileOut
strFile = strFile + ".cgr"
' Print "Rename " + strFile + " to " + strFileOut
Name strFile As strFileOut
' FileCopy strFileOut, finalFile
' kill strFileOut
Else
If inStr(uCase(DestinationfileName),".CATDRAWING" > 0 Then
Print "CATDrawing Delete extra file: " + strFile
' Kill the CATDrawing file
kill strFile
Print "CATDrawing file deleted"
' NOTE: Set this to your desired name
strFileOut = DestinationFileName + ".pdf" 'strCadId+"_"+strRev+"_"+User + ".pdf"
' finalFile = strDest_Path + "\" + strFileOut
' strFileOut =DestinationDirectory + "\" + strFileOut
strFile = strFile + ".pdf"
Print "Rename " + strFile + " to " + strFileOut
If fileExists(strFile) Then
Name strFile As strFileOut
Print "File renamed"
' FileCopy strFileOut, finalFile
' kill strFileOut
Else
Print "File did not exist and was not renamed"
msgbox "Source File missing. File NOT written to PLM " + strFileOut
End If
Else
' This would not be a CATPart, CATProduct or CATDrawing - nothing done
End If
End If
Print "Finished okay"
CopyFileFromVault = Err_None
End Function
Regards,
Hale
|