Get features of model in a Inventor drawing view

Get features of model in a Inventor drawing view

Anonymous
Not applicable
423 Views
3 Replies
Message 1 of 4

Get features of model in a Inventor drawing view

Anonymous
Not applicable

I am trying to get features of the model attached to a view in Inventor drawing (Inventor 2014) 

 

I tried the following code and it is failing. 

 

 

Private Sub GetFeaturesOfModelFromOccurence(oFrontView As DrawingView)
    Try

          ' Get the assembly document that is attached to the view (assuming assembly model in the view)
          Dim oDoc As AssemblyDocument

          oDoc = InventorApplication.Documents.ItemByName(oFrontView.ReferencedDocumentDescriptor.FullDocumentName)

 

          ' Get all the components in the assembly uisng component definition
           Dim compDef As AssemblyComponentDefinition
           compDef = oDoc.ComponentDefinition

 

           Dim compOccurences As ComponentOccurrences
           compOccurences = compDef.Occurrences

           Dim compOccurence As ComponentOccurrence


          For Each compOccurence In compOccurences

 

                ' Get the Inventor Document assocaited with the component occurence
                Dim partDoc As PartDocument

 

                ' The following statement fails - I think the argument ItemByName expects is FullDocumentName and I am passing Name
                ' How to get the document from component occurrence?
                partDoc = InventorApplication.Documents.ItemByName(compOccurence.Name)

 

                Dim partCompDef As PartComponentDefinition
                partCompDef = partDoc.ComponentDefinition

 

                Dim compPartFeatures As PartFeatures
                compPartFeatures = partCompDef.Features

 

                For Each feat As PartFeature In compFeatures
                          MessageBox.Show(" Feature name >" + feat.Name + "<")
                 Next

          Next

     Catch ex As Exception
            MessageBox.Show("ERROR : " + ex.Message + System.Environment.NewLine)
            MessageBox.Show(ERROR : " + ex.StackTrace + System.Environment.NewLine)
      End Try
End Sub

 

0 Likes
424 Views
3 Replies
Replies (3)
Message 2 of 4

wayne.brill
Collaborator
Collaborator

Hi,

 

You can get the document of the occurrence using the Document property of the ComponentDefinition. Here is a VBA snippet: 

 

 

   Dim oDef As ComponentDefinition
            Set oDef = occ.Definition
            Dim oDoc As Document
            Set oDoc = oDef.Document
            Debug.Print oDoc.FullDocumentName

 

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 4

Anonymous
Not applicable

Hi Wayne,

   Tried your suggestion. But I am getting the following error in the statement 

            partDoc = InventorApplication.Documents.ItemByName(oCompDoc.FullDocumentName)

 

            ERROR : Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

The following is the modified code based on your reply.

 

Private Sub GetFeaturesOfModelFromOccurence(oFrontView As DrawingView)
    Try

        ' Get the assembly document that is attached to the view (assuming assembly model in the view)
         Dim oDoc As AssemblyDocument
         oDoc = InventorApplication.Documents.ItemByName(oFrontView.ReferencedDocumentDescriptor.FullDocumentName)

 

         ' Get all the components in the assembly uisng component definition
         Dim compDef As AssemblyComponentDefinition
         compDef = oDoc.ComponentDefinition

 

         Dim compOccurences As ComponentOccurrences
         compOccurences = compDef.Occurrences

 

         Dim compOccurence As ComponentOccurrence

         Dim count As Integer = 1
         For Each compOccurence In compOccurences

                ' Get the Inventor Document assocaited with the component occurence
                Dim oDef As ComponentDefinition
                oDef = compOccurence.Definition

 

                Dim oCompDoc As Document
                oCompDoc = oDef.Document

 

                 Dim partDoc As PartDocument
                 partDoc = InventorApplication.Documents.ItemByName(oCompDoc.FullDocumentName)   ' ERROR occurs with this statement

 

                 Dim partCompDef As PartComponentDefinition
                 partCompDef = partDoc.ComponentDefinition

 

                 Dim compPartFeatures As PartFeatures
                 compPartFeatures = partCompDef.Features

 

                 For Each feat As PartFeature In compPartFeatures
                        MessageBox.Show(" Feature name >" + feat.Name + "<")
                 Next

            Next
       Catch ex As Exception
             MessageBox.Show("ERROR : " + ex.Message + System.Environment.NewLine)
             MessageBox.Show(ERROR : " + ex.StackTrace + System.Environment.NewLine)
        End Try
End Sub

 

Thanks

Regards

PV Subramanian

0 Likes
Message 4 of 4

wayne.brill
Collaborator
Collaborator

Hi PV,

 

Your code works ok for me. I am testing with a simple assembly with one part occurrence.

 

Does your assembly have sub assemblies that are not ipt files but iam files? That would cause that error because it is trying to case a assembly document to a part document.

 

Also I am not sure why you are using Documents.ItemByName(). The code already has the document.

oCompDoc = oDef.Document

 

You could test the Document DocumentType

 

Here is some code I added to test:

oCompDoc = oDef.Document

                'WB added
                Dim partDoc As PartDocument = Nothing
                Dim asmDoc As AssemblyDocument = Nothing
                If oCompDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
                    partDoc = oCompDoc
                ElseIf oCompDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    asmDoc = oCompDoc
                    'If this is hit need to go to next occurrence as partDoc is Nothing and code below will fail
                    Continue For
                End If
                'WB end added

 

If this does not help upload an assembly I could use to research the problem. (need all the files of the assembly)

 

Thanks,

Wayne 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes