How to add multiple parts within a loop

How to add multiple parts within a loop

Anonymous
Not applicable
243 Views
1 Reply
Message 1 of 2

How to add multiple parts within a loop

Anonymous
Not applicable
The first iteration through this loop adds a part successfully. The next time through gives this error:
"Method 'Add' of object _IRxComponentOccurrences Failed"

For x = 1 To lPartCount
Call oMatrix.SetToRotation(0, _
oTG.CreateVector(0, 0, 0), oTG.CreatePoint(0, 0, 0))

Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

Dim oOcc As ComponentOccurrence
sPartFileName = "D:/data/parts/Post.ipt"
Set oOcc = oAsmCompDef.Occurrences.Add(sPartFileName, oMatrix)
Next x

I think it's because I'm trying to add a part with the exact same name. Does anyone have examples of adding parts to an assembly drawing within a loop?

tia,

clu
0 Likes
244 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Your code looks ok except I believe each part may
have been placed directly on the previos part.  Here's some code that
places five occurrences in a row in the X direction.

 

Public Sub CreateAssembly()
    '
Get a reference to the active assembly document.
    Dim oDoc
As AssemblyDocument
    Set oDoc =
ThisApplication.ActiveDocument
   
    '
Create the matrix that will be used to define the position of the
occurrences.
    ' It is created as an identity matrix which
means the part will be placed in
    ' the assembly in the
same position it exists in the part.
    Dim oMatrix As
Matrix
    Set oMatrix =
ThisApplication.TransientGeometry.CreateMatrix

 

    ' Initialize the variable that
defines the X position of the transform.
    Dim dX As
Double
    dX = 0

 

    ' Add a series of
occurrences.
    Dim i As Long
    For i = 1
To 5
        Call
oDoc.ComponentDefinition.Occurrences.Add("C:\Temp\Part.ipt",
oMatrix)
       

        ' Increment the X position and
modify the X translation portion of the
matrix.
        dX = dX +
10
        oMatrix.Cell(1, 4) =
dX
    Next
End Sub

 

-Brian


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
The
first iteration through this loop adds a part successfully. The next time
through gives this error:
"Method 'Add' of object _IRxComponentOccurrences
Failed"

For x = 1 To lPartCount

         Call
oMatrix.SetToRotation(0, _

      oTG.CreateVector(0, 0, 0),
oTG.CreatePoint(0, 0, 0))

Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

Dim oOcc As ComponentOccurrence

        sPartFileName =
"D:/data/parts/Post.ipt"

        Set oOcc =
oAsmCompDef.Occurrences.Add(sPartFileName, oMatrix)

    Next x

I think it's because I'm trying to add a part with the exact same name.
Does anyone have examples of adding parts to an assembly drawing within a
loop?

tia,

clu

0 Likes