In this tutorial we will create a simple user defined report.
The tutorial covers these tasks.
- Selecting parts and sheets
- Creating a DataSet
- Running a report and creating a report template
Start by creating an IGEMS API project
Register the APP command
We start by registering the APP command
public static void DLLMain()
{
API.RegisterCommand("SimpleReportTutorial", "Simple report tutorial", null, SimpleReportTutorial);
}
private static void SimpleReportTutorial()
{
}
Selecting parts and sheets
In this tutorial we will select a list of parts and a list of sheets to be used in our report.
To select parts and sheets we can use the API.Select function with a filter. Or we can use the special API.SelectParts and API.SelectSheets.
private static void SimpleReportTutorial()
{
var parts=API.SelectParts("Select parts");
var sheets=API.SelectSheets("Select sheets");
}
This will let the user first select parts and then sheets.
Creating the DataSet
Next we will create a DataSet that we need to run the report. A DataSet is like a database with table, columns and rows. We create two tables Parts and Sheets. These tables will later be available in the report.
DataSet dataSet = new DataSet("SimpleReportTutorial");
var tableParts = dataSet.Tables.Add("Parts");
tableParts.Columns.Add("Name", typeof(string));
tableParts.Columns.Add("Quantity", typeof(int));
tableParts.Columns.Add("Image", typeof(Image));
var tableSheets = dataSet.Tables.Add("Sheets");
tableSheets.Columns.Add("Name", typeof(string));
tableSheets.Columns.Add("Image", typeof(Image));
First we create the DataSet.
We create a table called Parts with three columns Name, Quantity and Image. You need to tell the column what type it is as well.
We create a table called Sheets with two columns Name and Image.
Adding data to the DataSet
The next step is to add data to our DataSet. We do this by adding rows to the tables.
We use the API.GetPartInfo and API.GetSheetInfo to get info about each part and sheet.
We also use the API.PartImage and API.Image functions to generate the images.
foreach(var part in parts)
{
var partInfo=API.GetPartInfo(part);
var r=tableParts.Rows.Add();
r["Name"] = partInfo.Name;
r["Quantity"] = partInfo.Quantity;
r["Image"] = API.PartImage(part, 600);
}
foreach (var sheet in sheets)
{
var sheetInfo = API.GetSheetInfo(sheet);
var r = tableSheets.Rows.Add();
r["Name"] = sheetInfo.Name;
r["Image"] = API.Image(sheet, 600,0,Color.White);
}
Running the report
Now the only thing we need to do is call the API.Report function to generate the report.
API.Report("c:/temp/simplereporttutorial.tpl", dataSet);
The API.Report function takes two arguments. The report template file and a dataset. For the template you can create an empty text file and use the path to that file. If the file does not exist the API.Report function will create it. Note! The file will be created but the folders in the path must exist.
Complete code
Here is the complete code. Next step will be to design the report template in the next section. If you compile and run now the report will open with an empty report so we need to design the template as well.
using IGEMSAPI;
using System.Data;
using System.Drawing;
namespace SimpleReportTutorial
{
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("SimpleReportTutorial", "Simple report tutorial", null, SimpleReportTutorial);
}
private static void SimpleReportTutorial()
{
var parts=API.SelectParts("Select parts");
var sheets=API.SelectSheets("Select sheets");
DataSet dataSet = new DataSet("SimpleReportTutorial");
var tableParts = dataSet.Tables.Add("Parts");
tableParts.Columns.Add("Name", typeof(string));
tableParts.Columns.Add("Quantity", typeof(int));
tableParts.Columns.Add("Image", typeof(Image));
var tableSheets = dataSet.Tables.Add("Sheets");
tableSheets.Columns.Add("Name", typeof(string));
tableSheets.Columns.Add("Image", typeof(Image));
foreach(var part in parts)
{
var partInfo=API.GetPartInfo(part);
var r=tableParts.Rows.Add();
r["Name"] = partInfo.Name;
r["Quantity"] = partInfo.Quantity;
r["Image"] = API.PartImage(part, 600);
}
foreach (var sheet in sheets)
{
var sheetInfo = API.GetSheetInfo(sheet);
var r = tableSheets.Rows.Add();
r["Name"] = sheetInfo.Name;
r["Image"] = API.Image(sheet, 600,0,Color.White);
}
API.Report("c:/temp/simplereporttutorial.tpl", dataSet);
}
}
}
Designing the report template
Ok, now we build and run the APP. When running the APP we select some parts and sheets and the report will open with a blank report.
When the report is generated you will press F5 or use the File/Edit template menu option to edit the template.
We will add two tables to list the parts and sheets.
In the tables combo to the right select the Parts table and press Insert table.
Press the Table button and select 3 columns.
Now in the header of the table we type Name, Quantity and Image
Then place the cursor in the first cell on the second row and double click on the Name in the Parts table to insert the Name tag. Then continue to add the Quantity tag and the Image tag. It should look like this.
Next change to the Sheets table and add a new table with two columns. Type in Name and Image in the header and insert the Name and Image tag from the Sheets table.
We can resize the Sheet image a little and center it in the cell.
Now press F5 to test your template.
Close the report and Save the template.