This wrapper looks fairly basic (but where is the %module statement?), so you should be able to use most of it in C# with no changes. Luckily, I don’t see anything that is python-specific. Try changing SWIG’s command line argument to use -csharp instead of -python.


One problem I foresee is that it is using std::set, which appears to be supported in Python but not C#. Perhaps to get around this difficulty, you could copy Lib\python\std_set.i to Lib\csharp\std_set.i and then make changes so that the interface is a little more C#-friendly. Even with the original std_set.i, you might get a compilable C# wrapper.


From: Jim Mace []
Sent: Wednesday, April 28, 2010 2:03 PM
To: Tyler Sample
Subject: Re: [Swig-user] C# and C++


The /api/c++/include/farsite folder contains the following in the *.i (farsite.i)






#include "farsite/farsiteAPI.hpp"

#include "farsite/landscape.hpp"

#include "farsite/ProjectWriter.hpp"



/* Interface to STL */

%include "std_string.i"

%include "std_vector.i"

%include "std_pair.i"

%include "std_set.i"



/* Collect all the definitions in the farsite::data namespace. */

%import "farsite/data/farsiteData.i"



/* These things should generate code in the target scripting language. */

%include "farsite/config.hpp"

%include "farsite/control.hpp"

%include "farsite/landscape.hpp"

%include "farsite/ProjectWriter.hpp"


%template(WindFileVector) std::vector<farsite::FarsiteConfig::WindFile> ;

%template(WeatherFileVector) std::vector<farsite::FarsiteConfig::WeatherFile> ;

%template(BarrrierFileVector) std::vector<farsite::data::BarrierFile *> ;


namespace farsite {






The /api/c++/include/farsite/data folder contains the following in the *.i (farsiteData.)





#include "farsite/data/data.hpp"



/* Interface to STL */

%include <std_string.i>

%include <std_vector.i>

%include <std_pair.i>

%include <std_set.i>


/* Farsite Data API include files... */


 * NOTE: The ORDER is important, as SWIG does not include files included by these

 * files.  Hence, you must make sure that things are defined before they are used

 * by the order in which you include them.


%include "farsite/data/dataset.hpp"

%include "farsite/data/fileadapter.hpp"

%include "farsite/data/units.hpp"

%include "farsite/data/adjustment.hpp"

%include "farsite/data/arc.hpp"

%include "farsite/data/barriers.hpp"

%include "farsite/data/burnperiod.hpp"

%include "farsite/data/coarsewoody.hpp"

%include "farsite/data/customfuel.hpp"

%include "farsite/data/durationopts.hpp"

%include "farsite/data/firebehavioropts.hpp"

%include "farsite/data/fuelconversion.hpp"

%include "farsite/data/fuelmoisture.hpp"

%include "farsite/data/ignition.hpp"

%include "farsite/data/modelparams.hpp"

%include "farsite/data/outputopts.hpp"

%include "farsite/data/postfrontalopts.hpp"

%include "farsite/data/weather.hpp"

%include "farsite/data/wind.hpp"



/* Instantiate sets of various objects...  */

/* These need to be outside the namespace declarations because SWIG doesn't

 * handle the mixing of namespaces too good.


%template(AdjustmentSet) std::set<farsite::data::Adjustment> ;

%template(BurnPeriodSet) std::set<farsite::data::BurnPeriod> ;

%template(CoarseWoodySet) std::set<farsite::data::CoarseWoodyProfile> ;

%template(UnitlessCoarseWoodySet) std::set<farsite::data::DataSet<farsite::data::CoarseWoodyProfile, farsite::data::UnitSystem> > ;

%template(CustomFuelSet) std::set<farsite::data::CustomFuel> ;

%template(ConversionSet) std::set<farsite::data::FuelConversion> ;

%template(FuelMoistureSet) std::set<farsite::data::FuelMoisture> ;

%template(WeatherSet) std::set<farsite::data::Weather> ;

%template(WindSet) std::set<farsite::data::Wind> ;



namespace farsite { namespace data {


/* Ignore various boost definitions... */

%ignore ArcVectorFile::setFileName(const boost::filesystem::path &);

//%ignore BarrierFile::getFileName() ;

//%ignore BarrierFile::getFileName() const ;

//%ignore BarrierFile::setFileName(const boost::filesystem::path &) ;



/* Stuff for units.hpp */

%template(UnitSystem) NamedEnumVar<UnitNames> ;

%template(UnitSystemEnumFactory) NamedEnumVarFactory<UnitNames> ;

%ignore NamedEnumVar::setValue(char *) ;


/* Stuff for customfuel.hpp */

%template(FuelModelType) NamedEnumVar<FuelModelTypes> ;

%ignore FuelModelTypeFactory ;



/* Instantiate various data types for use with an ArcVectorFile */

//%template(SequenceList) std::vector<ArcVectorFile::PointSequence> ;

//%template(PointSequence) std::vector<ArcVectorFile::Point> ;

//%template(Point) std::pair<double, double> ;



/* Instantiate datasets of various objects */

%template(AdjustmentDataset) DataSet<Adjustment>;

%template(BurnPeriodDataset) DataSet<BurnPeriod>;

%template(UnitlessCoarseWoody) DataSet<CoarseWoodyProfile, CoarseWoodyHeader> ;

%template(CustomFuelDataset) DataSet<CustomFuel, UnitSystem> ;

%template(CoarseWoodyDataset) DataSet<UnitlessCoarseWoody, UnitSystem > ;

%template(ConversionDataset) DataSet<FuelConversion> ;

%template(FuelMoistureDataset) DataSet<FuelMoisture> ;

%template(WeatherDataset) DataSet<Weather, UnitSystem> ;

%template(WindDataset) DataSet<Wind, UnitSystem> ;


/* Instantiate FileAdapters for various objects */

%ignore FileAdapter::setFileName(boost::filesystem::path);

%ignore FileAdapter::setFileName(const char *) ;

%template(AdjustmentFile) FileAdapter<DataSet<Adjustment> >;

%template(BurnPeriodFile) FileAdapter<DataSet<BurnPeriod> >;

%template(CoarseWoodyFile) FileAdapter<DataSet<UnitlessCoarseWoody, UnitSystem> > ;

%template(CustomFuelFile) FileAdapter<DataSet<CustomFuel, UnitSystem> > ;

%template(ConversionFile) FileAdapter<DataSet<FuelConversion> > ;

%template(FuelMoistureFile) FileAdapter<DataSet<FuelMoisture> > ;

%template(WeatherFile) FileAdapter<DataSet<Weather, UnitSystem> > ;

%template(WindFile) FileAdapter<DataSet<Wind, UnitSystem> > ;





Just to give you an idea on how the API is used, below is some of the python file that runs the simulation


from import *

from org.firelab.farsite.api import *

import os.path

import sys


if len(sys.argv) != 2 :

