Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
About my WPF app to draw a Bolt Hole Pattern.
Message
From
31/07/2008 11:40:11
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01334102
Message ID:
01335552
Views:
11
Latest update:

Check out the new UI!!!! It also now supports 2 Hole Patterns

http://www.jordanmachine.com/BoltHole_WPF_2.jpg

XAML: (Needs a little less hard-coding, but I'm getting there)
<Window x:Class="WpfApplication1.Window1" Name="winBoltCircle"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"    
    Title="Bolt Hole Pattern" Height="700" Width="1050" Background="Gray">

    
    <Window.Resources>
        <local:FormattingConverter x:Key="formatter" />
    </Window.Resources>

    <Grid Margin="5,5,5,5">
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250" />
            <ColumnDefinition Width="250" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Pattern 1" Foreground="White" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center"/>
        <TextBlock Text="Pattern 2" Foreground="White" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" Grid.Column="1"/>
        
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
        <!-- - - - - - 1st Column for Hole Pattern #1 - - - - - -  -->
        <Border Grid.Row="1" Grid.Column="0" BorderBrush="Black" BorderThickness="1" CornerRadius="4"
                Padding="8" Margin="5,0,5,0" Grid.RowSpan="2" Background="White">
            <StackPanel>
                <StackPanel Orientation="Horizontal" >
                    <TextBlock Text="Bolt Cir Dia. " Width="75" />
                <TextBox Name="txtBoltCirDia1" Text="{Binding Path=CurrentHolePattern.BoltCirDia, ElementName=winBoltCircle}"
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75"
                         PreviewKeyDown="TextBox_PreviewKeyDown" />
            </StackPanel>
                <StackPanel Orientation="Horizontal" >
                    <TextBlock Text="No. of Holes: " Width="75" />
                <TextBox Name="txtHoleCount1" Text="{Binding Path=CurrentHolePattern.HoleCount, ElementName=winBoltCircle}"
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75" />
                <Button Name="btnHoleCountDecrease1" Click="btnHoleCountDecrease_Click" Margin="5,0,0,0" Width="25">-</Button>
                <Button Name="btnHoleCountIncrease1" Click="btnHoleCountIncrease_Click" Margin="5,0,0,0" Width="25">+</Button>
            </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Start Angle: " Width="75"/>
                <TextBox Name="txtStartAngle1" Text="{Binding Path=CurrentHolePattern.StartAngle, ElementName=winBoltCircle}" 
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75" />
                <Button Name="btnStartAngleDecrease1" Click="btnStartAngleDecrease_Click" Margin="5,0,0,0" Width="25">-</Button>
                <Button Name="btnStartAngleIncrease1" Click="btnStartAngleIncrease_Click" Margin="5,0,0,0" Width="25">+</Button>
            </StackPanel>
            <Slider Name="slider1" Value="{Binding Path=CurrentHolePattern.StartAngle, ElementName=winBoltCircle}"
                    Maximum="90" LargeChange="5" Ticks="0,45,90" TickPlacement="BottomRight" SmallChange="1" Height="22"  Width="75" />
            <!--Coordinate Grid - Pattern #1 -->
            <ListView x:Name="CoordinateGrid1" MouseDoubleClick="CoordinateGrid_MouseDoubleClick" MinHeight="300">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView >
                        <GridViewColumn Header="Number" DisplayMemberBinding="{Binding Path=HoleNumber}" />
                        <GridViewColumn Header="X Coord" Width="60">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                            Text="{Binding AbsX, Converter={StaticResource formatter}, ConverterParameter='\{0:f4\}'}" HorizontalAlignment="Right"  />
                        </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Y Coord" Width="60" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                            Text="{Binding AbsY, Converter={StaticResource formatter}, ConverterParameter='\{0:f4\}'}" HorizontalAlignment="Right"  />
                        </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Angle"  Width="60">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                            Text="{Binding Angle, Converter={StaticResource formatter}, ConverterParameter='\{0:f2\}'}" HorizontalAlignment="Right"  />
                        </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            </StackPanel>
        </Border>
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
        <!-- - - - - - 2nd Column for Hole Pattern #2 - - - - - -  -->
            <Border Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="1" CornerRadius="4"
                    Padding="8" Grid.RowSpan="2" Margin="5,0,5,0" Background="White">
                <StackPanel>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="Bolt Cir Dia. " Width="75" />
                        <TextBox Name="txtBoltCirDia2" Text="{Binding Path=HolePattern2.BoltCirDia, ElementName=winBoltCircle}"
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75"
                         PreviewKeyDown="TextBox_PreviewKeyDown" />
            </StackPanel>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="No. of Holes: " Width="75" />
                        <TextBox Name="txtHoleCount2" Text="{Binding Path=HolePattern2.HoleCount, ElementName=winBoltCircle}"
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75" />
                        <Button Name="btnHoleCountDecrease2" Click="btnHoleCountDecrease_Click" Margin="5,0,0,0" Width="25">-</Button>
                        <Button Name="btnHoleCountIncrease2" Click="btnHoleCountIncrease_Click" Margin="5,0,0,0" Width="25">+</Button>
            </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Start Angle: " Width="75"/>
                        <TextBox Name="txtStartAngle2" Text="{Binding Path=HolePattern2.StartAngle, ElementName=winBoltCircle}" 
                         PreviewGotKeyboardFocus="TextBox_PreviewGotKeyboardFocus" Height="25" Width="75" />
                        <Button Name="btnStartAngleDecrease2" Click="btnStartAngleDecrease_Click" Margin="5,0,0,0" Width="25">-</Button>
                        <Button Name="btnStartAngleIncrease2" Click="btnStartAngleIncrease_Click" Margin="5,0,0,0" Width="25">+</Button>
            </StackPanel>
                    <Slider Name="slider2" Value="{Binding Path=HolePattern2.StartAngle, ElementName=winBoltCircle}"
                    Maximum="90" LargeChange="5" Ticks="0,45,90" TickPlacement="BottomRight" SmallChange="1" Height="22" Width="75" />
            <!--Coordinate Grid - Pattern #2 -->
            <ListView x:Name="CoordinateGrid2" MouseDoubleClick="CoordinateGrid_MouseDoubleClick" MinHeight="300">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView >
                        <GridViewColumn Header="Number" DisplayMemberBinding="{Binding Path=HoleNumber}" />
                        <GridViewColumn Header="X Coord" Width="60">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                                Text="{Binding AbsX, Converter={StaticResource formatter}, ConverterParameter='\{0:f4\}'}" HorizontalAlignment="Right"  />
                            </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Y Coord" Width="60" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                                Text="{Binding AbsY, Converter={StaticResource formatter}, ConverterParameter='\{0:f4\}'}" HorizontalAlignment="Right"  />
                            </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Angle"  Width="60">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                                Text="{Binding Angle, Converter={StaticResource formatter}, ConverterParameter='\{0:f2\}'}" HorizontalAlignment="Right"  />
                            </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            </StackPanel>
        </Border>

        <StackPanel Grid.Row="1" Grid.Column="2" >
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="4"
                Padding="8" Height="50" VerticalAlignment="Top" Margin="5,0,0,0" Background="White">
            <StackPanel Grid.Column="2" Grid.Row="1" Orientation="Horizontal" >
                <Button Name="btnDraw" Click="btnDraw_Click">ReDraw</Button>
                <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
                    <TextBlock Text="Hole Size" VerticalAlignment="Center" Margin="10,0,10,0"/>
                    <Button Name="btnHoleSizeDecrease" Click="btnHoleSizeDecrease_Click" Margin="5,0,0,0" Width="25">-</Button>
                    <Button Name="btnHoleSizeIncrease" Click="btnHoleSizeIncrease_Click" Margin="5,0,0,0" Width="25">+</Button>
                </StackPanel>
                <Button Name="btnPrint" Width="75" Click="btnPrint_Click" Margin="115,0,0,0">Print</Button>
            </StackPanel>
        </Border>

        <Border Grid.Row="1" Grid.Column="2" BorderBrush="Black" BorderThickness="1" CornerRadius="4"
                Padding="8" VerticalAlignment="Top" Margin="5,10,0,0" Background="White">
            <StackPanel>
                <TextBlock Text="Double-Click a row in the grid to toggle hole visibility." />
                <StackPanel>
                 <Canvas Name="canvas1" Background="#7920619E" Margin="0,5,0,0" Height="500"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </StackPanel>            

    </Grid>
</Window>
code-behind:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;



namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {

        public ObservableCollection<HolePattern> HolePatterns { get; set; }
        public HolePattern CurrentHolePattern { get; set; }
        public HolePattern HolePattern2 { get; set; }

        // Canvas sketch related fields
        public double OuterSketchBoltCirRad;
        public double InnerSketchBoltCirRad;
        public double SketchX0 { get; set; }
        public double SketchY0 { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        //private void NotifyPropertyChanged(String info)
        //{
        //    if (PropertyChanged != null)
        //    {
        //        PropertyChanged(this, new PropertyChangedEventArgs(info));
        //    }
        //}
        
       
        public Window1()
        {
 
            HolePatterns = new ObservableCollection<HolePattern>();
         
            CurrentHolePattern = new HolePattern();
            HolePattern2 = new HolePattern();
            CurrentHolePattern.PatternName = "Pattern #1";
            HolePattern2.PatternName = "Pattern #2";

            HolePatterns.Add(CurrentHolePattern);
            HolePatterns.Add(HolePattern2);

            InitializeComponent();

            SketchX0 = 250;// canvas1.Width / 2;
            SketchY0 = 250;//canvas1.Height / 2;
            OuterSketchBoltCirRad = SketchX0 * 0.8;
            InnerSketchBoltCirRad = SketchX0 * 0.65;

            CurrentHolePattern.SketchX0 = SketchX0;
            CurrentHolePattern.SketchY0 = SketchY0;

            HolePattern2.SketchX0 = SketchX0;
            HolePattern2.SketchY0 = SketchY0;

            CurrentHolePattern.SketchBoltCirRad = OuterSketchBoltCirRad;
            HolePattern2.SketchBoltCirRad = InnerSketchBoltCirRad;

            CurrentHolePattern.PropertyChanged += new PropertyChangedEventHandler(CurrentHolePattern_PropertyChanged);
            CurrentHolePattern.CollectionChanged += new NotifyCollectionChangedEventHandler(CurrentHolePattern_CollectionChanged);

            HolePattern2.PropertyChanged += new PropertyChangedEventHandler(CurrentHolePattern_PropertyChanged);
            HolePattern2.CollectionChanged += new NotifyCollectionChangedEventHandler(CurrentHolePattern_CollectionChanged);

            //-- Set some default values for the UI
            CurrentHolePattern.BoltCirDia = 12.0;
            CurrentHolePattern.HoleCount = 6;
            CurrentHolePattern.StartAngle = 0;

            HolePattern2.BoltCirDia = 12.0;
            HolePattern2.HoleCount = 6;
            HolePattern2.StartAngle = 0;
            
            CoordinateGrid1.ItemsSource = CurrentHolePattern.HoleList;
            CoordinateGrid2.ItemsSource = HolePattern2.HoleList;

        }

        void CurrentHolePattern_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RedrawScreen();
        }

        void CurrentHolePattern_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            RepaintScreen();//  Repaint doesn't clear out the Canvas, just updates what's already there.
        }

       public void DrawGridLines()
        {
            int LineSpacing = 20;
            double LineWidth = 0.5;
            Brush LineColor = Brushes.Gray;

            //-- Horizontal Grid Lines (work from Center to Left, then Center to Right)
            for (double x = 0; x <= SketchX0; x += LineSpacing)
            {
                for (int y = -1; y <= 1; y += 2)
                {
                    Line VerticalGraphLine = new Line();
                    VerticalGraphLine.X1 = SketchX0 + x * y;
                    VerticalGraphLine.Y1 = 0;
                    VerticalGraphLine.X2 = VerticalGraphLine.X1;
                    VerticalGraphLine.Y2 = SketchX0*2;
                    VerticalGraphLine.Stroke = LineColor;
                    VerticalGraphLine.StrokeThickness = LineWidth;
                    canvas1.Children.Add(VerticalGraphLine);
                }
            }

            //-- Vertical Grid Lines (work from Center Up, then Center down)
            for (double x = 0; x <= SketchX0; x += LineSpacing)
            {
                for (int y = -1; y <= 1; y += 2)
                {
                    Line HorizontalGraphLine = new Line();
                    HorizontalGraphLine.X1 = 0;
                    HorizontalGraphLine.Y1 = SketchY0 + x * y;
                    HorizontalGraphLine.X2 = SketchY0*2;
                    HorizontalGraphLine.Y2 = HorizontalGraphLine.Y1;
                    HorizontalGraphLine.Stroke = LineColor;
                    HorizontalGraphLine.StrokeThickness = LineWidth;
                    canvas1.Children.Add(HorizontalGraphLine);
                }
            }

        }
        
       public void DrawBoltCir()
        {
            //-- Outer Bolt Circle ---
            Ellipse BoltCirEntity = new Ellipse();
            BoltCirEntity.Width = OuterSketchBoltCirRad * 2;
            BoltCirEntity.Height = OuterSketchBoltCirRad * 2;
            BoltCirEntity.Stroke = Brushes.Gray;
            BoltCirEntity.Margin = new Thickness(SketchX0 - OuterSketchBoltCirRad, SketchY0 - OuterSketchBoltCirRad, 0, 0);
            canvas1.Children.Add(BoltCirEntity);


            //-- Inner Bolt Circle ---
            BoltCirEntity = new Ellipse();
            BoltCirEntity.Width = InnerSketchBoltCirRad * 2;
            BoltCirEntity.Height = InnerSketchBoltCirRad * 2;
            BoltCirEntity.Stroke = Brushes.Gray;
            BoltCirEntity.Margin = new Thickness(SketchX0 - InnerSketchBoltCirRad, SketchY0 - InnerSketchBoltCirRad, 0, 0);
            canvas1.Children.Add(BoltCirEntity);

            Line HorizontalCL = new Line();
            HorizontalCL.X1 = 10;
            HorizontalCL.Y1 = SketchY0;
            HorizontalCL.X2 = SketchX0*2-10;
            HorizontalCL.Y2 = SketchY0;
            HorizontalCL.Stroke = Brushes.Black;
            HorizontalCL.StrokeThickness = 1;

            Line VerticalCL = new Line();
            VerticalCL.X1 = SketchX0;
            VerticalCL.Y1 = 10;
            VerticalCL.X2 = SketchX0;
            VerticalCL.Y2 = SketchY0*2-10;
            VerticalCL.Stroke = Brushes.Black;
            VerticalCL.StrokeThickness = 1;

            //-- Add objects to Canvas
            canvas1.Children.Add(HorizontalCL);
            canvas1.Children.Add(VerticalCL);

        }

       public void DrawHoles()
        {
           // This methods adds the HoleEntity and HoleLabel to the canvas
           // The method UpdateHoles() must be used to set the sketch positions for these objects
           // Canvas clearing should be handled outside this method as needed.
           foreach (Hole Hole in CurrentHolePattern.HoleList)
            {
                canvas1.Children.Add(Hole.HoleEntity);
                canvas1.Children.Add(Hole.HoleLabel);
            }
        }

       public void DrawHolePattern2()
       {
           // This methods adds the HoleEntity and HoleLabel to the canvas
           // The method UpdateHoles() must be used to set the sketch positions for these objects
           // Canvas clearing should be handled outside this method as needed.
           foreach (Hole Hole in HolePattern2.HoleList)
           {
               canvas1.Children.Add(Hole.HoleEntity);
               canvas1.Children.Add(Hole.HoleLabel);
           }
       }
 
       public partial class HolePattern: INotifyPropertyChanged, INotifyCollectionChanged
       {

           private int _HoleCount;
           private double _BoltCirDia;
           private double _StartAngle;
           private double _AngularSpacing;

           public string PatternName { get; set; }

           public double SketchX0 { get; set; }
           public double SketchY0 { get; set; }
           public double SketchBoltCirRad { get; set; }

           public ObservableCollection<Hole> HoleList { get; set; }

           public event PropertyChangedEventHandler PropertyChanged;
           public event NotifyCollectionChangedEventHandler CollectionChanged;

           private void NotifyPropertyChanged(String info)
           {
               if (PropertyChanged != null)
               {
                   PropertyChanged(this, new PropertyChangedEventArgs(info));
               }
           }

           private void NotifyCollectionChanged()
           {
               if (CollectionChanged != null)
               {
                   CollectionChanged(this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
               }
           }

           public HolePattern() //Constructor
           {
               HoleList = new ObservableCollection<Hole>();
               _HoleCount = 0;
           }

           public void CreateHoleList()
           {
               this.HoleList.Clear();

               if (this.HoleCount > 0)
               {
                   int x;
                   double CurrentAngle = this.StartAngle;

                   for (x = 1; x <= this.HoleCount; x++)
                   {
                       Hole NewHole = new Hole();
                       NewHole.HoleNumber = x;
                       NewHole.HoleEntity.Stroke = Brushes.Black;

                       //-- Set BCR and Angle, and let the Hole calculate the absolute coords of each hole
                       NewHole.BoltCirRad = this.BoltCirDia / 2.0;
                       NewHole.Angle = CurrentAngle;

                       //--Setup HoleLabel (position happens in UpdateHoles()---------
                       NewHole.HoleLabel.Text = x.ToString();
                       NewHole.HoleLabel.TextAlignment = TextAlignment.Center;
                       NewHole.HoleLabel.Foreground = Brushes.White;
                       NewHole.HoleLabel.FontSize = 12;

                       //HoleList.Add(NewHole);
                       this.HoleList.Add(NewHole);

                       CurrentAngle = this.StartAngle + x * this.AngularSpacing; // Increment the angle for the next Hole

                       NotifyCollectionChanged(); //Announce that HoleList collection has changed!

                   }
               }
           }

           public void UpdateHoles()
           {
               foreach (Hole HoleEnt in this.HoleList)
               {
                   //-- Update absolute coord points on the Hole by setting its properties -----
                   //-- The Hole object will calculate the absolute X and Y in its poperty setters
                   HoleEnt.BoltCirRad = this.BoltCirDia / 2;
                   HoleEnt.Angle = (HoleEnt.HoleNumber - 1) * this.AngularSpacing + this.StartAngle;

                   //--Update the Hole sketch data for the Canvas layout ----
                   HoleEnt.HoleEntity.Width = HoleEnt.HoleDia;
                   HoleEnt.HoleEntity.Height = HoleEnt.HoleDia;
                   HoleEnt.CanvasX = SketchX0 + SketchBoltCirRad * Math.Cos(dtr(HoleEnt.Angle)) - HoleEnt.HoleDia / 2;
                   HoleEnt.CanvasY = SketchY0 - SketchBoltCirRad * Math.Sin(dtr(HoleEnt.Angle)) - HoleEnt.HoleDia / 2;
                   HoleEnt.HoleEntity.Margin = new Thickness(HoleEnt.CanvasX, HoleEnt.CanvasY, 0, 0);

                   //---Update the HoleLabel sketch coords for the Canvas -----
                   double TextCLR = (SketchBoltCirRad - HoleEnt.HoleDia / 2 - HoleEnt.HoleLabel.FontSize);
                   double HoleLabelX = (SketchX0 + TextCLR * Math.Cos(dtr(HoleEnt.Angle)) - HoleEnt.HoleLabel.FontSize / 2);
                   double HoleLabelY = (SketchY0 - TextCLR * Math.Sin(dtr(HoleEnt.Angle)) - HoleEnt.HoleLabel.FontSize * .6);
                   HoleEnt.HoleLabel.Margin = new Thickness(HoleLabelX, HoleLabelY, 0, 0);

               }
           }

           public double BoltCirDia
           {
               get { return _BoltCirDia; }
               set
               {
                   _BoltCirDia = value;
                   NotifyPropertyChanged("BoltCirDia");
               }
           }

           public double StartAngle
           {
               get { return _StartAngle; }
               set
               {
                   _StartAngle = value;
                   NotifyPropertyChanged("StartAngle");
               }
           }

           public double AngularSpacing
           {
               get { return _AngularSpacing; }
               set 
               {
                    _AngularSpacing = value;
                    NotifyPropertyChanged("AngularSpacing");
               }
           }

           public int HoleCount
           {
               get { return _HoleCount; }
               set
               {
                   if (value >= 0)
                   {
                       _HoleCount = value;
                   }
                   else
                   {
                       _HoleCount = 0;
                   }
                   if (value > 0)
                   {
                       _AngularSpacing = 360.0 / _HoleCount;
                   }
                   else
                   {
                       _AngularSpacing = 0;
                   }
                   CreateHoleList();
                   NotifyPropertyChanged("HoleCount");
               }
           }

           public void ChangeHoleCount(int ChangeAmount)
           {
               if (this.HoleCount + ChangeAmount >= 0)
               {
                   this.HoleCount += ChangeAmount;
               }

           }

           public void ChangeStartAngle(int ChangeAmount)
           {
               this.StartAngle += ChangeAmount;
           }

       }

       public class Hole:INotifyPropertyChanged
        {
            public Ellipse HoleEntity = new Ellipse();
            public TextBlock HoleLabel = new TextBlock();

            public static double _HoleDia = 20;

            public int HoleNumber { get; set; }
            public double CanvasX { get; set; }
            public double CanvasY { get; set; }
            public double HoleLabelX { get; set; }
            public double HoleLabelY { get; set; }
            public string AbsXDisplay{ get; set; }
            public string AbsYDisplay { get; set; }
            private bool _Visible;
            private double _AbsX;
            private double _AbsY;
            private double _Angle;
            private double _BoltCirRad;

            public event PropertyChangedEventHandler PropertyChanged;

            public Hole()
            {
                HoleEntity.Width = _HoleDia;
                HoleEntity.Height = _HoleDia;
                Visible = true;
            }

           private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

            public bool Visible
            {
                get { return _Visible; }
                set
                {
                    _Visible = value;
                    HoleEntity.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                    NotifyPropertyChanged("Visible");
                }
            }
            
           public double AbsX
            {
                get { return _AbsX; }
                set
                {
                    _AbsX = value;
                    AbsXDisplay = String.Format("{0:f4}", value);
                    NotifyPropertyChanged("AbsX");
                }
            }

            public double AbsY
            {
                get { return _AbsY; }
                set
                {
                    _AbsY = value;
                    AbsYDisplay = String.Format("{0:f4}", value);
                    NotifyPropertyChanged("AbsY");
                }
            }

           public double HoleDia
            {
                get { return _HoleDia;}
                set {
                      _HoleDia = value;
                      NotifyPropertyChanged("HoleDia");
                    }
            }

            public double BoltCirRad
            {
                get{return _BoltCirRad;}
                set
                {
                    _BoltCirRad = value;
                    AbsX = (this.BoltCirRad * Math.Cos(dtr(this.Angle)));
                    AbsY = (this.BoltCirRad * Math.Sin(dtr(this.Angle)));
                }
            }

            public double Angle
            {
                get{return _Angle;}
                set
                {
                    _Angle = value;
                    AbsX = (this.BoltCirRad * Math.Cos(dtr(this.Angle)));
                    AbsY = (this.BoltCirRad * Math.Sin(dtr(this.Angle)));
                    NotifyPropertyChanged("Angle");
                }
            }
        
        }

       public static double dtr(double degrees)
        {
            double radians = (Math.PI / 180) * degrees;
            return (radians);
        }

       private void ChangeHoleSketchSize(int ChangeAmount)
        {
            if (Hole._HoleDia + ChangeAmount > 0)
            {
                Hole._HoleDia += ChangeAmount;
            }
            //UpdateHoles();
            RepaintScreen();
        }

       private void ToggleHoleVisiblity(Hole SelectedHole)
       {
           SelectedHole.Visible = (SelectedHole.Visible) ? false : true;
           RepaintScreen();
       }


        private void RedrawScreen()
        {
            canvas1.Children.Clear();
            //CreateHoleList(); // Recreate the Hole list collection, which re-calcs the absolute coordinates and angle of each hole
            DrawGridLines();
            DrawBoltCir();
            DrawHoles();
            DrawHolePattern2();
            RepaintScreen(); // Recalculate the abs and canvas coords on the HoleList. Will update grid and update on the canvas
        }

        private void RepaintScreen()
        {
            CurrentHolePattern.UpdateHoles(); // Recalculate the abs and canvas coords on the HoleList so we can draw the holes on the canvas
            HolePattern2.UpdateHoles();
        }


        private void btnDraw_Click(object sender, RoutedEventArgs e)
        {
            RedrawScreen();
        }

        private void btnHoleCountIncrease_Click(object sender, RoutedEventArgs e)
        {
            string name = ((FrameworkElement)sender).Name.ToString();
            if (name.EndsWith("1"))
            {
                CurrentHolePattern.ChangeHoleCount(1);
            }
            else
            {
                HolePattern2.ChangeHoleCount(1);
            }
        }

        private void btnHoleCountDecrease_Click(object sender, RoutedEventArgs e)
        {
            string name = ((FrameworkElement)sender).Name.ToString();
            if (name.EndsWith("1"))
            {
                CurrentHolePattern.ChangeHoleCount(-1);
            }
            else
            {
                HolePattern2.ChangeHoleCount(-1);
            }
        }

        private void btnStartAngleIncrease_Click(object sender, RoutedEventArgs e)
        {
            string name = ((FrameworkElement)sender).Name.ToString();
            if (name.EndsWith("1"))
            {
                CurrentHolePattern.ChangeStartAngle(1);
            }
            else
            {
                HolePattern2.ChangeStartAngle(1);
            }
        }
        
        private void btnStartAngleDecrease_Click(object sender, RoutedEventArgs e)
        {
            string name = ((FrameworkElement)sender).Name.ToString();
            if (name.EndsWith("1"))
            {
                CurrentHolePattern.ChangeStartAngle(-1);
            }
            else
            {
                HolePattern2.ChangeStartAngle(-1);
            }
        }

        //private void btnStartAngleIncrease_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    ChangeStartAngle(1);
        //}

        //private void btnStartAngleDecrease_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    ChangeStartAngle(-1);
        //}

        private void btnHoleSizeDecrease_Click(object sender, RoutedEventArgs e)
        {
            ChangeHoleSketchSize(-1);
        }

        private void btnHoleSizeIncrease_Click(object sender, RoutedEventArgs e)
        {
            ChangeHoleSketchSize(1);
        }

        private void CoordinateGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var lvRef=(ListView)sender;
            Hole SelectedHole = (Hole)lvRef.SelectedItem;

            ToggleHoleVisiblity(SelectedHole);
        }

        //http://www.madprops.org/blog/enter-to-tab-in-wpf/
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var uie = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            { e.Handled = true;
              uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }
        
        private void TextBox_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }



        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog prtDlg = new PrintDialog();
            if(prtDlg.ShowDialog() == true)
            {
                prtDlg.PrintVisual(winBoltCircle, "A simple drawing");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            HolePattern hp = new HolePattern();
            hp.PatternName = "Pattern2";
            HolePatterns.Add(hp);
        }


    }
    [ValueConversion(typeof(object), typeof(string))]
    public class FormattingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string formatString = parameter as string;
            if (formatString != null)
            {
                return string.Format(culture, formatString, value);
            }
            else
            {
                return value.ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform