authorit word publishing template - forums.autodesk.com file · web viewthe developer is able to...

35
PowerShape and PowerMill API User Guide - PowerShape • 1 User Guide - PowerShape

Upload: doanh

Post on 14-Feb-2019

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

PowerShape and PowerMill API

User Guide - PowerShape • 1

User Guide - PowerShape

Page 2: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

ContentsPowerShape and PowerMill API..........................................................................1

User Guide - PowerShape 1Introduction..........................................................................................................3

What is PowerShape and PowerMill API?..................................................3

Installing the API.........................................................................................4

Learning to code with PowerShape and PowerMill API..............................5

Examples...........................................................................................................18

Example 1 - Creating a Cylinder...............................................................18

Example 2 - Cleaning a Mesh...................................................................21

User Guide - PowerShape • 2

Page 3: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Introduction

What is PowerShape and PowerMill API?PowerShape and PowerMill API, hereafter referred to as the API, is an easy to use coding alternative to direct use of the macro language supplied by PowerShape and PowerMill.

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.

Advantages of the the API approach to application development are as follows:

Changes made to the macro language by PowerShape and PowerMill development teams are hidden from the developer, with version discrepancies being handled seamlessly by the API.

The developer is able to leverage all of the power provided by the VB.net and C# programming languages.

Significantly fewer lines of code are required to perform like-for-like operations using the API over direct macro language scripting, resulting in reduced complexity and shorter development times.

Throughout this document:

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

For versions of PowerShape below 15.1.46, returned values will be in the language of the PC’s current culture; this can cause problems for PowerShape and PowerMill API. 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.

User Guide - PowerShape • 3

Page 4: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

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.

User Guide - PowerShape • 4

Page 5: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Where to find the APIThe API is now open source at GitHub here:

https://github.com/Autodesk/PowerShapeAndPowerMillAPI

You can get the lastest alpha, beta or release version on NuGet here:

https://www.nuget.org/profiles/Autodesk

User Guide - PowerShape • 5

Page 6: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Learning to code with PowerShape and PowerMill APITo begin with, we will look at some of the more common tasks performed with the API such as starting a new project, connecting to PowerShape, creating simple models and so forth. After this we will look at some examples where macro code can be replaced with the API.

Creating a Project1 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;

User Guide - PowerShape • 6

Figure 1 – New Project

Page 7: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

namespace Example1

{

class Program

{

static void Main(string[] args)

{

}

}

}

Module Module1

Sub Main()

End Sub

End Module

5 The next step is to set references to custom software core. Figure 2 shows a reference diagram detailing the dependencies for each reference.

Figure 2 – Reference Diagram

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…”

User Guide - PowerShape • 7

Autodesk.ProductInterface.PowerMILL

Autodesk.ProductInterface.PowerSHAPE

Autodesk.ProductInterface

Autodesk.Geometry

Autodesk.Utilities

Page 8: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

8 Find available packages at nuget.org by searching for “Autodesk”. You will find (amongst others) the following packages:

Autodesk.ProductInterface.PowerShape – Use this one to create an application that will interface with PowerShape

Autodesk.WPFControls.ProductControls.PowerShape – Use this one to embed PowerShape into your WPF application.

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.

11 Navigate the list of references and select the following:

Autodesk.Geometry

Autodesk.ProductInterface

Autodesk.ProductInterface.PowerShape

Autodesk.Utilities

12 With the references added (manually or via NuGet), insert the following using directive at the top of your source file:

User Guide - PowerShape • 8

Figure 3 – Add Reference

Page 9: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

using Autodesk.ProductInterface.PowerShape;

Imports Autodesk.ProductInterface.PowerShape

This will save repeatedly typing in Autodesk.ProductInterface.PowerShape when accessing items within this class

Connecting to PowerShapeThe following section demonstrates how the API can be used to communicate with a running instance of PowerShape; required is a running instance of PowerShape with an active model.

1 Start an instance of PowerShape.

2 To access the PowerShape instance, add the following line to the main method of your source file:

