Machsupport Forum

Mach Discussion => Mach SDK plugin questions and answers. => Topic started by: SeattleMatt on May 20, 2009, 12:52:23 AM

Title: VS2008 Add Reference
Post by: SeattleMatt on May 20, 2009, 12:52:23 AM
Hi,

Inside VS2008 you can usually "Add Reference" on an dll or exe and it'll generate all the com interop stuff needed. I inhales the mach3.exe file no problem, and the Object Browser is showing stuff that makes sense.

Inside a button click even, I have the following:

 
Code: [Select]
     private void button1_Click(object sender, EventArgs e)
        {         
            Mach4.CMach4DocClass dc = new Mach4.CMach4DocClass();
            Mach4.IMyScriptObject so = (Mach4.IMyScriptObject)dc.GetScriptDispatch();
            so.SetDRO(1, 3.3);
            so.SetOEMDRO(1, 3.3);
        }


It takes about 30 seconds for the first line to return. It's always an error. If Mach3 is already running, nothing appears to happen during this time. If Mach3 is NOT running, then a config selector dlg pops up with a list of things I've not seen before (CPSOKBTasks...).

When the "new" finally returns, the error is below.

Any idea what I need to do to make this work? I've seen the work others have done for c++, and the zip file posted on the c# effort in which the com interop was done manually. But I'd really like to get this working as it's so clean and short.

Thanks in advance.



Code: [Select]

System.Runtime.InteropServices.COMException was unhandled
  Message="Retrieving the COM class factory for component with CLSID {CA7992B2-2653-4342-8061-D7D385C07809} failed due to the following error: 80080005."
  Source="Mach3Controller"
  ErrorCode=-2146959355
  StackTrace:
       at Mach3Controller.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\matt\Documents\Visual Studio 2008\Projects\Junk\Mach3Controller\Mach3Controller\Form1.cs:line 27
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Mach3Controller.Program.Main() in C:\Users\matt\Documents\Visual Studio 2008\Projects\Junk\Mach3Controller\Mach3Controller\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Title: Re: VS2008 Add Reference
Post by: SeattleMatt on May 21, 2009, 11:49:03 PM
OK, I figured this out thanks to zip file that babinda01 posted. I'm on vista, not sure that matters.

If you are in VS and want to control Mach via C# (and presumably VB), then you can start by creating a project, and then doing Project -> Add Reference and selecting the Mach3.exe.

Your project will have a new reference added called Mach4.

Add a button on your form. Make you code look as follows. Each time you hit a button, Mach will drive to the coords 2,2 and 6,3.

Don't try this connected to a real machine until you've studied it a bit.

Code: [Select]
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;

// I had to run VS as admin for this to work. I'm on Vista.

// Need this to drive Mach
using System.Runtime.InteropServices;

namespace Mach3ControltoControlPlugin
{
    public partial class Form1 : Form
    {
        // Add these two lines
        private Mach4.IMach4 Mach = null;
        private Mach4.IMyScriptObject Script = null;

        public Form1()
        {
            InitializeComponent();
        }

        // This is the button handler. Make it look like this.
        private void button1_Click(object sender, EventArgs e)
        {
            Mach = (Mach4.IMach4)Marshal.GetActiveObject("Mach4.Document");
            Script = (Mach4.IMyScriptObject)Mach.GetScriptDispatch();

            //_scripter.SetDRO(1, 3.3);
            Script.Code("G1 X2 Y2 F400");

            while (Script.IsMoving() != 0)
            {
                System.Threading.Thread.Sleep(25);
            }

            Script.Code("G1 X6 Y3 F400");
            while (Script.IsMoving() != 0)
            {
                System.Threading.Thread.Sleep(25);
            }
        }
    }
}



Title: Re: VS2008 Add Reference
Post by: DiyAddict on October 23, 2010, 08:41:50 AM
Congrats on achieving this with VS2008. Has anyone managed to do something similar with VB6?