Example #1
0
        /*
         * Creates a new Component instance based on a ComponentData object
         */
        public static Component CreateComponent(Circuit parent, Point origin, ComponentData data)
        {
            Component newComponent = null;

            switch (data.ComponentType)
            {
            case "Resistor":
                newComponent = new Resistor(parent, origin);
                break;

            case "SPDT Switch":
                newComponent = new Switch(parent, origin, true);
                break;

            case "Push Switch":
                newComponent = new Switch(parent, origin, false);
                break;

            case "Memory":
                newComponent = new Memory(parent, origin, data);
                break;

            case "Integrated Circuit":
                newComponent = new IntegratedCircuit(parent, origin, data.ComponentModel);
                break;

            case "Capacitor":
                newComponent = new Capacitor(parent, origin, false);
                break;

            case "Electrolytic Capacitor":
                newComponent = new Capacitor(parent, origin, true);
                break;

            case "Potentiometer":
                newComponent = new Potentiometer(parent, origin);
                break;

            case "LDR":
                newComponent = new LDR(parent, origin);
                break;

            case "Diode":
                newComponent = new Diode(parent, origin, data.ComponentModel);
                break;

            case "Transistor":
            case "NPN Transistor":
            case "PNP Transistor":
            case "N-channel MOSFET":
                newComponent = new Transistor(parent, origin, data.ComponentModel);
                break;

            case "LED":
                newComponent = new LED(parent, origin, data.ComponentModel);
                break;

            case "7-Segment Display":
                newComponent = new SevenSegment(parent, origin, data.ComponentModel);
                break;

            case "Probe":
                newComponent = new Probe(parent, origin);
                break;
            }
            if (newComponent != null)
            {
                newComponent.SetComponentValue(data.ComponentValue);
            }
            return(newComponent);
        }
Example #2
0
        public void UpdatePrompt()
        {
            switch (SelectedTool)
            {
            case "SELECT":
                if (circuit.GetSelectedWire() != null)
                {
                    Prompt.Text = "Wire selected";
                }
                else if (circuit.GetSelectedComponent() != null)
                {
                    Component c = circuit.GetSelectedComponent();
                    Prompt.Text = c.ComponentType + " " + c.ID + " selected";
                    if (c.ComponentModel != "")
                    {
                        Prompt.Text += ", model " + c.ComponentModel;
                    }
                    if (c.ComponentValue.ID != "")
                    {
                        Prompt.Text += ", value " + c.ComponentValue.ToString();
                    }
                }
                else
                {
                    Prompt.Text = "Select an object";
                }
                break;

            case "INTERACT":
                Prompt.Text = "Click on a component to interact with it";
                break;

            case "WIRE":
                if (Breadboard.StartedWire)
                {
                    Prompt.Text = "Click to finish placing wire";
                }
                else
                {
                    Prompt.Text = "Click to start placing wire";
                }
                break;

            case "COMPONENT":
                ComponentData selectedComponent = GetSelectedComponent();
                if (selectedComponent != null)
                {
                    if (Breadboard.StartedLeadedComponent)
                    {
                        Prompt.Text = "Click to finish placing " + selectedComponent.Label;
                    }
                    else
                    {
                        Prompt.Text = "Click to place " + selectedComponent.Label;
                    }
                }
                else
                {
                    Prompt.Text = "Select a component type from the panel on the left";
                }
                break;

            case "DELETE":
                Prompt.Text = "Click on a component or wire to delete it";
                break;
            }
        }
