• Lunes 29 de Abril de 2024, 14:41

Autor Tema:  Solicito Asesoria con GDI y problema de calculo :S  (Leído 1228 veces)

tux2003

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Solicito Asesoria con GDI y problema de calculo :S
« en: Martes 21 de Septiembre de 2010, 23:52 »
0
Hola que tal:

Estoy haciendo una aplicación en .Net para una empresa, el módulo que estoy haciendo grafica con GDI un pliego de papel de dimensiones W, H crea las reglas verticales, horizontales, dibuja las medidas, y lo hace en base a una escala predefinida. Al momento todo chévere hace el pliego y lo que necesito pero a ese pliego de papel se le van a realizar cortes de dimensión A, B ahora el sistema dice cuantos cortes caben en el pliego y cuanto de material se desperdicia pero lo que no he conseguido es que me grafique o dibuje los cortes en el pliego de papel.... seguramente es algo muy sencillo pero la verdad ya estoy desesperado y no logro concentrarme ojala alguien pueda asesorarme, guiarme o decirme como puedo resolver el problema debe hacer lo mismo que la siguiente pagina calcu pero en .Net

Dejo mi código... PD Esta es una versión previa a la que tengo completa ojala puedan ayudarme gracias de antemano

Imports System.Drawing.Printing
Imports System.Drawing.Drawing2D
Imports System.Windows

Public Class frmCortes

#Region "Variables"

#Region "Privadas"
    Private WithEvents prdDoc As New PrintDocument
    Private MedidaPapel As Size
    Private MedidaTrabajo As Size
    Dim bmpGrafico As New Bitmap(884, 540)
    Private CustomColors() As Integer
#End Region
#End Region

#Region "Constructor"
    Public Sub New(ByVal Medida_Papel As Size, ByVal Medida_Trabajo As Size)
        InitializeComponent()
        MedidaPapel = Medida_Papel
        MedidaTrabajo = Medida_Trabajo
    End Sub
#End Region

#Region "Eventos"
    Private Sub tsbCortes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCortes.Click
        CambiaColor(My.Settings.colCortes)
    End Sub

    Private Sub tsbGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbGuardar.Click
        GuardaImagen()
    End Sub

    Private Sub tsbHoja_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbHoja.Click
        CambiaColor(My.Settings.colHoja)
    End Sub

    Private Sub tsbImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbImprimir.Click
        ImprimeImagen()
    End Sub

    Private Sub tsbRegla_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbRegla.Click
        CambiaColor(My.Settings.colRegla)
    End Sub

    Private Sub tsbTextos_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbTextos.Click
        CambiaColor(My.Settings.colTexto)
    End Sub

    Private Sub tsbSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSalir.Click
        Me.Close()
    End Sub

    Private Sub frmCortes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.Control And e.KeyCode = Keys.Q Then
            Me.Close()
        ElseIf e.Control And e.KeyCode = Keys.P Then
            ImprimeImagen()
        ElseIf e.Control And e.KeyCode = Keys.G Then
            GuardaImagen()
        End If
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        DibujaAreaPapel()
    End Sub

    Private Sub prdDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prdDoc.PrintPage
        e.Graphics.DrawImage(picDraw.Image, 0, 0)
    End Sub
#End Region

#Region "Métodos"
    Private Sub CambiaColor(ByRef colColor As Color)
        Try
            With cdlgColor
                .Color = colColor
                .CustomColors = CustomColors
                .AllowFullOpen = True
                .AnyColor = True
                .FullOpen = True
                .SolidColorOnly = True
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    colColor = .Color
                    CustomColors = .CustomColors
                End If
                DibujaAreaPapel()
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
        End Try
    End Sub

    Private Sub DibujaAreaPapel()
        'Calcula numero de cortes que caben en el pliego
        Dim AreaPapel As Double = (MedidaPapel.Width * MedidaPapel.Height)
        Dim AreaTrabajo As Double = (MedidaTrabajo.Width * MedidaTrabajo.Height)
        Dim Unidades As Integer = AreaPapel / AreaTrabajo
        Me.Text = "Unidades por pliego: " & Unidades
        Me.Text = Me.Text & " | Desperdicio de Papel: " & CInt(100 - ((AreaTrabajo * 100) / AreaPapel) * Unidades) & " %"

        If (MedidaPapel.Height > MedidaPapel.Width) Then
            Dim aux As Double
            aux = MedidaPapel.Width
            MedidaPapel.Width = MedidaPapel.Height
            MedidaPapel.Height = aux
        End If

        'Iniciamos variables GDI Para Dibujo
        Dim g As Graphics = Graphics.FromImage(bmpGrafico)
        Dim Fuente As New Font("Segoe UI", 13, GraphicsUnit.Point)
        Dim MyBrush As Brush = New SolidBrush(My.Settings.colTexto)

        '->LIENZO
        g.FillRectangle(New SolidBrush(Color.White), 0, 0, picDraw.Width, picDraw.Height)

        '->REGLA VERTICAL
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 9, 10, 25, 10)
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 17, 10, 17, 500)
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 9, 500, 25, 500)
        g.DrawLine(New Pen(Color.White, 22), 0, 258, 35, 258)


        '->REGLA HORIZONTAL
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 40, 515, 40, 531)
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 40, 523, 850, 523)
        g.DrawLine(New Pen(My.Settings.colRegla, 1), 850, 515, 850, 531)
        g.DrawLine(New Pen(Color.White, 13), 425, 522, 460, 522)

        '->MEDIDAS DE PAPEL
        If Len(MedidaPapel.Height.ToString) = 2 Then
            g.DrawString(MedidaPapel.Height.ToString, Fuente, MyBrush, 5, 245)
        Else
            g.DrawString(MedidaPapel.Height.ToString, Fuente, MyBrush, 1, 245)
        End If
        If Len(MedidaPapel.Width.ToString) = 2 Then
            g.DrawString(MedidaPapel.Width.ToString, Fuente, MyBrush, 430, 510)
        Else
            g.DrawString(MedidaPapel.Width.ToString, Fuente, MyBrush, 426, 510)
        End If

        '---------------------------------------------------------------------------------
        Dim lapiz As New Pen(My.Settings.colCortes)
        'Crea una linea punteada
        lapiz.DashStyle = DashStyle.Dash
        Dim coordenadas As Point() = {New Point(60, 20), New Point(60, 200), New Point(400, 200), New Point(400, 20), New Point(60, 20)}
        g.DrawLines(lapiz, coordenadas)
        '---------------------------------------------------------------------------------

        '->MARCO DE PAPEL
        Dim points As Point() = {New Point(40, 9), New Point(40, 500), New Point(850, 500), New Point(850, 9), New Point(40, 9)}
        g.DrawLines(New Pen(My.Settings.colHoja, 3), points)
        picDraw.Image = bmpGrafico
    End Sub

    Private Sub GuardaImagen()
        Dim cdlgGuardar As New SaveFileDialog
        cdlgGuardar.Filter = "PNG (*.png)| *.png"
        cdlgGuardar.ShowDialog()
        If DialogResult = DialogResult.Cancel Then Exit Sub
        Try
            bmpGrafico.Save(cdlgGuardar.FileName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        cdlgGuardar.Dispose()
    End Sub

    Private Sub ImprimeImagen()
        Dim PrintDialog As New PrintDialog
        PrintDialog.Document = prdDoc
        If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then prdDoc.Print()
    End Sub
#End Region
End Class