    print 'Usage: ', sys.argv[0], ' <base path>'



base = sys.argv[1]

indir = os.path.join(base, 'input')

print indir

outdir = os.path.join(base, 'output')


cfg = FarsiteConfig()


# Required input files

cfg.setFuelMoistureFname(os.path.join(indir, 'ashley.fms'))

cfg.addWeatherFname(os.path.join(indir, 'ashley.wtr'))

cfg.addWindFname(os.path.join(indir, 'ashley.wnd'))

cfg.setLandscapeFname(os.path.join(indir, 'ashley.lcp'))


# Optional Input Files

cfg.setCustomFuelFname(os.path.join(indir, 'ashley.fmd'))

cfg.setAdjustmentFname(os.path.join(indir, 'ashley.adj'))


# merge changes to the adjustment data into the adjustment file

cfg.getAdjustmentFile().load() ;

adj = Adjustment(19, 0.85)




# generate burn period information

bpd = BurnPeriodDataset()






cfg.setBurnPeriodFname(os.path.join(indir, 'ashley.bpd'))


# load data from named files



# start the simulation

ctl = FarsiteControl.getInstance(base)


ctl.saveSettings(os.path.join(base, 'Settings.txt'))


timestep = 0

state = ctl.simulateNext()

print "Completed timestep: ", timestep

while ((state == FarsiteControl.SUSPENDED) or (state == FarsiteControl.RUNNING)) :

    state = ctl.simulateNext()

    timestep += 1

    print "Completed timestep: ", timestep




I could always learn to program in Python and be done with it, but I have so much invested in C#, personally and professionally. Plus, everything else for this application is written in C#.


Thanks for your help,



From: Tyler Sample []
Sent: Tuesday, April 27, 2010 12:40 PM
To: Jim Mace
Subject: Re: [Swig-user] C# and C++


Find the .i file.  If you can't send it to the mailing list, I can at least tell you if you'll need to full-on learn SWIG or if you can just get by picking up a couple key features.


From: Jim Mace

Sent: Tuesday, April 27, 2010 10:25 AM

To: David Piepgrass


Subject: Re: [Swig-user] C# and C++


I'm trying to track down the URL to the application's source code. The mailing list won't let me attach the 6 MB zip file.


From: David Piepgrass []
Sent: Friday, April 23, 2010 3:40 PM
To: Jim Mace
Subject: RE: C# and C++


Oooh, that’s a toughie. How long is the .i file? If it is short, or if it doesn’t use %typemaps and other SWIG features that involve python code, then the conversion might not be hard. But in the general case, converting SWIG code to another target language may be challenging, as is learning to use SWIG.


But you may be lucky. In simple cases, SWIG can switch to another target language with almost no changes.


From: Jim Mace []
Sent: Friday, April 23, 2010 2:24 PM
Subject: [Swig-user] C# and C++


User group,


I am somewhat lost. I am a C# programmer that mostly programs in for the Windows platform, but I am moving over to Linux. I have an application that was written by another developer and C++. He has developed an API and used SWIG to create wrappers for Python. How would I go about making wrappers for C#? I'm just not understanding how this works. If someone can point me in the right direction I would be very grateful.




Swig-user mailing list