Example #3
0
        public MainWindow()
        {
            System.Diagnostics.Trace.WriteLine(Environment.Version);

            if (!System.IO.Directory.Exists("res/"))
            {
                System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
                if (!System.IO.Directory.Exists("res/"))
                {
                    MessageBox.Show("The directory containing the resources needed by this application is missing. Please check your installation.", "Application Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Application.Current.Shutdown();
                }
            }

            string missingResource = "";

            if (CheckForMissingResources(ref missingResource))
            {
                MessageBox.Show("The resource \"" + missingResource + "\" is missing. Please check your installation.", "Application Error", MessageBoxButton.OK, MessageBoxImage.Error);
                Application.Current.Shutdown();
            }

            circuit = new Circuit(this);
            // AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            App.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            InitializeComponent();

            TreeViewItem customResistor = new TreeViewItem();

            PopulateMenu(Devices_Resistors, Constants.E6, 0, 7, "Ω Resistor", "Resistor");

            TreeViewItem customCapacitor = new TreeViewItem();

            PopulateMenu(Devices_Capacitors, Constants.E3, -10, -7, "F Capacitor", "Capacitor");
            PopulateMenu(Devices_ElectroCapacitors, Constants.E3, -6, -3, "F Capacitor", "Electrolytic Capacitor");

            PopulateMenuWithModels(Devices_DigitalIC, "Integrated Circuit", "res/models/ics.xml", "Digital");
            PopulateMenuWithModels(Devices_AnalogIC, "Integrated Circuit", "res/models/ics.xml", "Analog");

            PopulateMenuWithModels(Devices_Diodes, "Diode", "res/models/diodes.xml");
            PopulateMenuWithModels(Devices_NPN, "NPN Transistor", "res/models/transistors.xml", "npn");
            PopulateMenuWithModels(Devices_PNP, "PNP Transistor", "res/models/transistors.xml", "pnp");
            PopulateMenuWithModels(Devices_NMOS, "N-channel MOSFET", "res/models/transistors.xml", "nmos");


            PopulateMenuWithModels(Devices_Output, "LED", "res/models/leds.xml", null, " LED");
            PopulateMenuWithModels(Devices_Output, "7-Segment Display", "res/models/7seg.xml", null);

            ComponentData pot_10k = new ComponentData("Potentiometer", 10000, "10k Potentiometer");

            TreeViewItem pot_10k_item = new TreeViewItem();

            pot_10k_item.Header = pot_10k;
            Devices_Input.Items.Add(pot_10k_item);

            ComponentData ldr = new ComponentData("LDR", 0, "LDR");

            TreeViewItem ldr_item = new TreeViewItem();

            ldr_item.Header = ldr;
            Devices_Input.Items.Add(ldr_item);


            ComponentData spdt_switch = new ComponentData("SPDT Switch", 0, "SPDT Switch");

            TreeViewItem spdt_switch_item = new TreeViewItem();

            spdt_switch_item.Header = spdt_switch;
            Devices_Input.Items.Add(spdt_switch_item);


            ComponentData push_switch = new ComponentData("Push Switch", 0, "Push Switch");

            TreeViewItem push_switch_item = new TreeViewItem();

            push_switch_item.Header = push_switch;
            Devices_Input.Items.Add(push_switch_item);

            ComponentData probe = new ComponentData("Probe", 0, "Oscilloscope Probe");

            TreeViewItem probe_item = new TreeViewItem();

            probe_item.Header = probe;

            DevicePicker.Items.Add(probe_item);

            SetNumberOfBreadboards(1);

            UpdateTimer.Interval = TimeSpan.FromMilliseconds(50);
            UpdateTimer.Tick    += SimUpdate;

            SelectTool("SELECT");

            string[] args = Environment.GetCommandLineArgs();

            if (args.Length > 1)
            {
                if (System.IO.File.Exists(args[1]))
                {
                    circuit.ClearUndoQueue();
                    circuit.LoadCircuit(args[1]);
                    LastOpenedFile = args[1];
                    Title          = "Breadboard Simulator - " + System.IO.Path.GetFileNameWithoutExtension(args[1]);
                }
            }
            PopulateSamplesMenu(File_Samples, "res/samples");
        }
Example #4
0
        private void Breadboard_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ParentCircuit.DeselectAll();
            if (ParentCircuit.ParentWindow.SelectedTool == "WIRE")
            {
                if (StartedWire)
                {
                    Point actualCoord = GetHoleCoord(e.GetPosition(this), true);
                    if (BreadBoardHolePositions.Contains(actualCoord))
                    {
                        actualCoord = GetAbsolutePosition(actualCoord);
                        Orientation o      = Util.getBestOrientation(WirePointA, actualCoord);
                        int         length = 0;
                        if (o == Orientation.Horizontal)
                        {
                            length = (int)((actualCoord.X - WirePointA.X) / Constants.ScaleFactor);
                        }
                        else
                        {
                            length = (int)((actualCoord.Y - WirePointA.Y) / Constants.ScaleFactor);
                        }
                        NewWire.Length          = length;
                        NewWire.WireOrientation = o;
                        NewWire.MakePermanentWire();
                        StartedWire = false;
                        ParentCircuit.UpdateWireColours();
                        ParentCircuit.AddUndoAction(new AddAction(NewWire, ParentCircuit));
                    }
                }
                else
                {
                    Point rawCoord = GetHoleCoord(e.GetPosition(this), false);

                    Point actualCoord = new Point(0, 0);
                    bool  isValid     = false;

                    //We only want to start a new wire if the user clicks on or near a hole
                    foreach (Point p in BreadBoardHolePositions)
                    {
                        double distance = Math.Sqrt(Math.Pow((p.X - rawCoord.X), 2) + Math.Pow((p.Y - rawCoord.Y), 2));
                        if (distance < 0.4)
                        {
                            isValid     = true;
                            actualCoord = p;
                            break;
                        }
                    }
                    if (isValid)
                    {
                        WirePointA  = GetAbsolutePosition(actualCoord);
                        StartedWire = true;
                        NewWire     = new Wire(ParentCircuit, Orientation.Horizontal, 0,
                                               Constants.RandomWireColours[new Random().Next(Constants.RandomWireColours.Length)],
                                               new Point(WirePointA.X + 1, WirePointA.Y + 1));
                        NewWire.MakeTemporaryWire();
                        ParentCircuit.AddWire(NewWire);
                    }
                }
            }
            else if (ParentCircuit.ParentWindow.SelectedTool == "COMPONENT")
            {
                if (StartedLeadedComponent)
                {
                    Point actualCoord = GetHoleCoord(e.GetPosition(this), true);
                    if (BreadBoardHolePositions.Contains(actualCoord))
                    {
                        actualCoord = GetAbsolutePosition(actualCoord);
                        Orientation o      = Util.getBestOrientation(ComponentPointA, actualCoord);
                        int         length = 0;
                        if (o == Orientation.Horizontal)
                        {
                            length = (int)((actualCoord.X - ComponentPointA.X) / Constants.ScaleFactor);
                        }
                        else
                        {
                            length = (int)((actualCoord.Y - ComponentPointA.Y) / Constants.ScaleFactor);
                        }
                        if (Math.Abs(length) >= NewLeadedComponent.MinLength)
                        {
                            NewLeadedComponent.Length      = length;
                            NewLeadedComponent.orientation = o;
                            NewLeadedComponent.MakePermanent();
                            ParentCircuit.UpdateNetConnectivity();
                            StartedLeadedComponent = false;
                            ParentCircuit.AddUndoAction(new AddAction(NewLeadedComponent, ParentCircuit));
                        }
                    }
                }
                else
                {
                    ComponentData selectedComponentType = ParentCircuit.ParentWindow.GetSelectedComponent();
                    if (selectedComponentType != null)
                    {
                        Point rawCoord = GetHoleCoord(e.GetPosition(this), false);

                        Point actualCoord = new Point(0, 0);
                        bool  isValid     = false;

                        //We only want to start a new wire if the user clicks on or near a hole
                        foreach (Point p in BreadBoardHolePositions)
                        {
                            double distance = Math.Sqrt(Math.Pow((p.X - rawCoord.X), 2) + Math.Pow((p.Y - rawCoord.Y), 2));
                            if (distance < 0.4)
                            {
                                isValid     = true;
                                actualCoord = p;
                                break;
                            }
                        }
                        if (isValid)
                        {
                            Component newComponent = Component.CreateComponent(ParentCircuit, GetAbsolutePosition(actualCoord), selectedComponentType);
                            if (newComponent != null)
                            {
                                if (newComponent is LeadedComponent)
                                {
                                    ComponentPointA        = GetAbsolutePosition(actualCoord);
                                    StartedLeadedComponent = true;
                                    NewLeadedComponent     = (LeadedComponent)newComponent;
                                    NewLeadedComponent.MakeTemporary();
                                }
                                else
                                {
                                    ParentCircuit.AddUndoAction(new AddAction(newComponent, ParentCircuit));
                                }
                                ParentCircuit.AddComponent(newComponent);
                                ParentCircuit.UpdateNetConnectivity();
                            }
                        }
                    }
                }
            }
            ParentCircuit.ParentWindow.UpdatePrompt();
        }