@FLETCHER.A,
I made an edit to include the option for pre-selecting occurrences.
- If you Pre-Select Components, then it will auto align all of them together
- currently they all align to the first component [1 to 2, 1 to 3, 1 to 4, ...]
- I have a commented line that can be activated to "Daisy-Chain" The alignments [1 to 2, 2 to 3, 3 to 4, ...]
- If nothing is selected the rule functions as before
Here is the new code:
Sub Main
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub
Dim aDoc As AssemblyDocument = ThisApplication.ActiveDocument
PreSelect:
Dim selSet As SelectSet = aDoc.SelectSet
If selSet.Count < 1 Then GoTo ManualSelect
Logger.Trace("Pre-Selection Mode")
Dim coCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each Item In selSet
Dim co As ComponentOccurrence = TryCast(Item, ComponentOccurrence)
If Not IsNothing(co) Then coCollection.Add(co)
Next
If coCollection.Count < 1 Then Logger.Debug("No Component Occurence Objects were selected") : Exit Sub
If coCollection.Count = 1
Call FlushOriginAssembly(coCollection.Item(1), aDoc.ComponentDefinition) 'added edge case
Else
Dim prevCO As ComponentOccurrence
For i = 1 To coCollection.Count
'Skips first component, but sets as previous for next iteration
If IsNothing(prevCO) Then prevCO = coCollection.Item(i) : Continue For
'Un-comment line to daisy chain
' prevCO = coCollection.Item(i - 1)
'I'm not checking degrees of freedom for the batch process.
'It can be added, but i didn't want to interrupt the flow.
Call FlushOrigins(coCollection.Item(i), prevCO, aDoc.ComponentDefinition)
Next
End If
Exit Sub
ManualSelect:
Logger.Trace("Manual Selection Mode")
Dim oOcc1 As ComponentOccurrence
Dim oOcc2 As ComponentOccurrence
'User Selections
pick1: Dim PickThis As Object = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select Occurrence 1")
If IsNothing(PickThis) Then If MessageBox.Show("Nothing was selected for Occurrence #1. Would you like to pick again?", "Repeat?", MessageBoxButtons.YesNo) = vbYes Then GoTo pick1 Else Exit Sub
oOcc1 = PickThis
pick2: PickThis = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select Occurrence 2")
If IsNothing(PickThis) Then If MessageBox.Show("Nothing was selected for Occurrence #2. Would you like to pick again?", "Repeat?", MessageBoxButtons.YesNo) = vbYes Then GoTo pick2 Else Exit Sub
oOcc2 = PickThis
'Make sure 1 occurrence is fully un constrained
If FreedomDegrees(oOcc1) = 3 Or FreedomDegrees(oOcc2) = 3
Call FlushOrigins(oOcc1, oOcc2, aDoc.ComponentDefinition)
Else
If MessageBox.Show("If both Occurrences are have constraints, this rule may fail. Would you still like to run the rule?", "Attempt Constraints?", MessageBoxButtons.YesNo) = vbYes Then Call FlushOrigins(oOcc1, oOcc2, aDoc.ComponentDefinition)
End If
End Sub
Sub FlushOrigins(O1 As ComponentOccurrence, O2 As ComponentOccurrence, aCD As ComponentDefinition)
On Error GoTo EER
Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(aCD.Document, "Constraint Creation")
'Setup Proxies
Dim wPlane1 As WorkPlaneProxy
Dim wPlane2 As WorkPlaneProxy
'Run for 3 orgin planes:
For i = 1 To 3
O1.CreateGeometryProxy(O1.Definition.WorkPlanes.Item(i), wPlane1)
O2.CreateGeometryProxy(O2.Definition.WorkPlanes.Item(i), wPlane2)
Dim flushConst As AssemblyConstraint = aCD.Constraints.AddFlushConstraint(wPlane1, wPlane2, 0)
Next
oTrans.End
Exit Sub
EER :
oTrans.Abort
End Sub
Sub FlushOriginAssembly(O1 As ComponentOccurrence, aCD As ComponentDefinition)
On Error GoTo EER2
Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(aCD.Document, "Constraint Creation")
'Setup Proxies
Dim wPlane1 As WorkPlaneProxy
Dim wPlane2 As WorkPlane
'Run for Selected orgin planes:
For i = 1 To 3
O1.CreateGeometryProxy(O1.Definition.WorkPlanes.Item(i), wPlane1)
wPlane2 = aCD.WorkPlanes.Item(i)
Dim flushConst As AssemblyConstraint = aCD.Constraints.AddFlushConstraint(wPlane1, wPlane2, 0)
Next
oTrans.End
Exit Sub
EER2 :
oTrans.Abort
End Sub
Function FreedomDegrees(co As ComponentOccurrence) As Long
Dim OutputLong(1) As Long
Dim TranslationVectors As ObjectsEnumerator
Dim RotationVectors As ObjectsEnumerator
Dim DOFpoint As Point
Call co.GetDegreesOfFreedom(OutputLong(0), TranslationVectors, OutputLong(1), RotationVectors, DOFpoint)
Result = OutputLong(0)
Return Result
End Function
Let me know if you have any questions, or if this is not working as you would like.