Powermill Automation API

Powermill Automation API

lopezerik73
Contributor Contributor
3,955 Views
8 Replies
Message 1 of 9

Powermill Automation API

lopezerik73
Contributor
Contributor

Hello.

 

I´m newbie with Powermill API and i want to ask some help.

I have a little C# form menu with two buttons (For the moment) the first button works fine, but the second not work and get me an error.

Some body can help me please?

I attacht 2 images about the error i say.

Many thanks in advance.

 

kindly regards.

  

   Erik

ERROR1.jpg

 

 

ERROR2.jpg

 

 

 

//====================
//POWERMILL AUTOMATION
//====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Delcam.ProductInterface.PowerMILL;
using Delcam.ProductInterface;
using Delcam.Geometry;

namespace CS_FORM_TEST1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

PMAutomation powerMILL = new PMAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
PMProject pSession = powerMILL.ActiveProject;

}

private void button2_Click(object sender, EventArgs e)
{

Delcam.FileSystem.File importFile = new Delcam.FileSystem.File(@"C:\Users\*****\Desktop\HOLES.dgk");
PMModel myModel = pSession.Models.CreateModel(importFile);

}
}
}

 

 

 

0 Likes
Accepted solutions (1)
3,956 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

move this line just below your class declaration:

PMAutomation powerMILL = new PMAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);

and add another definition of the active session at the beginning of the button2_click event:

PMProject pSession = powerMILL.ActiveProject;
                Delcam.FileSystem.File importFile = new Delcam.FileSystem.File(@"C:\Users\*****\Desktop\HOLES.dgk");
                PMModel myModel = pSession.Models.CreateModel(importFile);

your problem is that the code inside of button2_click doesn't know what "pSession" is because it is being defined only in the other button's method.

 

This should work:

public partial class Form1 : Form
        {
            PMAutomation powerMILL = new PMAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                PMProject pSession = powerMILL.ActiveProject;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                PMProject pSession = powerMILL.ActiveProject;
                Delcam.FileSystem.File importFile = new Delcam.FileSystem.File(@"C:\Users\*****\Desktop\HOLES.dgk");
                PMModel myModel = pSession.Models.CreateModel(importFile);
            }
        }
Message 3 of 9

lopezerik73
Contributor
Contributor

Hello.

 

First  at all many thanks for your answer and help me nbaranowski.

Your solution is not correct at all.

Now when execute the code start automatically the Powermiil and not when press the button for start it.

But the second button works nice and import the file correctly.

Many thanks again.

 

Kindly regards.

 

  Erik

0 Likes
Message 4 of 9

Anonymous
Not applicable
Accepted solution

You have the object set to "UseExistingInstance" so i assumed your session was already open.

 

You can just move another copy of the PMAutomation object into the button2_click to get what you want.

 

 

This is tested and works:

//====================
//POWERMILL AUTOMATION
//====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Delcam.ProductInterface.PowerMILL;
using Delcam.ProductInterface;
using Delcam.Geometry;


namespace CS_FORM_TEST1
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            PMAutomation powerMILL = new PMAutomation(Delcam.ProductInterface.InstanceReuse.CreateNewInstance);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            PMAutomation powerMILL = new PMAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance);
            PMProject pSession = powerMILL.ActiveProject;
            Delcam.FileSystem.File importFile = new Delcam.FileSystem.File(@"C:\Users\*******\Desktop\HOLES.dgk");
            PMModel myModel = pSession.Models.CreateModel(importFile);

        }
    }
} 
Message 5 of 9

lopezerik73
Contributor
Contributor

Many thanks nbaranowski.Smiley Wink

 

Now works perfect as i want.

I try now to add more options to my little API.

Thanks again.

 

Kindly regards.

 

   Erik

0 Likes
Message 6 of 9

Khoa_NguyenDang
Advocate
Advocate

Khoa_NguyenDang_0-1625155273932.png

I have the same Problem but can not fix with your method

Please help me 

 

0 Likes
Message 7 of 9

luke.edwards.autodesk
Community Manager
Community Manager

Hi, what version of PowerMill are you running?  Can you open task manager and see if pmill.exe is running when you get this error?


Luke Edwards
Consulting Services Manager
0 Likes
Message 8 of 9

Khoa_NguyenDang
Advocate
Advocate

Hi @luke.edwards.autodesk 

i am using Power 2022 and PowerMill API 1.1.53

I aldready open PowerMill when run Form

0 Likes
Message 9 of 9

luke.edwards.autodesk
Community Manager
Community Manager

Hi, does it give a stack trace in the error report?  That would help me work out which line it is failing on.

One thing to check, is it PowerMill you are running or PowerMill Viewer?


Luke Edwards
Consulting Services Manager
0 Likes