/// <summary> /// Removes Terminals when needed /// </summary> protected void RemoveTerminals() { InputOutputElement ioElement = LinkedObject as InputOutputElement; //remove Outputs int count = CountTerminals(DirectionType.Output); while (count > ioElement.OutputCount) { GraphicTerminal terminal = m_Terminals.FindLast(delegate(GraphicTerminal element) { return((element.LinkedObject as Terminal).Direction == DirectionType.Output); }); RemoveTerminal(terminal); count--; } //remove Inputs count = CountTerminals(DirectionType.Input); while (count > ioElement.InputCount) { GraphicTerminal terminal = m_Terminals.FindLast(delegate(GraphicTerminal element) { return((element.LinkedObject as Terminal).Direction == DirectionType.Input); }); RemoveTerminal(terminal); count--; } }
private void CreateElements(Circuit circuit, CircuitData circuitData, SignalList signals, bool createGraphics) { Dictionary <BaseElement, BaseElementData> elemDict = new Dictionary <BaseElement, BaseElementData>(); foreach (BaseElementData elemData in circuitData.Elements) { InputOutputElement element = null; element = ConvertElementData(elemData, createGraphics, elemDict, signals); if (element != null) { circuit.AddElement(element); } } foreach (ConnectionData connectionData in circuitData.Connections) { Connection connection = new Connection(); connection.Name = connectionData.Name; ConnectTerminals(connection, circuit, elemDict); if (connection.Terminals.Count > 0) { if (createGraphics) { GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), connection) as GraphicConnection; CreateConnectionLines(circuit, graphicConnection, connectionData); } circuit.AddElement(connection); } } }
protected override void AddTerminals() { InputOutputElement ioElement = LinkedObject as InputOutputElement; //add the Output GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Terminal), ioElement.Outputs[0]); element.Location = new PointF(GraphicConstants.GraphicSignalWidth, GraphicConstants.GraphicSignalHeight / 2); AddTerminal(element as GraphicTerminal); }
private ConnectionNode SearchNode(string nodeName, Circuit circuit, GraphicConnection graphicConnection) { //node is already part of the connection if (graphicConnection.Children != null) { foreach (GraphicBaseElement element in graphicConnection.Children) { if (element is ConnectionNode && element.Name.Equals(nodeName)) { return(element as ConnectionNode); } } } //so the node must be part of a terminal, the name gives the clue string[] str = nodeName.Split(new char[] { '/' }, 2); if (str.Length == 2) { string elementName = str[0]; string terminalName = str[1]; foreach (BaseElement element in circuit) { //for security reasons check whether the found terminal is connected to the connection too //although the name should be all that's needed Connection connection = graphicConnection.LinkedObject as Connection; if (element is InputOutputElement && element.Name.Equals(elementName)) { InputOutputElement io = element as InputOutputElement; foreach (Terminal terminal in io.Inputs) { if (connection.Terminals.Contains(terminal) && terminal.Name.Equals(terminalName)) { return((terminal.LinkedObject as GraphicTerminal).ConnectionNode); } } foreach (Terminal terminal in io.Outputs) { if (connection.Terminals.Contains(terminal) && terminal.Name.Equals(terminalName)) { return((terminal.LinkedObject as GraphicTerminal).ConnectionNode); } } } } } return(null); }
protected virtual void BuildBody() { GraphicsPath bodypath = new GraphicsPath(); int termCount = 0; InputOutputElement ioElement = LinkedObject as InputOutputElement; if (ioElement != null) { termCount = ioElement.InputCount > ioElement.OutputCount ? ioElement.InputCount : ioElement.OutputCount; termCount--; } bodypath.AddRectangle(new Rectangle(0, 0, GraphicConstants.GraphicElementWidth, GraphicConstants.GraphicElementUpperSpace + GraphicConstants.GraphicElementLowerSpace + termCount * GraphicConstants.GraphicElementTerminalSpace)); //add terminals AddTerminals(); m_Body = new GraphicShape(bodypath); }
private void ConnectTerminals(Connection connection, Circuit circuit, Dictionary <BaseElement, BaseElementData> elemDict) { foreach (BaseElement be in circuit) { if (be is InputOutputElement == false) { continue; } BaseElementData elemData = elemDict[be]; if (elemData == null) { continue; } InputOutputElement io = be as InputOutputElement; if (elemData is IOElementData) { IOElementData ioelemData = elemData as IOElementData; ProcessTerminalList(ioelemData.Inputs, io.Inputs, connection); ProcessTerminalList(ioelemData.Outputs, io.Outputs, connection); } if (elemData is InputElementData) { InputElementData ioelemData = elemData as InputElementData; ProcessTerminalList(ioelemData.Outputs, io.Outputs, connection); } else if (elemData is ConstantInputData) { ConstantInputData ioelemData = elemData as ConstantInputData; ProcessTerminalList(ioelemData.Outputs, io.Outputs, connection); } if (elemData is ClockData) { ClockData ioelemData = elemData as ClockData; ProcessTerminalList(ioelemData.Outputs, io.Outputs, connection); } if (elemData is OutputElementData) { OutputElementData ioelemData = elemData as OutputElementData; ProcessTerminalList(ioelemData.Inputs, io.Inputs, connection); } } }
/// <summary> /// Adds terminals when needed /// </summary> protected virtual void AddTerminals() { InputOutputElement ioElement = LinkedObject as InputOutputElement; //add Outputs int count = CountTerminals(DirectionType.Output); while (count < ioElement.OutputCount) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Terminal), ioElement.Outputs[count]); if (ioElement.Outputs[count].Negated) { element.Location = new PointF(GraphicConstants.GraphicElementWidth, GraphicConstants.GraphicElementUpperSpace + count * GraphicConstants.GraphicElementTerminalSpace - 3); } else { element.Location = new PointF(GraphicConstants.GraphicElementWidth, GraphicConstants.GraphicElementUpperSpace + count * GraphicConstants.GraphicElementTerminalSpace); } AddTerminal(element as GraphicTerminal); count++; } //add Inputs count = CountTerminals(DirectionType.Input); while (count < ioElement.InputCount) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Terminal), ioElement.Inputs[count]); if (ioElement.Inputs[count].Negated) { element.Location = new PointF(-GraphicConstants.GraphicTerminalWidth, GraphicConstants.GraphicElementUpperSpace + count * GraphicConstants.GraphicElementTerminalSpace - 3); } else { element.Location = new PointF(-GraphicConstants.GraphicTerminalWidth, GraphicConstants.GraphicElementUpperSpace + count * GraphicConstants.GraphicElementTerminalSpace); } element.Angle = 180; AddTerminal(element as GraphicTerminal); count++; } }
private void DeleteElement(GraphicBaseElement element) { if (m_Editor == null) { return; } if (element is GraphicInputOutputElement) { //disconnect the element from all all connections InputOutputElement logicIO = (element.LinkedObject as InputOutputElement); foreach (Terminal terminal in logicIO.Inputs) { if (terminal.IsConnected == false) { continue; } Connection connection = terminal.Connection; //terminal.Disconnect(); connection.DisconnectTerminal(terminal); if (connection.Terminals.Count == 0) { m_Editor.RemoveElement(connection.LinkedObject as GraphicConnection); } } foreach (Terminal terminal in logicIO.Outputs) { if (terminal.IsConnected == false) { continue; } Connection connection = terminal.Connection; terminal.Disconnect(); if (connection.Terminals.Count == 0) { m_Editor.RemoveElement(connection.LinkedObject as GraphicConnection); } } //delete the element m_Editor.RemoveElement(element); } }
//private Connection ConvertConnectionData(ConnectionData connectionData) //directly done in CreateElements() //{ // Connection connection = new Connection(); // connection.Name = connectionData.Name; // return connection; //} private InputOutputElement ConvertElementData(BaseElementData elemData, bool createGraphics, Dictionary <BaseElement, BaseElementData> elemDict, SignalList signals) { InputOutputElement io = null; //determine type string desiredType = String.Empty; if (elemData is ClockData) { desiredType = typeof(Clock).Name; } if (elemData is ConstantInputData) { desiredType = typeof(ConstantInput).Name; } if (elemData is InputElementData) { desiredType = typeof(SignalInput).Name; } if (elemData is OutputElementData) { desiredType = typeof(SignalOutput).Name; } if (elemData is IOElementData) { desiredType = (elemData as IOElementData).Type; } if (elemData is MacroElementData) { desiredType = typeof(Macro).Name; MacroCache cache = MacroCache.Instance; MacroElementData macroElementData = elemData as MacroElementData; io = cache.GetMacro(macroElementData.Type); if (io == null) { string loaded = cache.LoadMacro(macroElementData.Reference); if (String.IsNullOrEmpty(loaded)) { throw new MacroReferenceNotFoundException(String.Format("Macro not found: \"{0}\".", macroElementData.Type)); } else if (loaded.CompareTo(macroElementData.Type) != 0) { throw new MacroReferenceTypeMismatchException(String.Format("Desired macro type \"{0}\" mismatches type \"{1}\" contained in file \"{2}\".", macroElementData.Type, loaded, macroElementData.Reference)); } io = cache.GetMacro(macroElementData.Type); } if (createGraphics) { GraphicBaseElement graphic = GraphicObjectFactory.CreateInstance(typeof(Macro), io); graphic.Location = new PointF(elemData.X, elemData.Y); } } if (String.IsNullOrEmpty(desiredType)) { throw new NotImplementedException(String.Format("Restoring of Type {0} not implemented", elemData.GetType().Name)); //return null; } //create instance of io element type type (not macros) foreach (Type type in m_ElementTypes) { if (type.Name.Equals(desiredType)) { io = (InputOutputElement)Activator.CreateInstance(type); if (elemData is InputElementData) { InputElementData inelemData = elemData as InputElementData; if (signals != null) { foreach (Signal signal in signals) { if (signal.Name.Equals(inelemData.SignalName)) { (io as SignalInput).Signal = signal; } } } } if (createGraphics) { GraphicBaseElement graphic = GraphicObjectFactory.CreateInstance(type, io); graphic.Location = new PointF(elemData.X, elemData.Y); } break; } } if (io == null) { return(null); } //restore terminals if (elemData is ClockData) { ClockData clockData = elemData as ClockData; io.OutputCount = clockData.Outputs.Length; RestoreTerminals(clockData.Outputs, io.Outputs); Clock clock = io as Clock; clock.HighDuration = clockData.HighDuration; clock.LowDuration = clockData.LowDuration; } if (elemData is ConstantInputData) { ConstantInputData inelemData = elemData as ConstantInputData; StateTypeConverter stateconv = new StateTypeConverter(); (io as ConstantInput).State = (State)stateconv.ConvertFromString(inelemData.State.Trim()); io.OutputCount = inelemData.Outputs.Length; RestoreTerminals(inelemData.Outputs, io.Outputs); } if (elemData is InputElementData) { InputElementData inelemData = elemData as InputElementData; io.OutputCount = inelemData.Outputs.Length; RestoreTerminals(inelemData.Outputs, io.Outputs); if (signals != null) { foreach (Signal signal in signals) { if (signal.Name.Equals(inelemData.SignalName)) { (io as SignalInput).Signal = signal; } } } } if (elemData is OutputElementData) { OutputElementData outelemData = elemData as OutputElementData; (io as SignalOutput).SignalName = outelemData.SignalName; io.InputCount = outelemData.Inputs.Length; RestoreTerminals(outelemData.Inputs, io.Inputs); } if (elemData is IOElementData || elemData is MacroElementData) { IOElementData ioelemData = elemData as IOElementData; io.InputCount = ioelemData.Inputs.Length; io.OutputCount = ioelemData.Outputs.Length; RestoreTerminals(ioelemData.Inputs, io.Inputs); RestoreTerminals(ioelemData.Outputs, io.Outputs); io.UpdateIndex = ioelemData.Index; io.UnitDelay = ioelemData.UnitDelay; io.NegEdgeDelay = ioelemData.NegativeEdgeDelay; io.PosEdgeDelay = ioelemData.PositiveEdgeDelay; } io.Name = elemData.Name; elemDict.Add(io, elemData); return(io); }
private BaseElementData ConvertIOElement(BaseElement element) { BaseElementData elemData = null; if (element is Macro) { Macro macro = (Macro)element; MacroElementData macroData = new MacroElementData(macro.Name, macro.TypeName, macro.FileReference); macroData.Inputs = ConvertTerminalArray(macro.Inputs); macroData.Outputs = ConvertTerminalArray(macro.Outputs); macroData.Index = macro.UpdateIndex; macroData.UnitDelay = macro.UnitDelay; macroData.NegativeEdgeDelay = macro.NegEdgeDelay; macroData.PositiveEdgeDelay = macro.PosEdgeDelay; elemData = macroData; } if (element is Clock) { Clock clock = (Clock)element; ClockData clockData = new ClockData(clock.Name, clock.LowDuration, clock.HighDuration); clockData.Outputs = ConvertTerminalArray(clock.Outputs); elemData = clockData; } if (element is SignalInput) { SignalInput sigInput = (SignalInput)element; InputElementData inData = new InputElementData(sigInput.Name, sigInput.SignalName); inData.Outputs = ConvertTerminalArray(sigInput.Outputs); elemData = inData; } else if (element is ConstantInput) { ConstantInput conInput = (ConstantInput)element; ConstantInputData inData = new ConstantInputData(conInput.Name, conInput.State.ToString()); inData.Outputs = ConvertTerminalArray(conInput.Outputs); elemData = inData; } if (element is SignalOutput) { SignalOutput sigOutput = (SignalOutput)element; OutputElementData outData = new OutputElementData(sigOutput.Name, sigOutput.SignalName); outData.Inputs = ConvertTerminalArray(sigOutput.Inputs); elemData = outData; } if (elemData == null && element is InputOutputElement) { InputOutputElement io = (InputOutputElement)element; IOElementData ioData = new IOElementData(io.Name, element.GetType().Name); ioData.Inputs = ConvertTerminalArray(io.Inputs); ioData.Outputs = ConvertTerminalArray(io.Outputs); ioData.Index = io.UpdateIndex; ioData.UnitDelay = io.UnitDelay; ioData.NegativeEdgeDelay = io.NegEdgeDelay; ioData.PositiveEdgeDelay = io.PosEdgeDelay; elemData = ioData; } if (elemData == null) { return(null); } if (element.LinkedObject != null) { GraphicInputOutputElement grIO = element.LinkedObject as GraphicInputOutputElement; elemData.X = grIO.Location.X; elemData.Y = grIO.Location.Y; } return(elemData); }
private void SetPayload(FrameworkElement element, bool isFlippedHorizontally) { object simulinkNodeElement; Guid? guid = GlobalVariableManager.LastSelectedElementIdentifer; switch (element.Name) { case "Constant": { Constant constant = (element as Constant); simulinkNodeElement = new InputElement(SimulinkInputType.Constant, guid); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(constant.Value), constant.Value); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Step": { Step step = (element as Step); simulinkNodeElement = new InputElement(SimulinkInputType.Step, guid); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(step.StepTime), step.StepTime); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(step.InitialValue), step.InitialValue); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(step.FinalValue), step.FinalValue); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(step.SampleTime), step.SampleTime); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Ramp": { Ramp ramp = (element as Ramp); simulinkNodeElement = new InputElement(SimulinkInputType.Ramp, guid); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(ramp.Slope), ramp.Slope); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(ramp.StartTime), ramp.StartTime); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add(nameof(ramp.InitialOutput), ramp.InitialOutput); ((ISimulinkNodeElement <SimulinkInputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Scope": simulinkNodeElement = new OutputElement(SimulinkOutputType.Scope, guid); break; case "Display": simulinkNodeElement = new OutputElement(SimulinkOutputType.Display, guid); break; case "TransferFunction": { TransferFunction transferFunction = (element as TransferFunction); simulinkNodeElement = new InputOutputElement(SimulinkInputOutputType.TransferFunction, guid); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(transferFunction.NumeratorCoefficients), transferFunction.NumeratorCoefficients); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(transferFunction.DenominatorCoefficients), transferFunction.DenominatorCoefficients); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "PidController": { PidController pidController = (element as PidController); simulinkNodeElement = new InputOutputElement(SimulinkInputOutputType.PidController, guid); // Selected Controller ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.SelectedPidController), pidController.SelectedPidController); // Controller Parameters ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.Proportional), pidController.Proportional); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.Integral), pidController.Integral); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.Derivative), pidController.Derivative); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.FilterCoefficient), pidController.FilterCoefficient); // Controller Initial Conditions ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.Integrator), pidController.Integrator); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(pidController.Filter), pidController.Filter); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Integrator": { Integrator integrator = (element as Integrator); simulinkNodeElement = new InputOutputElement(SimulinkInputOutputType.Integrator, guid); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(integrator.InitialCondition), integrator.InitialCondition); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Sum": { Sum sum = (element as Sum); simulinkNodeElement = new InputOutputElement(SimulinkInputOutputType.Sum, guid); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).NumberOfInputs = sum.Signs.CountNonEmpty(); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(sum.Signs), sum.Signs); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; case "Gain": { Gain gain = (element as Gain); simulinkNodeElement = new InputOutputElement(SimulinkInputOutputType.Gain, guid); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add(nameof(gain.Value), gain.Value); ((ISimulinkNodeElement <SimulinkInputOutputType>)simulinkNodeElement).Properties.Add("FlipHorizontally", isFlippedHorizontally); } break; default: throw new InvalidOperationException(); } SimulinkNodeElement = simulinkNodeElement; }