ansys / pymechanical

Pythonic interface to Ansys Mechanical β„’
https://mechanical.docs.pyansys.com/
MIT License
38 stars 19 forks source link

[Embedding] Cannot import materials library #95

Closed pmaroneh closed 1 year ago

pmaroneh commented 1 year ago

πŸ” Before submitting the issue

🐞 Description of the bug

In the GUI one can use: import materials but in embedded mode the module is not available: image

πŸ“ Steps to reproduce

See above.

πŸ’» Which operating system are you using?

Windows

🐍 Which Python version are you using?

3.7

πŸ“¦ Installed packages

ansys-api-platform-instancemanagement==1.0.0b3
-e git+https://github.com/pyansys/pymechanical.git@a7bb62749e64b43373fb4fa9b620cc642613d5ed#egg=ansys_mechanical_core
ansys-platform-instancemanagement==1.0.2
ansys-pythonnet==3.1.0rc1
anyio==3.6.2
appdirs==1.4.4
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.3
asttokens @ file:///opt/conda/conda-bld/asttokens_1646925590279/work
attrs==22.2.0
Babel==2.11.0
backcall @ file:///home/ktietz/src/ci/backcall_1611930011877/work
beautifulsoup4==4.11.1
bleach==5.0.1
cachetools==5.2.1
certifi @ file:///C:/b/abs_4f5wo627a3/croots/recipe/certifi_1663615677642/work/certifi
cffi==1.15.1
chardet==5.1.0
charset-normalizer==3.0.1
cloudpickle @ file:///tmp/build/80754af9/cloudpickle_1632508026186/work
clr-loader==0.2.5
colorama==0.4.6
debugpy @ file:///C:/ci/debugpy_1637091961445/work
decorator @ file:///opt/conda/conda-bld/decorator_1643638310831/work
defusedxml==0.7.1
distlib==0.3.6
docutils==0.19
entrypoints==0.4
executing @ file:///opt/conda/conda-bld/executing_1646925071911/work
fastjsonschema==2.16.2
filelock==3.9.0
flit==3.8.0
flit_core==3.8.0
fqdn==1.5.1
googleapis-common-protos==1.56.4
grpcio==1.49.1
idna==3.4
importlib-metadata==5.0.0
ipykernel @ file:///C:/b/abs_21ykzkm7y_/croots/recipe/ipykernel_1662361803478/work
ipython @ file:///C:/ci/ipython_1657634415474/work
ipython-genutils==0.2.0
isoduration==20.11.0
jedi @ file:///C:/ci/jedi_1644315428289/work
Jinja2==3.1.2
json5==0.9.11
jsonpointer==2.3
jsonschema==4.17.3
jupyter-events==0.6.3
jupyter_client==7.4.9
jupyter_core==5.1.3
jupyter_server==2.1.0
jupyter_server_terminals==0.4.4
jupyterlab==3.5.2
jupyterlab-pygments==0.2.2
jupyterlab_server==2.19.0
MarkupSafe==2.1.1
matplotlib-inline @ file:///C:/ci/matplotlib-inline_1661915841596/work
mistune==2.0.4
nbclassic==0.4.8
nbclient==0.7.2
nbconvert==7.2.8
nbformat==5.7.3
nest-asyncio @ file:///C:/ci/nest-asyncio_1649829929390/work
notebook==6.5.2
notebook_shim==0.2.2
packaging==23.0
pandocfilters==1.5.0
parso @ file:///opt/conda/conda-bld/parso_1641458642106/work
pickleshare @ file:///tmp/build/80754af9/pickleshare_1606932040724/work
platformdirs==2.6.2
pluggy==1.0.0
prometheus-client==0.15.0
prompt-toolkit @ file:///tmp/build/80754af9/prompt-toolkit_1633440160888/work
protobuf==3.20.3
protoc-gen-swagger==0.1.0
psutil @ file:///C:/Windows/Temp/abs_b2c2fd7f-9fd5-4756-95ea-8aed74d0039flsd9qufz/croots/recipe/psutil_1656431277748/work
pure-eval @ file:///opt/conda/conda-bld/pure_eval_1646925070566/work
pycparser==2.21
Pygments @ file:///opt/conda/conda-bld/pygments_1644249106324/work
pyparsing @ file:///C:/Users/BUILDE~1/AppData/Local/Temp/abs_7f_7lba6rl/croots/recipe/pyparsing_1661452540662/work
pyproject_api==1.4.0
pyrsistent==0.19.3
python-dateutil @ file:///tmp/build/80754af9/python-dateutil_1626374649649/work
python-json-logger==2.0.4
pytz==2022.7.1
pywin32==302
pywinpty==2.0.10
PyYAML==6.0
pyzmq==25.0.0
requests==2.28.2
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
Send2Trash==1.8.0
six @ file:///tmp/build/80754af9/six_1644875935023/work
sniffio==1.3.0
soupsieve==2.3.2.post1
spyder-kernels @ file:///C:/ci/spyder-kernels_1634237096710/work
stack-data @ file:///opt/conda/conda-bld/stack_data_1646927590127/work
terminado==0.17.1
tinycss2==1.2.1
tomli==2.0.1
tomli_w==1.0.0
tornado @ file:///C:/ci/tornado_1662458743919/work
tox==4.3.1
tqdm==4.64.1
traitlets==5.8.1
uri-template==1.2.0
urllib3==1.26.14
virtualenv==20.17.1
wcwidth @ file:///Users/ktietz/demo/mc3/conda-bld/wcwidth_1629357192024/work
webcolors==1.12
webencodings==0.5.1
websocket-client==1.4.2
wincertstore==0.2
zipp==3.8.1
koubaa commented 1 year ago

