:Start of MODI MODI.dsc
DragonSphereSoftware

DragonSphere Software Demos


If you are viewing this script in your browser you can save it as a VDS 5.x source file (*.dsc) to run it.




#
# DragonSphere Software's Home Page
#  
# All Source Files for this demo
#
# This Script attempts to duplicate the following Visual Basic code with VDS and Gadget's ActiveX abilities.
# This script requires the MODI object which is part of MS Office 2003/XP
# 
#  Dim miDoc As MODI.Document 
#  Dim miLayout As MODI.Layout 
#    
#  Set miDoc = New MODI.Document 
#  miDoc.Create "document1.tif" 
#   
#  miDoc.Images.OCR 
#   
#  Set miLayout = miDoc.Images.Layout 
# 
#  msgbox miLayout.Text 

Title VDS and MODI

If @Greater(@Name(@SYSINFO(DSVER)),4)
  # Load Gadget

External gadgetx.dll,@Sysinfo(DSVER)

  #DEFINE COMMAND,GADGETX,OLE,DEFINE,SET,CALL,FIREEVENT,SLEEP,VDSHASEVENTS
  #DEFINE FUNCTION,GADGETX,OLE,GET
  #DEFINE FUNCTION,TifToText
  #DEFINE FUNCTION,LoadTif
  #DEFINE COMMAND,UnLoadTif
Else
  Warn This demo is designed for VDS 5x
  Goto CLOSE
End

# Define a Gadget variable as a ActiveX object
Define Variable,Object,MODI 
Define Variable,Object,miDoc 
Define Variable,Object,miLayout 

# These are defined since VDS has no notion of what
# is true or false in the OLE world
Define Constant,OLE_TRUE,-1
Define Constant,OLE_FALSE,0

# Assign the Object's Event GUID to a constant
Define GUID,IDocumentEvents,{0CF31963-5E4D-4772-AE8D-82FE5A082B26}

# Initialize Gadgets OLE engine
Ole Init
# Create and load the MODI document object

#Set miDoc,@Ole(Create,MODI.Document,NULL)
Set MODI,@Ole(Create,{EF347A62-BA21-42E4-94A0-1C0A6D7FDFE7},NULL)
Set miDoc,@Ole(Create,MODI.Document,NULL)

If @Null(@Get(MODI))
  Warn This demo requires MS Office 2003/XP MODI object to operate.
  goto CLOSE
End
  DIALOG CREATE,TIF to Text,-1,0,617,586
# *** Modified by Dialog Designer on 12/15/2004 - 12:05 ***
  DIALOG ADD,EDIT,Output_edt,120,24,568,328,,,MULTI,SCROLL
  DIALOG ADD,STATUS,STATUS1,STATUS1
  DIALOG ADD,PROGRESS,PROGRESS1,464,28,564,24,0
  DIALOG ADD,TEXT,TEXT1,23,63,,,This example will take a multi-page *.tif image and OCR text in the image.
  DIALOG ADD,EDIT,ImageFile_edt,72,64,408,20,
  DIALOG ADD,BUTTON,imagefile_btn,72,472,32,20,...,Click to select Image
  DIALOG ADD,BUTTON,OCR_btn,520,172,64,24,&Convert
  DIALOG ADD,BUTTON,Quit_btn,520,344,64,24,&Quit
  DIALOG SHOW
  
# Map the Event method ID's to a name VDS can handle with @event()
Define EventMap,IDocumentEvents,OnOCRProgress,1
  
If @Get(miDoc)
  # Connect the object's event interface to VDS's event interface
  %%token = @Ole(ConnectEvents,miDoc,IDocumentEvents) 
End

%%Count = 0  
:evloop
  #Sleep 10
  Wait Event
  %E = @event()
  If %E
    Goto %E
  End
goto evloop
:TIMER

goto evloop
:Quit_btnBUTTON
:CLOSE
# Done with all the images in this file so free our Objects

If @Get(miDoc)
  # Disconnect the Object's event interface from VDS's event interface
  Ole DisConnectEvents,miDoc,IDocumentEvents,%%token
  # method Close
  Ole Call,miDoc,"Close(^b)",OLE_FALSE
  # Free MODI's document object
  Ole Free,Object,miDoc
End

If @Get(MODI)
  # Free MODI's document object
  Ole Free,Object,MODI
End
# Unload Gadget's OLE engine
Ole UnInit
# We are all done now;-)
Exit

:imagefile_btnBUTTON
  %F = @filedlg("Tiff Images|*.tif;*.tiff",Image too OCR) 
  If @Ok()
    Dialog Set,ImageFile_edt,%F
  End
