Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create simplified part from an assembly for particular occurrences

2 REPLIES 2
Reply
Message 1 of 3
ndillner343SKL
195 Views, 2 Replies

Create simplified part from an assembly for particular occurrences

ndillner343SKL
Enthusiast
Enthusiast

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)

 

0 Likes

Create simplified part from an assembly for particular occurrences

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)

 

2 REPLIES 2
Message 2 of 3
A.Acheson
in reply to: ndillner343SKL

A.Acheson
Mentor
Mentor

Hi @ndillner343SKL 

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 this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Hi @ndillner343SKL 

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 this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 3

nstevelmans
Advocate
Advocate

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

 

0 Likes

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.

Post to forums  

Autodesk Design & Make Report