示例#1
0
        protected override void OnDrop(DragEventArgs e)
        {
            //base.OnDrop(e);

            #region FileImport

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Can have more than one file.
                string[]      files         = (string[])e.Data.GetData(DataFormats.FileDrop);
                List <string> validSNPFiles = new List <string>();
                foreach (string file in files)
                {
                    if (TouchstoneFile.ExtensionIsValid(file))
                    {
                        validSNPFiles.Add(file);
                    }
                }

                if (validSNPFiles.Count > 0)
                {
                    Point  position       = e.GetPosition(this);
                    Vector positionOffset = new Vector(0, 0);
                    foreach (string validFile in validSNPFiles)
                    {
                        TouchstoneFile   touchstoneFile = new TouchstoneFile();
                        TouchstoneResult touchstoneResult;
                        try
                        {
                            touchstoneResult = touchstoneFile.Read(validFile);
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                            throw;
                        }

                        if (touchstoneResult != null)
                        {
                            ImportedComponentModel importedComponentModel = new ImportedComponentModel();
                            importedComponentModel.Size     = new Size(60, 50);
                            importedComponentModel.Position = Point.Add(position, positionOffset);
                            positionOffset = Vector.Add(positionOffset, new Vector(10, 10));
                            importedComponentModel.LoadFromTouchstoneResult(touchstoneResult);
                            importedComponentModel.FileName = validFile;
                            importedComponentModel.Name     = Path.GetFileNameWithoutExtension(validFile);

                            if (DataContext is MainVM mainVM)
                            {
                                if (mainVM.AddElement is RelayCommand relayCommand)
                                {
                                    relayCommand.Run(importedComponentModel);
                                }
                                else
                                {
                                    throw new Exception("MainVM AddElement Command should be type of RelayCommand");
                                }
                            }
                            else
                            {
                                throw new Exception("DataContext of DesignerCanvas should be MainVM type");
                            }
                        }
                    }
                }
            }

            #endregion

            #region Drag from toolbox / Copy by drag

            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
            DragString dragString = e.Data.GetData(typeof(DragString)) as DragString;
            if (dragObject != null)
            {
                Point position = e.GetPosition(this);

                double width  = dragObject.DesiredSize.Width;
                double height = dragObject.DesiredSize.Height;

                double left = Math.Max(0, position.X - width / 2);
                double top  = Math.Max(0, position.Y - height / 2);

                Element NewElement = Creator.CreateElementByType(dragObject.Type, width, height, top, left);

                if (DataContext is MainVM mainVM)
                {
                    if (mainVM.AddElement is RelayCommand relayCommand)
                    {
                        relayCommand.Run(NewElement);
                    }
                    else
                    {
                        throw new Exception("MainVM AddElement Command should be type of RelayCommand");
                    }
                }
                else
                {
                    throw new Exception("DataContext of DesignerCanvas should be MainVM type");
                }

                e.Handled = true;
            }
            else if (dragString != null)
            {
                if (DataContext is MainVM mainVM)
                {
                    Vector elementsShift = Point.Subtract(e.GetPosition(this), dragString.DragPointStart);
                    mainVM.ReadAndShiftElementsAndConnectionsFromString(dragString.DraggedString, elementsShift);
                }
            }

            #endregion
        }
示例#2
0
        public static Element CreateElementByType(ElementType type, double width, double height, double top, double left)
        {
            Element toReturn;

            switch (type)
            {
            case ElementType.Element:
                toReturn = new Element();
                break;

            case ElementType.Port:
                toReturn = new PortModel();
                break;

            case ElementType.ResistorInSeries:
                toReturn = new LumpedElement(ElementType.ResistorInSeries, ElementType.ResistorInSeries.ToString());
                break;

            case ElementType.ResistorInParallel:
                toReturn = new LumpedElement(ElementType.ResistorInParallel, ElementType.ResistorInParallel.ToString());
                break;

            case ElementType.InductorInSeries:
                toReturn = new LumpedElement(ElementType.InductorInSeries, ElementType.InductorInSeries.ToString());
                break;

            case ElementType.InductorInParallel:
                toReturn = new LumpedElement(ElementType.InductorInParallel, ElementType.InductorInParallel.ToString());
                break;

            case ElementType.CapacitorInSeries:
                toReturn = new LumpedElement(ElementType.CapacitorInSeries, ElementType.CapacitorInSeries.ToString());
                break;

            case ElementType.CapacitorInParallel:
                toReturn = new LumpedElement(ElementType.CapacitorInParallel, ElementType.CapacitorInParallel.ToString());
                break;

            case ElementType.OpenCircuit:
                toReturn = new OpenCircuitModel();
                break;

            case ElementType.ShortCircuit:
                toReturn = new ShortCircuitModel();
                break;

            case ElementType.TransmissionLine:
                toReturn = new TransmissionLineModel();
                break;

            case ElementType.ImportedComponent:
                toReturn = new ImportedComponentModel();
                break;

            default:
                toReturn = new Element();
                break;
            }

            toReturn.Position = new Point(left, top);
            toReturn.Size     = new Size(width, height);

            return(toReturn);
        }