void SnapDataBlockLogicItem(ILocation locacion, double dX, double dY, DataBlockSnapMode mode)
        {
            double left = SnapGrid.Snap(locacion.X + dX, UnitConverter.CmToDip(0.1));
            //double top = SnapGrid.Snap(locacion.Y + dY, UnitConverter.CmToDip(1.0), UnitConverter.CmToDip(0.1));
            double top = UnitConverter.CmToDip(Math.Floor(UnitConverter.DipToCm(locacion.Y + dY)) + 0.1);

            switch (mode)
            {
            case DataBlockSnapMode.Input:
            {
                left = UnitConverter.CmToDip(1.3);
            }
            break;

            case DataBlockSnapMode.Output:
            {
                left = UnitConverter.CmToDip(31.2);
            }
            break;

            case DataBlockSnapMode.Auto:
            {
                if (left < UnitConverter.CmToDip(1.3))
                {
                    // X < 1.3cm
                    left = UnitConverter.CmToDip(1.3);
                }
                else if (left >= UnitConverter.CmToDip(1.3) && left < UnitConverter.CmToDip(11.2))
                {
                    // 1.3cm >= X > 11.2cm
                    left = UnitConverter.CmToDip(1.3);
                }
                else if (left >= UnitConverter.CmToDip(11.2) && left < UnitConverter.CmToDip(31.2))
                {
                    // 11.2cm >= X > 31.2cm
                    if (left <= UnitConverter.CmToDip(21.2))
                    {
                        // move to inputs
                        left = UnitConverter.CmToDip(1.3);
                    }
                    else
                    {
                        // move to outputs
                        left = UnitConverter.CmToDip(31.2);
                    }
                }
                else
                {
                    // X >= 31.2cm
                    left = UnitConverter.CmToDip(31.2);
                }
            }
            break;
            }

            if (top <= UnitConverter.CmToDip(2.1))
            {
                // Y <= 2.1cm
                top = UnitConverter.CmToDip(2.1);
            }
            else if (top >= UnitConverter.CmToDip(25.1))
            {
                // Y >= 25.1
                top = UnitConverter.CmToDip(25.1);
            }
            else
            {
                // 2.1cm < Y < 25.1cm
                //top = top + UnitConverter.CmToDip(0.1);
            }

            locacion.X = left;
            locacion.Y = top;
        }
Ejemplo n.º 2
0
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (!(this.DataContext is Node))
            {
                base.OnPreviewMouseLeftButtonDown(e);
                return;
            }

            var diagram      = (this.DataContext as Node).Diagram;
            var element      = e.Source as FrameworkElement;
            var visualParent = FindVisualParent <DiagramCanvas>(this);
            var p            = e.GetPosition(visualParent);

            if (element.DataContext is Pin)
            {
                var pin = element.DataContext as Pin;
                if (visualParent.IsMouseCaptured)
                {
                    if (visualParent.HaveStart)
                    {
                        visualParent.CurrentWire.EndPin = pin;
                        pin.IsConnected = true;

                        visualParent.CurrentWire = null;
                        visualParent.StartPin    = null;
                        visualParent.HaveStart   = false;

                        visualParent.ReleaseMouseCapture();

                        e.Handled = true;
                    }
                }
                else
                {
                    Wire wire = new Wire()
                    {
                        Id      = Guid.NewGuid(),
                        X       = UnitConverter.CmToDip(0.0),
                        Y       = UnitConverter.CmToDip(0.0),
                        Z       = 0,
                        Diagram = diagram
                    };

                    wire.SetStartPin(pin);

                    Pin endPin = new Pin()
                    {
                        Id        = Guid.Empty,
                        ParentId  = Guid.Empty,
                        OffsetX   = 0.0,
                        OffsetY   = 0.0,
                        X         = p.X,
                        Y         = p.Y,
                        Alignment = PinAlignment.None
                    };

                    wire.SetEndPin(endPin);

                    visualParent.StartPin = pin;
                    diagram.Items.Add(wire);

                    visualParent.CurrentWire = wire;
                    visualParent.HaveStart   = true;

                    Mouse.Capture(visualParent, CaptureMode.SubTree);

                    e.Handled = true;
                }
            }
            else
            {
                if (visualParent.IsMouseCaptured == true)
                {
                    e.Handled = true;
                }

                base.OnPreviewMouseLeftButtonDown(e);
            }
        }
        void Print(PageItem Page)
        {
            try
            {
                Page.IsGridVisible = false;
                Page.IsPrinting    = true;

                ContentControl cc = new ContentControl();
                cc.ContentTemplate = this.FindResource("LogicPageItemTemplate1") as DataTemplate;
                cc.Content         = Page;

                PrintDialog dlg = new System.Windows.Controls.PrintDialog();
                dlg.PrintQueue  = LocalPrintServer.GetDefaultPrintQueue();
                dlg.PrintTicket = dlg.PrintQueue.DefaultPrintTicket;
                dlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
                dlg.PrintTicket.OutputQuality   = OutputQuality.High;
                //dlg.PrintTicket.OutputColor = OutputColor.Monochrome;
                dlg.PrintTicket.TrueTypeFontMode = TrueTypeFontMode.DownloadAsNativeTrueTypeFont;

                Nullable <bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    System.Printing.PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);

                    double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / UnitConverter.CmToDip(42.0),
                                            capabilities.PageImageableArea.ExtentHeight / UnitConverter.CmToDip(29.7));

                    cc.LayoutTransform = new ScaleTransform(scale, scale);

                    Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

                    cc.Measure(sz);
                    cc.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
                    cc.UpdateLayout();

                    dlg.PrintVisual(cc, "Page");
                }

                Page.IsGridVisible = true;
                Page.IsPrinting    = false;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }