Example:
The following example code extract shows - in principle - how to use/access the ASAP2 parser within a .NET environment working with C#.MIAsap2Parser parser = new MIAsap2Parser();
try
{
parser.OnError += new ErrorDelegate(OnError); // handle error messages
parser.OnProgress += new ProgressDelegate(OnProgress); // handle progress
MIDataAccess da = parser.ParseFile("c:\\temp\\myASAP2File.a2l");
if (da == null)
{
// The parsing of the ASAP2 file failed!
MessageBox.Show("ImpA2 Error: Parsing of ASAP2 file failed!");
return false;
}
// PROJECT
MIAsap2Element elemProject = da.RootBlock.GetElementByKey(da.PROJECT);
if (elemProject != null && elemProject.IsExisting())
{
MIAsap2Block blockProject = elemProject.GetEmbeddedBlock(0);_HandleProject(blockProject); // process, the PROJECT element -> see below
}
else
{
MessageBox.Show("Error: Can't access 'PROJECT' block");
}
}
catch (System.Runtime.InteropServices.COMException e)
{
switch ((HResultCodes)e.ErrorCode)
{
case HResultCodes.InternalError:
sErr += "A fatal error occured -> " + e.ErrorCode;
break;
case HResultCodes.Cancelled:...etc
}
}
private void _HandleProject(MIAsap2Block blockProject)
{
// process the PROJECT attributes
... etc// MODULE
MIAsap2Element elemModule = blockProject.GetElementByKey(da.MODULE);
if (elemModule != null && elemModule.IsExisting())
{
// get the first ASAP2 "MODULE"
MIAsap2Block blockModule = elemModule.GetEmbeddedBlock(0);// {-> MEASUREMENT}*
MIAsap2Element elemMeasurement = blockModule.GetElementByKey(da.MEASUREMENT);
uint iMeasurementCount = elemMeasurement.GetCount();
// loop over all ASAP2 "MEASUREMENT"'s
for(uint i = 0; i < iMeasurementCount; i++)
{
// get MEASUREMENT at index 'i'
MIAsap2Block blockMeasurement = elemMeasurement.GetEmbeddedBlock(i);// (1) name of MEASUREMENT
MIAsap2Element elem = blockMeasurement.GetElementByKey(da.Name);
string sNameOfMeasurement = elem.GetIdent(0).GetString()// (2) LongIdentifier
string sLongName = "";
if (_GetStringElement(blockMeasurement, da.LongIdentifier, ref sLongName))
{
// ...
}// (3) conversion
string sNameOfConversion = "";
if (_GetIdentElement(blockMeasurement, da.Conversion, ref sNameOfConversion))
{
// ...
}// (4) LowerLimit
double dLowerLimit = 0;
if (_GetRealElement(blockMeasurement, da.LowerLimit, ref dLowerLimit))
{
// ...
}... etc.
}
}
}
(*) The methods '_GetStringElement()', '_GetIdentElement()', etc. are simple helper methods accessing the attributes.
The ASAP2 Parser package contains the documentation of the COM/.NET API
and the test tool 'A2PrettyPrint'.
If requested, you can also get a sample code which shows how to access
the ASAP2 Parser.
By arrangement, Visu-IT! will also do some consulting on how to use the parser.
Please contact Visu-IT! to get more information about the ASAP2 Parser.