Constrain two selected parts together using flush constraints on origin planes

arron.craig
Collaborator
Collaborator

Constrain two selected parts together using flush constraints on origin planes

arron.craig
Collaborator
Collaborator

I would like to be able to select two parts in an assembly and constrain their origin planes to each other using flush constraints. Essentially exactly the same funtion as "Ground and Root " with flush constraints except with two parts instead of one part and the assemblies origin. 

Has anyone got something simular I could use or modify?

Reply
Accepted solutions (1)
2,169 Views
12 Replies
Replies (12)

J-Camper
Advisor
Advisor
Accepted solution

I've actually done this exact thing, turned it into a nice internal utility.  anyways here is the basic sub routine process:

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
	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

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 intended.

arron.craig
Collaborator
Collaborator

That works perfectly! Thankyou

0 Likes

FLETCHER.A
Enthusiast
Enthusiast

I've wanted this to be added to inventor for a while, wondering if its something in the works with Autodesk.

 

EDIT: just tested it and it works a charm. Question have you tried to develop it so you can add more then 2 selections I often want to be able to select say 5 parts to kind of group them. I can see how you can add more parts however I want to be able to select any number of parts not a predetermined number

J-Camper
Advisor
Advisor

@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.

FLETCHER.A
Enthusiast
Enthusiast

Works perfectly thanks mate!

rinderwiesenPBXRB
Participant
Participant

Is There any way to only choose Two Planes and Not all Three?

J-Camper
Advisor
Advisor

@rinderwiesenPBXRB,

 

Yes, that is definitely possible.  I have an options controller for the version I use.  It works by setting a List(Of Integer) for the origin planes selected from a user input.  Then it uses that list of integers instead of the generic loop in the "FlushOrigins" sub of the posted code: "For i = 1 To 3" --> "For Each Item In List(Of Integer)".  The code I use has heavy integration to look for some custom iProperties we use internally and changes the List(Of Integer) to user created planes instead of origin planes when it recognizes certain part types/combinations.

 

If you have specific options I could help modify the posted code for you.  Do you want to stick to the origin planes, but just want to select a group of 2 planes instead of all three? 

 

let me know if you want help, or have any other questions.

rinderwiesenPBXRB
Participant
Participant

Thanks for the response JCamper

I ended cutting the constraint check out as I'm using this in another rule that uses a pick command to select the face to mate too with a predetermined offset. Then I just changed the For i = 1 To 3    to  For i = 2 to 3 and this worked so I only had two planes constraint to selected components.

Your coding is great, Thanks

 

nirfH9YJR
Explorer
Explorer

hello all, can I use this ilogic to pre-named planes ? as to flush specific planes I defined in the components with a specific name.

0 Likes

J-Camper
Advisor
Advisor

@nirfH9YJR,

 

I have done that exact thing in my personal auto-constrain rule.  It can find user created planes that existing in our starter parts/assemblies when it recognizes certain custom iProperties.

 

I mentioned in an earlier comment that I change the flush constraint creation loop from "For i = 1 To 3" to "For Each Item As Integer in List(Of Integer)".  Before getting to that loop in check the selected occurrences to see if they should use the unique planes, or stick to the origin planes.

 

Could you share a bit more about how you want this rule to interact with the user?  Do you want the batch selection?  Do you want them all to try to find the same named planes?  Will the named planes be in the same order in the WorkPlanes collection for each intended occurrence?  Do you want the constrain to assembly option, and if so should we look for the same named planes in the parent assembly?

0 Likes

DamianoTasca
Enthusiast
Enthusiast

Thank you so much for sharing this script which is really helpful and much appreciated!

It would be even nicer and more awesome if you could add a form with a series of checkboxes to be able to select which reference planes to use and maybe be able to see the preview of the results before confirming.

More or less something similar to the attached image...

This way you will be able to see the various combinations of constraints and choose the one you need very quickly.

Finally, if there was also the possibility to set the offset for each work surface, it would really be the best!

 

Thank you in any case for your support!

 

 

0 Likes

J-Camper
Advisor
Advisor

@DamianoTasca,

I like those ideas.  I haven't really done any UI building, outside of the built in forms for simple controls, but this seems like a good project to get more into it.  I will work on this, but I'm going on holiday soon so it likely won't be a quick development.

Thanks for the suggestions, and I will post back when I come up with something.

0 Likes