How can we get the user files folder?

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 859
500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
✭✭✭✭
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: 859
    500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
    ✭✭✭✭
    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: 216
    Solution Developer Community of Practice Member First Anniversary Name Dropper 100 Comments
    ✭✭✭✭
    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: 859
    500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
    ✭✭✭✭

    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