Hello Guest it is April 19, 2024, 04:35:49 PM

Author Topic: Automating Mach from external software  (Read 18740 times)

0 Members and 1 Guest are viewing this topic.

Automating Mach from external software
« on: January 23, 2008, 09:48:50 PM »
I have just been introduced to the Mach3 software and am looking for ways of automating it from external software.

In skimming over the manual, I noticed in section 3.1.3, it states that using the OCX it is possible to control Mach3 without its GUI.  Where can I find more information on this?

After a few hours of digging around I've come across talk about Plugins, but can't find any "Getting Started" documentation (other than how to setup Visual Studio).  At first impression, "plugin" sounds like something used WITHIN Mach3 (and it's GUI), rather than something to control it externally.

Any good pointers on where to start?  I'm comfortable with most any development language/environment.

Any assistance greatly appreciated,
Dave

Offline kcrouch

*
  •  193 193
  • In way too deep!!!
    • View Profile
Re: Automating Mach from external software
« Reply #1 on: January 23, 2008, 09:55:58 PM »
Dave,
Have You looked at the Mach3 SDK about half way down the downloads page? It may help.
Kenny
Having way too much fun! Something must surely be wrong.
Re: Automating Mach from external software
« Reply #2 on: January 24, 2008, 11:31:40 AM »
Hi, look at the version 2.0 programmer information on this page:

http://www.machsupport.com/artsoft/downloads/downloads.htm

Mach3 has an Automation Object Model that can be used to control it externally.  My plugin tutorial and sample file set also show how to use this from inside a plugin.  There is a 'RemoteControl' application download on the downloads page that shows how to use the object model from a C++ program (for VC++ 2003).

-James Leonard

Re: Automating Mach from external software
« Reply #3 on: January 24, 2008, 12:31:52 PM »
Thanks guys.  I guess I was looking mainly at the menus of the website and not drilling down into each page enough.

I've got both VS2005 & VS2003, so I'll give it a look.

Dave
Re: Automating Mach from external software
« Reply #4 on: January 24, 2008, 12:39:34 PM »
You are welcome!

Get the latest Mach3, it has an embedded typelib.  Then just point VS2003 at it and do a create class from typelib.  You can then see all the automation methods.  Only a plugin has access to Mach3's internal data blocks directly.

The VBscript docs and some searches on macros will give you a good start on what the object methods can be used for.

-James Leonard
Re: Automating Mach from external software
« Reply #5 on: February 16, 2008, 02:29:03 PM »
Ok, I've got my hands dirty and I'm back with a few questions...

First, thanks James for your Plugin tutorial and whoever wrote MachRemote.

I've developed an application in C# (VS2005) and have been tasked with making it automate Mach3.

What I've done is created a DLL which exports many of the functions of the Mach3.exe Object Model.  I used James' Plugin Tutorial as a reference to get started.  I then imported these functions into C# and wrapped them with a C# class (which handles connection, exceptions, etc).

This isn't a plugin.  I'm not doing anything with dialogs.  I'm just setting DRO's, pushing buttons, etc.

So far it is working well except for when I shutdown my app and restart it.  After the first time I run, future runs of my app fail to connect to Mach3.  If I shutdown Mach3 and restart it, everything is fine (until I shutdown my app and restart it).

I've added some debug code and discovered subsequent runs are failing with a result code (HRESULT) of 1 from GetActiveObject(clsid,NULL,&lpUnk) (see Mach3ObjectModelStartup() in Plugin Tutorial; page 73/92 in V01).

So I started by looking at the shutdown code where I detach from Mach3.  In the plugin tutorial, it appears to only be calling "CoUninitialize();"  (see Mach3ObjectModelShutdown(VOID); page 74/92 in V01).

However, in MachRemote, I find this code in CMachRemoteDlg::OnBnClickedDisconnect():
    scripter.DetachDispatch();  //detach the scripter object
    //mach4.ShutDown(); //then shutdown MAch3's interface..

With only the CoUninitialize() call in Plugin Tutorial, Mach 3 would complain on exit with an Art Code of 9991.  After I added the scripter.DetachDispatch(), this complaint went away (mostly at least).  If I add the "mach4.ShutDown()", this creates an additional error.

Any insights on what I'm missing?
Why doesn't the Plugin Tutorial call scripter.DetachDispatch()? (oversight?  am I missing something?)
Why is mach4.ShutDown() commented out in MachRemote?

Any insights appreciated,
Dave
Re: Automating Mach from external software
« Reply #6 on: February 16, 2008, 02:35:48 PM »
Hi Dave,

It is the difference in the process models I believe.  The Mach3 plugins run in-process to Mach3 so it is too late for me to detach the dispatch interface at the point the plugin shutdown code gets called (I got a crash myself for trying it...).

The scripter.detach() call should return the IDispatch pointer, so try releasing it as so:

IDispatch tmp = scripter.detach;

tmp->Release();

-James
Re: Automating Mach from external software
« Reply #7 on: February 16, 2008, 02:53:24 PM »
The scripter.detach() call should return the IDispatch pointer, so try releasing it as so:

IDispatch tmp = scripter.detach;
tmp->Release();

James,

Here's the shutdown code I ended up with, based on your suggestion:

   LPDISPATCH tmp = scripter.DetachDispatch();  //detach the scripter object
   tmp->Release();
//   mach4.ShutDown();
   CoUninitialize();

Unfortunately this didn't appear to help.  I'm stilling getting HRESULT of 1 from GetActiveObject on my subsequent runs.
Re: Automating Mach from external software
« Reply #8 on: February 16, 2008, 02:58:29 PM »
Dave, I am totally clueless as to C#, but in VB (6) setting the object to Nothing would release all references.  It looks like you are somehow still holding a reference to Mach3.

Since you have your code in a DLL you should be sure the DLL is unloading also.  If not, maybe you have a persistant state in the DLL?

-James
Re: Automating Mach from external software
« Reply #9 on: February 16, 2008, 03:07:55 PM »
Figured it out...

I was just digging around CMach4.h and realized the ShutDown() call is to close the Mach3 app. ...not what I was trying to accomplish.

After a bit more digging, I noticed we're doing an AttachDispatch for both mach4 and scripter...but only doing the DetachDispatch for scripter.

This code is working for me well now:
     scripter.DetachDispatch();  //detach the scripter object
     mach4.DetachDispatch();
     CoUninitialize();

Thanks for your thoughts/time on a Saturday afternoon!

Dave