首页 > 解决方案 > 在蚱蜢中重新编号路径时,如何修复已存在具有相同键的条目?

问题描述

在蚱蜢中编码以尝试将我的树枝从 {0}、{1}、...重新编号为分支 {230}、{234}、...的差异索引。只有第一棵树的命名更规则,第二棵树来自一棵更大的树,我选择了这些树枝,因为我需要操纵它们。但是,在操作它们时,我必须将分支索引从 {230}、{234} .... 更改为以零开头的索引,以匹配传入数据的索引。结果,我尝试在 python 中操作我的数据以反转之前所做的操作。

我在 python 中尝试了一个代码,接收我需要的分支索引并根据 Rhino/Grasshopper 语法输出修改后的索引

import rhinoscriptsyntax as rs
for i in x:
    a = y.RenumberPaths("%s" %i)

具有操纵分支索引的数据树的预期输出。错误:运行时错误 (ArgumentException):已存在具有相同键的条目。

回溯:第 13 行,在脚本中

第 13 行只是说a = y.Renumber...

标签: pythongrasshopper

解决方案


我不熟悉 python API,但在 C# 中我会做类似的事情:

// inputDataTree is the tree you want to renumber, ideally you would change
// the DataTree<object> for your data type such as DataTree<Curve> or whatever.
DataTree<object> dataTree = new DataTree<object>();

for( int i = 0; i < inputDataTree.BranchCount; ++i )
{

    GH_Path path = new GH_Path(i);

    for( int j = 0; j < inputDataTree.Branch(i).Count; ++j )
    {

        // If you don't need the j index, you could compute the 
        // path in the outer loop. You can add logic to how you 
        // create branches.
        // GH_Path path = new GH_Path(i, j);

        dataTree.Add( inputDataTree.Branch(i)[j], path );

    }

}

推荐阅读