Inconsistent ComponentDefinition.Type and InvalidCastException

Inconsistent ComponentDefinition.Type and InvalidCastException

Anonymous
Not applicable
587 Views
6 Replies
Message 1 of 7

Inconsistent ComponentDefinition.Type and InvalidCastException

Anonymous
Not applicable

Here's my code. 

 

occ = assemdoc.ComponentDefinition.Occurrences.Add(partFile, location);
bool isPartComponentDefinition = occ.Definition.Type == ObjectTypeEnum.kPartComponentDefinitionObject; // true
((PartComponentDefinition)occ.Definition).Parameters["BDiameter"]); // System.InvalidCastException

 

It's throwing InvalidCastException on (PartComponentDefinition)occ.Definition, even though occ.Definition.Type == ObjectTypeEnum.kPartComponentDefinitionObject.

 

Is this a bug? Or do I have to do something before the occurrence from Occurrences.Add becomes valid?

0 Likes
588 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Here is stripped down version of the method I call to add occurrences to an assembly and an example of how I call it.

 

Public Function AddOccurrence(ByVal AssyCompDef As Inventor.AssemblyComponentDefinition, _
                                 ByVal OccurrName As String, _
                                 ByVal PosMtrx As Inventor.Matrix) As Inventor.ComponentOccurrence
        Dim NewOccurr As Inventor.ComponentOccurrence

        Try
             NewOccurr = AssyCompDef.Occurrences.Add(OccurrName, PosMtrx)
           
  Return NewOccurr
       
        Catch ex As Exception
                MessageBox.Show("could not place file " & OccurrName, "error", MessageBoxButtons.OK)
             
  Return Nothing
        End Try
 End Function


 Protected Friend Sub CreateSubAssy(fullpath As String, newAssyName as String)

        _invApp.SilentOperation = True
        Dim oAssyDoc As Inventor.AssemblyDocument
        Try
            oAssyDoc = CType(_invApp.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, _
                                                     TemplateFileName:=gTemplatePath & "CD80vSealAssy.iam", _
                                                     CreateVisible:=False), AssemblyDocument)

            oAssyDoc.SaveAs(fullpath & newAssyName & ".iam", False)

            Dim oAsmCompDef As AssemblyComponentDefinition
            oAsmCompDef = oAssyDoc.ComponentDefinition

            Dim oPositionMatrix As Inventor.Matrix
            oPositionMatrix = _invApp.TransientGeometry.CreateMatrix

            'place part
            Dim sFileName As String = fullpath & "PartName.ipt"

            Dim oPart As Inventor.ComponentOccurrence

            oPart = AddOccurrence(oAsmCompDef, sFileName, oPositionMatrix)         
           
        Catch ex As Exception

        Finally
            _invApp.SilentOperation = False
            oAssyDoc.Save2()
            oAssyDoc.Close(SkipSave:=False)
        End Try
 End Sub

0 Likes
Message 3 of 7

Anonymous
Not applicable
Yeah, that's essentially what we've got too, but that occurrence you get at
the end there, that's where my code comes in, and I get the type-cast
exception. It's like it's not really a full-fledged occurrence - like it's
only got part of the occurrence info.
0 Likes
Message 4 of 7

Anonymous
Not applicable
Yeah, that's essentially what we've got too, but that occurrence you get at
the end there, that's where my code comes in, and I get the type-cast
exception. It's like it's not really a full-fledged occurrence - like it's
only got part of the occurrence info.
0 Likes
Message 5 of 7

Anonymous
Not applicable
are you trying to modify a parameter in the part with the following line?
((PartComponentDefinition)occ.Definition).Paramete​rs["BDiameter"]); // System.InvalidCastException
0 Likes
Message 6 of 7

Anonymous
Not applicable
Yeah, that's my ultimate goal, and then I ran into this problem.
0 Likes
Message 7 of 7

Anonymous
Not applicable

 

try something like this to change the parameter value or expression

 

Dim oPartCompDef as PartComponentDefinition

oPartCompDef = occ.Definition

oPartCompDef.Parameters.Item("BDiameter").Value = xxx 'or .Expression = "xxx"

0 Likes