Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frame/Titleblock Macro - call image file in same parent folder 1

adibell

Mechanical
Oct 30, 2024
6
Hi all. Have been editing the Frame/Titleblock CATScript as many of you have.

I've put this on a central OneDrive location so that everyone just needs to point to that central folder and then I can update the file in that location. This means everyone is always up-to-date on the frame/Titleblock script, rather than having the script be on the user's local machine and then everyone having to replace it each time I make changes and then email it out.

However, within this script, I need it to point to an image file (the company logo) that's also in that parent folder. I've tried a number of different methods on google for how to report the parent folder but nothing works - they either give an error, or just return an empty string. Could anyone advise? As far as I understand; the Frame/Titleblock script I've written is called from a different in-built 3dexperience macro within the 'Page Layout' window that opens. Can anyone advise how I can find the folder path please?

Thanks
A
 
Replies continue below

Recommended for you

AFAIK there's no way to do it from CATScript since it's run by CATIA's scripting engine (not wscript or cscript) that can also run code not necessarily stored in a file.

However you can take advantage of the fact that titleblock scripts are stored in a directory referenced in CATIA settings. Not sure there's API to read that specific setting but it's possible to get value from catsetting file directly by reading it as binary.
 
AFAIK there's no way to do it from CATScript since it's run by CATIA's scripting engine (not wscript or cscript) that can also run code not necessarily stored in a file.

However you can take advantage of the fact that titleblock scripts are stored in a directory referenced in CATIA settings. Not sure there's API to read that specific setting but it's possible to get value from catsetting file directly by reading it as binary.
Hmm, I was just trying something that seemed to reference using an API to extract the catsetting file. However, when I hover over the little logo next to the Page Setup directory references a CATSettings folder and navigate to that folder, it seems to just contain an empty .txt file called 'Dummy_file' This might explain why when the last hotfix was pushed out, it didn;t save the Page setup Directory, even though we copied out our CATSettings file. It seemed like this particular setting wasn't saved in the CATSettings for some reason.....but then it has to be somewhere becuase it stays in there between closing and opening each session. Any ideas where this setting is probably stored please?


1733165179919.png
 
Hmm, I was just trying something that seemed to reference using an API to extract the catsetting file. However, when I hover over the little logo next to the Page Setup directory references a CATSettings folder and navigate to that folder, it seems to just contain an empty .txt file called 'Dummy_file' This might explain why when the last hotfix was pushed out, it didn;t save the Page setup Directory, even though we copied out our CATSettings file. It seemed like this particular setting wasn't saved in the CATSettings for some reason.....but then it has to be somewhere becuase it stays in there between closing and opening each session. Any ideas where this setting is probably stored please?


View attachment 1820

Oh - I found it. I've got the binary file that stores the Page Setup Directory path, and i just changed it to double check this was the correct one. Now I need to work out how on earth I make sense of it and extract this path from the file using a CATScript! (company name is redacted from the filepath!)

I'll keep working on it, but the rate I usually figure these things out, any help will be appreciated! Thankyou!

1733166392136.png
 
Take a look at the sample code that retrieves language settings:

 
Take a look at the sample code that retrieves language settings:

Thank you.

I ended managing to get it to work. It finds the CATSettings File, reads it looking for the string "DrwFrameAndTitleBlockPath" which I know is the name of the setting and comes just before the path I want to extract, then looks for the first instance of C: after this, then collects everything after "C:" until the first instance of character "Ÿ" which seems to mark the end of each CATSettings value.

It works really well, although I'm self-taught on coding purely for this specific task of writing our Frame/Titleblock script, so if there's anything in there that seems ridiculous or inefficient I'm all ears towards improving my stuff!

Function FindScriptLocation () As String
' Declare variables
Dim folderPath As String
Dim fileName As String
Dim fso As Object
Dim folder As Object
Dim file As Object
Dim fileCount As Integer
Dim filePath As String
Dim searchText As String
Dim fileContent As String
Dim found As Boolean
Dim foundPos As Integer
Dim startPos As Integer
Dim endPos As Integer
Dim TemplateFolderPath As String
Dim settingsPath As String

' Retrieve the CATIA application
Dim CATIA As Object
Set CATIA = GetObject(, "CATIA.Application")

' Access the environment variable for settings

settingsPath = CATIA.SystemService.Environ("CATUserSettingPath")
If settingsPath = "" Then
MsgBox "No folder path provided. Exiting..."
Exit Function
End If

' Specific settings file name containing the FrameTitleBlock setting
fileName = "DraftingOptions.CATSettings"
If fileName = "" Then
MsgBox "No file name provided. Exiting..."
Exit Function
End If

' Initialize FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

' Check if the file exists
If Not fso.FolderExists(settingsPath) Then
MsgBox "The specified file does not exist."
Exit Function
End If

' Concatenate the .CATSettings file name onto the folder path
filePath = settingsPath & "\" & fileName
If filePath = "" Then
MsgBox "No file path provided. Exiting..."
Exit Function
End If

' Text to search
searchText = "DrwFrameAndTitleBlockPath"

' Initialize FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

' Check if the file exists
If Not fso.FileExists(filePath) Then
MsgBox "The specified file does not exist."
Exit Function
End If

' Attempt to open the file and read its content
On Error Resume Next
Set file = fso_OpenTextFile(filePath, 1) ' 1 = ForReading
If Err.Number <> 0 Then
MsgBox "Failed to open the .CATSettings file. It may be in binary format."
Exit Function
End If
On Error GoTo 0

' Read content (may be garbage if file is binary)
fileContent = file.ReadAll
file.Close

' Find the position of the initial text
foundPos = InStr(1, fileContent, searchText)
If foundPos = 0 Then
MsgBox "The text '" & searchText & "' was not found in the file. Cannot decipher the Company Logo location folder - report to whoever is managing the Drawing Template Script"
Exit Function
End If

' Search for "C:" starting from the position of the found text
startPos = InStr(foundPos, fileContent, "C:")
If startPos = 0 Then
MsgBox "The string 'C:' was not found after the initial text."
Exit Function
End If

' Search for "Ÿ" after the "C:"
endPos = InStr(startPos, fileContent, "Ÿ")
If endPos = 0 Then
MsgBox "There was an error deciphering the folder the Company Logo is stored in - please report to whoever is managing the Drawing Template Script."
Exit Function
End If

' Extract the substring from "C:" to the end of "3dExperience Files"
TemplateFolderPath = Mid(fileContent, startPos, (endPos) - startPos)

FindScriptLocation = TemplateFolderPath

' Display the extracted text
'MsgBox "Folder path for the Frame/Title Block Template: " & vbCrLf & TemplateFolderPath

End Function
 
Great job, thanks for sharing.
Couple of suggestions:
1. Offset from a setting name to it's value is constant, so you can write it down in your code (this way you'll be able to get any path, not just on C: drive)
2. CATIA strings are null-terminated, so you better search for character with Asc(pos)=0 condition
 

Part and Inventory Search

Sponsor