Here is a quick little bonus macro for you all.
It is not flawless - sometimes patterns and work features mess it up, BUT it will duplicate the selection of parts and the constraints between those parts and the assembly the rule is ran from. It places the parts directly over top of the previous one for this reason, so if you want to quickly change it, it is recommended that you leave 1 DOF free.
Private oNewlyInsertedColl As Collection
Private oOriginalItemColl As Collection
Public Sub DupeSelectionWithConstraints()
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then MsgBox ("Rule not valid for non-assembly files!"): Exit Sub
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oSS As SelectSet
Set oSS = oDoc.SelectSet
If oSS.Count < 1 Then MsgBox ("Rule Requires a Select Set!"): Exit Sub
Call DuplicateSS(oDoc, oSS)
End Sub
Private Sub DuplicateSS(oParentDoc As Document, oSS As SelectSet)
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
Set oPasteMatrix = oTG.CreateMatrix()
Set oOriginalItemColl = New Collection
Set oNewlyInsertedColl = New Collection
For Each oItem In oSS
Set oPasteMatrix = oItem.Transformation
Set oNewOcc = oParentDoc.ComponentDefinition.Occurrences.Add(oItem.Definition.Document.FullDocumentName, oPasteMatrix)
oOriginalItemColl.Add oItem
oNewlyInsertedColl.Add oNewOcc
Next
Dim oTestoOcc1 As Object
Dim oTestoOcc2 As Object
Dim oOcc1 As ComponentOccurrence
Dim oOcc2 As ComponentOccurrence
Dim oEntityOne As Object
Dim oEntityTwo As Object
For Each oConstraint In oParentDoc.ComponentDefinition.Constraints
'Grab entities for new constraint to create
Set oTestoOcc1 = GrabObjectFromColl(oOriginalItemColl, oConstraint.OccurrenceOne)
Set oTestoOcc2 = GrabObjectFromColl(oOriginalItemColl, oConstraint.OccurrenceTwo)
If oTestoOcc1 Is Nothing And oTestoOcc2 Is Nothing Then GoTo NextConstraint
If oTestoOcc1 Is Nothing Then
Set oEntityOne = oConstraint.EntityOne
Else
For Each oPossibleOcc In oNewlyInsertedColl
If oPossibleOcc.Definition Is oTestoOcc1.Definition Then
Set oOcc1 = oPossibleOcc
End If
Next
Call oOcc1.CreateGeometryProxy(GetProxy(oConstraint.EntityOne, oOcc1), oEntityOne)
End If
If oTestoOcc2 Is Nothing Then
Set oEntityTwo = oConstraint.EntityTwo
Else
For Each oPossibleOcc In oNewlyInsertedColl
If oPossibleOcc.Definition Is oTestoOcc2.Definition Then
Set oOcc2 = oPossibleOcc
End If
Next
Call oOcc2.CreateGeometryProxy(GetProxy(oConstraint.EntityTwo, oOcc2), oEntityTwo)
End If
'End Grab entities
'Check type of constraint
Select Case oConstraint.Type
Case 100665088 'kAngleConstraintObject
'oParentDoc.Constraints.AddAngleConstraint(EntityOne As Object,
' EntityTwo As Object,
' Angle As Variant,
' [SolutionType] As AngleConstraintSolutionTypeEnum,
' [ReferenceVectorEntity] As Variant,
' [BiasPointOne] As Variant,
' [BiasPointTwo] As Variant )
' As AngleConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddAngleConstraint(oEntityOne, oEntityTwo, oConstraint.Angle, oConstraint.SolutionType, oConstraint.ReferenceVectorEntity)
Case 100707840 'kAssemblySymmetryConstraintObject
'oParentDoc.AddSymmetryConstraint( EntityOne As Object,
' EntityTwo As Object,
' SymmetryPlane As Object,
' [EntityOneInferredType] As InferredTypeEnum,
' [EntityTwoInferredType] As InferredTypeEnum,
' [NormalsOpposed] As Boolean )
' As AssemblySymmetryConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddSymmetryConstraint(oEntityOne, oEntityTwo, oConstraint.SymmetryPlane, oConstraint.EntityOneInferredType, oConstraint.EntityTwoInferredType, oConstraint.NormalsOpposed)
Case 100666368 'kFlushConstraintObject
'oParentDoc.AddFlushConstraint( EntityOne As Object,
' EntityTwo As Object,
' Offset As Variant,
' [BiasPointOne] As Variant,
' [BiasPointTwo] As Variant )
' As FlushConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddFlushConstraint(oEntityOne, oEntityTwo, oConstraint.Offset.Expression)
Case 100665344 'kInsertConstraintObject
'oParentDoc.AddInsertConstraint( EntityOne As Object,
' EntityTwo As Object,
' AxesOpposed As Boolean,
' Distance As Variant,
' [BiasPointOne] As Variant,
' [BiasPointTwo] As Variant )
' As InsertConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddInsertConstraint(oEntityOne, oEntityTwo, oConstraint.AxesOpposed, oConstraint.Distance.Expression)
Case 100665856 'kMateConstraintObject
'oParentDoc.AddMateConstraint( EntityOne As Object,
' EntityTwo As Object,
' Offset As Variant,
' [EntityOneInferredType] As InferredTypeEnum,
' [EntityTwoInferredType] As InferredTypeEnum,
' [BiasPointOne] As Variant,
' [BiasPointTwo] As Variant )
' As MateConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddMateConstraint(oEntityOne, oEntityTwo, oConstraint.Offset.Expression, oConstraint.EntityOneInferredType, oConstraint.EntityTwoInferredType)
Case 100665600 'kTangentConstraintObject
'.AddTangentConstraint( EntityOne As Object,
' EntityTwo As Object,
' InsideTangency As Boolean,
' Offset As Variant,
' [BiasPointOne] As Variant,
' [BiasPointTwo] As Variant )
' As TangentConstraint
Call oParentDoc.ComponentDefinition.Constraints.AddTangentConstraint(oEntityOne, oEntityTwo, oConstraint.InsideTangency, oConstraint.Offset.Expression)
End Select
NextConstraint:
Next 'constraint
End Sub
Private Function GrabObjectFromColl(ByVal oColl As Collection, ByVal oObj As Object) As Object
For Each oItem In oColl
If oItem Is oObj Then
Set GrabObjectFromColl = oObj
Exit Function
End If
Next
Set GrabObjectFromColl = Nothing
End Function
Private Function GetProxy(ByRef Prxy As Object, ByRef ContOcc As ComponentOccurrence) As Object
Dim TempPrxy As Object
Dim Occ As Object
If Prxy.ContainingOccurrence.Type = kComponentOccurrenceObject Then
Set Occ = ContOcc
Else
On Error Resume Next
Set Occ = ContOcc.Definition.Occurrences.ItemByName(Prxy.ContainingOccurrence.Name)
If Err.Number <> 0 Then
On Error GoTo 0
Set TempPrxy = Prxy.ContainingOccurrence
Call ContOcc.CreateGeometryProxy(GetProxy(TempPrxy, ContOcc), Occ)
End If
End If
Call Occ.CreateGeometryProxy(Prxy.NativeObject, GetProxy)
End Function
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.