Adding Occurence Error

Adding Occurence Error

meck
Collaborator Collaborator
420 Views
2 Replies
Message 1 of 3

Adding Occurence Error

meck
Collaborator
Collaborator

Hi all,

I am trying to use some code that I found to add an occurrence to an assembly. I've placed this code in an iLogic rule. When the code runs I get this error "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

 

I've verified that the file name and path way is correct so I do not think that is the problem. Below is the code and it fails on the line "Call oOccs.Add(sFilePath, oMatrix)"

 

Any help would really be appreciated.

 

Public Sub Place_Part(sFilePath As String)
Dim oApp As Application
oApp = ThisApplication
' if active document isn't an assembly then exit
If Not TypeOf oApp.ActiveDocument Is AssemblyDocument Then Exit Sub
Dim oDoc As AssemblyDocument
oDoc = oApp.ActiveDocument
' define filepath

Dim oOccs As ComponentOccurrences
oOccs = oDoc.ComponentDefinition.Occurrences
' Set a reference to the transient geometry object.
Dim oTG As TransientGeometry
oTG = oApp.TransientGeometry
' Create a matrix. A new matrix is initialized with an identity matrix.
Dim oMatrix As Matrix
oMatrix = oTG.CreateMatrix
' check if file already exists in assembly
If CompExist(oDoc, sFilePath) Then
 ans = MsgBox("Component already exists within assembly. Place component anyway?", vbYesNo)
 If ans = vbYes Then
  Call oOccs.Add(sFilePath, oMatrix)
 Else
  Exit Sub
 End If
Else
 Trace.WriteLine("iLogic: sFilePath: " & sFilePath)
 Call oOccs.Add(sFilePath, oMatrix)
 MsgBox ("Hello")
End If
End Sub

Function CompExist(ByRef oAssy As Inventor.AssemblyDocument, sFilePath As String) As Boolean
Dim oOcc As ComponentOccurrence
Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAssy.ComponentDefinition
' Iterate through all of the occurrence in this collection. This
' represents the occurrences at the top level of an assembly.
For Each oOcc In oCompDef.Occurrences
 ' get filepath of occurrence
 sOccFilePath = oOcc.Definition.Document.FullFileName
 If sOccFilePath = sFilePath Then
  CompExist = True
  Exit Function
 End If
Next oOcc
End Function  

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Accepted solutions (1)
421 Views
2 Replies
Replies (2)
Message 2 of 3

Owner2229
Advisor
Advisor
Accepted solution

Hey, creating the matrix is not enough, you need to assign it a value (position and direction):

 

Public Sub Place_Part(sFilePath As String)
    Dim oApp As Application = ThisApplication
    Dim oDoc As Document = oApp.ActiveDocument
    If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
    Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
    
    If CompExist(oOccs, sFilePath) Then
        Dim ans As MsgBoxResult = MsgBox("Component already exists within assembly. Place component anyway?", vbYesNo)
        If ans <> vbYes Then Exit Sub
    End If
    
    Dim oTG As TransientGeometry = oApp.TransientGeometry
    Dim oMatrix As Matrix = oTG.CreateMatrix
    Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
    Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)
    Call oOccs.Add(sFilePath, oMatrix)
End Sub

Function CompExist(ByRef oOccs As ComponentOccurrences, sFilePath As String) As Boolean
    For Each oOcc As ComponentOccurrence In oOccs
        Dim sOccFilePath = oOcc.Definition.Document.FullFileName
        If sOccFilePath = sFilePath Then Return True
    Next
    Return False
End Function

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 3

meck
Collaborator
Collaborator

Thanks for the reply! That was the fix.

I was wondering if the matrix needed coordinates, but I had use the same code before and it worked OK. 

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes