Accessing Forest and Trees from an ASP page using OLE Automation

Document ID : KB000056070
Last Modified Date : 14/02/2018
Show Technical Document Details

It is possible to use Forest & Trees to store and/or retrieve data from an FTV with OLE automation via an ASP page. The scope of this article is to provide a general overview of using OLE Automation on an ASP web page.

The simple example ASP application below can be used to store an inventory of items and calculate and retrieve a graph from Forest & Trees.

The files that are used in this demonstration can be downloaded from here:-

ftp://ftp.ca.com/CAproducts/forest-trees/Samples/OLEAuto.zip

Requirements

  • Forest & Trees with the OLE Automation Server Add-in installed (FTWAUSVR.FXA)
  • IIS

Preparing the FTV

Create a table view with 'Formula' interface and call it inventory_totals.

Enter the initial formula for this view as

Calculate the view. Using Column layout, change the COLUMN1 caption and title to 'item' and the COLUMN2 caption and title to 'amount'.

Create a default graph as follows, click Ok.

Name it 'Inventory', click OK.

Accept the defaults that appear, click OK.

On the toolbar, Click the Pie chart button.

Right click the graph and click, Annotations, Text. Then modify the Point Labels as shown.

You may want to add further formatting to the graph at this point, but this is beyond the scope of this article.

Click the Fast Exit button.

Now that the column titles and graph have been defined, change the view definition of 'inventory_totals' to a view with 'No Interface'.

Now save the FTV in a folder and call it inventory.ftv.

Using ASP to access the FTV

We use the ASP page to write new values to the inventory_totals view within inventory.ftv. The graph is then redrawn and the image is exported to JPEG file which is then displayed on the web page.

The full source of the ASP page, inventory.asp, is listed below but here is a description of the significant VB Script code fragments.

Dim ftobj Set ftobj = Server.CreateObject("Forest.Trees.Application")

This creates the OLE Server object, once this object is instantiated all Forest & Trees automation methods can be executed.

The FTExecuteFormula method allows the execution of any F&T function externally.

ftobj.FTExecuteFormula "<F&T function>"

This function is used to open the FTV file, add the new values, to export the graph, to discard the data and to save the updated file. There are many other F&T Methods for automation and these are described in the F&T help file.

Set ftobj = Nothing

This closes down OLE Server object.

Place inventory.asp into the same folder as inventory.ftv.

IIS Setup

Create a virtual directory in IIS and call it inventory and point to the folder where inventory.asp and inventory.ftv reside.

Setup the virtual directory to Enable Content Expiration immediately as follows. This is necessary because the default behaviour of IIS is to cache images, so the updated graph image would be displayed unless the content of the virtual directory expires immediately.

'inventory.asp' Source

<%@ Language=VBScript %>
<html>
<head>
<title>Inventory F&T OLE Server example</title>
</head>
<body>
<%
   Sub RecalcInventory (item, amount)
      Dim ftobj

      Set ftobj = Server.CreateObject("Forest.Trees.Application")

      app_path = Request.ServerVariables("APPL_PHYSICAL_PATH")
      
      'Open FTV file
       ftobj.FTExecuteFormula "File('open';'" & app_path &
"inventory.ftv')"
       
       'Set autosave with no prompting
       ftobj.FTExecuteFormula "File('SetAutoSave';1)"
       
       If (item <> "") And (amount <> "") Then
          'Add new value to view
          ftobj.FTExecuteFormula "View( 'AddValue',
'inventory_totals', '" & item & "', " & amount & " )"
       End If
       
       'Calculate View
      ftobj.FTExecuteFormula "View( 'Calculate',
'inventory_totals' )"
      
      'Draw graph
      ftobj.FTExecuteFormula "Graph( 'Calculate',
'inventory_totals', 'Inventory' )"
      ftobj.FTExecuteFormula "View( 'Paint', 'inventory_totals' )"
      
      'Write graph to jpeg file
      ftobj.FTExecuteFormula "Graph( 'Export',
'inventory_totals', 'Inventory', '&thisAppPath&inventory.jpg', 'JPEG'
)"
      ftobj.FTExecuteFormula "File('new')"

      Set ftobj = Nothing
   End Sub
   
   Sub DiscardData
      Dim ftobj

      Set ftobj = Server.CreateObject("Forest.Trees.Application")

      app_path = Request.ServerVariables("APPL_PHYSICAL_PATH")
      
      'Open FTV file
       ftobj.FTExecuteFormula "File('open';'" & app_path &
"inventory.ftv')"
       
       'Set autosave with no prompting
       ftobj.FTExecuteFormula "File('SetAutoSave';1)"
      
      'Clear all data from the view
      ftobj.FTExecuteFormula "View('discarddata',
'inventory_totals' )"
      
      'Calculate View
      ftobj.FTExecuteFormula "View( 'Calculate',
'inventory_totals' )"
      
      'Draw graph
      ftobj.FTExecuteFormula "Graph( 'Calculate',
'inventory_totals', 'Inventory' )"
      ftobj.FTExecuteFormula "View( 'Paint', 'inventory_totals'
)"
      
      'Write graph to jpeg file
      ftobj.FTExecuteFormula "Graph( 'Export',
'inventory_totals', 'Inventory', '&thisAppPath&inventory.jpg', 'JPEG'
)"
      ftobj.FTExecuteFormula "File('new')"
      
      Set ftobj = Nothing
   End Sub
%>

<h3>Example using ASP to return a graph from F&T OLE Automation
server</h3>
Enter item name, then the amount, click enter to update values in the
FTV and redisplay the graph.

<form method="POST" action="inventory.asp">
<p>
Item name <input type="text" name="item" size="20">
Amount <input type="text" name="amount" size="10">
 <input type="submit" value="Enter">
</p>
</form>

<%
   If Request("cleardata") = 1 Then
      DiscardData
   Else   
      RecalcInventory Request.Form("item"),
Request.Form("amount")
   End If
   
   Response.Write "<img border='0' src='inventory.jpg'>"
%>

<form method="POST" action="inventory.asp?cleardata=1">
<p>
<input type="submit" value="Clear data">
</p>
</form>

</body>
</html>