PSAutomation powerSHAPE = new PSAutomation (Autodesk.ProductInterface.InstanceReuse.UseExistingInstance);

Dim powerSHAPE As New PSAutomation(Autodesk.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 After attaching to PowerShape it is often desirable to reset the list of available models, thereby closing them, and open a new one. This can be achieved as follows:

powerSHAPE.Reset();

powerSHAPE.Reset()

4 To attach to the active PowerShape model, add the following code to the main method:

PSModel psModel = powerSHAPE.ActiveModel;

User Guide - PowerShape • 9

Page 10: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Dim psModel As PSModel = powerSHAPE.ActiveModel()

Alternatively, to create a new model rather than attach to that which is currently active, use:

PSModel psModel = powerSHAPE.Models.CreateEmptyModel();

Dim psModel As PSModel = powerSHAPE.Models.CreateEmptyModel()

5 When controlling PowerShape remotely in this manner, it is important to disable all forms and dialogs; doing so increases the speed at which operations complete. Add the following code to the main method:

powerSHAPE.FormUpdateOff();

powerSHAPE.RefreshOff();

powerSHAPE.DialogsOff();

powerSHAPE.FormUpdateOff()

powerSHAPE.RefreshOff()

powerSHAPE.DialogsOff()

6 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()

7 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 the API.

User Guide - PowerShape • 10

Page 11: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Creating GeometryFigure 4 below details a the API 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.

Figure 4 – Class Diagram

As a simple introduction we will create a line between two points.

1 Two Autodesk.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.

Autodesk.Geometry.Point startPoint = new Autodesk.Geometry.Point(0,0,0);

Autodesk.Geometry.Point endPoint = new Autodesk.Geometry.Point(10, 5, 0);

Dim startPoint As Autodesk.Geometry.Point = New Autodesk.Geometry.Point(0, 0, 0)

User Guide - PowerShape • 11

*

*

*

*

PSSolid

PSLine

PSCurve

PSArc

Active Model

PSLinesCollection

PSSolidsCollection

PSCurvesCollection

PSArcsCollection

PSModel

PSAutomation

Page 12: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Dim endPoint As Autodesk.Geometry.Point = New Autodesk.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.

2 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 Autodesk.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.

4 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:

Autodesk.Geometry.MM lineLength = myLine.Length;

Dim lineLength As Autodesk.Geometry.MM = myLine.Length

Lengths are stored in a Autodesk.Geometry.MM as it facilitates length-specific operations such as converting to inches:

Inch inchLength = (Inch)lineLength;

Dim inchLength As Inch = CType(lineLength, Inch)

PowerShape and PowerMill API always assumes the PowerShape session to be running in mm; thus the return value will always be treated as mm. If you attempted the following: Autodesk.Geometry.Inch sizeX = myModel.BoundingBox.MaxX with a session running in inches, the result would be read in Inches but then multiplied by 25.4 as the API assumes it to be in mm.

User Guide - PowerShape • 12

Page 13: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

5 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’.

6 Add a using directive to the top of the main source file:

using System.Windows.Forms;

Imports System.Windows.Forms;

7 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")

8 Run the code once more and a message box will appear (Error: Reference source notfound).

Creating Solids, Surfaces and MeshesWe shall now see how the API can be used to create other objects in PowerShape by defining a simple solid block.

1 Delete from your main method all lines of code added in the previous section including points, lines and message box.

2 To create a simple block enter the following code:

PSSolid mySolid = psModel.Solids.CreateBlock(startPoint, 25, 50, 10, 0);

User Guide - PowerShape • 13

Figure 5 – Line Length Message Box

Page 14: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Dim mySolid As PSSolid = psModel.Solids.CreateBlock(startPoint,25,50,10,0)

3 This creates a block at the origin (0, 0, 0) of 25mm length, 50mm width, 10mm height and with a draft angle of 0⁰.

4 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()

5 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)

6 Alternatively, the solid can be turned into surfaces thus:

