Cad Image to JPG in VB.NET example!!

Discuss and ask questions about CAD DLL (CAD Image DLL, CAD Importer DLL).

Moderators: admin, support, SDS

Post Reply
ticoadf
Posts: 1
Joined: 22 Apr 2005, 10:23

Cad Image to JPG in VB.NET example!!

Post by ticoadf » 22 Apr 2005, 10:26

Code: Select all

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Imports System.Drawing

Public Class DLLWin32Import

    Public Shared CADFile As New IntPtr()
    Public Shared fAbsHeight, fAbsWidth As Double
    Public Shared fScale As Integer = 100
    Public Shared FX, FY As Integer
    Public Shared fScaleRect As New FPoint()
    Public Shared fStart As New Point()
    Public Shared fOld As New Point()
    Private Shared Ancho As Single
    Private Shared Alto As Single

    Public Sub DwgToJpg(ByVal strRutaDwg As String, ByVal strRutaJpg As String, ByVal dblAncho As Integer, ByVal dblAlto As Integer)

        If Not DLLWin32Import.CADFile.Equals(IntPtr.Zero) Then
            DLLWin32Import.CloseCAD(DLLWin32Import.CADFile)
            DLLWin32Import.CADFile = IntPtr.Zero
        End If

        DLLWin32Import.CADFile = DLLWin32Import.CreateCAD(IntPtr.Zero, strRutaDwg)
        If DLLWin32Import.CADFile.Equals(IntPtr.Zero) Then
            'MessageBox.Show("Error open file:" + strRutaDwg)
            Return
        End If

        Dim fRectExtentsCAD As New FRect()
        DLLWin32Import.GetBoxCAD(DLLWin32Import.CADFile, Ancho, Alto)
        DLLWin32Import.GetExtentsCAD(DLLWin32Import.CADFile, fRectExtentsCAD)
        DLLWin32Import.fAbsHeight = fRectExtentsCAD.Top - fRectExtentsCAD.Bottom
        DLLWin32Import.fAbsWidth = fRectExtentsCAD.Right - fRectExtentsCAD.Left
        Dim cnt As Integer = DLLWin32Import.CADLayerCount(DLLWin32Import.CADFile)
        Dim aData As New DXFData()

        Dim Layer As IntPtr
        Dim C As Single
        Dim i As Integer
        For i = 0 To cnt - 1
            Layer = DLLWin32Import.CADLayer(DLLWin32Import.CADFile, i, aData)
            C = aData.Color
            If (aData.Flags And 1) <> 0 Then
                C = C OrElse &H80000000&
            End If
            If aData.Text <> Nothing Then
                'MessageBox.Show("[" & i & "] " & aData.Text)
                'cbLayers.Items.Add(aData.Text); //??
            End If
        Next

        If DLLWin32Import.CADFile.Equals(IntPtr.Zero) Then Return
        Dim fRect As New Rect()

        Dim cadDraw As New CADDraw()
        Dim framePointer As System.IntPtr
        Dim b As New Bitmap(dblAncho, dblAlto)
        Dim g As System.Drawing.Graphics
        Dim po As System.IntPtr
        Dim Koef As Single

        fRect.Left = 0
        fRect.Top = 0

