Table Of Contents

Previous topic

Perl

Next topic

Ruby

This Page

CSharp and OBDotNet

OBDotNet is a compiled assembly that allows Open Babel to be used from the various .NET languages (e.g. Visual Basic, C#, IronPython, IronRuby, and J#) on Windows, Linux and MacOSX. The current version is OBDotNet 0.4.

Installation

Windows

The OBDotNet.dll assembly provided on Windows was compiled using the .NET framework v3.5 for the x86 platform. To use it, you will need to compile your code using .NET v3.5 or newer and you will also need to target x86 (/platform:x86).

The following instructions describe how to compile a simple C# program that uses OBDotNet:

  1. First you need to download and install the OpenBabelGUI version 2.3.1

  2. Next create an example CSharp program that uses the Open Babel API (see below for one or use this link). Let’s call this example.cs.

  3. Copy OBDotNet.dll from the Open Babel installation into the same folder as example.cs.

  4. Open a command prompt at the location of example.cs and compile it as follows:

    C:\Work> C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
                               /reference:OBDotNet.dll /platform:x86 example.cs
  5. Run the created executable, example.exe, to discover the molecule weight of propane:

    C:\Work> example.exe
    44.09562

If you prefer to use the MSVC# GUI, note that the Express edition does not have the option to choose x86 as a target. This will be a problem if you are using a 64-bit operating system. There’s some information at Coffee Driven Development on how to get around this.

MacOSX and Linux

On Linux and MacOSX you need to use Mono, the open source implementation of the .NET framework, to compile the bindings. The following instructions describe how to compile and use these bindings:

  1. OBDotNet.dll is included in the Open Babel source distribution in scripts/csharp. To compile a CSharp application that uses this (e.g. the example program shown below), use a command similar to the following:

    gmcs example.cs /reference:../openbabel-2.3.1/scripts/csharp/OBDotNet.dll
  2. To run this on MacOSX or Linux you need to compile the CSharp bindings as described in the section Compile language bindings. This creates lib/libopenbabel_csharp.so in the build directory.

  3. Add the location of OBDotNet.dll to the environment variable MONO_PATH. Add the location of libopenbabel_csharp.so to the environment variable LD_LIBRARY_PATH. Additionally, if you have not installed Open Babel globally you should set BABEL_LIBDIR to the location of the Open Babel library and BABEL_DATADIR to the data directory.

  4. Run example.exe:

    $ ./example.exe
    44.09562

OBDotNet API

The API is almost identical to the Open Babel C++ API. Differences are described here.

Using iterators

In OBDotNet, iterators are provided as methods of the relevant class. The full list is as follows:

Such iterators are used as follows:

foreach (OBAtom atom in myobmol.Atoms())
    System.Console.WriteLine(atom.GetAtomType());

Other iterators in the C++ API not listed above can still be used through their IEnumerator methods.

Handling OBGenericData

To cast OBGenericData to a specific subclass, you should use the .Downcast <T> method, where T is a subclass of OBGenericData.

Open Babel Constants

Open Babel constants are available in the class openbabelcsharp.

Examples

The following sections show how the same example application would be programmed in C#, Visual Basic and IronPython. The programs print out the molecular weight of propane (represented by the SMILES string “CCC”).

C#

using System;
using OpenBabel;

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            OBConversion obconv = new OBConversion();
            obconv.SetInFormat("smi");
            OBMol mol = new OBMol();
            obconv.ReadString(mol, "CCC");
            System.Console.WriteLine(mol.GetMolWt());
        }
    }
}

Visual Basic

Imports OpenBabel

Module Module1

    Sub Main()
        Dim OBConv As New OBConversion()
        Dim Mol As New OBMol()

        OBConv.SetInFormat("smi")
        OBConv.ReadString(Mol, "CCC")
        System.Console.Write("The molecular weight of propane is " & Mol.GetMolWt())
    End Sub

End Module

IronPython

import clr
clr.AddReference("OBDotNet.dll")

import OpenBabel as ob

conv = ob.OBConversion()
conv.SetInFormat("smi")
mol = ob.OBMol()
conv.ReadString(mol, "CCC")
print mol.GetMolWt()