Visual Basic Customization

Reply
New Member
mgozdz
Posts: 2
Registered: ‎03-28-2001
Message 1 of 4 (50 Views)

Export PaperSheet to BMP or TIFF File

50 Views, 3 Replies
08-24-2000 07:22 AM
Hi.

I have a problem with exporting drawning from AutoCAD 2000 to BMP (or TIF) file. I must to transform PaperSheet to one of these file type. If I selected elements from model space viewport (MSpace=true), ACAD received only elements from model space viewport. If I selected elements from paper space viewport (MSpace=false), ACAD received only elements, which thera are on the model space viewport.
I don't know the way, how to select all elements end exports all elements from PaperSheet to graphical file.
Maybe is another way to do it?

Help me, please!!!
Martin G.
*Gagne, Denis
Message 2 of 4 (50 Views)

Re: Export PaperSheet to BMP or TIFF File

08-27-2000 01:55 PM in reply to: mgozdz
Martin,

If you use the EXPORT method, you will have to combine the two resulting
bitmaps (model and paper).

This can be done using the BitBlt win32 api function. Basically, you would
have to copy the model space bmp and the paper space bmp to a device context
containing only a white background. You will use the SRCAND flag so that the
the copied bmp will be transparent. You will also need to calculate an
offset for the model space window (the top left corner o/s in pixels) in the
current view.

Note: The exported bmp's appear to be nothing more than a window capture. If
this assumption is correct, you could use the printscreen and obtain similar
results or win32 api functions as in the following example. The code was not
tested very much, it may not work in some situations. If you experiment a
problem, let me know.

Denis

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&Paste in a Userform with 5 commandbuttons:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

'--------------------------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright: Denis Gagne 2000-08-27
'
'--------------------------------------------------------------------

Option Explicit
'--------------------------------------------------------------------
' Capture the entire screen.

Private Sub CommandButton1_Click()
Me.Hide
DoEvents
Set Me.Picture = CaptureScreen()
SavePicture Me.Picture, "C:\Acad2000\Bitmap\CapScreen.bmp"
Me.Show
End Sub

' Capture the entire document including toolbars and dockable windows.
Private Sub CommandButton2_Click()
Dim Rc As RECT
Dim hWndWindow As Long
Me.Hide
DoEvents
' Get a handle to the document window.
hWndWindow = ThisDrawing.hWnd
GetWindowRect hWndWindow, Rc

' Call CaptureWindow to capture the entire document give the handle
' and return the resulting Picture object.

Set Me.Picture = CaptureWindow(hWndWindow, False, 0, 0, _
Rc.Right - Rc.Left, _
Rc.Bottom - Rc.Top)
SavePicture Me.Picture, "C:\Acad2000\Bitmap\CapDocument.bmp"
Me.Show
End Sub

' Capture the client view of the document.
Private Sub CommandButton3_Click()
Dim Rc As RECT
Dim hWndWindow As Long
Me.Hide
DoEvents
' Get a handle to the document window.
hWndWindow = GetWindow(ThisDrawing.hWnd, GW_CHILD)
GetWindowRect hWndWindow, Rc
' Call CaptureWindow to capture the entire document given the handle
' and size and return the resulting Picture object.

Set Me.Picture = CaptureWindow(hWndWindow, False, 0, 0, _
Rc.Right - Rc.Left, _
Rc.Bottom - Rc.Top)
SavePicture Me.Picture, "C:\Acad2000\Bitmap\CapView.bmp"
Me.Show
End Sub

' Capture the plot area.
Private Sub CommandButton4_Click()

Dim pixSize As Double
Dim TLV(1) As Double 'Top left coord expr. in pixels
Dim TLP(1) As Double 'Top left coord expr. in pixels
Dim Ofs(1) As Long
Dim pt1(2) As Double, pt2(2) As Double
Dim screensize As Variant
Dim vcu As Variant
Dim originValue As Variant
Dim PaperWidth As Double
Dim PaperHeight As Double
Dim scFactor As Double
Dim MarginLowerLeft As Variant, MarginUpperRight As Variant
Dim Lay As AcadLayout

