In this tutorial we will create a dialog box to list and preview parts.
The tutorial covers these tasks.
- Selecting parts with a filter
- Getting the name of a part
- Create a part image
Start by creating an IGEMS API project
Adding a Form
We start by adding a form to our project to display the selected parts.
Right click on the project and select Add/Form (Windows Forms)...
Now in the form add a ListBox and a PictureBox. For the PictureBox we set the BackColor to White and the SizeMode to CenterImage.
Code
We start by registering the APP command.
public static void DLLMain()
{
API.RegisterCommand("PartViewerTutorial", "Part viewer", null, PartViewerTutorial);
}
We then add the PartViewerTutorial method and implement it.
public static void DLLMain()
{
API.RegisterCommand("PartViewerTutorial", "Part viewer", null, PartViewerTutorial);
}
public static void PartViewerTutorial()
{
var ents = API.Select("Select parts", (ent) => { return API.EntityType(ent) == "PART"; });
if (ents.Count <= 0)
return;
using (Form1 frm = new Form1(ents))
{
frm.ShowDialog();
}
}
Ok, in this function we call the API.Select function with a filter to only allow Parts to be selected.
var ents = API.Select("Select parts", (ent) => { return API.EntityType(ent) == "PART"; });
We make an error check if nothing is selected. Just return in that case.
if (ents.Count <= 0)
return;
Next we instantiate the Form and add the selected parts as an argument.
using (Form1 frm = new Form1(ents))
{
frm.ShowDialog();
}
Next we modify the Form constructor like this and we also add a List<object> parts to the class.
List<object> parts;
public Form1(List<object> entities)
{
InitializeComponent();
this.parts = entities;
}
Now we need to add the Load event for the Form and the SelectedIndexChanged event for the ListBox.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var prt in parts)
listBox1.Items.Add(API.GetVar(prt, "NAME").ToString());
listBox1.SelectedIndex = 0;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object part = parts[listBox1.SelectedIndex];
pictureBox1.Image = API.PartImage(part, Math.Min(pictureBox1.Width, pictureBox1.Height));
}
In the Load event we loop the selected parts and add the Name of the part to the listbox. Here we use the API.GetVar function to get the name of the part.
After adding the parts to the listbox we set the selectedindex=0. This will trigger an SelectedIndexChanged event.
In the SelectedIndexChanged event we get the selected part and set the picturebox image by calling the API.PartImage function.
Complete code
using IGEMSAPI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PartViewerTutorial
{
public partial class Form1 : Form
{
List<object> parts;
public Form1(List<object> entities)
{
InitializeComponent();
this.parts = entities;
}
public static void DLLMain()
{
API.RegisterCommand("PartViewerTutorial", "Part viewer", null, PartViewerTutorial);
}
public static void PartViewerTutorial()
{
var ents = API.Select("Select parts", (ent) => { return API.EntityType(ent) == "PART"; });
if (ents.Count <= 0)
return;
using (Form1 frm = new Form1(ents))
{
frm.ShowDialog();
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var prt in parts)
listBox1.Items.Add(API.GetVar(prt, "NAME").ToString());
listBox1.SelectedIndex = 0;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object part = parts[listBox1.SelectedIndex];
pictureBox1.Image = API.PartImage(part, Math.Min(pictureBox1.Width, pictureBox1.Height));
}
}
}