Get User Files Dir. in SpaceClaim

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

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: 266
    Ansys Employee Solution Developer Community of Practice Member First Anniversary 25 Up Votes
    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: 85
    First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Likes
    ✭✭✭✭

    @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: 7
    First Comment Name Dropper
    **

    @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 Posts: 286
    First Answer First Comment 5 Likes First Anniversary
    ✭✭✭✭

    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: 7
    First Comment Name Dropper
    **

    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 Posts: 286
    First Answer First Comment 5 Likes First Anniversary
    ✭✭✭✭
    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: 7
    First Comment Name Dropper
    **

    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: 85
    First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Likes
    ✭✭✭✭

    @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)