Get User Files Dir. in SpaceClaim

SchluppIng
SchluppIng Member Posts: 12
Name Dropper First Comment
**

Hi,

I would like to get the user files directory via python script in SpaceClaim. If I use doc_path = GetActiveWindow().Document.Path, I get a different path when I use in Workbench path = GetUserFilesDirectory()
But I want to get the path as for GetUserFilesDirectory() in SpaceClaim. To open SpaceClaim, create a surface or solid, close SpaceClaim, open SpaceClaim again is not an option for my automized python script.

Is there a similar function to get the path as for GetUserFilesDirectory() or a nice workaround?

Thx in advance.

Answers

  • James Derrick
    James Derrick Administrator, Employee Posts: 283
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    Hi Schlupping! I am not sure but I think someone from @AKD-Scripting-Team should be able to help.

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 90
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @SchluppIng Are you launching spaceclaim from workbench page? You know that SpaceClaim can be launched independently, and it does not have any connection with workbench project typically.

    What is the goal here?

  • SchluppIng
    SchluppIng Member Posts: 12
    Name Dropper First Comment
    **

    @Rajesh Meena yes, I'm launching SpaceClaim from WB.
    I use an ACT to create several geometries and static structurals in order to carry out various analyses. When starting SpaceClaim via the Python code, I pass a Python script to create the geometry. This script contains a path to an *.xml file to read in values that are important for creating the geometry.
    However, if I have several workbenches open, this *.xml file is overwritten because it is always saved in the same place. If I use the path to the user files directory, the problem should be solved.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    I usually pass the user directory to SC from whatever is calling it (e.g. Workbench). For example, in Workbench:

    SCcmds = "userfolder = r'{}' ".format(GetUserFilesDirectory())
    geometry1.RunScript(SCcmds)
    geometry1.RunScript(SCscript)
    
  • SchluppIng
    SchluppIng Member Posts: 12
    Name Dropper First Comment
    **

    Thanks for the quick response, but how do I get access to the "userfolder" variable in the SCscript?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited August 22

    In the above example it will be a variable.

    You can also append it to a script template:

    TemplateSCscript = os.path.join(installDir, 'myscript.py')
    UserSCscript = os.path.join(userdirectory, 'local_myscript.py')
    SCcmds = "userfolder = r'{}'\n".format(GetUserFilesDirectory())
    with open(TemplateSCscript,'r') as f:
        SCcmds += "".join(f.readlines()) 
    with open(UserSCscript,'w') as f:
        f.write(SCcmds)
    geometry1.RunScript(UserSCscript)
    

    This method is a bit more roundabout but has the advantage of being able to debug the SC script outside the context of WB.

  • SchluppIng
    SchluppIng Member Posts: 12
    Name Dropper First Comment
    **

    Thanks, I thought about the same to append the script file by adding/changing a line in the script, but it's not my favorite soultion.
    BTW: If you try to use the code from above, I get the following error in ACT Log:
    Code:
    SCcmds = "userfolder = r'{}'\n".format(GetUserFilesDirectory())
    ExtAPI.Log.WriteMessage(SCcmds)
    solidContainer.RunScript(SCcmds)
    solidContainer.RunScript(scriptPath)

    Error:
    File "C:\Users\user.name\AppData\Roaming\Ansys\v232\ACT\extensions\TowerCreator\main.py", line 237, in createTower
    Exception: The given path's format is not supported.
    Traceback (most recent call last):

    Therefore the it fails to send the SCcmds command/path.
    SystemError: The given path's format is not supported

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 90
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @SchluppIng

    another solution is to define your own environment variable on workbench page before running script and access it in spaceclaim script.

    import os
    
    os.environ['my_user_path'] = GetUserFilesDirectory()
    SCcmds = "import os
    os.environ['my_user_path']"
    geometry1.RunScript(SCscript)