DADiSP as an Automation Client

 

So far, we've shown examples of controlling DADiSP from Visual Basic and C++. In these case, Visual Basic or C++ acts as the "client" and DADiSP acts as the ActiveX "server". However, DADiSP can also function as an ActiveX client to control and make use of the features of ActiveX servers such as Excel, Word, database programs or data acquisition drivers.

 

The DADiSP SPL syntax for ActiveX is very similar to the Visual Basic syntax mentioned above. For example, to connect to an ActiveX object, you use the CreateObject function. Properties can be set and retrieved with standard variable assignments and methods executed with the "dot" syntax. For example, the following SPL code fragment connects to Word and makes it visible:

 

/* start and display Word in SPL using ActiveX */

ShowWord()

{

    local word;

 

    // start word

    word = CreateObject("Word.Application");

 

    // show it!

    word.visible = 1;

}

 

 

DADiSP can access any function exposed by the ActiveX Automation server. For example, Word has a very elaborate ActiveX hierarchy, consisting of Documents, Ranges, Tables and a host of other objects all accessible to DADiSP.

The full SPL example below demonstrates the use of these objects to create and copy a DADiSP Worksheet to a Word document.

 

/* demonstrates ActiveX calls to MS Word */

msword2()

{

    local word, doc;

    

    /* new 4 Window worksheet */

    if (newworksheet(4) == 0) return;

 

    /*

     *  generate noisy sine in W1, set units to Volts and

     *  add a nice label

     */

 

    setwf(w1, 'gnorm(1000, .01) * 0.1 + gsin(1000, .01);setvunits("V");

    label("Noisy Sinewave")');

 

    /* turn on axes labels */

    scales(w1, 2);

 

    /*

     *  single pole filter in W2, set color to LRED, set plot ranges

     *  to the same ranges as W1 and add a label

     */

 

    setwf(w2, 'Slp(W1, 1);Label("Single Pole Filter Output");

    sercolor(lred);sety(getyb(w1), getyt(w1)); setytic(getytic(w1))');

 

    /* turn on axes labels */

    scales(w2, 2);

 

    /* Now calculate corresponding spectra in W3 and W4 */

    plotmode(w3, 0); /* shut off plotting */

    plotmode(w4, 0);

 

    /*

     *  W3:

     *  Calculate spectrum of W1, set to log axes, turn on solid grids,

     *  set plotting range from 0.001 to 10 and tic every decade

     */

 

    setwf(w3, 'Spectrum(w1, 4096)');

    setxlog(w3, 1);

    setylog(w3, 1);

    setgridstyle(w3, 1, 1);

    setgridstyle(w3, 2, 1);

    sety(w3, 0.001, 10.0);

    setytic(w3, 1);

    scales(w3, 2);

 

    /*

     *  W4:

     *  Calculate spectrum of W2, set to log axes, turn on solid grids,

     *  set plotting range from 0.001 to 10 and tic every decade

     */

 

    setwf(w4, 'Spectrum(w2, 4096)');

    setxlog(w4, 1);

    setylog(w4, 1);

    setgridstyle(w4, 1, 1);

    setgridstyle(w4, 2, 1);

    sety(w4, 0.001, 10.0);

    setytic(w4, 1);

    scales(w4, 2);

 

    /* turn on plots */

    plotmode(w3, 1);

    plotmode(w4, 1);

 

    /* copy worksheet into clipboard */

    copyworksheet();

    

    echo("Starting Word...");

 

    // start Word, equivalent to Visual Basic's:

    // Dim word As New Word.Application

    word = CreateObject("Word.Application");

    

    /* remove messages */

    echo("");

 

    // get "Document" object, in VB:

    // Dim doc As word.document

    doc  = word.Documents;

 

    // Add a new Document and get range object

    range = doc.Add().Range();

    

    /* paste worksheet as Enhanced Metafile */

    word.Selection.PasteSpecial(0, 0, 0, 0, 9);

 

    // let's see it!

    word.Visible = 1;

 

    /*

     *  Note: ActiveX objects are automatically released when the

     *        local (or global) variables are deleted

     */

 

    // return the handle so we can assign it (if desired)

    if (outargc > 0)

    {

        return(word);

    }

}

 

 

Here’s an SPL example that creates a pie chart in Excel:

 

#define XL3DPIE -4102

#define XLROWS  1

 

/* tests Automation with Excel */

excelpie(usecur)

{

    local xl = -1, Books, Book, Sheet, Range, Chart;

    

    if (argc < 1)

    {

        usecur = 0;

    }

    if (usecur)

    {

        // try connecting to running Excel - don't show errors

        xl = getobject("Excel.Application", 0);

    }

    if (not(isobject(xl)))

    {

        // start new Excel

        xl = CreateObject("Excel.Application");

    }

 

    if (not(isobject(xl))) error("excelpie - cannot start Excel");

 

    // make it visible

    xl.Visible = 1;

 

    // create Workbook and select sheet

    Books = xl.Workbooks;

    Book  = Books.Add();

    Sheet = xl.ActiveSheet;

    Sheet.Name = "Market Share";

 

    // create data

    Sheet.Range("A2").Value = "DSP Development Corporation";

    Sheet.Range("B2").Value = "Microsoft";

    Sheet.Range("C2").Value = "IBM";

    Sheet.Range("D2").Value = "Borland";

    Sheet.Range("A3").Value = 75.0;

    Sheet.Range("B3").Value = 14.0;

    Sheet.Range("C3").Value = 7.0;

    Sheet.Range("D3").Value = 4.0;

    Range = Sheet.Range("A2:D3");

    Chart = Book.Charts.Add();

 

    // use chart wizard to produce the chart

    Chart.ChartWizard(Range, XL3DPIE, 7, XLROWS,

    1, 0, 2, "Engineering Spreadsheet Market Share");

 

    // return handle if requested

    if (outargc > 0)

    {

        return(xl);

    }

}

 

 

The GetObject function is similar to CreateObject, except GetObject attempts to connect to a running instance of the program. IsObject tests whether the input parameter is a valid ActiveX object. If a valid object is not obtained via GetObject, then new instance of Excel is attempted.

 

See the \spl\activex release directory for more examples of ActiveX Automation with DADiSP.