Me.Hide
DoEvents

ThisDrawing.ActiveSpace = acPaperSpace
ThisDrawing.MSpace = False

Set Lay = ThisDrawing.ActiveLayout
originValue = Lay.PlotOrigin

'those papermargins and papersize properties, what a ...
Lay.GetPaperMargins MarginUpperRight, MarginLowerLeft
If Lay.PlotRotation = AcPlotOrientation.acPlotOrientationLandscape Then
Lay.GetPaperSize PaperHeight, PaperWidth
PaperWidth = PaperWidth - MarginLowerLeft(1) - MarginUpperRight(1)
PaperHeight = PaperHeight - MarginLowerLeft(0) - MarginUpperRight(0)
Else
Lay.GetPaperSize PaperWidth, PaperHeight
PaperWidth = PaperWidth - MarginLowerLeft(0) - MarginUpperRight(0)
PaperHeight = PaperHeight - MarginLowerLeft(1) - MarginUpperRight(1)
End If

scFactor = IIf(Lay.PaperUnits = acInches, 12 / 304.8, 1)

pt1(0) = originValue(0): pt1(1) = originValue(1)
pt2(0) = pt1(0) + PaperWidth * scFactor
pt2(1) = pt1(1) + PaperHeight * scFactor

ZoomWindow pt1, pt2

pixSize = PixelSize
screensize = ThisDrawing.GetVariable("screensize")

vcu = ThisDrawing.GetVariable("viewctr")

TLV(0) = vcu(0) - (pixSize * screensize(0)) / 2
TLV(1) = vcu(1) + (pixSize * screensize(1)) / 2

TLP(0) = originValue(0)
TLP(1) = originValue(1) + PaperHeight * scFactor

Ofs(0) = (TLP(0) - TLV(0)) / pixSize
Ofs(1) = (TLV(1) - TLP(1)) / pixSize

Set Me.Picture = CaptureWindow(GetWindow(ThisDrawing.hWnd, GW_CHILD), _
False, Ofs(0), Ofs(1), _
PaperWidth * scFactor / pixSize, _
PaperHeight * scFactor / pixSize)
SavePicture Me.Picture, "C:\Acad2000\Bitmap\CapPlot.bmp"
Me.Show

End Sub

' Clear out the picture.
Private Sub CommandButton5_Click()
Set Me.Picture = Nothing
End Sub

' Initialize the form
Private Sub UserForm_Initialize()
Me.Caption = "Capture Example"
CommandButton1.Caption = "Capture Screen"
CommandButton2.Caption = "Capture Document"
CommandButton3.Caption = "Capture Acad View"
CommandButton4.Caption = "Capture Plot Area"
CommandButton5.Caption = "Clear Picture"
End Sub

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&Paste in a bas module:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

'--------------------------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright: Denis Gagne 2000-08-27
'
'
' This module is an adaptation of pieces of code found at MSDN
'
' It contains several routines for capturing into a picture
' different parts of AutoCAD window.
'
' CreateBitmapPicture - Creates a picture object from a bitmap and
' palette.
' CaptureWindow - Captures any window given a window handle.
' CaptureActiveWindow - Captures the active window on the desktop.
' CaptureScreen - Captures the entire screen.
'
' NOTES
' - No error trapping is included in these routines.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Option Explicit
Option Base 0

Private Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type

Private Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors.
End Type

Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type

Private Const RASTERCAPS As Long = 38
Private Const RC_PALETTE As Long = &H100
Private Const SIZEPALETTE As Long = 104
Public Const GW_CHILD As Long = 5
Public Const SRCCOPY = &HCC0020
Public Const CF_BITMAP As Long = 2

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function CreateCompatibleDC Lib "GDI32" ( _
ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "GDI32" ( _
ByVal hDC As Long, ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
Private Declare Function GetDeviceCaps Lib "GDI32" ( _
ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "GDI32" ( _
ByVal hDC As Long, ByVal wStartIndex As Long, _
ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) _
As Long
Private Declare Function CreatePalette Lib "GDI32" ( _
lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectObject Lib "GDI32" ( _
ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "GDI32" ( _
ByVal hDCDest As Long, ByVal XDest As Long, _
ByVal YDest As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hDCSrc As Long, _
ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) _
As Long
Private Declare Function DeleteDC Lib "GDI32" ( _
ByVal hDC As Long) As Long
Private Declare Function GetForegroundWindow Lib "USER32" () _
As Long
Private Declare Function SelectPalette Lib "GDI32" ( _
ByVal hDC As Long, ByVal hPalette As Long, _
ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "GDI32" ( _
ByVal hDC As Long) As Long
Private Declare Function GetWindowDC Lib "USER32" ( _
ByVal hWnd As Long) As Long
Private Declare Function GetDC Lib "USER32" ( _
ByVal hWnd As Long) As Long
Declare Function GetWindowRect Lib "USER32" ( _
ByVal hWnd As Long, lpRect As RECT) As Long
Declare Function GetWindow Lib "USER32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function ReleaseDC Lib "USER32" ( _
ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDesktopWindow Lib "USER32" () As Long

Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type

Private Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CreateBitmapPicture
' - Creates a bitmap type Picture object from a bitmap and
' palette.
'
' hBmp
' - Handle to a bitmap.
'
' hPal
' - Handle to a Palette.
' - Can be null if the bitmap doesn't use a palette.
'
' Returns
' - Returns a Picture object containing the bitmap.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CreateBitmapPicture(ByVal hBmp As Long, _
ByVal hPal As Long) As Picture

Dim r As Long

Dim Pic As PicBmp
' IPicture requires a reference to "Standard OLE Types."
Dim IPic As IPicture
Dim IID_IDispatch As GUID

' Fill in with IDispatch Interface ID.
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With

' Fill Pic with necessary parts.
With Pic
.Size = Len(Pic) ' Length of structure.
.Type = 1 ' Type of Picture (bitmap).
.hBmp = hBmp ' Handle to bitmap.
.hPal = hPal ' Handle to palette (may be null).
End With

' Create Picture object.
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

' Return the new Picture object.
Set CreateBitmapPicture = IPic
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureWindow
' - Captures any portion of a window.
'
' hWndSrc
' - Handle to the window to be captured.
'
' Client
' - If True CaptureWindow captures from the client area of the
' window.
' - If False CaptureWindow captures from the entire window.
'
' LeftSrc, TopSrc, WidthSrc, HeightSrc
' - Specify the portion of the window to capture.
' - Dimensions need to be specified in pixels.
'
' Returns
' - Returns a Picture object containing a bitmap of the specified
' portion of the window that was captured.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''
'
Public Function CaptureWindow(ByVal hWndSrc As Long, _
ByVal Client As Boolean, ByVal LeftSrc As Long, _
ByVal TopSrc As Long, ByVal WidthSrc As Long, _
ByVal HeightSrc As Long) As Picture

Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long

Dim LogPal As LOGPALETTE

' Depending on the value of Client get the proper device context.
If Client Then
hDCSrc = GetDC(hWndSrc) ' Get device context for client area.
Else
hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire
' window.
End If

' Create a memory device context for the copy process.
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC.
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)

' Get screen properties.
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
' capabilities.
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
' support.
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
' palette.

' If the screen has a palette make a copy and realize it.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
' Create a copy of the system palette.
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hDCSrc, 0, 256, _
LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
' Select the new palette into the memory DC and realize it.
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If

' Copy the on-screen image into the memory DC.
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
LeftSrc, TopSrc, SRCCOPY)

' Remove the new copy of the on-screen image.
hBmp = SelectObject(hDCMemory, hBmpPrev)

' If the screen has a palette get back the palette that was
' selected in previously.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If

' Release the device context resources back to the system.
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)

' Call CreateBitmapPicture to create a picture object from the
' bitmap and palette handles. Then return the resulting picture
' object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureScreen
' - Captures the entire screen.
'
' Returns
' - Returns a Picture object containing a bitmap of the screen.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureScreen() As Picture
Dim Rc As RECT
Dim hWndScreen As Long

' Get a handle to the desktop window.
hWndScreen = GetDesktopWindow()
GetWindowRect hWndScreen, Rc
' Call CaptureWindow to capture the entire desktop give the handle
' and return the resulting Picture object.

Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, _
Rc.Right, _
Rc.Bottom)
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureActiveWindow
' - Captures the currently active window on the screen.
'
' Returns
' - Returns a Picture object containing a bitmap of the active
' window.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureActiveWindow() As Picture
Dim hWndActive As Long
Dim r As Long
Dim RectActive As RECT

' Get a handle to the active/foreground window.
hWndActive = GetForegroundWindow()

' Get the dimensions of the window.
r = GetWindowRect(hWndActive, RectActive)

' Call CaptureWindow to capture the active window given its
' handle and return the Resulting Picture object.
Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, _
RectActive.Right - RectActive.Left, _
RectActive.Bottom - RectActive.Top)
End Function