List<PSSurface> newSurface = psModel.Surfaces.CreateSurfacesFromSolid(mySolid);

Dim newSurface As List(Of PSSurface) = psModel.Surfaces.CreateSurfacesFromSolid(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.

Importing and Exporting a ModelIt is common practice to import and export solids, surfaces and so forth in PowerShape. This section will focus on how to achieve this using the API.

1 Create an instance of Autodesk.FileSystem.File with which to encapsulate an import file location:

Autodesk.FileSystem.File importFile = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\Example.dgk");

Dim importFile As Autodesk.FileSystem.File = New

User Guide - PowerShape • 14

Page 15: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Autodesk.FileSystem.File("C:\the API PowerShape Examples\Example.dgk")

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 Import the DGK file into the model:

psModel.Import(importFile);

psModel.Import(importFile)

3 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

4 The solid can now be manipulated as before. The same principle can be applied to surfaces, meshes, curves and so forth.

5 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()

6 The solid will now be exported back to its original file and location:

psModel.Export(importFile, ExportItemsOptions.Selected);

psModel.Export(importFile, ExportItemsOptions.Selected)

Executing Macro CommandsIt is also possible to execute macro commands from within our code, the mechanism for doing which is outlined in the following.

User Guide - PowerShape • 15

Page 16: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

1 Create a simple block using macro commands. Note this is purely for demonstration purposes since a similar result was achieved earlier using the API 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")

2 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 important to note that entities created within PowerShape using macro commands will not appear within the the API cache until it is updated. To update the cache, issue the following command:

psModel.Refresh();

psModel.Refresh()

4 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];

User Guide - PowerShape • 16

Page 17: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

powerSHAPE.Execute("CREATE LINE SINGLE")

powerSHAPE.Execute("0 0 0")

powerSHAPE.Execute("10 0 0")

psModel.Refresh()

Dim myLine As PSLine = psModel.Lines(0)

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 SolutionsOnce 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.

2 Next, right click on your solution and select ‘Add’ then ‘New Project’ (Error:Reference source not found).

User Guide - PowerShape • 17

Page 18: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

3 Select ‘Other Project Types’ followed by ‘Visual Studio Installer’ from the left-hand-side of the new window; then select ‘Setup Project’.

4 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.

5 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 (Error: Reference source not found).

User Guide - PowerShape • 18

Figure 6 – Adding a new project

Figure 7 – Setting Manufacturer, ProductName

Figure 8 – Adding a project output

Page 19: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

If you cannot see the File System window, select ‘File System Editor’ from the Solution Explorer toolbar (Error: Reference source not found).

6 From the dialog box, select ‘Primary Output’ and click ‘Ok’.

7 You will notice references appear in the right hand box (Error: Reference source notfound).

8 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.

9 If it is desired to add a shortcut to the start menu then right click ‘User’s Program’s Menu’ and select ‘Add’ followed by ‘Folder’; name the folder appropriately (Error:Reference source not found).

User Guide - PowerShape • 19

Figure 9 – File System Editor

Figure 10 – Adding References

Page 20: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

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’.

12 In the resulting dialog, double click the ‘Application Folder’ and select the item starting with ‘Primary output from…’ Rename it to whatever you wish.

13 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.

User Guide - PowerShape • 20

Figure 11 – Set an install folder

Figure 12 – Adding shortcut to program menu

Page 21: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Examples

Example 1 - Creating a CylinderExample 1 demonstrates how to use the API 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:\the API PowerShape Examples\Cylinder.dgk

WIZEXPORT

The the API steps are as follows:

1 Start a new console application in Microsoft Visual Studio as detailed previously.

2 Add the following references to your solution (if you have forgotten how to do this refer back).

Autodesk.Geometry

Autodesk.ProductInterface

Autodesk.ProductInterface.PowerShape

Autodesk.Utilities

3 Add the following using directive to the top of the main source file.

using Autodesk.ProductInterface.PowerShape;

User Guide - PowerShape • 21

Page 22: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Imports Autodesk.ProductInterface.PowerShape

