Using Inventor View from 64 and 32 bit processes

<?xml encoding=”UTF-8″>By Adam Nagy

As mentioned in this blog post you need to use some workaround to get the Inventor View 2015 component to show more than just a blank screen with a toolbar. 

You can set the PATH either system wide in System Properties >> Environment Variables, or just for your own application using System.Environment.SetEnvironmentVariable()

If you want to use Any CPU compilation for your application so that on a 32 bit OS it runs as a 32 bit process and on a 64 bit OS it runs as a 64 bit process, then you can just set the PATH variable to <Inventor install path>Bin

However, if you want to compile your application with the x86 platform option so that it always runs as a 32 bit process no matter what OS it is on, then you could take advantage of the Environment.Is64BitOperatingSystem property introduced in .NET Framework 4.0, and could set the PATH variable based on the OS. To make the code work for all combinations of OS’s and process bitnesses, you could use this in combination with Environment.Is64BitProcess:

public void AddInventorPath()
{
  // Note: System.Environment.Is64BitProcess and
  // System.Environment.Is64BitProcess were
  // introduced in .NET Framework 4.0
  string path =
    System.
Environment.GetEnvironmentVariable(“PATH”);

  // In case process and OS bitness match it’s
  // C:Program FilesAutodeskInventor 2015
  // otherwise it’s
  // C:Program FilesAutodeskInventor 2015bin
  string inventorPath = m_oserver.InstallPath;

  if (System.Environment.Is64BitOperatingSystem &&
      !System.Environment.Is64BitProcess)
  {
    // If you are running the app as a 32 bit process
    // on a 64 bit OS then you’ll need this
    path += “;” + inventorPath + “Bin32”;
  }
  else
  {
    // Otherwise you need this
    path += “;” + inventorPath + “Bin”;
  }

  System.Environment.SetEnvironmentVariable(“PATH”, path);
}

public Form1()
{
  // Try to create an instance of apprentice server
  try
  {
    m_oserver = newApprenticeServerComponent();
    AddInventorPath();
  }
  catch (SystemException exception)
  {
    MessageBox.Show(this,
      “Failed to create an instance of Apprentice server.”,
      “CSharpFileDisplaySample”,
      MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return;
  }

  m_odocument = null;
  m_oview = null;
  m_ocamera = null;
  m_bmouseDown = false;

  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
}

I started with the C:UsersPublicDocumentsAutodeskInventor 2015SDKDeveloperToolsSamplesVCSharp.NETStandalone ApplicationsApprenticeServerFileDisplay sample as a test, but got this error when running the application if I changed the project’s Target framework to .NET Framework 4:

System.IO.FileNotFoundException was unhandled
HResult=-2147024894
Message=Could not load file or assembly 'Interop.InventorApprentice, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

Error

I had the same issue even after changing the Interop.InventorViewControlLib assembly’s Embed Interop Types to False. So I created new interop assemblies for the Inventor View component following this info: http://adndevblog.typepad.com/autocad/2012/07/use-64-bit-activex-component-from-a-net-assembly.html

Aximp

Then I replaced the references in the project with the newly created ones, but still got the same error. However, once I changed the InventorViewControlLib assembly’s Embed Interop Types to False the error went away.

EmbedInteropTypes

See the sample in action:

Sample project: https://github.com/adamenagy/InventorView-FileDisplay-.NET-4.0 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading