'
' AttachRasterTest() - Prompts it attach a raster to the DGN file at (0,0) at the size of the raster.
' Uses RAS/Edit RaseditAPI_getRasterInfo() to get the size and DPI of the raster
' so it can compute the size to attach it at.
'
Public Sub AttachRasterTest()
Dim rasterName As String
Dim sugName As String
Dim x_dpi As Double ' x-dpi
Dim y_dpi As Double ' y-dpi
Dim x_pixels As Double ' x-size in pixels
Dim y_pixels As Double ' y-size in pixels
Dim x_size As Double ' x-size in inches
Dim y_size As Double ' y-size in inches
Dim invert As Integer ' results of histogram

Dim strResult As String ' return string from RASEDIT RaseditAPI_getRasterInfo
Dim strExtent As String ' string that is the keyin of the extent of the raster
Dim extent As Point3d


' Prompt for raster file via MDL file open dialog ...
rasterName = Space(1024)
If mdlDialog_fileOpen(rasterName, 0, 0, "", "*.tif", ActiveDesignFile.Path, "Select Raster to Attach...") <> 0 Then Exit Sub
rasterName = TruncateAtEOS(rasterName)

' Get the raster info from RASEDIT
strResult = RaseditAPI_getRasterInfo(rasterName, x_dpi, y_dpi, x_pixels, y_pixels, invert)
If strResult <> "SUCCESS" Then
Response = MsgBox(rasterName + ": " + strResult, vbOKOnly + vbExclamation, "RAS/Edit API Error", "", 0)
Exit Sub
End If

Debug.Print ""
Debug.Print ""
Debug.Print "RASEDIT VBA API AttachRasterTest() Example"
Debug.Print "-------------------------------------------"

' print raster name to attach
Debug.Print "Raster file to attach = " & rasterName

' print size in pixels and DPI
Debug.Print "x_dpi=" & x_dpi, "y_dpi=" & y_dpi
Debug.Print "x_pixels=" & x_pixels, "y_pixels=" & y_pixels

' compute the size of the raster in inches (size in Inches = #pixels / DPI)
x_size = x_pixels / x_dpi
y_size = y_pixels / y_dpi
Debug.Print "x_size=" & x_size, "y_size=" & y_size


' Compute the size of a shape to place around the raster (assumes MU=Ft, SU=In)
extent.X = x_size / 12#
extent.Y = y_size / 12#

' Place a shape the size of the raster
CadInputQueue.SendKeyin "PLACE BLOCK"
CadInputQueue.SendKeyin "XY=0,0"

' Create keyin string based on size of raster (i.e., XY=0:17,0:24)
' (assumes master units=FT and sub units=IN)
strExtent = "XY=" + "0:" + LTrim(str(x_size)) + "," + "0:" + LTrim(str(y_size))

CadInputQueue.SendKeyin strExtent

' Attach raster to dimensions of shape
CadInputQueue.SendKeyin "RASTER ATTACH INTERACTIVE " + rasterName
CadInputQueue.SendKeyin "XY=0,0"
CadInputQueue.SendKeyin strExtent
End Sub

' Truncates a buffer to just contain the string that
' the C function returned.
Private Function TruncateAtEOS(str As String) As String
If str <> "" Then ' This test avoids exception on zero-length string
TruncateAtEOS = Left$(str, InStr(1, str, vbNullChar) - 1)
End If
End Function