史上第一次堆叠溢出柱!
我在Dymola2021x工作,我试图运行一个模型,一个同事提供给我。其中一个组件的package.mo文件正在用
```fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Master_Branch/components.../filename.mos")```
Now, as far as I understood, what **should** happen is that "modelica://" in that path is the working directory, so that the recources can be found no matter where I place the project folder. However, what **does** happen is the following:
When I try to simulate the component, I get messages during translation that all the files referenced with "modelica://..." can't be found. I get errors like the following:
```javascript
无法打开文件"C:/Program /Dymola 2021x/Modelica/Master_Branch/modelica:/Master_Branch/.../filename.mos“,以便读取:
无效参数
Now _I think_ what happens is that the program does not recognise that modelica:// is supposed to be the working directory, and instead tries to find the directory modelica:// INSIDE the working directory.
My colleague does not get this error. We are both not super well-versed in Dymola, and so I was wondering if there is something basic that I might be missing? For reference, the libraries that I am using are:
```javascript
MSL3.2.3
ModelicaServices 3.2.3
AixLib 0.10.7
BuildingSystems 2.0.0-Beta
Modelica_Synchronous 0.92.2
NCDataReader2 2.5
SDF 0.41
Modelica_DeviceDrivers 1.7.0
ExternData 2.5.0
我试过查看https://specification.modelica.org/v3.4/Ch13.html上的modelica文档(13.2.3外部资源),但并没有从中获得多少信息,以及https://doc.modelica.org/om/Modelica.Utilities.Files.loadResource.html上的load.Resource()命令的文档
欢迎任何帮助或建议!干杯,谢谢,泰斯
发布于 2021-12-03 00:22:33
函数ModelicaServices.ExternalReferences.loadResource()
不返回工作目录的路径。它有着不同的用途--见下文。如果加载的库列表是正确的,它将无法工作,因为您必须引用已加载的类。
引用的错误信息似乎很奇怪。但如果没有一个不完整的例子,就很难在这一具体案例中提供帮助。不过,我会尽量提供一些可能有帮助的一般资料。
获取工作目录
要获取当前工作目录,请使用以下命令:
Modelica.Utilities.System.getWorkDirectory();
= "C:/tmp/dymola"
loadResource()的用法
loadResource()
允许您检索存储在硬盘上的modelica库中的资源(非模型文件)的绝对文件路径。典型用法是引用图像、脚本或数据集。
例如,您可以通过以下调用在硬盘上获取Modelica库的位置:
import Modelica.Utilities.Files.loadResource;
loadResource("modelica://Modelica/");
= "C:/Program Files/Dymola 2022x/Modelica/Library/Modelica 4.0.0/"
这会将绝对文件路径返回到存储当前加载的Modelica库的目录。现在,您可以扩展此调用以引用资源:
loadResource("modelica://Modelica/Resources/Scripts/Dymola/Mechanics/Rotational/CoupledClutches.mos")
= "C:/Program Files/Dymola 2022x/Modelica/Library/Modelica 4.0.0/Resources/Scripts/Dymola/Mechanics/Rotational/CoupledClutches.mos"
因此,传递给loadResource
的参数可以分解如下:
[modelica-uri][top-level-package]/[relative-file-path]
通过以下方式:
[modelica-uri]
:modelica://[package]
:当前加载的package/
:的绝对类路径斜杠是至关重要的。来自modelica规范:在包名之后包含斜杠的任何Modelica方案URI都被解释为对资源的引用。
[relative-file-path]
:路径到文件,相对于package
的位置
请记住这一点:
引用的类必须是loaded
loadResource()
不检查的,如果引用的文件存在
loadResource("modelica://Modelica/")
loadResource("modelica://Modelica")
ModelicaServices对Modelica.Utilities
ModelicaServices.ExternalReferences.loadResource()
等同于Modelica.Utilities.Files.loadResource()
,但我建议使用后者。用户可以看到Modelica
库。ModelicaSerivces
也是由Dymola在启动时自动加载的,但是它隐藏在包浏览器中。
https://stackoverflow.com/questions/70198203
复制