goto evloop
:IDocumentEventsOnOCRProgress
  %%Params = @Ole(EventParamsList,IDocumentEventsOnOCRProgress)
  If %%Params
    %%OldSep = @fsep()
    Option FieldSep,@chr(44)
    PARSE "%%Progress;%%Cancel",%%Params
    dialog set,PROGRESS1,%%Progress
    Option FieldSep,%%OldSep
  End
goto evloop
:OCR_btnBUTTON
  %F = @dlgtext(ImageFile_edt)
  If %F
    If @Not(@Equal(%%NewFile,%F))
      DIALOG SET,Output_edt,
      If @Greater(@Name(@SYSINFO(DSVER)),4)
        # If this is VDS 5 or above call the subroutine as a Function
        # Load the Tif Image and get the number of pages to process
        %%NumPages = @LoadTif(%F)
      Else
        # If this is VDS 4 or below call the subroutine With GoSub
        # Load the Tif Image and get the number of pages to process
        %1 = %F
        GoSub LoadTif
        %%NumPages = %R
        %R =
      End
      %O = 
    End
    %%NewFile = %F
    # Display the text from the image how ever you want.
    If @Greater(@Name(@SYSINFO(DSVER)),4)
      # If this is VDS 5 or above call the subroutine as a Function
      If @Equal(%%Count,%%NumPages)
        # We are done with all the pages so stop firing events and set
        # the status and progress bar
        Dialog Set,STATUS1,%%NumPages pages converted...
        Dialog Set,PROGRESS1,100
        %%Count = 0
        %%NewFile =
        #UnLoadTif OLE_FALSE
      Else
        # Hey we have work to do
        %O = @dlgtext(Output_edt)
        Dialog Set,STATUS1,Converting page @succ(%%Count) of %%NumPages
        # OCR the text for this page
        %%Text = @TifToText(%%Count)
        Dialog Set,Output_edt,%OPage @succ(%%Count) of %%NumPages@CR()@LF()%%Text@CR()@LF()
        FireEvent OCR_btnBUTTON        
        %%Count = @succ(%%Count)
      End
    Else
      # If this is VDS 4 or below call the subroutine With GoSub
      If @Equal(%%Count,%%NumPages)
        # We are done with all the pages so stop firing events and set
        # the status and progress bar
        Dialog Set,STATUS1,%%NumPages pages converted...
        Dialog Set,PROGRESS1,100
        %%Count = 0
        %%NewFile =
        %1 = OLE_FALSE
        GoSub UnLoadTif
      Else
        # Hey we have work to do
        %O = @dlgtext(Output_edt)
        Dialog Set,STATUS1,Converting page @succ(%%Count) of %%NumPages
        # OCR the text for this page
        %1 = %%Count
        GoSub TifToText
        %%Text = %R
        Dialog Set,Output_edt,%OPage @succ(%%Count) of %%NumPages@CR()@LF()%%Text@CR()@LF()
        FireEvent OCR_btnBUTTON        
        %%Count = @succ(%%Count)
        %R =
      End      
    End
  End
goto evloop

:LoadTif
  If @Greater(@Ole(Get,miDoc,^d,Images.Count),-1)
    Ole Call,miDoc,"Close(^b)",OLE_FALSE
  End
  %R =
  # Create the MODI document and load our *.TIF image into the document
  Ole Call,miDoc,"Create(^B)",%1
  If @Not(@OK())
    # I really don't know why but for some reason under my Windows XP Pro machine
    # MODI throws a Class not registered 80040154 error the first time I try to
    # load the TIF image. So load it a second time.
    Ole Call,miDoc,"Create(^B)",%1
  End
  # Since Gadget is not Visual Basic it handles Enumerated objects
  # a little different.  We have to walk through each object in a loop
  # Get the number of pages/layers in the *.TIF image
  %R = @Ole(Get,miDoc,^d,Images.Count)
Exit %R
:UnLoadTif
  Ole Call,miDoc,"Close(^b)",%1
Exit
:TifToText
  %R =
  %I = %1
  If @Greater(%I,-1)
    # OCR the page/layer number specified 
    Ole Call,miDoc,"Images(^d).OCR",%I
    # Get the Layout of the page/layer
    # Set miLayout,@Ole(Call,miDoc,^o,"Images(^d).Layout",%I)
    # Get the OCR'ed Text and return it.
    # %R = @Ole(Call,miLayout,^B,Text)
    %R = @Ole(Call,miDoc,^B,"Images(^d).Layout.Text",%I)
    # wait 0.03
    # Free our Layout..
    # Ole Free,Object,miLayout
  End
Exit %R

:End of MODI