Code sample Home

Convert vector drawing file into raster image file.
"Silent" mode, without displaying the drawing in a window.

int _tmain (int argc, _TCHAR* argv[])
{
  HANDLE hDrw, hBlock;
  BOOL   bRet;
  int    ImgWidth;
  double Xmin, Ymin, Xmax, Ymax, W, Gap;
  double Lef, Bot, Rig, Top;
  WCHAR* szFileName   = L"c:/Projects/_drawings/Lenovo.dxf";
  WCHAR* szRasterFile = L"c:/Projects/_drawings/Lenovo.png";

  lcInitialize( 0, 0, 0 );
  lcPropPutStr( 0, LC_PROP_G_REGCODE, L"your code" );

  hDrw = lcCreateDrawing();
  bRet = lcDrwLoad( hDrw, szFileName, 0 );
  if (bRet){
    // Set back color of Model space to White
    lcPropPutInt( hDrw, LC_PROP_DRW_COLORBACKM, RGB(255,255,255) );
    // Set fore color of Model space to Black
    lcPropPutInt( hDrw, LC_PROP_DRW_COLORFOREM, RGB(0,0,0) );
    // get Model block
    hBlock = lcPropGetHandle( hDrw, LC_PROP_DRW_BLOCK_MODEL );
    // get extents
    Xmin = lcPropGetFloat( hBlock, LC_PROP_BLOCK_XMIN );
    Ymin = lcPropGetFloat( hBlock, LC_PROP_BLOCK_YMIN );
    Xmax = lcPropGetFloat( hBlock, LC_PROP_BLOCK_XMAX );
    Ymax = lcPropGetFloat( hBlock, LC_PROP_BLOCK_YMAX );
    W = Xmax - Xmin;
    if (W > 0.0){
      // increase output rect
      Gap = W / 30.0;  // blank space
      Lef = Xmin - Gap;
      Bot = Ymin - Gap;
      Rig = Xmax + Gap;
      Top = Ymax + Gap;
      // make raster image and save it in a file
      ImgWidth = 1500;  // image width, pixels
      bRet = lcBlockRasterize( hBlock, szRasterFile, Lef, Bot, Rig, Top, ImgWidth, 0 );
    }
  }
  lcUninitialize( false ); // if true - save config
  return 0;
}

Variant 2 (as a function)

void DemoRasterize ()
{
  HANDLE hDrw, hBlock;
  BOOL   bRet;
  int    ImgWidth;
  double Xmin, Ymin, Xmax, Ymax, W, Gap;
  double Lef, Bot, Rig, Top;
  WCHAR* szFileName   = L"c:/!OK/Projects/_drawings/DWG_DXF/Lenovo.dxf";
  WCHAR* szRasterFile = L"c:/!OK/Projects/_drawings/DWG_DXF/Lenovo.png";

  hDrw = lcCreateDrawing();
  bRet = lcDrwLoad( hDrw, szFileName, 0 );
  if (bRet){
    // Set back color of Model space to White
    lcPropPutInt( hDrw, LC_PROP_DRW_COLORBACKM, RGB(255,255,255) );
    // Set fore color of Model space to Black
    lcPropPutInt( hDrw, LC_PROP_DRW_COLORFOREM, RGB(0,0,0) );
    // get Model block
    hBlock = lcPropGetHandle( hDrw, LC_PROP_DRW_BLOCK_MODEL );
    // get extents
    Xmin = lcPropGetFloat( hBlock, LC_PROP_BLOCK_XMIN );
    Ymin = lcPropGetFloat( hBlock, LC_PROP_BLOCK_YMIN );
    Xmax = lcPropGetFloat( hBlock, LC_PROP_BLOCK_XMAX );
    Ymax = lcPropGetFloat( hBlock, LC_PROP_BLOCK_YMAX );
    W = Xmax - Xmin;
    if (W > 0.0){
      // increase output rect
      Gap = W / 30.0;  // blank space
      Lef = Xmin - Gap;
      Bot = Ymin - Gap;
      Rig = Xmax + Gap;
      Top = Ymax + Gap;
      // make raster image and save it in a file
      ImgWidth = 1500;  // image width, pixels
      bRet = lcBlockRasterize( hBlock, szRasterFile, Lef, Bot, Rig, Top, ImgWidth, 0 );
      if (bRet){
        ::MessageBox( 0, L"Rasterize OK", L"Test", MB_OK );
        return;
      }
    }
  }
  ::MessageBox( 0, L"Rasterize Fails", L"Test", MB_OK );
}

Visual Basic NET code

  Private Sub RasterizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RasterizeToolStripMenuItem.Click
    Dim hDrw, hBlock As Integer
    Dim bRet As Integer
    Dim ImgWidth As Integer
    Dim Xmin, Ymin, Xmax, Ymax, W, Gap As Double
    Dim Lef, Bot, Rig, Top As Double
    Dim szFileName, szRasterFile As String

    szFileName = "c:/!OK/Projects/_drawings/DWG_DXF/Lenovo.dxf"
    szRasterFile = "c:/!OK/Projects/_drawings/DWG_DXF/Lenovo.png"

    hDrw = Lcad.CreateDrawing()
    bRet = Lcad.DrwLoad(hDrw, szFileName, 0)
    If bRet = 1 Then
      ' Set back color of Model space to White
      Lcad.PropPutInt(hDrw, LC_PROP_DRW_COLORBACKM, RGB(255, 255, 255))
      ' Set fore color of Model space to Black
      Lcad.PropPutInt(hDrw, LC_PROP_DRW_COLORFOREM, RGB(0, 0, 0))
      ' get Model block
      hBlock = Lcad.PropGetHandle(hDrw, LC_PROP_DRW_BLOCK_MODEL)
      ' get extents
      Xmin = Lcad.PropGetFloat(hBlock, LC_PROP_BLOCK_XMIN)
      Ymin = Lcad.PropGetFloat(hBlock, LC_PROP_BLOCK_YMIN)
      Xmax = Lcad.PropGetFloat(hBlock, LC_PROP_BLOCK_XMAX)
      Ymax = Lcad.PropGetFloat(hBlock, LC_PROP_BLOCK_YMAX)
      W = Xmax - Xmin
      If W > 0.0 Then
        ' increase output rect
        Gap = W / 30.0  ' blank space
        Lef = Xmin - Gap
        Bot = Ymin - Gap
        Rig = Xmax + Gap
        Top = Ymax + Gap
        ' make raster image and save it in a file
        ImgWidth = 1500  ' image width, pixels
        bRet = Lcad.BlockRasterize(hBlock, szRasterFile, Lef, Bot, Rig, Top, ImgWidth, 0)
        If bRet = 1 Then
          MessageBox.Show("Rasterize OK")
        End If
      End If
    End If
  End Sub


Used functions:

lcInitialize
lcUninitialize

lcCreateDrawing
lcDrwLoad
lcBlockRasterize

lcPropGetFloat
lcPropGetHandle
lcPropPutInt