'Miro el tamaсo --> 1 cm = 32 pixels
        If Alto > b.Height / 32 Then
            fRect.Right = Ancho * 32
            fRect.Bottom = Alto * 32
        Else
            fRect.Right = b.Width
            fRect.Bottom = b.Height
        End If

        fRect.BottomRight = New RPoint(fRect.Right, fRect.Bottom)
        fRect.TopLeft = New RPoint(fRect.Left, fRect.Top)

        Koef = Alto / Ancho
        fRect.Bottom = CInt(Math.Round(fRect.Top + (fRect.Right - fRect.Left) * Koef))
        If fRect.Bottom > b.Height Then
            'Compruebo si se pasa de alto y ajusto el coeficiente de reduccion
            While fRect.Bottom > b.Height
                Alto = Alto - 1
                Koef = Alto / Ancho
                fRect.Bottom = CInt(Math.Round(fRect.Top + (fRect.Right - fRect.Left) * Koef))
            End While
        End If

        fRect.Left = fRect.Left * DLLWin32Import.fScale / 100 + DLLWin32Import.FX
        fRect.Top = fRect.Top * DLLWin32Import.fScale / 100 + DLLWin32Import.FY
        fRect.Right = fRect.Right * DLLWin32Import.fScale / 100 + DLLWin32Import.FX
        fRect.Bottom = fRect.Bottom * DLLWin32Import.fScale / 100 + DLLWin32Import.FY
        fRect.BottomRight = New RPoint(fRect.Right, fRect.Bottom)
        fRect.TopLeft = New RPoint(fRect.Left, fRect.Top)
        DLLWin32Import.fScaleRect.X = DLLWin32Import.fAbsWidth / (fRect.Right - fRect.Left)
        DLLWin32Import.fScaleRect.Y = DLLWin32Import.fAbsHeight / (fRect.Bottom - fRect.Top)

        'Guardo el archivo como jpg
        cadDraw.Size = Marshal.SizeOf(cadDraw)
        cadDraw.R.Left = fRect.Left * DLLWin32Import.fScale / 100 + DLLWin32Import.FX
        cadDraw.R.Top = fRect.Top * DLLWin32Import.fScale / 100 + DLLWin32Import.FY
        cadDraw.R.Right = fRect.Right * DLLWin32Import.fScale / 100 + DLLWin32Import.FX
        cadDraw.R.Bottom = fRect.Bottom * DLLWin32Import.fScale / 100 + DLLWin32Import.FY
        cadDraw.R.BottomRight = New RPoint(fRect.Right, fRect.Bottom)
        cadDraw.R.TopLeft = New RPoint(fRect.Left, fRect.Top)
        cadDraw.DrawMode = 0
        framePointer = DLLWin32Import.DrawCADtoJpeg(DLLWin32Import.CADFile, cadDraw)

        g = System.Drawing.Graphics.FromImage(b)
        g.FillRectangle(System.Drawing.Brushes.White, 0, 0, b.Width, b.Height)
        po = g.GetHdc()
        DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, fRect)
        g.ReleaseHdc(po)
        '//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
        b.Save(strRutaJpg, ImageFormat.Jpeg)
    End Sub



    <DllImport("CADImage.dll")> _
     Public Shared Function CreateCAD(ByVal hWindow As IntPtr, ByVal lpFileName As String) As IntPtr
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function CloseCAD(ByVal hObject As IntPtr) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function DrawCAD(ByVal hObject As IntPtr, ByVal hDC As IntPtr, ByRef lprc As Rect) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function GetExtentsCAD(ByVal handle As IntPtr, ByRef fRect As FRect) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function CADLayerCount(ByVal hObject As IntPtr) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function CADLayer(ByVal hObject As IntPtr, ByVal nIndex As Integer, ByRef lpData As DXFData) As IntPtr
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function GetLastErrorCAD(<MarshalAs(UnmanagedType.LPStr)> ByVal lbBuf As String) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function DrawCADEx(ByVal hObject As IntPtr, ByRef lpcd As CADDraw) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function CADLayerVisible(ByVal hObject As IntPtr, ByVal visible As Integer) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function CADVisible(ByVal hObject As IntPtr, <MarshalAs(UnmanagedType.LPStr)> ByVal lpLayerName As String) As Integer
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function DrawCADtoBitmap(ByVal hObject As IntPtr, ByRef lpcd As CADDraw) As IntPtr
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function DrawCADtoGif(ByVal hObject As IntPtr, ByRef lpcd As CADDraw) As IntPtr
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function DrawCADtoJpeg(ByVal hObject As IntPtr, ByRef lpcd As CADDraw) As IntPtr
    End Function

    <DllImport("CADImage.dll")> _
    Public Shared Function GetBoxCAD(ByVal hObject As IntPtr, ByRef AbsWidht As Single, ByRef AbsHeight As Single) As Integer
    End Function

End Class

Public Structure FRect
    Public Left, Top, Z1, Right, Bottom, Z2 As Single
    Public TopLeft, BottomRight As FPoint
End Structure

Public Structure Rect
    Public Left, Top, Right, Bottom As Integer
    Public TopLeft, BottomRight As RPoint
End Structure

Public Structure FPoint
    Public X, Y, Z As Single
End Structure

Public Structure RPoint

    Public _X, _Y As Single
    Public Sub New(ByVal x As Single, ByVal y As Single)
        _X = x
        _Y = y
    End Sub
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure DXFData

    Public Tag As UInt16
    Public Count As UInt16
    Public TickCount As UInt16
    Public Flags As Byte
    Public Style As Byte
    Public Dimension As Integer
    Public DashDots As FPoint
    Public DashDotsCount As Integer
    Public Color As Integer
    Public Ticks As Integer
    Public Thickness As Single
    Public Rotation As Single
    <MarshalAs(UnmanagedType.LPStr)> _
    Public Layer As String

    <MarshalAs(UnmanagedType.LPStr)> _
    Public Text As String

    Public Point1 As FPoint
    Public Point2 As FPoint
    Public Point3 As FPoint
    Public Point4 As FPoint

    Public Radius, StartAngle, EndAngle As Single
    Public Block As IntPtr
    Public Scale As FPoint
    Public FHeight, FScale, RWidth, RHeight As Single
    Public HAlign, VAlign As Byte
    Public Shared PolyPoints As FPoint()
End Structure

Public Structure CADDraw
    Public Size As Long
    Public DC As IntPtr
    Public R As Rect
    Public DrawMode As Byte
End Structure

Post Reply