4 Connect to PowerShape and turn off all dialogs as before.

PSAutomation powerSHAPE = new PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance);

PSModel psModel = powerSHAPE.ActiveModel;

powerSHAPE.FormUpdateOff();

powerSHAPE.RefreshOff();

powerSHAPE.DialogsOff();

Dim powerSHAPE As PSAutomation = New PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance)

Dim psModel As PSModel = powerSHAPE.ActiveModel

powerSHAPE.FormUpdateOff()

powerSHAPE.RefreshOff()

powerSHAPE.DialogsOff()

5 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()

6 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 the API). 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).

User Guide - PowerShape • 22

Page 23: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

Autodesk.Geometry.Point originPoint = new Autodesk.Geometry.Point(0,0,0);

Autodesk.Geometry.Point startPoint = new Autodesk.Geometry.Point(10, 0, 0);

PSArc myCircle = psModel.Arcs.CreateArcCircle(originPoint, startPoint, 20.0);

Dim originPoint As Autodesk.Geometry.Point = New Autodesk.Geometry.Point(0, 0, 0)

Dim startPoint As Autodesk.Geometry.Point = New Autodesk.Geometry.Point(10, 0, 0)

Dim myCircle As PSArc = psModel.Arcs.CreateArcCircle(originPoint, startPoint, 20)

7 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)

8 The cylinder thus formed can now be exported by firstly creating a Autodesk.FileSystem.File object initialised with an appropriate export path and then writing the data to it.

Autodesk.FileSystem.File exportLocation = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\Cylinder.dgk");

psModel.Export(exportLocation, ExportItemsOptions.Selected);

Dim exportLocation As New Autodesk.FileSystem.File("C:\the API PowerShape Examples\Cylinder.dgk")

psModel.Export(exportLocation, ExportItemsOptions.Selected)

9 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 System;

using System.Collections.Generic;

using System.Linq;

User Guide - PowerShape • 23

Page 24: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

using System.Text;

using System.Threading.Tasks;

using Autodesk.ProductInterface.PowerShape;

namespace CylinderExample

{

class Program

{

static void Main(string[] args)

{

PSAutomation powerSHAPE = new PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance);

PSModel psModel = powerSHAPE.ActiveModel;

powerSHAPE.FormUpdateOff();

powerSHAPE.RefreshOff();

powerSHAPE.DialogsOff();

Autodesk.Geometry.Point originPoint = new Autodesk.Geometry.Point(0,0,0);

Autodesk.Geometry.Point startPoint = new Autodesk.Geometry.Point(10, 0, 0);

PSArc myCircle = psModel.Arcs.CreateArcCircle(originPoint, startPoint, 20.0);

PSSolid myCylinder = psModel.Solids.CreateSolidExtrusionFromWireframe(myCircle, 10.0, 0.0);

Autodesk.FileSystem.File exportLocation = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\Cylinder.dgk");

psModel.Export(exportLocation, ExportItemsOptions.Selected);

powerSHAPE.FormUpdateOn();

powerSHAPE.RefreshOn();

powerSHAPE.DialogsOn();

User Guide - PowerShape • 24

Page 25: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

}

}

}

Imports Autodesk.ProductInterface.PowerShape

Module Module1

Sub Main()

Dim powerSHAPE As PSAutomation = New PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance)

Dim psModel As PSModel = powerSHAPE.ActiveModel

powerSHAPE.FormUpdateOff()

powerSHAPE.RefreshOff()

powerSHAPE.DialogsOff()

Dim originPoint As Autodesk.Geometry.Point = New Autodesk.Geometry.Point(0, 0, 0)

Dim startPoint As Autodesk.Geometry.Point = New Autodesk.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 Autodesk.FileSystem.File("C:\the API PowerShape Examples\Cylinder.dgk")

psModel.Export(exportLocation, ExportItemsOptions.Selected)

powerSHAPE.FormUpdateOn()

powerSHAPE.RefreshOn()

User Guide - PowerShape • 25

Page 26: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

