2 Replies Latest reply on Feb 27, 2014 8:00 AM by andrew_french

    use C# for scripting in Expedition

    christopher.branch

      Hey every one

       

      i wouled like to start Scripting for expedition in C#

      so i am loking for some sample scripts for C# like the Via counter Script witch Mentor provides.

      or i am glad for any typ a help to get startet with scripting in C#

       

      Thanks

      Regards Christoher

        • 1. Re: use C# for scripting in Expedition
          kendall_hiles

          Christopher;

           

          In the AATK/Examples directory there is a C# example that Mike Walsh wrote and he seems to like C#.  The rest of us write everything for AATK in VBS or put the VBS in a form because it can run on all platforms and does not have to be recompiled for different releases of the tools. I posted a video on how to write VBS scripts with the free Micosoft VBA debugger in Excel. If your new with scripting I would go down the VB route, if C# is your go to language you'll figure it out because it uses COM just like all the Microsoft tools.

           

          -kendall

          • 2. Re: use C# for scripting in Expedition
            andrew_french

            I highly prefer to use C# for my code and I support a windows only environment which makes this workable for me.  I have a class written that I use to connect to Expedition for all my code and then in my form1_loading function I make the calls to the class.  I also use form1 closing to drop the COM connections.  I don't bother dealing with more than one instance of Expedition yet, just haven't had time to bother with it yet.  Theres a few of us on here that use C# so go ahead and ask your questions.

             

            MGCPCB.Application ExpApp;   //variable to be used to access controls for Expedition

            MGCPCB.Document ExpDoc;      //variable to be used to access controls for the project open in Expedition

             

            Form1_loading

            {

            try

                        {

                            ExpApp = GetExp.getExpedition();

                            ExpDoc = GetExp.getExpProject(ExpApp);

                            GetExp.getAutomationLic(ExpDoc);

             

                        }

                        catch (Exception ex)

                        {

                            MessageBox.Show(ex.Message.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                            Application.Exit();

                        }

            }

             

            class GetExp

                {

                    /// <summary>

                    /// Gets the currently running instance of Expedition

                    /// </summary>

                    /// <returns>COM variable for Expedition Application</returns>

                    public static MGCPCB.Application getExpedition()

                    {

                        try

                        {

                            MGCPCB.Application app = (MGCPCB.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("MGCPCB.Application");

                            return app;

                        }

                        catch { throw new ArgumentException("Running instance of Expedition not found!"); }

             

                    }

             

                    /// <summary>

                    /// Gets the currently open project in the running instance of Expedition

                    /// </summary>

                    /// <param name="app">The current instance of Expedtion</param>

                    /// <returns>COM object for Expedition project</returns>

                    public static MGCPCB.Document getExpProject(MGCPCB.Application app)

                    {

                        //Creates hook into Expedition Project

                        MGCPCB.Document doc = app.ActiveDocument;

                        if (doc == null)

                        {

                            throw new ArgumentNullException("No Active Project Open!");

                        }

                        return doc;

                    }

             

                    /// <summary>

                    /// Licenses the current Expedition project for Automation

                    /// </summary>

                    /// <param name="doc"></param>

                    public static void getAutomationLic(MGCPCB.Document doc)

                    {

                        //Verifies an automation licesnse is available

                        long key;

                        long licenseToken;

             

                        key = doc.Validate(0);

                        MGCPCBAutomationLicensing.Application licenseServer = new MGCPCBAutomationLicensing.Application();

                        licenseToken = licenseServer.GetToken((int)key);

                        doc.Validate((int)licenseToken);

             

                    }

             

                    /// <summary>

                    /// Release the passed COM object

                    /// </summary>

                    /// <param name="obj"></param>

                    public static void dropCom(object obj)

                    {

                        try

                        {

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

                            obj = null;

                        }

                        catch { obj = null; }

                    }

             

                }