Public Function PixelSize() As Double
Dim viewsize As Double
Dim screensize As Variant

viewsize = ThisDrawing.GetVariable("viewsize")
screensize = ThisDrawing.GetVariable("screensize")
PixelSize = viewsize / screensize(1)
End Function

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&End of bas module
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

"mgozdz" a écrit dans le message news:
ef3903d.-1@WebX.SaUCah8kaAW...
Hi.
I have a problem with exporting drawning from AutoCAD 2000 to BMP (or TIF)
file. I must to transform PaperSheet to one of these file type. If I
selected elements from model space viewport (MSpace=true), ACAD received
only elements from model space viewport. If I selected elements from paper
space viewport (MSpace=false), ACAD received only elements, which thera are
on the model space viewport.
I don't know the way, how to select all elements end exports all elements
from PaperSheet to graphical file.
Maybe is another way to do it?
Help me, please!!!
Martin G.
*Tanzillo, Tony
Message 3 of 4 (50 Views)

Re:

08-30-2000 06:15 PM in reply to: mgozdz
Denis - Why not just send a WM_PRINT message to the window?

Denis Gagne wrote:
>
> Martin,
>
> If you use the EXPORT method, you will have to combine the two resulting
> bitmaps (model and paper).
>
> This can be done using the BitBlt win32 api function. Basically, you would
> have to copy the model space bmp and the paper space bmp to a device context
> containing only a white background. You will use the SRCAND flag so that the
> the copied bmp will be transparent. You will also need to calculate an
> offset for the model space window (the top left corner o/s in pixels) in the
> current view.
>
> Note: The exported bmp's appear to be nothing more than a window capture. If
> this assumption is correct, you could use the printscreen and obtain similar
> results or win32 api functions as in the following example. The code was not
> tested very much, it may not work in some situations. If you experiment a
> problem, let me know.
>
> Denis
>
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> &&Paste in a Userform with 5 commandbuttons:
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>
> '--------------------------------------------------------------------
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' Copyright: Denis Gagne 2000-08-27

--

Checkout the AcadX(tm) ActiveX Extension Library at:

http://www.caddzone.com/acadx/acadx.htm

/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://www.caddzone.com */
/*********************************************************/
*Gagne, Denis
Message 4 of 4 (50 Views)

Re:

08-31-2000 04:07 AM in reply to: mgozdz
> Denis - Why not just send a WM_PRINT message to the window?
>
Tony ,

With BitBlt we can specify the portion of the window to copy.

I don't know if this is possible with the WM_PRINT message...

The difficulty from a vba perspective was more to create a Picture from a
DC.

Denis

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community