Error when inserting iAssembly

Error when inserting iAssembly

larry.daubenspeck
Advocate Advocate
662 Views
5 Replies
Message 1 of 6

Error when inserting iAssembly

larry.daubenspeck
Advocate
Advocate

I have a VB.net function which inserts iAssemblies.  I pass it several parameters, including one to return any error messages along the way.  It works fine with the debugger, but once compiled, it returns an error and poops out.  Here is the text of the function:

 

Friend Function InsertiAssy(ByRef invApp As Inventor.Application, ByVal FilePath As String, ByVal FactoryName As String, ByVal ThisRow As String, ByRef ErrorMsg As String) As ComponentOccurrence

 

        Dim asmDoc As AssemblyDocument = invApp.ActiveDocument
        Dim TG As TransientGeometry = invApp.TransientGeometry
        Dim myOccur As ComponentOccurrence = Nothing

               

        Try
            myOccur = asmDoc.ComponentDefinition.Occurrences.AddiAssemblyMember(FilePath & FactoryName, TG.CreateMatrix, ThisRow)
        Catch ex As Exception
            asmDoc.Save()
            ErrorMsg = "Trouble inserting iAssembly " & FactoryName & ", " & ThisRow & vbCrLf & vbCrLf & ex.ToString
            Return Nothing
            Exit Function
        End Try

 

        Return myOccur

 

    End Function

 

Here is the returned error message...

 

Trouble inserting iAssembly C419.007WA-F.iam, C419.007WA.024


System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Inventor.ComponentOccurrences.AddiAssemblyMember(String FactoryDocumentName, Matrix Position, Object Row, Object Options)
   at DD_CAD_NO1.Module1.InsertiAssy(Application& invApp, String FilePath, String FactoryName, String ThisRow, String& ErrorMsg)

 

Any ideas?  Thanks in advance!

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Accepted solutions (1)
663 Views
5 Replies
Replies (5)
Message 2 of 6

larry.daubenspeck
Advocate
Advocate

I think I have found my own answer...

The automated process I'm using throws an exception when encountering the AddiAssembly method.  My guess is that it's due to its own internal mechanism that starts Excel, which doesn't like to be started that way.  My workaround is to utilize the folders, named after the factories, and the part or assembly files they contain.  Then, just use the regular Add method:

 

        Try
           myOccur = asmDoc.ComponentDefinition.Occurrences.AddiAssemblyMember(FilePath & FactoryName, TG.CreateMatrix, ThisRow)
        Catch ex As Exception
            Try
                Dim folderName As String = Left(FactoryName, FactoryName.Length - 4)
                myOccur = asmDoc.ComponentDefinition.Occurrences.Add(FilePath & folderName & "\" & ThisRow & ".iam", TG.CreateMatrix)
            Catch ex2 As Exception
                asmDoc.Save()
                ErrorMsg = "Trouble inserting iAssembly " & FactoryName & ", " & ThisRow & vbCrLf & vbCrLf & ex2.ToString
                Return Nothing
                Exit Function
            End Try
        End Try

 

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 3 of 6

Balaji_Ram
Alumni
Alumni

Hi Larry,

 

Does it mean your original code using AddiAssemblyMember still throws an exception ?

 

Can you please try using a row index as Long when using the AddiAssemblyMember ? 

 

Dim oiAssyFactory As iAssemblyFactory
oiAssyFactory = oCompDef.iAssemblyFactory

Dim iNumRows As Integer
iNumRows = oiAssyFactory.TableRows.Count

' Get the occurrences in the active document
oDoc = m_inventorApp.ActiveDocument
Dim oOccs As ComponentOccurrences
oOccs = oDoc.ComponentDefinition.Occurrences

Dim oPos As Matrix
oPos = m_inventorApp.TransientGeometry.CreateMatrix
        Dim oStep As Double
        oStep = 0.0#

Dim iRow As Long

' Add an occurrence for each member in the factory.
For iRow = 1 To iNumRows
	oStep = oStep + 50
    
	' Add a translation along X axis
    oPos.SetTranslation(m_inventorApp.TransientGeometry.CreateVector(oStep, oStep, 0))
    
	Dim oOcc As ComponentOccurrence
    oOcc = oOccs.AddiAssemblyMember(iAssemblyFilePath, oPos, iRow)
Next

Please try the above code snippet that creates an occurrence for each row in the iAssembly table.

 

If that does not help, can you please share a non-confidential iAssembly file to help reproduce the behavior ?

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 6

larry.daubenspeck
Advocate
Advocate

Hi Balaji,

 

No, the code doesn't work when using a long for the row index.  That was one of the first things I tried. 

 

I'm pretty sure I know what the problem is.  This particular program is automated, and runs unattended.  It gets launched by Windows Task Scheduler.  Neither AutoCad, nor Excel, can successfully be launched with the normal Activator.CreateInstance method.  They just don't like to be launched that way, and I have never found the right combination of permissions that worked (please refer to http://forums.autodesk.com/t5/net/error-while-programmatically-launching-autocad/m-p/5670056#M44894)... Rather, we have had to resort to using a Shell command to start them, which slows everything down, but, at least it works. 

 

My guess is that if and when Autodesk fixes this bug (it might not be a bug; it might be intentional), the AddiAssemblyMember method will suddenly start working as well.

 

On the other hand, if you can show me a better method (that works!) for how to start AutoCad with the Task Scheduler, I might have to revisit this issue as well.

 

Thank you for your time.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 5 of 6

Balaji_Ram
Alumni
Alumni
Accepted solution

Hi Larry,

 

I am getting your point now. 

 

I do not know much about overcoming the limitation of the Windows Task scheduler.

 

But, using the Inventor's Task scheduler, this should be possible.

 

It is possible to setup a scheduled task and the createInstance and AddiAssemblyMember both work ok.

 

The task scheduler provides a way to provide the owner login credentials and this is used to launch the Inventor application.

 

I have attached a screenshot of a sample task which completed successfully.

It added an iAssembly member and saved the new assembly document using my login credentials.

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 6

larry.daubenspeck
Advocate
Advocate

Balaji,

 

Thanks!  That seems to work!

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes