1 Reply Latest reply on Dec 1, 2015 2:05 AM by christopher.branch

    Hooking on open ViewDraw or Expedition in C#

    christopher.branch

      Hi evory one,

       

      I have ben Scripting for expedition for a wile and I always assumed that there is just one View draw process running at once and if there were more I just told the user to close one.

      In the new VX there are now always two processes named View draw running.

      the question now is which one to Hook on to ?

      and

      How do you fellow users ensure that your c# script connects to the right Application if there is more than one open dxDesigner ?

       

       

      I access dxd usually like this

       

       

      or

       

      Dos any one know how to acces the ViewDraw application if i loop throw all open ViewDraw Processes

       

       

      Thank for any help

        • 1. Re: Hooking on open ViewDraw or Expedition in C#
          christopher.branch

          Hi I found a way to do it.

           

          I wrote a class which will get me a list of Viewdraw or Expedition Objects.

          after that i loop throw all the instances and look for a temp file i wrote wenn the script was called.

           

          returns the List of ComObj

          '--------------------------------------------------------------------------------------------------------------------------------

          using System;

          using System.Collections.Generic;

          using System.Linq;

          using System.Text;

          using System.IO;

          using System.Runtime.InteropServices;

          using System.Runtime.InteropServices.ComTypes;

           

          namespace EditProj

          {

              public static class ROT

              {

           

                  [DllImport("ole32.dll")]

           

                  static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);

           

                  [DllImport("ole32.dll")]

           

                  public static extern void GetRunningObjectTable(int reserved, out IRunningObjectTable prot);

           

                  // Requires Using System.Runtime.InteropServices.ComTypes

           

                  // Get all running instance by querying ROT

           

                  public static List<object> GetRunningInstances(string progName)

                  {

                      // get Running Object Table ...

                      IRunningObjectTable Rot = null;

           

                      GetRunningObjectTable(0, out Rot);

           

                      if (Rot == null)

                      {

                          return null;

                      }

           

                      // get enumerator for ROT entries

                      IEnumMoniker monikerEnumerator = null;

           

                      // returns a pointer to an enumerator that can list the monikers of all the objects currently registered in the running object table (ROT).

                      Rot.EnumRunning(out monikerEnumerator);

           

                      if (monikerEnumerator == null)

                      {

                          return null;

                      }

           

                      // Resets the enumeration sequence to the beginning.

                      monikerEnumerator.Reset();

           

           

                      // List of ViewDraw Instances

                      List<object> instances = new List<object>();

           

                      IntPtr pNumFetched = new IntPtr();

           

                      // Monikers are used as the basis for linking in COM. A linked object contains a moniker that identifies its source.

                      // When the user activates the linked object to edit it, the moniker is bound; this loads the link source into memory.

                      IMoniker[] monikers = new IMoniker[1];

                      List<string> test = new List<string>();

                      // go through all entries and identifies app instances

                      while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)

                      {

                          //

                          IBindCtx bindCtx;

           

                          CreateBindCtx(0, out bindCtx);

           

                          if (bindCtx == null)

                          {

                              continue;

                          }

           

                          // Get the DiyplayName of all Objects in ROT Table

                          monikers[0].GetDisplayName(bindCtx, null, out displayName);

           

                          // get ViewDraw Instances = ViewDraw.Application

                          if (displayName.Contains(progName))

                          {

                              object ComObject;

           

                              Rot.GetObject(monikers[0], out ComObject);

           

                              if (ComObject == null)

                              {

                                  continue;

                              }

           

                              instances.Add(ComObject);

                          }

                      }

                      return instances;

                  }

              }

          }

           

          loops throw tha open Instances

          '-------------------------------------------------------------------

          public static MGCPCB.Application getExpedition()

          {

              // Get a list with objects of the Viewdraw Instances

              List<object> XpedInst = ROT.GetRunningInstances("ExpeditionPCBApplication");        <-- to get ViewDraw use "Viewdraw.Application"

           

               MGCPCB.Application pcbApp = null;

              //ViewDraw.Application DxdApp = null;

           

              // Check if there are several Viewdraw instances open

              if (XpedInst.Count() < 2)

              {

                  try

                  {

                      // Get active Xped App

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

                      //DxdApp = (ViewDraw.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("ViewDraw.Application");

                  }

                  catch

                  {

                      return null;

                  }

                  return pcbApp;

              }

              else

              {

                  foreach (Object obj in XpedInst)

                 {

                      try

                      {

                          // get ViewDraw.Application object

                          pcbApp = (MGCPCB.Application)obj as MGCPCB.Application;

                          //DxdApp = (ViewDraw.Application)obj as ViewDraw.Application;

           

                          // Check if the referenc file is there

                          if ( " if Temp File exists " )

                          {

                              return pcbApp;

                          }

                       }

                       catch

                       {

                           return null;

                       }

                   }

               }

               return null;

          }

           

          Have fun

           

          Christopher