If you want to store the position of a ComponentOccurrence in order to be able to restore it later on, then you could simply store the Transformation values of the occurrence. They could be saved in a separate file, in Attributes, some other place – up to you where.
This VBA sample will store the values in an AttributeSet:
Sub StorePosition()
Const kAttSetName = "Adam.OccurrencePosition"
Const kCellNamePrefix = "cell"
Dim asm As AssemblyDocument
Set asm = ThisApplication.ActiveDocument
Dim occ As ComponentOccurrence
Set occ = asm.SelectSet(1)
Dim cells() As Double
Call occ.Transformation.GetMatrixData(cells)
Dim attValues(15) As Variant
Dim attNames(15) As String
Dim attTypes(15) As ValueTypeEnum
Dim i As Integer
For i = LBound(cells) To UBound(cells)
attValues(i) = cells(i)
attNames(i) = kCellNamePrefix + Trim(str(i))
attTypes(i) = kDoubleType
Next
Dim attSet As AttributeSet
If occ.AttributeSets.NameIsUsed(kAttSetName) Then
Set attSet = occ.AttributeSets("Adam.OccurrencePosition")
Else
Set attSet = occ.AttributeSets.Add("Adam.OccurrencePosition")
End If
Dim attEnum As AttributesEnumerator
' If the name was not Trim()-ed this would give an error
Set attEnum = attSet.AddAttributes(attNames, attTypes, attValues, True)
End Sub
Sub RestorePosition()
Const kAttSetName = "Adam.OccurrencePosition"
Const kCellNamePrefix = "cell"
Dim asm As AssemblyDocument
Set asm = ThisApplication.ActiveDocument
Dim occ As ComponentOccurrence
Set occ = asm.SelectSet(1)
Dim attSet As AttributeSet
If occ.AttributeSets.NameIsUsed(kAttSetName) Then
Set attSet = occ.AttributeSets("Adam.OccurrencePosition")
Else
Call MsgBox("Position data was not stored for this occurrence!")
Exit Sub
End If
Dim cells(15) As Double
Dim i As Integer
For i = 0 To 15
Dim cellName As String
cellName = kCellNamePrefix + Trim(str(i))
If Not attSet.NameIsUsed(cellName) Then
Call MsgBox("Not all position data stored for this occurrence!")
Exit Sub
End If
cells(i) = attSet(cellName).value
Next
Dim mx As Matrix
Set mx = occ.Transformation
Call mx.PutMatrixData(cells)
occ.Transformation = mx
End Sub
The code in action:
-Adam




Leave a Reply