Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JhoelForshav
in reply to: shiftctrl.io

Hi @shiftctrl.io 

Try this code, it should work :slightly_smiling_face:

The difficult part here was to find the direction to searc for the faces in the panels that is not selected, since there are an infinite number of perpendicular faces to those two selected. I ended up using the edges of one of the faces outer edgeloop to get the directions to investigate.

 

Now it works as requested in your original post.

Sub Main
Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oDef As AssemblyComponentDefinition = oAsm.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim cmdM As CommandManager = ThisApplication.CommandManager
Dim oOcc1 As ComponentOccurrence = cmdM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Pick first panel.")
If oOcc1 Is Nothing Then Exit Sub
Dim oOcc2 As ComponentOccurrence = cmdM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Pick second panel.")
If oOcc2 Is Nothing Then Exit Sub

Dim oCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

Dim oPt1 As Point = oOcc1.MassProperties.CenterOfMass
Dim oPt2 As Point = oOcc2.MassProperties.CenterOfMass
Dim oPt1Pt2 As Vector = oPt1.VectorTo(oPt2)

'Find first two planes:
Dim oFirstFaces As ObjectsEnumerator = oDef.FindUsingVector(oPt1, oPt1Pt2.AsUnitVector, {SelectionFilterEnum.kPartFacePlanarFilter })
'Add the two first planes found (inside planes) to collection
oCol.Add(oFirstFaces(1))
oCol.Add(oFirstFaces(2))

'Move oPt1 to be the centerpoint of the partition
oPt1Pt2.ScaleBy(1 / 2)
oPt1.TranslateBy(oPt1Pt2)

'Use the outer edgeloop of one of the faces to get directions for finding the other faces
For Each oLoop As EdgeLoopProxy In oFirstFaces(1).EdgeLoops
	If oLoop.IsOuterEdgeLoop
		For Each oEdge As EdgeProxy In oLoop.Edges
			If TypeOf (oEdge.Geometry) Is LineSegment
				Dim oLine As LineSegment = oEdge.Geometry
				Dim oFoundEnts As ObjectsEnumerator = _
				oDef.FindUsingVector(oPt1, oLine.Direction, _
				{SelectionFilterEnum.kPartFacePlanarFilter })
				If oFoundEnts.Count > 0
					Dim oInvVector As UnitVector = oTG.CreateUnitVector _
					(oLine.Direction.X * -1, oLine.Direction.Y * -1, oLine.Direction.Z * -1)
					Dim oFoundEnts2 As ObjectsEnumerator = _
					oDef.FindUsingVector(oPt1, oInvVector, _
					{SelectionFilterEnum.kPartFacePlanarFilter })
					If oFoundEnts2.Count > 0
						oCol.Add(oFoundEnts(1))
						oCol.Add(oFoundEnts2(1))
					End If
				End If
			End If
		Next
		Exit For
	End If
Next
For Each oFace As FaceProxy In oCol
AddWorkPlaneInAssembly(oAsm, oFace, 0)
Next
End Sub

Sub AddWorkPlaneInAssembly(oAsm As AssemblyDocument, oFace As Object, oOffset As Double)
	Dim oDef As AssemblyComponentDefinition = oAsm.ComponentDefinition
	Dim oOriginPnt As Point
	Dim oXaxis, oYaxis As UnitVector
	oOriginPnt = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)
	oXaxis = ThisApplication.TransientGeometry.CreateUnitVector(1, 0, 0)
	oYaxis = ThisApplication.TransientGeometry.CreateUnitVector(0, 1, 0)
	Dim oPlane As WorkPlane = oDef.WorkPlanes.AddFixed(oOriginPnt, oXaxis, oYaxis)
	oPlane.AutoResize = True
	oDef.Constraints.AddFlushConstraint(oFace, oPlane, oOffset)
	oPlane.AutoResize = False
End Sub

The result in your latest picture will be obtained by selecting theese two components (with no respect to selection order)

selection.PNG