10 Replies Latest reply on Jan 21, 2013 3:23 AM by Klaas Oudejans

    Add-in error when calling 'OnCmd' method

    Klaas Oudejans

      Dear readers,

       

      I have build an add-in for our Enterprise PDM setup. We use it with Inventor. On releasing files, I want this add-in to export pdf files for all drawings in the release batch.

       

      I got this add-in to work, but it only seems to work once. The next time I run it, I get an error stating that:

       

      "Call to method 'OnCmd' in add-in 'ExportPFFiles', written by company 'Intralox' failed. Error cod = 0x080131603 (Unknown error 0x80131603).

       

      But at some point, this error message will not show, and I will be able to run it once again. I don't know where or when this reset happens.

       

      I also sometimes get an error message about the RPC server being unavailable.

       

      I have no idea where this is going wrong, and I am hoping that somebody can point me in a direction of what can be causing these errors.

       

      I am using Visual C#.

       

      This is how I started my code:

       

      using System;

      using System.IO;

      //using System.Windows.Forms;

      using System.Collections.Generic;

      using System.Text;

      using EdmLib;

      using Inventor;

       

       

      namespace ExportPDFFiles

      {

          public class Addin : IEdmAddIn5

          {

              public Inventor.Application oApp;

              public Inventor.AssemblyDocument oAssyDoc;

       

       

              public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 vault, IEdmCmdMgr5 cmdMgr)

              {

                  poInfo.mbsAddInName = "ExportPDFFiles";

                  poInfo.mbsCompany = "Intralox";

                  poInfo.mbsDescription = "Exports all .pdf files that are related to the assembly on which this add-in is executed";

                  poInfo.mlAddInVersion = 1;

                  poInfo.mlRequiredVersionMajor = 5;

                  poInfo.mlRequiredVersionMinor = 2;

                  //This add-in is reached through a hook. Everytime a file passed to a new state, this add-in is called.

                  cmdMgr.AddHook(EdmCmdType.EdmCmd_PostState);

              }

       

       

              public void OnCmd(ref EdmCmd edmCmd, ref Array data)

              {

                  try

                  {

                      if (data == null)

                          return;

                      //Get the current vault

                      IEdmVault11 vault = (IEdmVault11)edmCmd.mpoVault;

       

                      Boolean bFilesPDFHandled = false;

       

       

                      // this doesn't do much. Everytime the sequence is here, it is called by the poststate trigger / hook

                      if (edmCmd.meCmdType == EdmCmdType.EdmCmd_PostState)

                     

                     And so forth

        • Re: Add-in error when calling 'OnCmd' method
          Jim Sculley

          Have you tried running it in the debugger?  Set a breakpoint in the OnCmd method and step through it until the crash.  You will at least get a better idea of where the error is coming from.

           

          Jim S.

            • Re: Add-in error when calling 'OnCmd' method
              Klaas Oudejans

              Yes, I tried running it in the debugger, but I'm not able to make any breakpoints in there. It won't stop on them. I'm using Visual C# Studio 2010 Express.

              Any tips on that matter?

                • Re: Add-in error when calling 'OnCmd' method
                  Jim Sculley

                  To debug an EPDM add-in you have to install it as a debug add-in in the vault:

                   

                  1.  Right click on 'Add-ins in the vaultr admin tool and select Debug Add-ins.  Click the Add Add-in button, browse to the add-in DLL created by Visual C# Express, click Open and then click OK.  This will add  the add-in to the vault in debug mode.

                   

                  2.  Next, you have to tell Visual C# Express what application to execute when debugging by creating a user defined project configuration file.  To do this, find the .csproj file associated with your project and make a new file in the same directory with a .user extension.  So, if your project was called Foo, you would find the Foo.csproj file and make a new file called Foo.csproj.user in the same directory.

                   

                  The new file should contain something like this:

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

                    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

                      <StartAction>Program</StartAction>

                      <StartProgram>C:\Program Files\SolidWorks Corp\SolidWorks\SLDWORKS.exe</StartProgram>

                    </PropertyGroup>

                    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

                      <StartAction>Program</StartAction>

                      <StartProgram>C:\Program Files\SolidWorks Corp\SolidWorks\SLDWORKS.exe</StartProgram>

                    </PropertyGroup>

                    <PropertyGroup>

                      <ProjectView>ProjectFiles</ProjectView>

                    </PropertyGroup>

                  </Project>

                   

                  Verify that the path to SolidWorks is correct, or substitute a different app (i.e. Inventor) if necessary.

                   

                  3. Save the file.

                  4. Put a break point on the first line in the OnCmd method.

                  5. Restart Visual C# Express. 

                  6. Right click your project and select Debug...Start New Instance

                   

                  SolidWorks (or whatever app you specified) should start and whwen you do something that triggers the add-in (i.e. state change) the debugger should stop at your breakpoint.

                   

                  Jim S.

                    • Re: Add-in error when calling 'OnCmd' method
                      Klaas Oudejans

                      Wow! Thanks Jim!

                       

                      I will try this today, I see that I already have a cspoj.user filer though. This is the content:

                       

                      <?xml version="1.0" encoding="utf-8"?>

                      <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

                        <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">

                          <StartWorkingDirectory>C:\Users\koudejan\Documents\Visual Studio 2010\Projects\ExportPDFFiles\ExportPDFFiles\bin\Release\</StartWorkingDirectory>

                        </PropertyGroup>

                        <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">

                          <StartWorkingDirectory>C:\Users\koudejan\Documents\Visual Studio 2010\Projects\ExportPDFFiles\ExportPDFFiles\bin\Debug\</StartWorkingDirectory>

                        </PropertyGroup>

                      </Project>

                       

                      Should I delete all that? By the way, I have added this add-in to the debugger environment in EPDM, and when I release files, the hook works, and it does start the add-in, but I just cannot break it.

                      But I will try this

                        • Re: Add-in error when calling 'OnCmd' method
                          Jim Sculley

                          Klaas Oudejans wrote:

                           

                          Wow! Thanks Jim!

                           

                          I will try this today, I see that I already have a cspoj.user filer though. This is the content:

                           

                          <?xml version="1.0" encoding="utf-8"?>

                          <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

                            <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">

                              <StartWorkingDirectory>C:\Users\koudejan\Documents\Visual Studio 2010\Projects\ExportPDFFiles\ExportPDFFiles\bin\Release\</StartWorkingDirectory>

                            </PropertyGroup>

                            <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">

                              <StartWorkingDirectory>C:\Users\koudejan\Documents\Visual Studio 2010\Projects\ExportPDFFiles\ExportPDFFiles\bin\Debug\</StartWorkingDirectory>

                            </PropertyGroup>

                          </Project>

                           

                          Should I delete all that? By the way, I have added this add-in to the debugger environment in EPDM, and when I release files, the hook works, and it does start the add-in, but I just cannot break it.

                          But I will try this

                          No need to delete anything.  Just add the

                           

                          <StartAction>Program</StartAction>

                              <StartProgram>C:\Program Files\SolidWorks Corp\SolidWorks\SLDWORKS.exe</StartProgram>

                           

                          lines before each of the </PropertyGroup> tags.

                           

                          When your add-in code is run, are the breakpoints in the debugger solid red dots or are they clear dots with a yellow exclamation point?  If they aren't red dots, then your code isn't running in the debugger.  If you have the add-in loaded as a normal add-in, you should remove it so that only the Debug add-in is present.  Some other questions:

                           

                          Did you build the project in Debug mode?

                           

                          Did you select the correct DLL file when telling the Admin tool where to find your add-in?  You typically want the DLL in the bin/Debug folder of your project.

                           

                          Jim S.

                  • Re: Add-in error when calling 'OnCmd' method
                    Lee CS Young

                    The code you've posted wouldn't cause an error. Any chance you could post your full code?

                     

                    My best guess: Are you double checking that the file you're trying to convert is supported by Inventor?

                      • Re: Add-in error when calling 'OnCmd' method
                        Klaas Oudejans

                        Sure I can post it    This is the code where it went wrong. I've been looking into this matter, and would it be possible that it has something to do with releasing COM objects?

                         

                        I use two public objects, could it be they cause the RPC server to hang or something like that?

                         

                         

                        using System;

                        using System.IO;

                        using System.Windows.Forms;

                        using System.Collections.Generic;

                        using System.Text;

                        using EdmLib;

                        using Inventor;

                         

                         

                        namespace ExportPDFFiles

                        {

                            public class Addin : IEdmAddIn5

                            {

                                public Inventor.Application oApp;

                                public Inventor.AssemblyDocument oAssyDoc;

                         

                         

                                public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 vault, IEdmCmdMgr5 cmdMgr)

                                {

                                    poInfo.mbsAddInName = "ExportPDFFiles";

                                    poInfo.mbsCompany = "Intralox";

                                    poInfo.mbsDescription = "Exports all .pdf files that are related to the assembly on which this add-in is executed";

                                    poInfo.mlAddInVersion = 1;

                                    poInfo.mlRequiredVersionMajor = 5;

                                    poInfo.mlRequiredVersionMinor = 2;

                         

                                    //This add-in is reached through a hook. Everytime a file passed to a new state, this add-in is called.

                                    cmdMgr.AddHook(EdmCmdType.EdmCmd_PostState);

                                   

                         

                         

                                }

                         

                         

                                public void OnCmd(ref EdmCmd edmCmd, ref Array data)

                                {

                         

                         

                                    if (data == null)

                                        return;

                                    //Get the current vault

                                    IEdmVault11 vault = (IEdmVault11)edmCmd.mpoVault;

                         

                                    //if (vault.MsgBox(0, "The Export PDF script is being started, continue? ", EdmMBoxType.EdmMbt_YesNo, "Run PDF script warning") == EdmMBoxResult.EdmMbr_No)

                                    //{

                                    //    return;

                                    //}

                                    Boolean bFilesPDFHandled = false;

                                   

                                    // this doesn't do much. Everytime the sequence is here, it is called by the poststate trigger / hook

                                    if (edmCmd.meCmdType == EdmCmdType.EdmCmd_PostState)

                                    {

                         

                         

                                        //The user action for changing file states has data attached to his call. This data is stored in the cmData array

                                        foreach (EdmCmdData cmdData in data)

                                        {

                         

                         

                                            try

                                            {

                                                //for every line in the data arrya, we do these actions:

                                                //if the status that we are being pushed to, is the "released status" then we need to do something.

                                                //If not, we need to do nothing.

                                                //These are objects in the data array

                                                IEdmFile5 file = (IEdmFile5)vault.GetObject(EdmObjectType.EdmObject_File, cmdData.mlObjectID1);

                         

                         

                                                IEdmPos5 pos = file.GetFirstFolderPosition();

                         

                         

                                                IEdmFolder5 folder = (IEdmFolder5)file.GetNextFolder(pos);

                          

                                                //This is the file name of the EPDM file object.

                                                string sFileName = folder.LocalPath + @"\" + file.Name;

                                                //MessageBox.Show(sFileName);

                                                //These are booleans to control whether we need to update these files or not.

                                                Boolean bPartInProject = true;

                                                Boolean bUnsuitedFile = true;

                                                string sExtension = System.IO.Path.GetExtension(sFileName);

                                                //MessageBox.Show(sExtension);

                                                //If this is an ".idw" it is a suited file for this sequence.

                                                if (sExtension.Contains("idw"))

                                                {

                                                    bUnsuitedFile = false;

                                                }

                                                try

                                                {

                                                    if (!bUnsuitedFile)

                                                    {

                         

                         

                                                        if (cmdData.mbsStrData2.Equals("Released"))

                                                        {

                                                            //MessageBox.Show(cmdData.mbsStrData2);

                                                            //We also want to skip library files.

                                                            bPartInProject = folder.LocalPath.Contains(@"\PROJECTS\");

                                                            //If all that is true, we do the actual actions.

                                                            //MessageBox.Show(bPartInProject.ToString() + " should we handle this file?");

                                                            if (bPartInProject)

                                                            {

                                                                if (!bFilesPDFHandled)

                                                                {

                                                                    //vault.MsgBox(0, "The Export PDF script is being started", EdmMBoxType.EdmMbt_OKOnly, "Run PDF script warning");

                                                                }

                                                                bFilesPDFHandled = true;

                                                                if (oApp == null)

                                                                {

                                                                    //RUN THE CAD MANAGER;

                                                                    AISCadManager();

                                                                }

                         

                         

                                                                // MessageBox.Show("Cad manager started");

                                                                //Open the file in ivnenotr

                                                                oApp.Documents.Open(sFileName);

                         

                         

                                                                Inventor.TranslatorAddIn oTranslator = (Inventor.TranslatorAddIn)oApp.ApplicationAddIns.get_ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}");

                                                                //string sBla = oTranslator.Type.ToString();

                                                                //MessageBox.Show("Translator object created");

                                                                Inventor.TransientObjects oTranslatorObjects = oApp.TransientObjects;

                                                                Inventor.TranslationContext oTranslatorContext = oTranslatorObjects.CreateTranslationContext();

                                                                Inventor.NameValueMap oTranslatorNameValueMap = oTranslatorObjects.CreateNameValueMap();

                                                                Inventor.DataMedium oDataMedium = oTranslatorObjects.CreateDataMedium();

                                                                //MessageBox.Show("all translator options set");

                                                                if (oTranslator.get_HasSaveCopyAsOptions(oApp, oTranslatorContext, oTranslatorNameValueMap))

                                                                {

                                                                    oTranslatorNameValueMap.set_Value("Sheet_Range", Inventor.PrintRangeEnum.kPrintAllSheets);

                                                                    oTranslatorContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism;

                         

                         

                                                                    string sDocNamePath = System.IO.Path.GetFileName(sFileName);

                                                                    string sPath = System.IO.Path.GetFullPath(sFileName);

                                                                    string sLastCut = "";

                                                                    while (!sPath.EndsWith(@"PROJECTS"))

                                                                    {

                                                                        sLastCut = sPath.Substring(sPath.LastIndexOf(@"\") + 1);

                                                                        //MessageBox.Show(sLastCut);

                                                                        sPath = sPath.Substring(0, sPath.LastIndexOf(@"\"));

                                                                    }

                                                                    //MessageBox.Show("Uit de loop; sPath = " + sPath + " slastcut = " + sLastCut);

                                                                    sPath = sPath +@"\" + sLastCut;

                                                                    //MessageBox.Show(sPath);

                         

                         

                                                                    //MessageBox.Show("De top project folder for this file is:  "+sPath);

                                                                    //Allways go to: PROJECT_FOLDER/CAD/EXCHANGE/DXF/ for the export location.

                                                                    //First check if the folders exist, if not, create them.

                                                                    IEdmFolder5 top_project_folder = vault.GetFolderFromPath(sPath);

                                                                    //MessageBox.Show(top_project_folder.Name);

                         

                         

                                                                    //IEdmFolder5 sub_folder_cad;

                                                                    IEdmFolder5 sub_folder_exchange;

                                                                    IEdmFolder5 sub_folder_pdf;

                                                                    try

                                                                    {

                                                                        try

                                                                        {

                                                                            sub_folder_exchange = top_project_folder.GetSubFolder("EXCHANGE");

                                                                            //System.Windows.Forms.MessageBox.Show("Deze folder was er al: " + sub_folder_exchange.Name);

                                                                        }

                                                                        catch (Exception)

                                                                        {

                                                                            sub_folder_exchange = top_project_folder.AddFolder(top_project_folder.ID, "EXCHANGE");

                         

                         

                                                                            //System.Windows.Forms.MessageBox.Show("Deze folder was er nog niet: " + sub_folder_exchange.Name);

                                                                        }

                                                                        try

                                                                        {

                                                                            sub_folder_pdf = sub_folder_exchange.GetSubFolder("PDF");

                                                                            //System.Windows.Forms.MessageBox.Show("Deze folder was er al: " + sub_folder_dxf.Name);

                                                                        }

                                                                        catch (Exception)

                                                                        {

                                                                            sub_folder_pdf = sub_folder_exchange.AddFolder(sub_folder_exchange.ID, "PDF");

                                                                            //System.Windows.Forms.MessageBox.Show("Deze folder was er nog niet: " + sub_folder_dxf.Name);

                                                                        }

                                                                        //Put the export filename in the retrieved or created folder.

                                                                        string sNewFileName = sub_folder_pdf.LocalPath + @"\" + System.IO.Path.GetFileNameWithoutExtension(sFileName) + "_rev" + file.CurrentRevision + ".pdf";

                                                                        //MessageBox.Show("nieuwe filename is: "+sNewFileName);

                         

                         

                                                                        oDataMedium.FileName = sNewFileName;

                                                                        oTranslator.SaveCopyAs(oApp.ActiveDocument, oTranslatorContext, oTranslatorNameValueMap, oDataMedium);

                                                                        oApp.ActiveDocument.Close(true);

                                                                    }

                                                                    catch (Exception)

                                                                    {

                                                                    }

                                                                }//if (oTranslator.get_HasSaveCopyAsOptions(oApp, oTranslatorContext, oTranslatorNameValueMap))

                                                                try

                                                                {

                                                                    oApp.ActiveDocument.Close(true);

                                                                }

                                                                catch (Exception)

                                                                {

                         

                         

                         

                         

                                                                }

                                                            }//if (bPartInProject)

                                                        }//if (cmdData.mbsStrData2.Equals("Released"))

                                                        //MessageBox.Show("dit was een suited file, maar geen released status");

                                                    }//if (!bUnsuitedFile)

                                                    //MessageBox.Show("dit was geen suited file, alles overgeslagen");

                                                }

                                                catch (Exception)

                                                {

                                               

                                                }

                                            }

                                            catch (Exception)

                                            {

                                               

                         

                         

                                            }

                         

                         

                                        }

                                    }

                                    if (oApp!=null)

                                          {

                                        oApp.Quit();

                                          }

                                    else

                                    {

                                        //MessageBox.Show("Het oApp object was leeg");

                                    }

                                    if (bFilesPDFHandled)

                                    {

                                        //vault.MsgBox(0, "The Export PDF script is done", EdmMBoxType.EdmMbt_OKOnly, "Run PDF script finished");

                                    }

                                   

                         

                         

                                }

                         

                         

                                public void AISCadManager()

                                {

                                    try

                                    {

                                        //// Attempt to get a reference to a running instance of Inventor.

                                        //oApp = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");

                                        //oApp.Visible = false;

                                        ////System.Windows.Forms.MessageBox.Show(" Inventor Gevonden");

                                        //oApp.SilentOperation = true;

                                    }

                                    catch

                                    {

                         

                         

                                    }

                                    if (oApp == null)

                                    {

                                        try

                                        {

                                            // Start Inventor.

                                            System.Type oType = System.Type.GetTypeFromProgID("Inventor.Application");

                                            oApp = (Inventor.Application)System.Activator.CreateInstance(oType);

                                            //System.Windows.Forms.MessageBox.Show("Inventor geopend");

                                            // Make Inventor visible.

                                            oApp.Visible = false;

                                            oApp.SilentOperation = true;

                                        }

                                        catch

                                        {

                         

                         

                                        }

                                    }

                                    try

                                    {

                         

                         

                                    }

                                    catch

                                    {

                         

                         

                                    }

                                }

                            }

                        }

                          • Re: Add-in error when calling 'OnCmd' method
                            Lee CS Young

                            IEdmFile5 file = (IEdmFile5)vault.GetObject(EdmObjectType.EdmObject_File, cmdData.mlObjectID1);

                             

                            'file' could be null if the "file" is a cutlist item or an item. The error you posted sounds to me like you're trying to open a file Inventor doesn't support which could be an item. The app might be hanging on that which could cause the RPC error.

                             

                            // Double check.

                             

                            if(file != null)

                            {

                            }

                             

                            From this line: oApp.Documents.Open(sFileName)

                             

                            I would put the rest into another method and make sure oApp is valid before calling the method.

                             

                            You mentioned that your breakpoints aren't being hit when debugging. Are you adding the add-in to the debugging addins dialog?

                              • Re: Add-in error when calling 'OnCmd' method
                                Klaas Oudejans

                                Good tip! Thanks, I'll implement this. Something that I've tested with is to get the add-in to give some feedback when it's finished. So I figured, if it got to the finishing point, it would have run succesfully without errors.

                                 

                                What I changed yesterday in my code, is that it no longer works with public objects. Up until now, I haven't received the same error messages anymore. Maybe I got lucky...

                                 

                                 

                                using System;

                                using System.IO;

                                //using System.Windows.Forms;

                                using System.Collections.Generic;

                                using System.Text;

                                using EdmLib;

                                using Inventor;

                                 

                                 

                                namespace ExportPDFFiles

                                {

                                    public class Addin : IEdmAddIn5

                                    {

                                 

                                 

                                 

                                 

                                        public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 vault, IEdmCmdMgr5 cmdMgr)

                                        {

                                            poInfo.mbsAddInName = "ExportPDFFiles";

                                            poInfo.mbsCompany = "Intralox";

                                            poInfo.mbsDescription = "Exports all .pdf files that are related to the assembly on which this add-in is executed";

                                            poInfo.mlAddInVersion = 2;

                                            poInfo.mlRequiredVersionMajor = 5;

                                            poInfo.mlRequiredVersionMinor = 2;

                                            //This add-in is reached through a hook. Everytime a file passed to a new state, this add-in is called.

                                            cmdMgr.AddHook(EdmCmdType.EdmCmd_PostState);

                                        }

                                 

                                 

                                        public void OnCmd(ref EdmCmd edmCmd, ref Array data)

                                        {

                                            Inventor.Application oApp=null;

                                           // Inventor.AssemblyDocument oAssyDoc;

                                            try

                                            {

                                                if (data == null)

                                                    return;

                                                //Get the current vault

                                                IEdmVault11 vault = (IEdmVault11)edmCmd.mpoVault;

                                                Boolean bFilesPDFHandled = false;

                                 

                                 

                                                // this doesn't do much. Everytime the sequence is here, it is called by the poststate trigger / hook

                                                if (edmCmd.meCmdType == EdmCmdType.EdmCmd_PostState)

                                                {

                                 

                                 

                                                    //The user action for changing file states has data attached to his call. This data is stored in the cmData array

                                                    foreach (EdmCmdData cmdData in data)

                                                    {

                                 

                                 

                                                        try

                                                        {

                                                            //for every line in the data arrya, we do these actions:

                                                            //if the status that we are being pushed to, is the "released status" then we need to do something.

                                                            //If not, we need to do nothing.

                                                            //These are objects in the data array

                                                            IEdmFile5 file = (IEdmFile5)vault.GetObject(EdmObjectType.EdmObject_File, cmdData.mlObjectID1);

                                 

                                 

                                                            IEdmPos5 pos = file.GetFirstFolderPosition();

                                 

                                 

                                                            IEdmFolder5 folder = (IEdmFolder5)file.GetNextFolder(pos);

                                 

                                 

                                                            //This is the file name of the EPDM file object.

                                                            string sFileName = folder.LocalPath + @"\" + file.Name;

                                                            //MessageBox.Show(sFileName);

                                                            //These are booleans to control whether we need to update these files or not.

                                                            Boolean bPartInProject = true;

                                                            Boolean bUnsuitedFile = true;

                                                            string sExtension = System.IO.Path.GetExtension(sFileName);

                                                            //MessageBox.Show(sExtension);

                                                            //If this is an ".idw" it is a suited file for this sequence.

                                                            if (sExtension.Contains("idw"))

                                                            {

                                                                bUnsuitedFile = false;

                                                            }

                                                            try

                                                            {

                                                                if (!bUnsuitedFile)

                                                                {

                                 

                                 

                                                                    if (cmdData.mbsStrData2.Equals("Released"))

                                                                    {

                                                                        //We also want to skip library files.

                                                                        bPartInProject = folder.LocalPath.Contains(@"\PROJECTS\");

                                                                        //If all that is true, we do the actual actions.

                                                                        //MessageBox.Show(bPartInProject.ToString() + " should we handle this file?");

                                                                        if (bPartInProject)

                                                                        {

                                                                            if (!bFilesPDFHandled)

                                                                            {

                                                                                //vault.MsgBox(0, "The Export PDF script is being started", EdmMBoxType.EdmMbt_OKOnly, "Run PDF script warning");

                                                                            }

                                                                            bFilesPDFHandled = true;

                                                                            if (oApp == null)

                                                                            {

                                                                                try

                                                                                {

                                                                                    // Start Inventor.

                                                                                    System.Type oType = System.Type.GetTypeFromProgID("Inventor.Application");

                                                                                    oApp = (Inventor.Application)System.Activator.CreateInstance(oType);

                                                                                    //System.Windows.Forms.MessageBox.Show("Inventor geopend");

                                                                                    // Make Inventor visible.

                                                                                    oApp.Visible = false;

                                                                                    oApp.SilentOperation = true;

                                                                                }

                                                                                catch

                                                                                {

                                                                                    return;

                                                                                }

                                                                            }

                                                                            // MessageBox.Show("Cad manager started");

                                                                            //Open the file in ivnenotr

                                                                            oApp.Documents.Open(sFileName);

                                                                            Inventor.TranslatorAddIn oTranslator = (Inventor.TranslatorAddIn)oApp.ApplicationAddIns.get_ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}");

                                                                            //string sBla = oTranslator.Type.ToString();

                                                                            //MessageBox.Show("Translator object created");

                                                                            Inventor.TransientObjects oTranslatorObjects = oApp.TransientObjects;

                                                                            Inventor.TranslationContext oTranslatorContext = oTranslatorObjects.CreateTranslationContext();

                                                                            Inventor.NameValueMap oTranslatorNameValueMap = oTranslatorObjects.CreateNameValueMap();

                                                                            Inventor.DataMedium oDataMedium = oTranslatorObjects.CreateDataMedium();

                                                                            //MessageBox.Show("all translator options set");

                                                                            if (oTranslator.get_HasSaveCopyAsOptions(oApp, oTranslatorContext, oTranslatorNameValueMap))

                                                                            {

                                                                                oTranslatorNameValueMap.set_Value("Sheet_Range", Inventor.PrintRangeEnum.kPrintAllSheets);

                                                                                oTranslatorContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism;

                                 

                                 

                                                                                string sDocNamePath = System.IO.Path.GetFileName(sFileName);

                                                                                string sPath = System.IO.Path.GetFullPath(sFileName);

                                                                                string sLastCut = "";

                                                                                while (!sPath.EndsWith(@"PROJECTS"))

                                                                                {

                                                                                    sLastCut = sPath.Substring(sPath.LastIndexOf(@"\") + 1);

                                                                                    //MessageBox.Show(sLastCut);

                                                                                    sPath = sPath.Substring(0, sPath.LastIndexOf(@"\"));

                                                                                }

                                                                                //MessageBox.Show("Uit de loop; sPath = " + sPath + " slastcut = " + sLastCut);

                                                                                sPath = sPath + @"\" + sLastCut;

                                                                                //MessageBox.Show(sPath);

                                 

                                 

                                                                                //MessageBox.Show("De top project folder for this file is:  "+sPath);

                                                                                //Allways go to: PROJECT_FOLDER/CAD/EXCHANGE/DXF/ for the export location.

                                                                                //First check if the folders exist, if not, create them.

                                                                                IEdmFolder5 top_project_folder = vault.GetFolderFromPath(sPath);

                                                                                //MessageBox.Show(top_project_folder.Name);

                                 

                                 

                                                                                //IEdmFolder5 sub_folder_cad;

                                                                                IEdmFolder5 sub_folder_exchange;

                                                                                IEdmFolder5 sub_folder_pdf;

                                                                                try

                                                                                {

                                                                                    try

                                                                                    {

                                                                                        sub_folder_exchange = top_project_folder.GetSubFolder("EXCHANGE");

                                                                                        //System.Windows.Forms.MessageBox.Show("Deze folder was er al: " + sub_folder_exchange.Name);

                                                                                    }

                                                                                    catch (Exception)

                                                                                    {

                                                                                        sub_folder_exchange = top_project_folder.AddFolder(top_project_folder.ID, "EXCHANGE");

                                 

                                 

                                                                                        //System.Windows.Forms.MessageBox.Show("Deze folder was er nog niet: " + sub_folder_exchange.Name);

                                                                                    }

                                                                                    try

                                                                                    {

                                                                                        sub_folder_pdf = sub_folder_exchange.GetSubFolder("PDF");

                                                                                        //System.Windows.Forms.MessageBox.Show("Deze folder was er al: " + sub_folder_dxf.Name);

                                                                                    }

                                                                                    catch (Exception)

                                                                                    {

                                                                                        sub_folder_pdf = sub_folder_exchange.AddFolder(sub_folder_exchange.ID, "PDF");

                                                                                        //System.Windows.Forms.MessageBox.Show("Deze folder was er nog niet: " + sub_folder_dxf.Name);

                                                                                    }

                                                                                    //Put the export filename in the retrieved or created folder.

                                                                                    string sNewFileName = sub_folder_pdf.LocalPath + @"\" + System.IO.Path.GetFileNameWithoutExtension(sFileName) + "_rev" + file.CurrentRevision + ".pdf";

                                                                                    //MessageBox.Show("nieuwe filename is: "+sNewFileName);

                                 

                                 

                                                                                    oDataMedium.FileName = sNewFileName;

                                                                                    oTranslator.SaveCopyAs(oApp.ActiveDocument, oTranslatorContext, oTranslatorNameValueMap, oDataMedium);

                                                                                    oApp.ActiveDocument.Close(true);

                                                                                }

                                                                                catch (Exception)

                                                                                {

                                                                                }

                                                                            }//if (oTranslator.get_HasSaveCopyAsOptions(oApp, oTranslatorContext, oTranslatorNameValueMap))

                                                                            try

                                                                            {

                                                                                oApp.ActiveDocument.Close(true);

                                                                            }

                                                                            catch (Exception)

                                                                            {

                                 

                                 

                                 

                                 

                                                                            }

                                                                        }//if (bPartInProject)

                                                                    }//if (cmdData.mbsStrData2.Equals("Released"))

                                                                    //MessageBox.Show("dit was een suited file, maar geen released status");

                                                                }//if (!bUnsuitedFile)

                                                                //MessageBox.Show("dit was geen suited file, alles overgeslagen");

                                                            }

                                                            catch (Exception)

                                                            {

                                 

                                 

                                                            }

                                                        }

                                                        catch (Exception)

                                                        {

                                 

                                 

                                 

                                 

                                                        }

                                 

                                 

                                                    }

                                                }

                                            }

                                            catch (Exception)

                                            {

                                                if (oApp != null)

                                                {

                                                    oApp.Quit();

                                                }

                                            }

                                            finally

                                            {

                                                if (oApp != null)

                                                {

                                                    oApp.Quit();

                                                }

                                            }

                                        }

                                    }

                                }

                          • Re: Add-in error when calling 'OnCmd' method
                            Klaas Oudejans

                            Thanks for all your help guys! I learned a lot from it, it is fixed now. I think the public object were the issue, but your tips made the code safer again, and made it possible for me to better debugging, so thanks a lot!