Im looking for a good way to obtain a files last saved/modified date. The purpose is to create a metric that can find the days in state (i.e. 13 days in manufacturing approval). The best i could find online was a stack overflow response by @AmenJlili saying to use ISwDMDocument19. However IT is being difficult to obtain/release the licence key for swDocMgr (im just an intern they don’t trust me yet). Is there anyway to alter this code that might help me find this metric?
Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using SolidWorks.Interop.swdocumentmgr;
using EPDM.Interop.epdm;
namespace PDMFileDetails
{
class Program
{
private static IEdmVault5 vault;
private static SwDMClassFactory swClassFactory;
private static SwDMApplication swDocMgr;
[STAThread]
private static void Main(string[] args)
{
string vaultName = "PDM Sandbox"; // Replace with your actual vault name
string rootFolderPath = @""; // Adjust this to match your root folder path
string requestedProjID = ""; // Replace with the PROJ_ID you are looking for
try
{
// Initialize Document Manager
swClassFactory = new SwDMClassFactory();
swDocMgr = (SwDMApplication)swClassFactory.GetApplication("your license key");
vault = new EdmVault5Class();
vault.LoginAuto(vaultName, 0);
if (vault.IsLoggedIn)
{
Console.WriteLine($"Connected to vault: {vaultName}");
List<string> matchingFiles = new List<string>();
IEdmFolder5 rootFolder = vault.GetFolderFromPath(rootFolderPath);
TraverseFolder(rootFolder, requestedProjID, matchingFiles);
Console.WriteLine("Files with matching PROJ_ID:");
foreach (var file in matchingFiles)
{
Console.WriteLine(file);
}
// Write status update email
WriteStatusUpdateEmail(matchingFiles);
}
else
{
Console.WriteLine("Failed to log in to the vault.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
private static void TraverseFolder(IEdmFolder5 folder, string requestedProjID, List<string> matchingFiles)
{
try
{
Console.WriteLine($"Accessing folder: {folder.Name}");
IEdmPos5 pos = folder.GetFirstFilePosition();
while (!pos.IsNull)
{
try
{
IEdmFile5 file = folder.GetNextFile(pos);
if (file != null)
{
var configurations = GetConfigurations(file);
foreach (var config in configurations)
{
var projID = GetVariableValue(file, "PROJ_ID", config, folder.ID);
if (projID == requestedProjID)
{
string state = GetFileState(file);
string dateModified = GetFileDateModified(file, folder.ID);
matchingFiles.Add($"{file.Name}, State: {state}, Date Modified: {dateModified}");
break;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing file: {ex.Message}");
}
}
IEdmPos5 subPos = folder.GetFirstSubFolderPosition();
while (!subPos.IsNull)
{
try
{
IEdmFolder5 subFolder = folder.GetNextSubFolder(subPos);
TraverseFolder(subFolder, requestedProjID, matchingFiles);
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing subfolder: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing folder {folder.Name}: {ex.Message}");
}
}
static List<string> GetConfigurations(IEdmFile5 file)
{
var configurations = new List<string>();
IEdmStrLst5 configList = file.GetConfigurations();
IEdmPos5 pos = configList.GetHeadPosition();
while (!pos.IsNull)
{
try
{
string configName = configList.GetNext(pos);
configurations.Add(configName);
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing configuration: {ex.Message}");
}
}
return configurations;
}
public static string GetVariableValue(IEdmFile5 file, string variableName, string config, int folderID)
{
if (file == null)
throw new ArgumentNullException(nameof(file));
if (string.IsNullOrEmpty(variableName))
throw new ArgumentNullException(nameof(variableName));
var enumVariable = file.GetEnumeratorVariable() as IEdmEnumeratorVariable10;
if (enumVariable == null)
{
Console.WriteLine("Failed to get enumerator variable.");
return "N/A";
}
try
{
enumVariable.GetVar2(variableName, config, folderID, out object variableValue);
if (variableValue == null)
{
return "N/A";
}
return variableValue.ToString() ?? "N/A";
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving variable {variableName} in configuration {config}: {ex.Message}");
return "N/A";
}
}
public static string GetFileState(IEdmFile5 file)
{
try
{
return file.CurrentState.Name;
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving file state: {ex.Message}");
return "N/A";
}
}
public static string GetFileDateModified(IEdmFile5 file, int folderID)
{
try
{
// Initialize Document Manager
SwDMClassFactory swClassFactory = new SwDMClassFactory();
SwDMApplication swDocMgr = (SwDMApplication)swClassFactory.GetApplication("your_license_key");
// Get the document path from PDM
string documentPath = file.GetLocalPath(folderID);
if (string.IsNullOrEmpty(documentPath))
{
Console.WriteLine($"File path is empty: {documentPath}");
return "File path is empty";
}
// Load the document using SolidWorks Document Manager
SwDmDocumentType docType = SwDmDocumentType.swDmDocumentUnknown;
SwDmDocumentOpenError retVal;
ISwDMDocument19 swDoc = (ISwDMDocument19)swDocMgr.GetDocument(documentPath, docType, false, out retVal);
if (swDoc != null)
{
DateTime lastSavedDate = DateTime.Parse(swDoc.LastSavedDate2.ToString());
return lastSavedDate.ToString();
}
else
{
Console.WriteLine($"Failed to open document: {documentPath}, error: {retVal}");
return "N/A";
}
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving file date modified: {ex.Message}");
return "N/A";
}
}
private static void WriteStatusUpdateEmail(List<string> matchingFiles)
{
try
{
string emailContent = "Status Update for Project ID: 11953\n\n";
emailContent += "Total files processed: \n";
emailContent += "There is files in state for more than 10 days\n";
emailContent += "Name of File, state, days in state \n";
foreach (var file in matchingFiles)
{
emailContent += file + "\n";
}
string emailFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StatusUpdateEmail.txt");
File.WriteAllText(emailFilePath, emailContent);
Console.WriteLine($"Status update email saved to: {emailFilePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error writing status update email: {ex.Message}");
}
}
}
}