🔢

Dynamoサンプル:数字の操作

2022/06/16に公開

連番リストを作成

1~10の連番リストを作成したい場合、Code Block1..10;を入力。

リストから和のリスト(累積和)を作成

Python Scriptで、最初のほうにimport itertoolsimport operatorを加える。最後のOUT=0OUT = itertools.accumulate(IN[0])に変更する。

# Load the Python Standard and DesignScript Libraries
import sys
import clr
+ import itertools
+ import operator
# Assign your output to the OUT variable.
-    OUT=0
+    OUT = itertools.accumulate(IN[0])

エラー対処(2022以前)

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

- with adoc.LockDocument():
-    with adoc.Database as db:
-
-        with db.TransactionManager.StartTransaction() as t:
-            # Place your code below
-            # 
-            #
-
-            # Commit before end transaction
-            #t.Commit()
-            pass

+ def cumsum(xs):
+  result = [xs[0]]
+  for x in xs[1:]:
+    result.append(result[-1] + x)
+  return result

# Assign your output to the OUT variable.
+OUT = cumsum(IN[0])

小数点の桁数を指定して文字列に変換

小数点を3桁にしたい場合、Python Scriptで、一番最後のOUT=0OUT = '{:.3f}'.format(IN[0])に変更する。

# Assign your output to the OUT variable.
-    OUT=0
+    OUT = '{:.3f}'.format(IN[0])

割り算の商と余りを取得

Python Scriptで、一番最後のOUT=0OUT = divmod(IN[0], IN[1])に変更する。

# Assign your output to the OUT variable.
-    OUT=0
+    OUT = divmod(IN[0], IN[1])

距離を測点に変更

距離から「00+00」形式の文字列に変更する。この辺はPython Scriptが便利な感じ。

# Assign your output to the OUT variable.
-    OUT=0
+    num=divmod(IN[0], IN[1])
+    result = str(int(num[0])) + "+" + '{:.3f}'.format(num[1])
+    OUT = result

距離を測点に変更(リスト)

距離のリストから「00+00」形式の文字列に変更する。この辺はPython Scriptが便利な感じ。
ただし、リストに対応する必要があるので、少し記述が増えるけど、慣れればそれほど難しくない。

# Assign your output to the OUT variable.
-    OUT=0
+    result = []
+    for num1 in IN[0]:
+        num2 = divmod(num1, IN[1])
+        str1 = str(int(num2[0])) + "+" + '{:.3f}'.format(num2[1])
+        result.append(str1)
+    OUT = result

Discussion