@pmaroneh

materials.py comes from the installation at vNNN/Addins/ACT/libraries/Mechanical. You can import them if you first run:

import sys
sys.path.append('/path/to/vNNN/Addins/ACT/libraries/Mechanical')

this location has these files:

These names are quite general, and I feel a little uncomfortable with the idea of adding this path by default. This will reserve and overwrite the statements import graphics import ansys, etc, when the user uses pymechanical. @akaszynski thoughts?

koubaa commented 1 year ago

As another workaround I currently do this: material_file = material_file.replace("\", "\\") script = 'DS.Tree.Projects.Item(1).LoadEngrDataLibraryFromFile("' + material_file + '");' ExtAPI.Application.ScriptByName('jscript').ExecuteCommand(script)

pmaroneh commented 1 year ago

Hi @koubaa , thanks for your comments on this. I think the first solution is OK as long as we explain this in the doc somewhere and show some examples that use this. I don't like the second solution much as we've been told to tell our users to not use jscript and that its use is not supported (at least with standard support in standard Mechanical). So if I have to vote I think the first proposal is good.

pmaroneh commented 1 year ago

Method 1 works fine. I had an issue with Method 2: image

koubaa commented 1 year ago

@pmaroneh github comments apparently don't handle backslashes properly. Here it is correctly: material_file = get_material_file().replace("\\", "\\\\")

koubaa commented 1 year ago

@paroneh I also don't like jscript, but we can hide this behind a python API inside of pymechanical that we can change to the real API once its supported by the Mechanical team (planned for 232). I still don't like the idea of adding a bunch of generic named libraries to the user's environment by default.

koubaa commented 1 year ago

@pmaroneh a function to import a material library is now available, try this:

from ansys.mechanical.core.embedding import shims
shims.import_materials(app, material_file)

The shims module is a place where we put functions that require jscript in current releases but will have direct python apis in future releases. This protects scripts from directly invoking jscript, which we want to avoid.

I'll keep this issue open while we debate what to do about vNNN/Addins/ACT/libraries/Mechanical

pmaroneh commented 1 year ago

This is great news @koubaa !

pmaroneh commented 1 year ago

@RemishR please open a separate issue for this as this is not related to the issue mentioned in the title, and also avoid posting the same issue several times. Thank you