Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
www.delcam-services.com
Acknowledgements
This documentation references a number of registered trademarks and these are the
property of their respective owners. For example, Microsoft and Windows are either
registered trademarks or trademarks of Microsoft Corporation in the United States.
Contents
Custom Software Core ........................................................................................... 1
Introduction ............................................................................................................. 4
What is Custom Software Core? ................................................................... 4
Installing CSC ................................................................................................ 5
Learning to code with Custom Software Core .............................................. 6
Examples .............................................................................................................. 19
Example 1 - Creating a Cylinder ................................................................. 19
Example 2 - Cleaning a Mesh ..................................................................... 22
Introduction
What is Custom Software Core?
Custom Software Core, hereafter referred to as CSC, is an easy to use coding
alternative to direct use of the macro language supplied by PowerSHAPE and
PowerMILL.
The power of CSC can be harnessed from within the Microsoft Visual Studio
programming environment and used for creating both Delcam Automation addins and
standalone executables. The programmer can choose to develop in either C# or VB.net.
Advantages of the CSC approach to application development are as follows:
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 Custom Software Core
For versions of PowerSHAPE below 15.1.46, returned values will be in the
language of the PCs current culture; this can cause problems for Custom
Software Core. To resolve this issue, add to the windows environment the
variable LC_ALL = English, which will force PowerSHAPE to run in English. The
downside to this is that the UI will also be displayed in English. If, afterwards,
you wish to revert the change, it is only necessary to delete the new
environment variable.
We recommend that, if running a 32Bit version of PowerSHAPE, you compile your
applications as 32Bit; likewise, if you are running a 64Bit version of PowerSHAPE,
compile in 64Bit.
Installing CSC
Ensure you have downloaded the CSC installer onto your PC from the Delcam website,
a link to which can be found in the same location as the link for this document.
http://www.delcam.com/downloads/custom-core/index.asp
To install the software, double click the installer and follow the instructions as required.
Once your install is complete, you must ensure you have extracted and saved the
example files for CSC in an accessible location on your local drive before proceeding.
These files will be used throughout the remainder of the documentation.
Creating a Project
1
Select Visual C# or Visual Basic (based on your preference) from the options on
the left, then select Console Application (Figure 1).
Your main project source file will contain the following code:
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
namespace Example1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Module Module1
Sub Main()
End Sub
End Module
The next step is to set references to custom software core. Figure 2 shows a
reference diagram detailing the dependencies for each reference.
Delcam.Utilities
Delcam.Geometry
Delcam.ProductInterface
Delcam.ProductInterface.
PowerSHAPE
Delcam.ProductInterface.
PowerMILL
To add the required references, right click on References within your solution in the
Solution Explorer on the left and then select Add then Reference (Figure 3).
From within the window that now appears, select Assemblies and then Extensions
from the list.
Delcam.Geometry
Delcam.ProductInterface
Delcam.ProductInterface.PowerSHAPE
Delcam.Utilities
With the references added, insert the following using directive at the top of your
source file:
using Delcam.ProductInterface.PowerSHAPE;
Imports Delcam.ProductInterface.PowerSHAPE
Connecting to PowerSHAPE
The following section demonstrates how CSC can be used to communicate with a
running instance of PowerSHAPE; required is a running instance of PowerSHAPE
with an active model.
1
To access the PowerSHAPE instance, add the following line to the main method of
your source file:
PSAutomation powerSHAPE = new PSAutomation
(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
Dim powerSHAPE As New PSAutomation(Delcam.ProductInterface.
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 PowerSHAPE); Create a single instance (close any
PowerSHAPE instances already open and create a new one) or
UseExistingInstance (attach to a running instance of PowerSHAPE).
If you continue the line by typing a . after your selection of InstanceReuse,
you are presented with a number of additional options including the mode in
which to open PowerSHAPE (drafting, pro, toolmaker, for example) and the
version and/or maximum version of the PowerSHAPE installation to run.
3
To attach to the active PowerSHAPE model, add the following code to the main
method:
PSModel psModel = powerSHAPE.ActiveModel;
Dim psModel As PSModel = powerSHAPE.ActiveModel()
These will need to be re-enabled before the application exits for PowerSHAPE to
function normally. Add the code to do this now and all subsequent code in between
this and the previous block.
powerSHAPE.FormUpdateOn();
powerSHAPE.RefreshOn();
powerSHAPE.DialogsOn();
powerSHAPE.FormUpdateOn()
powerSHAPE.RefreshOn()
powerSHAPE.DialogsOn()
Type psModel into the main method (refer to the previous step to ensure you add it
in the right place) followed by a full stop (period). IntelliSense reveals a list of
methods that can be invoked on the model and a selection of properties that can be
retrieved or set. The following sections will explore a small subset of the operations
that can be performed on PowerSHAPE using CSC.
Creating Geometry
Figure 4 below details a CSC PowerSHAPE class hierarchy. PSAutomation contains a
PSModel, which contains collections of arcs, curves, lines and so forth, each
containing one or more objects of its type. Objects can be added to and removed
from collections and the objects themselves manipulated with the various operations
available on them: in short, a typical object oriented strategy.
PSAutomation
Active
Model
PSModel
PSArcsCollection
PSArc
PSCurvesCollection
PSCurve
PSLinesCollection
PSLine
PSSolidsCollection
PSSolid
Two Delcam.Geometry.Point objects are used to define the start and end points of
the line and are created with the following code, which should be added to the main
method of the application source file.
Delcam.Geometry.Point startPoint = new Delcam.Geometry.Point(0,0,0);
Delcam.Geometry.Point endPoint = new Delcam.Geometry.Point(10, 5, 0);
Dim startPoint As Delcam.Geometry.Point = New Delcam.Geometry.Point(0, 0, 0)
Dim endPoint As Delcam.Geometry.Point = New Delcam.Geometry.Point(10, 5, 0)
startPoint is initialised with coordinates (0, 0, 0), which is the origin of the model,
and endpoint is given the coordinates (10, 5, 0), which represents displacement in
both X and Y.
The two points can now be passed to PowerSHAPE and a line drawn between them:
PSLine myLine = psModel.Lines.CreateLine(startPoint, endPoint);
Dim myLine As PSLine = psModel.Lines.CreateLine(startPoint, endPoint)
PSLine myLine holds a reference to the newly created PowerSHAPE object and
is defined in the Delcam.ProductInterface.PowerSHAPE namespace.
3
The code can now be tested. Ensure there is an open session of PowerSHAPE to
connect to, click the Start icon in Microsoft Visual Studio and observe the
PowerSHAPE active window where a line will be created. If any problems are
encountered, review all previous steps to this point and ensure they have been
completed in accordance with the document.
The line can now be manipulated in code via the PSLine object it is mapped to
(myLine). As an example, let us return the length of the line and store it as a
delcam.geometry.MM length object. Add the following code to the main method:
Delcam.Geometry.MM lineLength = myLine.Length;
Dim lineLength As Delcam.Geometry.MM = myLine.Length
Create a message box to display the line length. Firstly add a reference to
Window.Systems.Forms by right clicking on your project in the Solution Explorer
and selecting Add then Add Reference from the list. Select Assemblies from the
left then Framework from the resulting drop down. Select System.Windows.Forms
from the list and click OK.
Instantiate a message box in which to display the line length. Add the following
code to the main method:
MessageBox.Show("The line is " + lineLength.ToString() + "mm");
MessageBox.Show("This line is" & lineLength.ToString & " mm")
Run the code once more and a message box will appear (Figure 5).
Delete from your main method all lines of code added in the previous section
including points, lines and message box.
This creates a block at the origin (0, 0, 0) of 25mm length, 50mm width, 10mm
height and with a draft angle of 0.
Many operations can now be performed from within code on the newly created solid
through its PSSolid reference object. If, for example, it is desired to delete the solid
from PowerSHAPE, enter the following:
mySolid.Delete();
mySolid.Delete()
With the addition of a single line of code a mesh can be created from the solid:
PSMesh myMesh = psModel.Meshes.CreateMeshFromSolid(mySolid);
Dim myMesh As PSMesh = psModel.Meshes.CreateMeshFromSolid(mySolid)
Surfaces must be stored in a list as such transforms may return many; six in
the case of a cube.
7
Delete all code entered in this section before moving to the next.
Example.dgk will be located in the path to which you extracted your example
files earlier and may be different to that specified above.
2
The solid has now been imported into the open model. To obtain an instance of it
from the solids collection enter the following line of code:
PSSolid mySolid = psModel.Solids.LastItem();
Dim mySolid As PSSolid = psModel.Solids.LastItem
The solid can now be manipulated as before. The same principle can be applied to
surfaces, meshes, curves and so forth.
To export the solid, it is first necessary to ensure it is the only selected object. The
following two lines of code clear all selected items and then reselect the object that
is to be exported.
psModel.ClearSelectedItems();
mySolid.AddToSelection();
psModel.ClearSelectedItems()
mySolid.AddToSelection()
The solid will now be exported back to its original file and location:
psModel.Export(importFile, ExportItemsOptions.Selected);
psModel.Export(importFile, ExportItemsOptions.Selected)
Create a simple block using macro commands. Note this is purely for demonstration
purposes since a similar result was achieved earlier using CSC functions.
powerSHAPE.Execute("CREATE SOLID BLOCK");
powerSHAPE.Execute("BLOCK");
powerSHAPE.Execute("0,0,0");
powerSHAPE.Execute("CREATE SOLID BLOCK")
powerSHAPE.Execute("BLOCK")
powerSHAPE.Execute("0,0,0")
Alternatively you may wish to acquire information from PowerSHAPE to use within
your program. If, for example, you want to find the length of a line, enter the
following:
Double lineLength2 =
(Double)powerSHAPE.ExecuteEx("line[TempLine].length");
Dim lineLength2 As Double = powerSHAPE.ExecuteEx("line[TempLine].length")
You will need to know the name of the line to perform this operation.
3
It is now possible to create in code an instance of the PowerSHAPE object that was
created with the macro command. If, for example, you create a line in macro code,
create a PSLine instance as follows:
powerSHAPE.Execute("CREATE LINE SINGLE");
powerSHAPE.Execute("0 0 0");
powerSHAPE.Execute("10 0 0");
psModel.Refresh();
PSLine myLine = psModel.Lines[0];
powerSHAPE.Execute("CREATE LINE SINGLE")
powerSHAPE.Execute("0 0 0")
powerSHAPE.Execute("10 0 0")
psModel.Refresh()
Dim myLine As PSLine = psModel.Lines(0)
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. Note that the end user will require a licensed copy of CSC installed on
their target system in order to execute your application.
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.
Next, right click on your solution and select Add then New Project (Figure 6).
Select Other Project Types followed by Visual Studio Installer from the left-handside of the new window; then select Setup Project.
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.
Right click the Application Folder from within the File System window to the right of
Solution Explorer. Select Add and then select Project Output from the resultant
context menus (Figure 8).
If you cannot see the File System window, select File System Editor from the
Solution Explorer toolbar (Figure 9).
From the dialog box, select Primary Output and click Ok.
You will notice references appear in the right hand box (Figure 10).
As it is a requirement that the end user install CSC, 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.
If it is desired to add a shortcut to the start menu then right click Users Programs
Menu and select Add followed by Folder; name the folder appropriately (Figure
11).
10 Right click the newly created folder and select Create New Shortcut.
11 Select the new folder and, in the empty panel to the right, right click and select
Create new shortcut.
Examples
Example 1 - Creating a Cylinder
Example 1 demonstrates how to use CSC to create a circle in PowerSHAPE, extrude it to
form a solid and then export the result to file.
For comparison, macro code to accomplish the same task is presented below.
CREATE ARC
FULL
0,0,0
MODIFY
DIMENSION 20
ACCEPT
CREATE SOLID EXTRUSION
MODIFY
LENGTH 50
ACCEPT
FILE EXPORT WIZARD
NEXT
C:\CSC PowerSHAPE Examples\Cylinder.dgk
WIZEXPORT
Add the following references to your solution (if you have forgotten how to do this refer
back).
Delcam.Geometry
Delcam.ProductInterface
Delcam.ProductInterface.PowerSHAPE
Delcam.Utilities
Add the following using directive to the top of the main source file.
using Delcam.ProductInterface.PowerSHAPE;
Imports Delcam.ProductInterface.PowerSHAPE
It is usually desirable to re-enable forms and dialogs before the program exits. Add this
code now and all subsequent lines between this and the previous block.
powerSHAPE.FormUpdateOn();
powerSHAPE.RefreshOn();
powerSHAPE.DialogsOn();
powerSHAPE.FormUpdateOn()
powerSHAPE.RefreshOn()
powerSHAPE.DialogsOn()
Two points are required to create a circle; the first is the origin of the circle and the
second the position at which the circle starts (typically this is of no concern, but is
required by CSC). The following code creates a circle centred at (0, 0, 0) and starting
from the x-axis (the co-ordinate position of this point is not important so long as it lies
on the axis).
Delcam.Geometry.Point originPoint = new Delcam.Geometry.Point(0,0,0);
Delcam.Geometry.Point startPoint = new Delcam.Geometry.Point(10, 0, 0);
PSArc myCircle = psModel.Arcs.CreateArcCircle(originPoint,
startPoint, 20.0);
Dim originPoint As Delcam.Geometry.Point = New Delcam.Geometry.Point(0, 0, 0)
Dim startPoint As Delcam.Geometry.Point = New Delcam.Geometry.Point(10, 0, 0)
Dim myCircle As PSArc = psModel.Arcs.CreateArcCircle(originPoint,
startPoint, 20)
The circle will now be extruded by 50.0 mm in the positive to form a solid.
PSSolid myCylinder =
psModel.Solids.CreateSolidExtrusionFromWireframe(myCircle, 50.0, 0.0);
Dim myCylinder As PSSolid =
psModel.Solids.CreateSolidExtrusionFromWireframe(myCircle, 50, 0)
The code can now be run. If the result is not as expected, ensure you have followed
verbatim all he steps in this example.
It is worthy of note that, should the code required to disable and re-enable forms and
dialogs be excluded from the count, the number of lines required to accomplish our
objective here is six, eight fewer than the macro code solution.
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
Delcam.ProductInterface.PowerSHAPE;
namespace CylinderExample
{
class Program
{
static void Main(string[] args)
{
PSAutomation powerSHAPE = new
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
PSModel psModel = powerSHAPE.ActiveModel;
powerSHAPE.FormUpdateOff();
powerSHAPE.RefreshOff();
powerSHAPE.DialogsOff();
Delcam.Geometry.Point originPoint = new
Delcam.Geometry.Point(0,0,0);
Delcam.Geometry.Point startPoint = new
Delcam.Geometry.Point(10, 0, 0);
PSArc myCircle = psModel.Arcs.CreateArcCircle(originPoint,
startPoint, 20.0);
PSSolid myCylinder =
psModel.Solids.CreateSolidExtrusionFromWireframe(myCircle, 10.0, 0.0);
Delcam.FileSystem.File exportLocation = new
Delcam.FileSystem.File(@"C:\CSC PowerSHAPE Examples\Cylinder.dgk");
psModel.Export(exportLocation, ExportItemsOptions.Selected);
powerSHAPE.FormUpdateOn();
powerSHAPE.RefreshOn();
powerSHAPE.DialogsOn();
}
}
}
Imports Delcam.ProductInterface.PowerSHAPE
Module Module1
Sub Main()
Dim powerSHAPE As PSAutomation = New
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance)
Dim psModel As PSModel = powerSHAPE.ActiveModel
powerSHAPE.FormUpdateOff()
powerSHAPE.RefreshOff()
powerSHAPE.DialogsOff()
Dim originPoint As Delcam.Geometry.Point = New
Delcam.Geometry.Point(0, 0, 0)
Dim startPoint As Delcam.Geometry.Point = New
Delcam.Geometry.Point(10, 0, 0)
Dim myCircle As PSArc =
psModel.Arcs.CreateArcCircle(originPoint, startPoint, 20)
Dim myCylinder As PSSolid =
psModel.Solids.CreateSolidExtrusionFromWireframe(myCircle, 50, 0)
Dim exportLocation As New Delcam.FileSystem.File("C:\CSC
PowerSHAPE Examples\Cylinder.dgk")
psModel.Export(exportLocation, ExportItemsOptions.Selected)
powerSHAPE.FormUpdateOn()
powerSHAPE.RefreshOn()
powerSHAPE.DialogsOn()
End Sub
End Module
Start by adding the required references along with the following standard code with
which you should now be familiar.
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
Delcam.ProductInterface.PowerSHAPE;
namespace CleanUpMesh
{
class Program
{
static void Main(string[] args)
{
PSAutomation powerSHAPE = new
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
PSModel psModel = powerSHAPE.ActiveModel;
powerSHAPE.FormUpdateOff();
powerSHAPE.RefreshOff();
powerSHAPE.DialogsOff();
powerSHAPE.FormUpdateOn();
powerSHAPE.RefreshOn();
powerSHAPE.DialogsOn();
}
}
}
Imports Delcam.ProductInterface.PowerSHAPE
Module Module1
Sub Main()
Dim powerSHAPE As New
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance)
Dim psModel As PSModel = powerSHAPE.ActiveModel
powerSHAPE.FormUpdateOff()
powerSHAPE.RefreshOff()
powerSHAPE.DialogsOff()
powerSHAPE.FormUpdateOn()
powerSHAPE.RefreshOn()
powerSHAPE.DialogsOn()
End Sub
End Module
Create a Delcam.FileSystem.File object with which to import the .STL file and create
a second file object to which the final result will be exported.
Delcam.FileSystem.File importFile = new Delcam.FileSystem.File(@"C:\CSC
PowerSHAPE Examples\SampleMesh.stl");
Delcam.FileSystem.File exportFile = new Delcam.FileSystem.File(@"C:\CSC
PowerSHAPE Examples\SplitMesh.dmt");
psModel.Import(importFile);
PSMesh myMesh = psModel.Meshes.LastItem();
Dim importFile As New Delcam.FileSystem.File("C:\CSC PowerSHAPE
Examples\SampleMesh.stl")
Dim exportFile As New Delcam.FileSystem.File("C:\CSC PowerSHAPE
Examples\SplitMesh.dmt")
psModel.Import(importFile)
Dim myMesh As PSMesh = psModel.Meshes.LastItem
Split the mesh and store in a list the pieces returned by PowerSHAPE.
List<PSMesh> meshList = myMesh.Split();
Dim meshList As List(Of PSMesh) = myMesh.Split
Cycle through each entity to determine the largest bounding box. This can be
achieved with a single line of code:
meshList.OrderBy(m => m.BoundingBox.Volume).Last().AddToSelection(true);
meshList.OrderBy(Function(m) m.BoundingBox.Volume).Last.AddToSelection(true)
Now only the largest piece of the mesh is selected PowerSHAPE, it can be exported
to file in the manner described earlier in the document.
psModel.Export(exportFile, ExportItemsOptions.Selected);
psModel.Export(exportFile, ExportItemsOptions.Selected)
It should be noted that a complex operation has been reduced to a small number of
concisely written and easy to comprehend lines of code, the full listing of which is given
below.
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
Delcam.ProductInterface.PowerSHAPE;
namespace CleanUpMesh
{
class Program
{
static void Main(string[] args)
{
PSAutomation powerSHAPE = new
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
powerSHAPE.Reset();
powerSHAPE.FormUpdateOff();
powerSHAPE.RefreshOff();
powerSHAPE.DialogsOff();
Delcam.FileSystem.File importFile = new
Delcam.FileSystem.File(@"C:\CSC PowerSHAPE Examples\SampleMesh.stl");
Delcam.FileSystem.File exportFile = new
Delcam.FileSystem.File(@"C:\ CSC PowerSHAPE Examples \SplitMesh.dmt");
psModel.Import(importFile);
PSMesh myMesh = psModel.Meshes.LastItem();
List<PSMesh> meshList = myMesh.Split();
meshList.OrderBy(m =>
m.BoundingBox.Volume).Last().AddToSelection(true);
psModel.Export(exportFile, ExportItemsOptions.Selected);
powerSHAPE.FormUpdateOn();
powerSHAPE.RefreshOn();
powerSHAPE.DialogsOn();
}
}
}
Imports Delcam.ProductInterface.PowerSHAPE
Module Module1
Sub Main()
Dim powerSHAPE As New
PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance)
Dim psModel As PSModel = powerSHAPE.ActiveModel
powerSHAPE.FormUpdateOff()
powerSHAPE.RefreshOff()
powerSHAPE.DialogsOff()
Dim importFile As New Delcam.FileSystem.File("C:\CSC PowerSHAPE
Examples\SampleMesh.stl")
Dim exportFile As New Delcam.FileSystem.File("C:\CSC PowerSHAPE
Examples\SplitMesh.dmt")
psModel.Import(importFile)
Dim myMesh As PSMesh = psModel.Meshes.LastItem
Dim meshList As List(Of PSMesh) = myMesh.Split
meshList.OrderBy(Function(m)
m.BoundingBox.Volume).Last.AddToSelection(True)
psModel.Export(exportFile, ExportItemsOptions.Selected)
powerSHAPE.FormUpdateOn()
powerSHAPE.RefreshOn()
powerSHAPE.DialogsOn()
End Sub
End Module
learn: www.delcam.tv/lz
watch: www.delcam.tv
visit: www.delcam-services.com
Delcam
Small Heath Business Park, Birmingham B10 0HJ UK
+44 (0)121 766 5544 | delcamconsulting@delcam.com
www.delcam-services.com
Copyright Delcam Ltd 2015. All trademarks are the property of their respective owners.