PowerMill API Documentation
PowerMill API Documentation
Examples............................................................................................................16
The power of the API can be harnessed from within the Microsoft Visual Studio
programming environment and used for standalone executables. The programmer can
choose to develop in either C# or VB.net.
The developer is able to leverage all of the power provided by the VB.net and C#
programming languages.
Notes have been written in both C# and VB.net and have been colour coded as
follows:
C#
VB.net
You will need a licensed copy of Microsoft Visual Studio 2010 or later to make
use of PowerShape and PowerMill API
https://github.com/Autodesk/PowerShapeAndPowerMillAPI
You can get the lastest alpha, beta or release version on NuGet here:
https://www.nuget.org/profiles/Autodesk
Creating a Project
1 Open up Microsoft Visual Studio and start a new project.
2 Select ‘Visual C#’ or ‘Visual Basic’ (based on your preference) from the options on
the left, then select ‘Console Application’ (Error: Reference source not found).
3 Give the project a name and a save location (Error: Reference source not found).
4 Your main project source file will contain the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
Module Module1
Sub Main()
End Sub
End Module
Autodesk.Utilities
Autodesk.Geometry
Autodesk.ProductInterfac
e
Autodesk.ProductInterfac Autodesk.ProductInterfac
e.PowerShape e.PowerMill
6 To add the required references there are now 2 options. The preferred method is to
use NuGet. If you are using an older version that came with an installer then go to
step 9 to 11.
7 In your project, right click on “References” and choose “Manage NuGet Packages…”
8 Find available packages at nuget.org by searching for “Autodesk”. You will find
(amongst others) the following packages:
You will have to choose show prerelease versions to add alpha or beta
versions.
9 If using an installed verison of the API, right click on ‘References’ within your
solution in the Solution Explorer on the left and then select ‘Add’ then ‘Reference’
(Error: Reference source not found).
10 From within the window that now appears, select ‘Assemblies’ and then ‘Extensions’
from the list.
Autodesk.Geometry
Autodesk.ProductInterface
Autodesk.ProductInterface.PowerMill
Autodesk.Utilities
12 With the references added (manuall or via NuGet), insert the following using
directive at the top of your source file:
using Autodesk.ProductInterface.PowerMill;
Imports Autodesk.ProductInterface.PowerMill
Connecting to PowerMill
The following section demonstrates how the API can be used to communicate with a
running instance of PowerMill.
InstanceReuse.UseExistingInstance)
While typing this line IntelliSense gives a number of options. For example,
after InstanceReuse we have options to: create a new instance (in addition to
any existing instance of PowerMill); Create a single instance (close any
PowerMill instances already open and create a new one) or
UseExistingInstance (attach to a running instance of PowerMill) see Error:
Reference source not found.
If you continue the line by typing a ‘.’ after your selection of InstanceReuse,
you are presented with an additional option to display or not the PowerMill
GUI.
13 Now attach to the PowerMill active project by adding the following code to the main
method of your source file:
14 The PMProject object (session) can now be used to perform a number of operations
on PowerMill (Figure 5).
15 At this point it may be desirable to disable PowerMill dialogs so that code is free to
run without user prompts.
powerMill.DialogsOff();
powerMill.DialogsOn();
powerMill.DialogsOff()
powerMill.DialogsOn()
Importing/Exporting a Model
To import a solid/surface, create a Autodesk.FileSystem.File object and add this to
the session.
17 Operations can now be performed upon the model. For example, should you wish to
find the maximum size of the model in X, use the bounding box method shown
here:
18 If we wished to select the model (to perform an operation only on selected items,
for example) we can use the code:
myModel.AddToSelection(true);
myModel.AddToSelection(True)
19 This method can also be applied to importing a DMT file as a block to machine
using:
session.CreateBlock(blockFile);
1 Assume there exists within the session a single tool about which you require
information. Firstly you must instantiate a PMTool object within your code and
assign to it the tool about which information is required. This is achieved with the
following code:
You will need to know the index of the tool to create an instance of it or,
alternatively, the name of the tool, which can be used to achieve the same
result thus: session.Tools.get_Item(<name>).
20 We are now able to perform a number of operations on the tool; for example, we
can change the diameter of the tool as follows:
tool.Diameter = 20.0;
tool.Diameter = 20.0
If there exists a pre-calculated toolpath that uses this tool, it will be invalidated
by the above operation and PowerMill will issue a warning to that effect. It is,
therefore, necessary to invalidate all toolpaths before changing the tool. Code
to manage this is presented in the accompanying samples.
21 Alternatively, if you want to know the diameter of the tool, use the following:
You will need to know the index of the toolpath to create an instance of it or,
alternatively, the name of the toolpath, which can be used to achieve the
same result thus: session.Toolpaths.get_Item(<name>).
23 We are now able to perform a number of operations on the toolpath; for example,
we can set the name of the toolpath using:
To check if the toolpath has been calculated and to calculate it if it has not, use the
following:
if (toolpath.IsCalculated == false)
toolpath.Calculate();
toolpath.Calculate()
End If
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.ProductInterface.PowerMill;
namespace PMillWalkthrough
class Program
Imports Autodesk.ProductInterface.PowerMill
Module Module1
Sub Main()
End Sub
End Module
24 Now load a PowerMill project and create a PMProject object with which to
manipulate it.
powerMill.LoadProject(projectDirectory)
The Boundary Example folder will be located in a path relative to that which you
extracted your example files to earlier and may be different to that specified
above.
powerMill.DialogsOff();
powerMill.DialogsOff()
26 Now initialise a the API boundary object with the single boundary available in the
PowerMill project.
You will need to know the index of the boundary to create an instance of it or,
alternatively, the name of the boundary, which can be used to achieve the
same result thus: session.Boundaries.get_Item(<name>).
27 Now import from file the boundary that is to be used. This may be accomplished
using the following code:
myBoundary.InsertFile(boundaryPath);
BoundaryFile.dgk will be located in the path to which you extracted your example
files earlier and may be different to that specified above.
28 With the new boundary applied, create an instance of the toolpath and calculate it:
myToolpath.Calculate();
myToolpath.Calculate()
1 To issue a macro command to create a blank pattern, for example, use the
following:
29 Alternatively you may wish to acquire information from PowerMill to use within your
program. If, for example, you wished to retrieve the units PowerMill is currently
running in, issue the following:
powerMill.ExecuteEx("PRINT PAR TERSE ‘Units’");
30 It is important to note that entities created within PowerMill using macro commands
will not appear within the the API cache until it is updated. To update the cache,
issue the following command:
session.Refresh();
session.Refresh()
powerMill.Execute("TOOL ACCEPT");
session.Refresh();
powerMill.Execute("TOOL ACCEPT")
session.Refresh()
It is strongly recommended that you always use available the API functionality in
preference to issuing raw macro commands. If you feel that the API is lacking
the functionality you require, please contact the forum at:
http://forum.delcam.com/viewforum.php?f=53
Deploying Solutions
Once your development is complete, you will need a way of deploying your
application to end users. This section will guide you through the process of creating
an installer.
1 Begin by ensuring that the Microsoft Visual Studio Installer Projects extension is
installed within your copy of Microsoft Visual Studio. If not, select ‘Tools’ followed by
‘Extensions and Updates’; now locate the extension from within the resulting
window and install it.
3 Next, right click on your solution and select ‘Add’ then ‘New Project’ (Error:
Reference source not found).
33 Now configure a Manufacturer and Product Name. Select ‘Setup1’ from within
Solution Explorer and edit the Manufacturer and ProductName fields in the
Properties tab as demonstrated in figure 7. Note: If the Properties tab is not visible,
select it from the View menu.
If you cannot see the File System window, select ‘File System Editor’ from the
Solution Explorer toolbar (Error: Reference source not found).
36 You will notice references appear in the right hand box (Error: Reference source not
found).
37 As it is a requirement that the end user install the API, the Delcam references are
not required and should be excluded from the project. Select each in turn and set
‘Exclude’ to True in the Properties tab.
39 Right click the newly created folder and select ‘Create New Shortcut’.
40 Select the new folder and, in the empty panel to the right, right click and select
‘Create new shortcut’.
41 In the resulting dialog, double click the ‘Application Folder’ and select the item
starting with ‘Primary output from…’ Rename it to whatever you wish.
42 To create the installer, right click ‘Setup1’ from within Solution Explorer and select
‘Build’. Once the operation has completed, browse to the location of the solution on
the local drive, open Setup1 and then browse to either the Release or Debug folder
- depending upon your solution settings – and the installer will be available therein.
Examples
2 Select ‘Insert’ from the PowerMill main menu and ‘Template Object’ from the drop-
down. From within the resulting dialog, browse the location to which was extracted
the supplied sample data and select ‘Template_Example.ptf’. Click ‘Open’.
3 From within the PowerMill explorer tree, open the preferences form for ‘NC
programs’ and set the ‘Output Folder’ to a location on your desktop. All other
preferences will be set from within code.
4 Save the template by selecting ‘File’ from the PowerMill main menu and ‘Save
Template Objects…’ from the drop-down. Specify a filename and location to which to
save the template file. This can be the same as the original if so desired.
6 Create a new console application project and save it to a location of your choice.
Autodesk.Geometry
Autodesk.ProductInterface
Autodesk.ProductInterface.PowerMill
Autodesk.Utilities
44 Add the following using directive to the top of the source file containing the main
method.
using Autodesk.ProductInterface.PowerMill;
Imports Autodesk.ProductInterface.PowerMill
InstanceReuse.UseExistingInstance)
When you run your code make sure PowerMill is open as we have chosen to use
an existing instance rather than open a new one.
45 Attach to the PowerMill active project by adding the following code to the main
method of the source file:
powerMill.DialogsOff();
powerMill.DialogsOff()
session.ImportTemplateFile(pTemplate);
session.ImportTemplateFile(pTemplate)
49 Select the model from the session and create a block. This requires a mixture of the
API and macro code:
myModel.AddToSelection(true);
powerMILL.Execute("FORM BLOCK");
powerMILL.Execute("BLOCK ACCEPT");
myModel.AddToSelection(True)
powerMILL.Execute("FORM BLOCK")
powerMILL.Execute("BLOCK ACCEPT")
50 Now calculate each toolpath in the template and set the correct rapid heights by
cycling through each toolpath in turn:
toolpath.IsActive = true;
toolpath.Calculate();
powerMILL.Execute("FORM TOOLZHEIGHTS");
powerMILL.Execute("TOOLZHEIGHTS ACCEPT");
toolpath.IsActive = false;
toolpath.IsActive = True
toolpath.Calculate()
powerMILL.Execute("FORM TOOLZHEIGHTS")
powerMILL.Execute("TOOLZHEIGHTS ACCEPT")
toolpath.IsActive = False
Next
51 Each toolpath can now be posted. This will be accomplished by assigning sequential
names through use of a counter:
Int64 count = 1;
52 Since the NC programs already exist within the session, we need only give them an
option file, an output workplane, a name and then write them:
sNCProgram.OutputWorkplaneName = "OWP";
sNCProgram.Write();
count = count + 1;
sNCProgram.OutputWorkplaneName = "OWP"
sNCProgram.Write()
Next
powerMill.DialogsOn();
powerMill.DialogsOn()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.ProductInterface.PowerMill;
namespace UsingTemplateFiles
class Program
powerMILL.DialogsOff();
session.Initialise();
myModel.AddToSelection(true);
powerMILL.Execute("FORM BLOCK");
powerMILL.Execute("BLOCK ACCEPT");
toolpath.IsActive = true;
toolpath.Calculate();
powerMILL.Execute("FORM TOOLZHEIGHTS");
powerMILL.Execute("TOOLZHEIGHTS ACCEPT");
toolpath.IsActive = false;
Int64 count = 1;
sNCProgram.MachineOptionFileName =
@"C:\Users\asr\Desktop\the API PowerMill
Examples\DMU160P_S840D_JM_Rev7.pmoptz";
sNCProgram.OutputWorkplaneName = "OWP";
sNCProgram.Write();
powerMILL.DialogsOn();
Imports Autodesk.ProductInterface.PowerMill
Module Module1
Sub Main()
powerMILL.DialogsOff()
session.ImportTemplateFile(pTemplate)
myModel.AddToSelection(True)
powerMILL.Execute("FORM BLOCK")
powerMILL.Execute("BLOCK ACCEPT")
toolpath.IsActive = True
toolpath.Calculate()
powerMILL.Execute("FORM TOOLZHEIGHTS")
powerMILL.Execute("TOOLZHEIGHTS ACCEPT")
toolpath.IsActive = False
Next
sNCProgram.OutputWorkplaneName = "OWP"
sNCProgram.Write()
count = count + 1
Next
End Sub
End Module