Hi i’m trying to create a ZeroTouchNode that takes Dictionary as an input, and displays the keys as a dropdown combo box, and the values from the selected key in textboxes under.
I know that firslty the type from dynamo (IDictionary) needs to be translated to Dictionary for windows system.
As i referred from existing forum post: Input a dictionary from dynamo (DesignScript.Builtin.Dictionary) to a custom ZeroTouch node - Developers - Dynamo (dynamobim.com)
this is what i have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.DesignScript.Runtime;
using Autodesk.DesignScript.Interfaces;
namespace PrintedGeometryGUI
{
public class PrintingFormNode
{
public List<object> UI(IDictionary<string, List<int>> MaterialComposition)
{
List<object> listReturn = new List<object>();
Dictionary<string, List<int>> materialComposition = DictionaryUtility.ConvertToDictionary(MaterialComposition);
PrintingForm window = new PrintingForm(materialComposition);
var res = window.ShowDialog();
if (res.HasValue && res.Value)
{
string selectedKey = window.comboBox_material.SelectedItem.ToString();
if (materialComposition.TryGetValue(selectedKey, out List<int> values))
{
listReturn.Add(selectedKey);
listReturn.Add(values[0]);
listReturn.Add(values[1]);
listReturn.Add(values[2]);
}
}
return listReturn;
}
}
}
To which this is to convert the dictionary type:
using System.Collections.Generic;
using System.Linq;
namespace PrintedGeometryGUI
{
public static class DictionaryUtility
{
public static Dictionary<string, List<int>> ConvertToDictionary(IDictionary<string, List<int>> inputDictionary)
{
Dictionary<string, List<int>> convertedDictionary = new Dictionary<string, List<int>>();
foreach(var entry in inputDictionary)
{
convertedDictionary[(string)entry.Key] = (List<int>)entry.Value;
}
return convertedDictionary;
}
}
}
And my windows form class:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace PrintedGeometryGUI
{
/// <summary>
/// Interaktionslogik für UserControl1.xaml
/// </summary>
public partial class PrintingForm : Window
{
private Dictionary<string, List<int>> materialComposition;
public PrintingForm(Dictionary<string, List<int>> materialComposition)
{
InitializeComponent();
this.materialComposition = materialComposition;
comboBox_material.ItemsSource = materialComposition.Keys;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox_material.SelectedItem != null)
{
string selectedKey = comboBox_material.SelectedItem.ToString();
if (materialComposition.TryGetValue(selectedKey, out List<int> values))
{
if (values.Count >= 3)
{
densityTextBox.Text = values[0].ToString();
initialYieldStressTextBox.Text = values[1].ToString();
structuralBuildUpRateTextBox.Text = values[2].ToString();
}
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void cancel_button_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}
}
My node is giving an error saying “At least one element from source array could not be converted to target array type.”
Would appreciate any insights and help!