I'm looking to create a simplified part from an assembly and only include certain components of the assembly. Line 10 seems to be the issue in the code below. Any help with this would be greatly appreciated!
Dim wsPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Dim simplifiedPath As String = wsPath & "\Pattern.ipt"
Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oDef.Occurrences
Dim dirDef As DerivedPartDefinition = oDef.ReferenceComponents.DerivedPartComponents.CreateDefinition
For Each oOcc As ComponentOccurrence In oOccs
If oOcc.Name.Contains("Vert") Or oOcc.Name.Contains("Horz") Then dirDef.AddOccurrence(oOcc)
Next
Dim simplifiedPart As DerivedPartComponent = oDef.ReferenceComponents.DerivedPartComponents.Add(dirDef)
Dim simplifiedDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)
simplifiedDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(dirDef)
I'm looking to create a simplified part from an assembly and only include certain components of the assembly. Line 10 seems to be the issue in the code below. Any help with this would be greatly appreciated!
Dim wsPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Dim simplifiedPath As String = wsPath & "\Pattern.ipt"
Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oDef.Occurrences
Dim dirDef As DerivedPartDefinition = oDef.ReferenceComponents.DerivedPartComponents.CreateDefinition
For Each oOcc As ComponentOccurrence In oOccs
If oOcc.Name.Contains("Vert") Or oOcc.Name.Contains("Horz") Then dirDef.AddOccurrence(oOcc)
Next
Dim simplifiedPart As DerivedPartComponent = oDef.ReferenceComponents.DerivedPartComponents.Add(dirDef)
Dim simplifiedDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)
simplifiedDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(dirDef)
If you look at the derivedpartcomponents here you will see there is no method for create definition. Use one of the other available methods. The sample will give some more pointers as to how to set this up.
If you look at the derivedpartcomponents here you will see there is no method for create definition. Use one of the other available methods. The sample will give some more pointers as to how to set this up.
Hi, you can try this code,
Traverse to the assembly find all occurrences and add them to a list off Occurrences
Turn off visibility off the occurrences that not include Vert, Horz
Create the derived part and save the part
Turn on visibility off all occurrences off the assy.
Sub main Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oFileName As String = oAssyDoc.FullDocumentName 'Traverse to the assembly find all occurrences add them to a list off Occurrences Dim oOccs As List(Of ComponentOccurrence) = TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences, 1) ' Turn off visibility off the occurrences SetVisibility(oOccs, False) oAssyDoc.Save2() ' Create the derived part and save the par CreateDerivePart(oFileName) ' Turn on visibility off all occurrences SetVisibility(oOccs, True) End Sub Private Function TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) As List(Of ComponentOccurrence) Dim result As New List(Of ComponentOccurrence) Dim oOcclist As New List(Of ComponentOccurrence) Try ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences oOcclist.Add(oOcc) If oOcc._DisplayName.Contains("Horz") Then oOcclist.Remove(oOcc) End If If oOcc._DisplayName.Contains("Vert") Then oOcclist.Remove(oOcc) End If ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) End If result = oOcclist Next Catch ex As Exception End Try Return result End Function Private Sub SetVisibility(ByVal oOccs As List(Of ComponentOccurrence), VisSet As Boolean) Dim oOcc As ComponentOccurrence Try For Each oOcc In oOccs oOcc.Visible = VisSet Next Catch ex As Exception End Try End Sub Private Sub CreateDerivePart(ByVal FullFileName As String) Dim oPartDocTemplate As String oPartDocTemplate = ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject, SystemOfMeasureEnum.kDefaultSystemOfMeasure, DraftingStandardEnum.kDefault_DraftingStandard) Dim oPartDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, oPartDocTemplate, True) ' Create a derived definition for the molded assembly. Dim oDerivedAsmDef As DerivedAssemblyDefinition oDerivedAsmDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(FullFileName) ' Create the derived assembly. oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef) Dim oFileNameCount As Integer = Strings.Len(FullFileName) Dim oFileNameNew As String = Strings.Left(FullFileName, (oFileNameCount - 4)) oPartDoc.SaveAs(oFileNameNew & ".ipt", False) oPartDoc.Close() End Sub
Hi, you can try this code,
Traverse to the assembly find all occurrences and add them to a list off Occurrences
Turn off visibility off the occurrences that not include Vert, Horz
Create the derived part and save the part
Turn on visibility off all occurrences off the assy.
Sub main Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oFileName As String = oAssyDoc.FullDocumentName 'Traverse to the assembly find all occurrences add them to a list off Occurrences Dim oOccs As List(Of ComponentOccurrence) = TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences, 1) ' Turn off visibility off the occurrences SetVisibility(oOccs, False) oAssyDoc.Save2() ' Create the derived part and save the par CreateDerivePart(oFileName) ' Turn on visibility off all occurrences SetVisibility(oOccs, True) End Sub Private Function TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) As List(Of ComponentOccurrence) Dim result As New List(Of ComponentOccurrence) Dim oOcclist As New List(Of ComponentOccurrence) Try ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences oOcclist.Add(oOcc) If oOcc._DisplayName.Contains("Horz") Then oOcclist.Remove(oOcc) End If If oOcc._DisplayName.Contains("Vert") Then oOcclist.Remove(oOcc) End If ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) End If result = oOcclist Next Catch ex As Exception End Try Return result End Function Private Sub SetVisibility(ByVal oOccs As List(Of ComponentOccurrence), VisSet As Boolean) Dim oOcc As ComponentOccurrence Try For Each oOcc In oOccs oOcc.Visible = VisSet Next Catch ex As Exception End Try End Sub Private Sub CreateDerivePart(ByVal FullFileName As String) Dim oPartDocTemplate As String oPartDocTemplate = ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject, SystemOfMeasureEnum.kDefaultSystemOfMeasure, DraftingStandardEnum.kDefault_DraftingStandard) Dim oPartDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, oPartDocTemplate, True) ' Create a derived definition for the molded assembly. Dim oDerivedAsmDef As DerivedAssemblyDefinition oDerivedAsmDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(FullFileName) ' Create the derived assembly. oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef) Dim oFileNameCount As Integer = Strings.Len(FullFileName) Dim oFileNameNew As String = Strings.Left(FullFileName, (oFileNameCount - 4)) oPartDoc.SaveAs(oFileNameNew & ".ipt", False) oPartDoc.Close() End Sub
Can't find what you're looking for? Ask the community or share your knowledge.