powerSHAPE.DialogsOn()

End Sub

End Module

Example 2 - Cleaning a MeshConsider the case of importing a .STL scan file, the mesh of which contains a number of undesired detached pieces that are to be removed, thereby leaving only the largest piece of the mesh.

To achieve this it is necessary to split the mesh into its individual pieces, consider each one in turn, retain the largest and export this back to file. A macro code solution requires IF statements, which can be confusing and difficult; the following steps demonstrate how the problem can be solved using the API.

1 Start by adding the required references along with the following standard code with which you should now be familiar.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.ProductInterface.PowerShape;

namespace CleanUpMesh

{

class Program

{

static void Main(string[] args)

{

PSAutomation powerSHAPE = new PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance);

PSModel psModel = powerSHAPE.ActiveModel;

powerSHAPE.FormUpdateOff();

powerSHAPE.RefreshOff();

User Guide - PowerShape • 26

Page 27: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

powerSHAPE.DialogsOff();

powerSHAPE.FormUpdateOn();

powerSHAPE.RefreshOn();

powerSHAPE.DialogsOn();

}

}

}

Imports Autodesk.ProductInterface.PowerShape

Module Module1

Sub Main()

Dim powerSHAPE As New PSAutomation(Autodesk.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

User Guide - PowerShape • 27

Page 28: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

2 Create a Autodesk.FileSystem.File object with which to import the .STL file and create a second file object to which the final result will be exported.

Autodesk.FileSystem.File importFile = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\SampleMesh.stl");

Autodesk.FileSystem.File exportFile = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\SplitMesh.dmt");

psModel.Import(importFile);

PSMesh myMesh = psModel.Meshes.LastItem();

Dim importFile As New Autodesk.FileSystem.File("C:\the API PowerShape Examples\SampleMesh.stl")

Dim exportFile As New Autodesk.FileSystem.File("C:\the API PowerShape Examples\SplitMesh.dmt")

psModel.Import(importFile)

Dim myMesh As PSMesh = psModel.Meshes.LastItem

3 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

4 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)

5 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.

User Guide - PowerShape • 28

Page 29: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.ProductInterface.PowerShape;

namespace CleanUpMesh

{

class Program

{

static void Main(string[] args)

{

PSAutomation powerSHAPE = new PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance);

powerSHAPE.Reset();

PSModel psModel = powerSHAPE.ActiveModel;

powerSHAPE.FormUpdateOff();

powerSHAPE.RefreshOff();

powerSHAPE.DialogsOff();

Autodesk.FileSystem.File importFile = new Autodesk.FileSystem.File(@"C:\the API PowerShape Examples\SampleMesh.stl");

Autodesk.FileSystem.File exportFile = new Autodesk.FileSystem.File(@"C:\ the API 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);

User Guide - PowerShape • 29

Page 30: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

psModel.Export(exportFile, ExportItemsOptions.Selected);

powerSHAPE.FormUpdateOn();

powerSHAPE.RefreshOn();

powerSHAPE.DialogsOn();

}

}

}

Imports Autodesk.ProductInterface.PowerShape

Module Module1

Sub Main()

Dim powerSHAPE As New PSAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance)

Dim psModel As PSModel = powerSHAPE.ActiveModel

powerSHAPE.FormUpdateOff()

powerSHAPE.RefreshOff()

powerSHAPE.DialogsOff()

Dim importFile As New Autodesk.FileSystem.File("C:\the API PowerShape Examples\SampleMesh.stl")

Dim exportFile As New Autodesk.FileSystem.File("C:\the API PowerShape Examples\SplitMesh.dmt")

psModel.Import(importFile)

Dim myMesh As PSMesh = psModel.Meshes.LastItem

Dim meshList As List(Of PSMesh) = myMesh.Split

User Guide - PowerShape • 30

Page 31: AuthorIT Word Publishing Template - forums.autodesk.com file · Web viewThe developer is able to leverage all of the power provided by the VB.net and C# programming languages. Significantly

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

User Guide - PowerShape • 31