openscad有用于字符串键关联数组(即哈希映射,即字典)的语言基元吗?或者,对于如何模拟关联数组,有什么约定吗?
到目前为止,我所能想到的只是使用向量和使用变量将索引映射到向量到人类可读的名称。这意味着没有很好的,可读的方法来定义向量,你只需要注释它。
假设我想编写类似于Python数据结构的东西:
bobbin_metrics = {
'majacraft': {
'shaft_inner_diameter': 9.0,
'shaft_outer_diameter': 19.5,
'close_wheel_diameter': 60.1,
# ...
},
'majacraft_jumbo': {
'shaft_inner_diameter': 9.0,
'shaft_outer_diameter': 25.0,
'close_wheel_diameter': 100.0,
},
# ...
}这样,我就可以以某种可以识别的散列映射的方式在模型定义中引用它,比如将bobbin_metrics['majacraft']传递给metrics之类的东西,并引用metrics['close_wheel_diameter']。
到目前为止,我尽了最大的努力
# Vector indexes into bobbin-metrics arrays
BM_SHAFT_INNER_DIAMETER = 0
BM_SHAFT_OUTER_DIAMETER = 1
BM_CLOSE_WHEEL_DIAMETER = 2
bobbin_metrics_majacraft = [
9.0, # shaft inner diameter
19.5, # shaft outer diameter
60.1, # close-side wheel diameter
# ....
];
bobbin_metrics_majacraft_jumbo = [
9.0, # shaft inner diameter
25.0, # shaft outer diameter
100.0, # close-side wheel diameter
# ....
];
bobbin_metrics = [
bobbin_metrics_majacraft,
bobbin_metrics_majacraft_jumbo,
# ...
];
# Usage when passed a bobbin metrics vector like
# bobbin_metrics_majacraft as 'metrics' to a function
metrics[BM_SHAFT_INNER_DIAMETER]我想那会管用的。但它是UG.L.Y。不完全是“我用bash编写应用程序”很难看,但距离不远。
有更好的办法吗?
我准备在openscad之外维护数据集,并在必要时为包含文件设置一个生成器,但我宁愿不要。
另外,为了纪念4月1日,我错过了闪烁的标签,不知道滚动框是否会工作?(试过:)
发布于 2019-04-04 11:10:48
我使用了OpenSCAD search()函数,这个函数在这里的手册中有记录;
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Search下面的模式允许一种形式的关联列表,它可能不是最优的,但确实提供了一种方法来建立一个字典结构和检索一个值的字符串键;
// associative searching
// dp 2019
// - define the dictionary
dict = [
["shaft_inner_diameter", 9.0],
["shaft_outer_diameter", 19.5],
["close_wheel_diameter", 60.1]
];
// specify the serach term
term = "close_wheel_diameter";
// execute the search
find = search(term, dict);
// process results
echo("1", find);
echo ("2",dict[find[0]]);
echo ("3",dict[find[0]][1]);上述生产;
Compiling design (CSG Tree generation)...
WARNING: search term not found: "l"
...
WARNING: search term not found: "r"
ECHO: "1", [2, 0]
ECHO: "2", ["close_wheel_diameter", 60.1]
ECHO: "3", 60.1 就我个人而言,我会在Python中完成这类工作,然后生成OpenSCAD作为中间文件,或者使用SolidPython库。
发布于 2021-12-26 22:03:20
使用search()且不产生任何警告的函数的示例。
available_specs = [
["mgn7c", 1,2,3,4],
["mgn7h", 2,3,4,5],
];
function selector(item) = available_specs[search([item], available_specs)[0]];
chosen_spec = selector("mgn7c");
echo("Specification was returned from function", chosen_spec);以上将产生以下产出:
ECHO: "Specification was returned from function", ["mgn7c", 1, 2, 3, 4]另一种非常类似的方法是使用带有条件语句的清单理解,就像在Python中一样。做同样的事情,看起来更简单一些。
function selector(item) = [
for (spec = available_specs)
if (spec[0] == item)
spec
];https://stackoverflow.com/questions/55448011
复制相似问题