Insert copies of Content Center parts

<?xml encoding=”UTF-8″>By Adam Nagy

You might need to insert lots of instances of the same Content Center part in an assembly – e.g. placing rivets in similar holes on the same part. You could write some code that would help replicate an instance, so that you would only need to insert a single component manually then select the inserted component and all the holes where you would need the same component and the code would insert them and add the constraints.

This sample is just to show the idea and is far from a robust solution which would work in all scenarios. In this case we assume that the rivet has been placed with a single insert constraint and the selected holes are the same size as the original one.

So we already have a rivet placed in the assembly and now we select it and the other holes where we want to place them:

Rivets1

Then we run this VBA code:

Sub InsertSameInstances()
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument
Dim acd As AssemblyComponentDefinition
Set acd = doc.ComponentDefinition
' Instance to copy
Dim occ As ComponentOccurrence
Set occ = doc.SelectSet(1)
' Store selected faces
Dim fs As ObjectCollection
Set fs = ThisApplication.TransientObjects.CreateObjectCollection
Dim i As Integer
For i = 2 To doc.SelectSet.Count
Call fs.Add(doc.SelectSet(i))
Next
' Faces of holes that the copies should be placed to
Dim h As Face
For Each h In fs
' Insert same component
Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry
Dim occNew As ComponentOccurrence
Set occNew = acd.Occurrences.AddByComponentDefinition( _
occ.Definition, tg.CreateMatrix())
' Check original component's constraints
Dim c As AssemblyConstraint
For Each c In occ.Constraints
If TypeOf c Is InsertConstraint Then
Dim ic As InsertConstraint
Set ic = c
' Get native edge
Dim e As Edge
Set e = c.EntityOne.NativeObject
' Get its equivalent in the new occurrence
Dim ep As EdgeProxy
Call occNew.CreateGeometryProxy(e, ep)
' Get hole's edge
Dim eh As Edge
Set eh = h.Edges(1)
Call acd.Constraints.AddInsertConstraint( _
ep, eh, ic.AxesOpposed, ic.Distance.value)
End If
Next
Next
End Sub

And get this:

Rivets2

I think based on this someone could create a nice Inventor App Store plugin – hint, hint ;)


Comments

4 responses to “Insert copies of Content Center parts”

  1. Martin Avatar
    Martin

    This code is great! It does a lot of work automatically. For our automation it would be great if i could, instead of select hole after hole, select the area where the holes are in. I would Need just two clicks for hundreds of rivets. By the way, we use rivets DIN 7337 and DIN 124. There is one Problem left, if we have 3 plates to put together with rivets, plate 1 with 2 and plate 2 with 3 – in this case the code have to check if there is a hole still with an rivet in.
    May be there is anyone who can modifiy the code…
    Thanks in advance
    Martin

  2. Dušan Nauš Avatar
    Dušan Nauš

    Hi
    I have Error in Inventor Professional 2017
    Error in rule program format:
    All other Sub’s or Function’s must be after Sub Main()

  3. Dušan Nauš Avatar
    Dušan Nauš

    Thank you,
    you’re quick. I try, the code does not work. I do not know. What have I to do with Select Set There I have an error. I followed the address what you’ve sent me.
    Sub Main()
    ‘ Do something
    ‘ Then call another method
    Call InsertSameInstances
    End Sub
    Sub InsertSameInstances()
    Dim doc As AssemblyDocument
    doc = ThisApplication.ActiveDocument
    Dim acd As AssemblyComponentDefinition
    acd = doc.ComponentDefinition
    ‘ Instance to copy
    Dim occ As ComponentOccurrence
    ‘ occ = doc.SelectSet(1)

    ‘ ‘ Store selected faces
    ‘ Dim fs As ObjectCollection
    ‘ fs = ThisApplication.TransientObjects.CreateObjectCollection

    ‘ Dim i As Integer
    ‘ For i = 2 To doc.SelectSet.Count
    ‘ Call fs.Add(doc.SelectSet(i))
    ‘ Next

    ‘ ‘ Faces of holes that the copies should be placed to
    ‘ Dim h As Face
    ‘ For Each h In fs
    ‘ ‘ Insert same component
    ‘ Dim tg As TransientGeometry
    ‘ tg = ThisApplication.TransientGeometry

    ‘ Dim occNew As ComponentOccurrence
    ‘ occNew = acd.Occurrences.AddByComponentDefinition(occ.Definition, tg.CreateMatrix())

    ‘ ‘ Check original component’s constraints
    ‘ Dim c As AssemblyConstraint
    ‘ For Each c In occ.Constraints
    ‘ If TypeOf c Is InsertConstraint Then
    ‘ Dim ic As InsertConstraint
    ‘ ic = c

    ‘ ‘ Get native edge
    ‘ Dim e As Edge
    ‘ e = c.EntityOne.NativeObject

    ‘ ‘ Get its equivalent in the new occurrence
    ‘ Dim ep As EdgeProxy
    ‘ Call occNew.CreateGeometryProxy(e, ep)

    ‘ ‘ Get hole’s edge
    ‘ Dim eh As Edge
    ‘ eh = h.Edges(1)

    ‘ Call acd.Constraints.AddInsertConstraint(ep, eh, ic.AxesOpposed, ic.Distance.Value)
    ‘ End If
    ‘ Next
    ‘ Next
    End Sub

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading