<?xml encoding=”UTF-8″>By Adam Nagy
In the UI it’s quite straight-forward to copy occurrences: just select the components in the UI, do Ctrl+C, then click inside the Model View to make sure it has the focus, then click Ctrl+V. This copy/paste will keep the constraints between the copied occurrences in tact.
This functionality is not exposed in the API, but you can still use the built-in copy/paste commands to automate things. The other options would be to programmatically copy the occurrences and then recreate the constraints between them yourself, which would be more cumbersome.
Here is a VBA sample code which can do the copying:
Private Declare PtrSafe Function WinAPISetFocus Lib "user32" _
Alias "SetFocus" (ByVal hwnd As LongPtr) As Long
Sub CopyOccurrencesWithConstraints()
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument
Dim compDef As AssemblyComponentDefinition
Set compDef = doc.ComponentDefinition
' Occurrence count originally
Dim occCount As Integer
occCount = compDef.Occurrences.Count
' Select the occurrences you want to copy
' Either in the UI or programmatically
' Programmatic way could be e.g.:
'Dim occ As ComponentOccurrence
'Set occ = compDef.Occurrences(1)
'Call doc.SelectSet.Select(occ)
'Set occ = compDef.Occurrences(2)
'Call doc.SelectSet.Select(occ)
Dim cmdMgr As CommandManager
Set cmdMgr = ThisApplication.CommandManager
' Now copy them using the built-in command
Dim copyDef As ControlDefinition
Set copyDef = cmdMgr.ControlDefinitions.Item("AppCopyCmd")
Call copyDef.Execute
' ... and paste them
' Here we first have to make sure that the
' model view has the focus otherwise the paste
' command has no effect
Call WinAPISetFocus(ThisApplication.ActiveView.hwnd)
' Now execute the built-in Paste command
Dim pasteDef As ControlDefinition
Set pasteDef = cmdMgr.ControlDefinitions.Item("AppPasteCmd")
Call pasteDef.Execute
' Set the position of the new components using a matrix
Dim tr As TransientGeometry
Set tr = ThisApplication.TransientGeometry
Dim mx As Matrix
Set mx = tr.CreateMatrix()
Call mx.SetTranslation(tr.CreateVector(10, 10, 10))
Dim i As Integer
For i = occCount + 1 To compDef.Occurrences.Count
compDef.Occurrences(i).Transformation = mx
Next
End Sub
Result of running the code:


Leave a Reply to John HackneyCancel reply