API is returning 0 tool paths where there are some.

API is returning 0 tool paths where there are some.

jeff.davis
Participant Participant
1,216 Views
9 Replies
Message 1 of 10

API is returning 0 tool paths where there are some.

jeff.davis
Participant
Participant

My code uses the API "Session.Toolpaths.Count" to return the tool path count, but many times this returns 0 tool paths when there ARE many uncalculated tool paths in the current session.  Does anyone know why this would happen?

0 Likes
Accepted solutions (1)
1,217 Views
9 Replies
Replies (9)
Message 2 of 10

luke.edwards.autodesk
Community Manager
Community Manager

Hi Jeff

 

Are you able to give an example of this happening?  Please ensure you are using the latest API version from NuGet.

 

It could be that you are using commands to create or import entities using Execute.  If you do this then the API can become skewed with what is in the project.  If this happens you can do:

powerMill.ActiveProject.Refresh()

If so, then let me know what command you are using, perhaps there is a suitable replacement within the API.

 

Luke


Luke Edwards
Consulting Services Manager
0 Likes
Message 3 of 10

jeff.davis
Participant
Participant

Thanks for helping me Luke.

I'll give a more complete description of what I'm doing.  I'm building an offline toolpath calculation farm.  We call it a "Crunching Farm".  I'm building this in VB.Net, and it's the first time using the API instead of the old OLE from Delcam.

I did get the newest API from NuGet.  My UI has two parts, one is for the users to submit their jobs to the Crunching Farm.  The other part is the server side, showing how many servers there are and what they are doing.

I use SQL server database to hold the jobs and keep track of the status of them.

So I have the users side built already.  It is simple because all I do is check to see if the current session has tool paths and if so do a save as to a jobs folder on the database server. 

So you can see from my uploaded screen shots my "ProcessJob" code and the two test files I have in the database to work on.

It's strange because I can process tool paths before the save as command happened but loading the files after the save as produces "0 tool paths" in my code.

0 Likes
Message 4 of 10

jeff.davis
Participant
Participant

Well I figured out how to stop the false reading of the toolpaths, but I don't know why I had to do this..

Doing a bunch of testing I found out that having the "LoadProject" and "ToolPath.Count" in the same subroutine was causing the return of 0 toolpaths.  I just made a new subroutine and move the calculate toolpath into this and the issue was fixed.

 

 

 

 

          'Setup the API and connect to PM
            Dim PM As New PMAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance)
            Dim Session As PMProject = PM.ActiveProject
            PM.DialogsOff()

            'Load the job into PowerMill 
            Dim SourcePath As New Autodesk.FileSystem.Directory(JobPathSource)
            PM.LoadProject(SourcePath)

            'Update database and start to Calculate the toolpaths
            SqlStatusUpdate.Parameters.Item("@ServerName").Value = ServerName
            SqlStatusUpdate.Parameters.Item("@StatusWaiting").Value = 0
            SqlStatusUpdate.Parameters.Item("@StatusProcessing").Value = 1
            SqlStatusUpdate.Parameters.Item("@StatusComplete").Value = 0
            SqlStatusUpdate.Parameters.Item("@Filename").Value = JobPathName
            Connect_CruncherJobs.Open()
            SqlStatusUpdate.ExecuteNonQuery()
            Connect_CruncherJobs.Close()

            'Added message box to stop program for testing purpose
            MsgBox("There are " & Session.Toolpaths.Count & " Toolpaths")

            For I As Int32 = 0 To Session.Toolpaths.Count - 1
                Dim toolpath As PMToolpath = Session.Toolpaths(I)
                If toolpath.IsCalculated = False Then
                    toolpath.Calculate()
                End If
            Next
0 Likes
Message 5 of 10

luke.edwards.autodesk
Community Manager
Community Manager

Do you have a small example application that could show this happening?


Luke Edwards
Consulting Services Manager
0 Likes
Message 6 of 10

jeff.davis
Participant
Participant

OK I had some time to create a repeatable small application that shows this issue.

In VB.net I created a project.  I added a single button and the code is below.  This issue happens when I load a job from the API.  If I remove the code that loads the job and already have the job loaded in a powermill session then all toolpath is found.

My work around was to place the code to calculate the toolpath outside of this subroutine and into it's own subroutine because I needed the code to load a job.

 

Imports Autodesk.ProductInterface.PowerMILL

Public Class Form1

    Private Sub CmdProcess_Click(sender As Object, e As EventArgs) Handles CmdProcess.Click

        'Setup the API and connect to PowerMill
        Dim PM As New PMAutomation(Autodesk.ProductInterface.InstanceReuse.UseExistingInstance)
        Dim Session As PMProject = PM.ActiveProject
        PM.DialogsOff()

        'Load job into PowerMill
        Dim SourcePath As New Autodesk.FileSystem.Directory("\\beavertn-svr-py\teamshare\Jeff D\SAMPLE 1")
        PM.LoadProject(SourcePath)

        For I As Int32 = 0 To Session.Toolpaths.Count - 1
            Dim Toolpath As PMToolpath = Session.Toolpaths(I)
            If Toolpath.IsCalculated = False Then
                Toolpath.Calculate()
            End If
        Next

    End Sub
End Class
0 Likes
Message 7 of 10

Anonymous
Not applicable
Accepted solution

The problem here is that the "PM.LoadProject()" command doesn't refresh your "Session" variable.

So either you use

Session.Refresh()

after loading the project or you initialize your "Session" variable after you loaded the project

PM.LoadProject(SourcePath)
Dim Session As PMProject = PM.ActiveProject
0 Likes
Message 8 of 10

luke.edwards.autodesk
Community Manager
Community Manager

You can even simplify it into one line:

Dim session = PM.LoadProject(SourcePath)

Luke


Luke Edwards
Consulting Services Manager
0 Likes
Message 9 of 10

jeff.davis
Participant
Participant

Yes, refreshing the session did the trick.  It's still not clear to me why moving the tool path processing code out to a subroutine and calling that sub made this work for me.

0 Likes
Message 10 of 10

luke.edwards.autodesk
Community Manager
Community Manager

I wouldn't advise using the Refresh.  I would advise the line of code I showed:

Dim session = PM.LoadProject(ProjectPath)

It would have worked using the subroutine call presumably because you did the following inside the subroutine:

Dim session = PM.ActiveProject

Luke Edwards
Consulting Services Manager
0 Likes