Table Of Contents

Previous topic

Perl

Next topic

Ruby

This Page

CSharp and OBDotNet

OBDotNet is a compiled assembly that allows OpenBabel 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.3.

Installation

C# and Visual Basic

The following instructions describe how to use OBDotNet in a C# project using Microsoft Visual C# 2008 Express Edition. You should be able to apply these instructions to more recent compilers, and also to Visual Basic projects.

  1. First you need to download and install the OpenBabelGUI version 2.2.3
  2. Download and extract OBDotNet-0.3.zip
  3. Start Microsoft Visual C# Express Edition and create a new new project, for example a console application
  4. Replace the code in Program.cs by this example program and save the project somewhere
  5. Copy and paste the entire contents of the extracted OBDotNet-0.3.zip folder into the project directory in bin/Debug
  6. Right click on References in the right hand pane and choose the Browse Tab to add a reference to OBDotNet.dll in the bin/Debug directory.
  7. If you build the project now, it should work.
  8. Uncomment the final line, and run it under Debug/Start Debugging. You should see the output of 44.0952, the molecular weight of propane.
  9. To run the program at the command line, you just type the full path to the exe located in the Debug folder.

Running under Mono

For Linux or MacOSX follow the instructions in the README file in scripts/csharp.

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

OpenBabel 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()