Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
7192f5216This file is pinned to the selected release. Download the original to open it in your design tool.
using System.Collections.Generic;
using OpenScadGraphEditor.Library;
using OpenScadGraphEditor.Library.IO;
using OpenScadGraphEditor.Utils;
namespace OpenScadGraphEditor.Nodes
{
public class SetVariable : ScadNode, IReferToAVariable
{
public VariableDescription VariableDescription { get; private set; }
public override string NodeTitle => $"Set {VariableDescription?.Name ?? "Variable"}";
public override string NodeDescription => "Sets a variable. Note, that in OpenSCAD variables are global and the last assignment to a variable will be used. This can be counter-intuitive. If you need a temporary variable use a 'let' block.";
// no point in having a shortcut as this is repeated for each variable
public override string NodeQuickLookup => "";
public SetVariable()
{
OutputPorts
.Geometry();
}
public override string GetPortDocumentation(PortId portId)
{
switch (portId.Port)
{
case 0 when portId.IsInput:
return "The value to which the variable should be set";
case 0 when portId.IsOutput:
return "Output 'geometry'. Variable assignments will not actually output geometry, but this can be used to link the assignment to a specific scope.";
default:
return "";
}
}
public override void SaveInto(SavedNode node)
{
node.SetData("variable_description_id", VariableDescription.Id);
base.SaveInto(node);
}
public override void RestorePortDefinitions(SavedNode node, IReferenceResolver referenceResolver)
{
SetupPorts(referenceResolver.ResolveVariableReference(node.GetDataString("variable_description_id")));
base.RestorePortDefinitions(node, referenceResolver);
}
public IEnumerable<PortId> GetPortsReferringToVariable()
{
yield return PortId.Input(0);
}
public override string Render(ScadGraph context, int portIndex)
{
if (portIndex != 0)
{
return "";
}
var value = RenderInput(context, 0).OrUndef();
return $"{VariableDescription.Name} = {value};";
}
public void SetupPorts(VariableDescription description)
{
VariableDescription = description;
var portType = description.TypeHint;
InputPorts.Clear();
InputPorts.PortType(portType, literalType: portType.GetMatchingLiteralType());
}
}
}