I solved this problem in a stupid way, reading the Journal file and reading the last determined material name...
internal class MaterialBrowserEventHandler : IExternalEventHandler
{
public void Execute(UIApplication app)
{
GetMaterialText(GetJournalFilePath(app));
}
public string GetName()
{
return "MaterialBrowserEventHandler";
}
private string GetJournalFilePath(UIApplication application)
{
string filePath = application.Application.RecordingJournalFilename;
TaskDialog.Show("test", filePath);
return filePath;
}
private void GetMaterialText(string journalFilePath)
{
using (FileStream fileStream = new FileStream(journalFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] buffer = new byte[4096]; // Buffer to read file content
string prevLine = null;
// Start reading from the end of the file
long position = fileStream.Length;
while (position > 0)
{
// Calculate the position to start reading from
position = Math.Max(0, position - buffer.Length);
fileStream.Seek(position, SeekOrigin.Begin);
// Read data into the buffer
int bytesRead = fileStream.Read(buffer, 0, (int)Math.Min(buffer.Length, fileStream.Length - position));
// Convert the read bytes to string
string fileContent = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// Split the content into lines
string[] lines = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
// Iterate over lines in reverse order
for (int i = lines.Length - 1; i > 0; i--)
{
string line = lines[i];
// Use regular expression to match the part containing "OK, IDOK"
Match match = Regex.Match(line, @"OK, IDOK");
prevLine = lines[i - 1];
if (match.Success && prevLine != null)
{
// Match the field content in the previous line
Match fieldNameMatch = Regex.Match(prevLine, @"(?<=- )(\S+)(?= , Dialog)");
if (fieldNameMatch.Success)
{
string fieldName = fieldNameMatch.Value;
TaskDialog.Show("MaterialBrowser", fieldName);
Console.WriteLine("Found line containing 'OK, IDOK', field content is: " + fieldName);
return;
}
}
}
}
}
}
}