In this tutorial we will open a DXF file and create a part with a toolpath.
The tutorial covers these tasks.
- Loading a material and a machine
- Opening a DXF or DWG file
- Converting the loaded drawing to a part
- Adding a toolpath to the part
Start by creating an IGEMS API project.
Register the APP
In your DLL register an APP using this code.
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("ImportDXFTutorial", "Import DXF", null, ImportDXF);
}
private static void ImportDXF()
{
}
Loading a machine and material
We start by loading a machine and material from code. You can omit this and IGEMS will prompt you to select a machine and material if none is selected in the current drawing. You might want to check the return value of these functions to check if the machine and material exists and could be loaded correctly.
private static void ImportDXF()
{
API.LoadMaterial("Aluminium", "Standard", 5);
API.LoadMachine("AWJ");
}
Selecting the DXF or DWG file
Next step is to select the file we like to open. In this tutorial we use the OpenFile dialog. If no file was selected we just return.
private static void ImportDXF()
{
API.LoadMaterial("Aluminium", "Standard", 5);
API.LoadMachine("AWJ");
OpenFileDialog f = new OpenFileDialog();
f.Filter = "*.dxf;*.dwg|*.dxf;*.dwg";
if (f.ShowDialog() != DialogResult.OK)
return;
}
Opening the file
We have now selected a file to open. We insert the file into the current drawing.
After inserting the file we need to collect all the inserted entities and convert them to a Region2d object.
The Region2d is a Genamo class for geometry that we need when we create the part later.
After converting the inserted entities to a Region2d we delete them from the drawing.
private static void ImportDXF()
{
API.LoadMaterial("Aluminium", "Standard", 5);
API.LoadMachine("AWJ");
OpenFileDialog f = new OpenFileDialog();
f.Filter = "*.dxf;*.dwg|*.dxf;*.dwg";
if (f.ShowDialog() != DialogResult.OK)
return;
var ents = API.InsertDrawing(f.FileName);
Region2d r = new Region2d();
foreach (var n in ents)
{
r.Add(API.ToRegion(n));
API.Delete(n);
}
}
Creating the part (or parts)
We now have all the geometry from the inserted file and we can create the part.
If the inserted file contains multiple outer geometries then multiple parts will be created.
We start by generating a part name using the name of the selected file and we set quantity=1.
The API.CreatePart function will return a list of part objects.
private static void ImportDXF()
{
API.LoadMaterial("Aluminium", "Standard", 5);
API.LoadMachine("AWJ");
OpenFileDialog f = new OpenFileDialog();
f.Filter = "*.dxf;*.dwg|*.dxf;*.dwg";
if (f.ShowDialog() != DialogResult.OK)
return;
var ents = API.InsertDrawing(f.FileName);
Region2d r = new Region2d();
foreach (var n in ents)
{
r.Add(API.ToRegion(n));
API.Delete(n);
}
string partname = Path.GetFileNameWithoutExtension(f.FileName);
int quantity = 1;
var parts = API.CreatePart(r, partname, quantity, "", DateTime.Now);
}
Adding toolpaths
The final step of this tutorial is to add toolpaths to the parts we created. We just loop the parts and add toolpaths to them. Then finally we do a ZoomExtents to make sure we see the full drawing.
This concludes this tutorial.
Full code.
using IGEMSAPI;
using IGS.Genamo;
using System;
using System.IO;
using System.Windows.Forms;
namespace ImportDXFTutorial
{
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("ImportDXFTutorial", "Import DXF", null, ImportDXF);
}
private static void ImportDXF()
{
API.LoadMaterial("Aluminium", "Standard", 5);
API.LoadMachine("AWJ");
OpenFileDialog f = new OpenFileDialog();
f.Filter = "*.dxf;*.dwg|*.dxf;*.dwg";
if (f.ShowDialog() != DialogResult.OK)
return;
var ents = API.InsertDrawing(f.FileName);
Region2d r = new Region2d();
foreach (var n in ents)
{
r.Add(API.ToRegion(n));
API.Delete(n);
}
string partname = Path.GetFileNameWithoutExtension(f.FileName);
int quantity = 1;
var parts = API.CreatePart(r, partname, quantity, "", DateTime.Now);
foreach (var part in parts)
API.CreateToolpath(part);
API.ZoomExtents();
}
}
}