private void combo_SymbolName_SelectedIndexChanged(object sender, EventArgs e) { if ((String)combo_SymbolName.SelectedItem == "AndGate") { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(AndGate), new AndGate(5)); element.Location = new PointF(8, 0); symbolView1.Element = element; } if ((String)combo_SymbolName.SelectedItem == "OrGate") { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(OrGate), new OrGate(5)); element.Location = new PointF(8, 0); symbolView1.Element = element; } if ((String)combo_SymbolName.SelectedItem == "NandGate") { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(NandGate), new NandGate(5)); element.Location = new PointF(8, 0); symbolView1.Element = element; } if ((String)combo_SymbolName.SelectedItem == "NorGate") { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(NorGate), new NorGate(5)); element.Location = new PointF(8, 0); symbolView1.Element = element; } if ((String)combo_SymbolName.SelectedItem == "NotGate") { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(NotGate), new NotGate()); element.Location = new PointF(8, 0); symbolView1.Element = element; } }
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); } } }
private void menuItem_OpenMacro_Click(object sender, EventArgs e) { if (macroForm == null) { macroForm = new MacroForm(); } if (macroForm.ShowDialog() == DialogResult.OK) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Macro), MacroCache.Instance.GetMacro(macroForm.SelectedMacro)); SetGateTool(element); int count = 0; foreach (ToolStripItem tsitem in btn_Macro.DropDownItems) { if (tsitem.Tag != null && tsitem.Tag is int && (int)tsitem.Tag == -10) { tsitem.Text = macroForm.LastSelectedMacros[count]; count++; } } while (count < macroForm.LastSelectedMacros.Count) { ToolStripMenuItem menuItem = new ToolStripMenuItem(macroForm.LastSelectedMacros[count]); menuItem.Click += new EventHandler(SelectRecentMacro); menuItem.Tag = -10; btn_Macro.DropDownItems.Add(menuItem); count++; } } }
void SelectRecentMacro(object sender, EventArgs e) { ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem == null) { return; } Macro macro = MacroCache.Instance.GetMacro(menuItem.Text); if (macro == null) { return; } GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Macro), macro); SetGateTool(element); }
private void GenerateItems() { MacroCache macroCache = MacroCache.Instance; List <string> macroNames = macroCache.GetMacroNames(); macroNames.Sort(String.Compare); foreach (string name in macroNames) { //Symbol symbol = macroCache.GetSymbol(name); GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Macro), MacroCache.Instance.GetMacro(name)); element.Location = new PointF(element.Bounds.X * -1, element.Bounds.Y * -1); int width = (int)element.Bounds.Width + 1; int height = (int)element.Bounds.Height + 1; if (width < 48) { width = 48; } if (height < 48) { height = 48; } if (width > 48 && height <= 48) { element.Location = new PointF(element.Location.X, element.Location.Y + (width - height)); height = width; } if (height > 48 && width <= 48) { element.Location = new PointF(element.Location.X + (height - width), element.Location.Y); width = height; } Image img = new Bitmap(width, height); element.Paint(Graphics.FromImage(img)); imageList_Symbols.Images.Add(name, img); ListViewItem item = listView_Macros.Items.Add(name, imageList_Symbols.Images.IndexOfKey(name)); } }
//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 void menuItem_Output_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(SignalOutput), new SignalOutput()); circuitEditor.CurrentTool = new GateTool(element); }
private void menuItem_NOT_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(NotGate), new NotGate()); circuitEditor.CurrentTool = new GateTool(element); }
/// <summary> /// Connect to a Terminal (resp. its connection) /// </summary> /// <param name="graphicTerminal"></param> private void TryConnectToTerminal(GraphicTerminal graphicTerminal, PointF location) { Terminal toTerminal = graphicTerminal.LinkedObject as Terminal; if (toTerminal.Connection != null) { //merging connections not supported by clicking terminals return; } if (m_FromElement is GraphicTerminal) { Terminal fromTerminal = m_FromElement.LinkedObject as Terminal; //if (fromTerminal.Connection != null && toTerminal.Connection != null) //{ // MergeConnections(fromTerminal.Connection.LinkedObject as GraphicConnection, m_LastMouseLocation, // toTerminal.Connection.LinkedObject as GraphicConnection, location); //} //else if (fromTerminal.Connection != null) //{ // //IDEE: suche über allen linien die kürzeste entfernung vom ziel zur verbindung // GraphicConnection graphicConnection = fromTerminal.Connection.LinkedObject as GraphicConnection; // ConnectionLine line = new ConnectionLine((m_FromElement as GraphicTerminal).ConnectionNode, graphicTerminal.ConnectionNode); // graphicConnection.AddChild(line); // graphicConnection.ConnectTerminal(graphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (toTerminal.Connection != null) //{ // GraphicConnection graphicConnection = toTerminal.Connection.LinkedObject as GraphicConnection; // graphicConnection.ConnectTerminal(m_FromElement as GraphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (fromTerminal.Connection == null && toTerminal.Connection == null) { GraphicTerminal fromGraphicTerminal = m_FromElement as GraphicTerminal; if (IsOrthogonal(fromGraphicTerminal.ConnectionNode.Location, graphicTerminal.ConnectionNode.Location) == false) { return; } GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.ConnectTerminal(fromGraphicTerminal); graphicConnection.ConnectTerminal(graphicTerminal); ConnectionLine line = new ConnectionLine(fromGraphicTerminal.ConnectionNode, graphicTerminal.ConnectionNode); graphicConnection.AddChild(line); m_Editor.AddElement(graphicConnection); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } } if (m_FromElement is GraphicConnection) { GraphicConnection fromConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = fromConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { ConnectionLine line = new ConnectionLine(connectionItem as ConnectionNode, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } else if (connectionItem is ConnectionLine) { ConnectionLine prevLine = connectionItem as ConnectionLine; ConnectionNode node = new ConnectionNode(prevLine.NearestPointOnLine(m_LastMouseLocation)); ConnectionLine line1 = new ConnectionLine(prevLine.Nodes[0], node); ConnectionLine line2 = new ConnectionLine(prevLine.Nodes[1], node); fromConnection.RemoveChild(prevLine); fromConnection.AddChild(node); fromConnection.AddChild(line1); fromConnection.AddChild(line2); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } fromConnection.ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } if (m_FromElement is ConnectionNode) { m_FromElement.Location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_FromElement.Location); ConnectionLine line = new ConnectionLine(m_FromElement as ConnectionNode, graphicTerminal.ConnectionNode); m_FromElement.Parent.AddChild(line); (m_FromElement.Parent as GraphicConnection).ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } m_FromElement = null; m_Editor.Invalidate(); }
/// <summary> /// Create a floating connnection, i.e. not completely connected everywhere /// </summary> /// <param name="location"></param> private void FloatingConnection(PointF location) { if (m_FromElement is GraphicConnection) { GraphicConnection graphicConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = graphicConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { //user clicked near a node at fist, connect to this ConnectionNode prevNode = connectionItem as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); m_FromElement.AddChild(node); m_FromElement.AddChild(line); m_FromElement = node; } } else if (connectionItem is ConnectionLine) { //user clicked near a line at first ConnectionLine prevLine = connectionItem as ConnectionLine; //place a connection node at location - split connection line ConnectionNode node = SplitConnectionLine(prevLine, m_LastMouseLocation); //the new node/line location = ForceOrthogonality(node.Location, m_Editor.AlignToGrid(location)); ConnectionNode fnode = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, fnode); m_FromElement.AddChild(fnode); m_FromElement.AddChild(line); m_FromElement = fnode; } } else if (m_FromElement is GraphicTerminal) { //floating connection from a terminal GraphicTerminal graphicTerminal = m_FromElement as GraphicTerminal; location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_Editor.AlignToGrid(location)); ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.AddChild(node); graphicConnection.AddChild(line); graphicConnection.ConnectTerminal(graphicTerminal); m_Editor.AddElement(graphicConnection); //proceed connecting with this node m_FromElement = node; } else if (m_FromElement is ConnectionNode) { //special case -> could be covered by "is GraphicConnection" //sequential floating lines ConnectionNode prevNode = m_FromElement as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); GraphicConnection graphicConnection = prevNode.Parent as GraphicConnection; graphicConnection.AddChild(node); graphicConnection.AddChild(line); m_FromElement = node; } } m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); m_Editor.Invalidate(); }
private void menuItem_Clock_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(Clock), new Clock()); SetGateTool(element); }
private void menuItem_Input_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(SignalInput), new SignalInput()); SetGateTool(element); }
private void menuItem_Buffer_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(BufferGate), new BufferGate()); SetGateTool(element); }
private void menuItem_NAND_Click(object sender, EventArgs e) { GraphicBaseElement element = GraphicObjectFactory.CreateInstance(typeof(NandGate), new NandGate(2)); SetGateTool(element); }