How can we get the user files folder?

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭
edited June 2023 in Structures

How can we get the user files folder of a project, from Mechanical?

Tagged:

Best Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    The following function can be used:

        import os
        def GetUserFileFolder(analysis):
            '''
                Return UserDir: path of the folder
            '''
            try:
                WorkDir = analysis.WorkingDir
                UserDir = os.path.dirname(WorkDir)
                for i in range(3):
                    UserDir = os.path.dirname(UserDir)
                UserDir = os.path.join(UserDir, 'user_files')
                if not os.path.exists(UserDir):
                    os.makedirs(UserDir)
                return UserDir
            except:
                ExtAPI.Log.WriteMessage("Error : Exception in GetUserFileFolder()")
                return None
    
  • Vishnu
    Vishnu Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    Answer ✓

    In case we do not have any analysis created you can get them using wbjn command:

    userfilesdir = wbjn.ExecuteCommand(ExtAPI,"""returnValue(GetUserFilesDirectory())""")
    

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    This can also be used:

    def user_files_directory():
        user_files = ''
     
        if ExtAPI.Context == 'Project':
            user_files = GetUserFilesDirectory()
        elif ExtAPI.Context == 'Mechanical':
            import wbjn
            user_files = wbjn.ExecuteCommand(ExtAPI, "returnValue(GetUserFilesDirectory())")
        return user_files