Reply

Create relation between occurrence and suboccurrence - vb.net

Hi,

I have a problem with creating a relation between the refence planes of a occurrence and a suboccurrence of a assembly.  I have attached the code that I have written so far.

 

The code works if both parts are occurrences, but when one of them is a suboccurrence the code won't work. I think it has something to do with creating a reference.

 

Does anyone have a idea how to solve this problem?

 

Kind regards

 

Paul Ebbers

 

Option Explicit 

Imports SolidedgeCommunity.Extensions
Imports SolidEdgeGeometry
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.linq
Imports System.text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

'-------------------------------------------------​--------------------------------- 
'When clicking the button, an "trolley.asm" must be open to run the example as is. 
'-------------------------------------------------​--------------------------------- 
Public Class Coordinates
<STAThread()> 
Public Shared Sub Run()	
	Dim objApplication As SolidEdgeFramework.Application = nothing
	Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
	Dim objDocuments As SolidEdgeFramework.Documents = Nothing
	Dim filename As String 
	
	' register with OLE to handle concurrence issues on the current thread.
	SolidEdgeCommunity.OleMessageFilter.Register()
	'connect to a running instance of Solid Edge.
	objApplication = Marshal.GetActiveObject("SolidEdge.Application")
	' Get a reference to the documents collection
	objDocuments = objApplication.Documents
	' get the active document, save it and get it again
	objAssembly = objApplication.ActiveDocument
	'Activate all parts 
	Call objAssembly.ActivateAll 
	
	filename = "Plate 3.par"
	selectPartsAnyLevel(objAssembly, filename) 
	
End Sub

'-------------------------------------------------​--------------------------------------- 
'This routine selects all the parts called "filename" at any level in the main asm 
'-------------------------------------------------​--------------------------------------- 
Public shared Sub selectPartsAnyLevel(objAssembly As SolidEdgeAssembly.AssemblyDocument, filename As String) 
			Dim objApplication As SolidEdgeFramework.Application = Nothing
			Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
			Dim Occurrence As SolidEdgeAssembly.Occurrence = Nothing
			Dim SubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing
			Dim SubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing
			Dim Occurrence1 As SolidEdgeAssembly.Occurrence = Nothing
			Dim Occurrence2 As SolidEdgeAssembly.Occurrence = Nothing
			Dim objPart1 As SolidEdgePart.PartDocument = Nothing
			Dim objPart2 As SolidEdgePart.PartDocument = Nothing
			Dim ObjPart1FrontPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart1RightPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart1TopPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2FrontPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2RightPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2TopPlane As SolidEdgeFramework.Reference = Nothing
			Dim Refplanes1 As SolidEdgePart.RefPlanes = Nothing
			Dim Refplanes2 As SolidEdgePart.RefPlanes = Nothing
			Dim XYZPoints1(0 To 5) As Double
			Dim XYZPoints2(0 To 5) As Double
			Dim XYZPoints3(0 To 5) As Double
			Dim XYZPoints4(0 To 5) As Double
			Dim XYZPoints5(0 To 5) As Double
			Dim XYZPoints6(0 To 5) As Double
			Dim Plane1 As SolidEdgePart.RefPlane = Nothing
			Dim Plane2 As SolidEdgePart.RefPlane = Nothing
			Dim Plane3 As SolidEdgePart.RefPlane = Nothing
			Dim Plane4 As SolidEdgePart.RefPlane = Nothing
			Dim Plane5 As SolidEdgePart.RefPlane = Nothing
			Dim Plane6 As SolidEdgePart.RefPlane = Nothing
			Dim PlanarRelationFront As SolidEdgeAssembly.PlanarRelation3d = Nothing
			Dim PlanarRelationRight As SolidEdgeAssembly.PlanarRelation3d = Nothing
			Dim PlanarRelationTop As SolidEdgeAssembly.PlanarRelation3d = Nothing 

			'Go through the occurrences in the assembly 
			For Each Occurrence In objAssembly.Occurrences 
				'If the part to select is a first level occurrence, i select it directly, there's no need to create a reference. 
				If StrComp(Path2File(Occurrence.OccurrenceFileName), filename, vbTextCompare) = 0 Then 
				Occurrences = objAssembly.Occurrences
				' get a reference to Occurrence1
				Occurrence1 = occurrences.Item(1)
				' get a reference to Occurrence2
				Occurrence2 = occurrences.AddByFilename("H:\Projects\5129 Suspended Coaster\Macro\Solid Edge Files\Plate 2.par")
	'			Occurrence2 = occurrences.AddByFilename(Filename)
				' All Occurrences placed through automation are placed "Grounded."
				' You must delete the ground constraint on the second Occurrence before you can place other relationships. 		          	
	            Dim objRelations3d As SolidEdgeAssembly.Relations3d = Occurrence2.Relations3d
		  			For Each objRelation In objRelations3d
		    			Dim relationType As Integer = objRelation.GetType().InvokeMember("Type", Reflection.BindingFlags.GetProperty, Nothing, objRelation, Nothing)
		    			If relationType = SolidEdgeFramework.ObjectType.igGroundRelation3d Then
		      			objRelation.GetType().InvokeMember("Delete", Reflection.BindingFlags.InvokeMethod, Nothing, objRelation, Nothing)
		     			Exit For
		   			 End If
		  			Next
				' get a reference to the Occurence1 document
				objPart1 = Occurrence1.OccurrenceDocument
				' get a reference to the Occurence2 document
				objPart2 = Occurrence2.OccurrenceDocument
				'get a reference to the ref planes collection of the first model
				Refplanes1 = objPart1.RefPlanes
				'get a reference to the ref planes collection of the first model
				Refplanes2 = objPart2.RefPlanes	
				'			
				' Part 1
				' Get the front plane of part 1
				Plane1 = Refplanes1.GetFrontPlane
				ObjPart1FrontPlane = objAssembly.CreateReference(Occurrence1, Plane1)		 
				Call Plane1.GetRootPoint(XYZPoints1)
				' Get the right plane of part 1
				Plane2 = Refplanes1.GetRightPlane
				ObjPart1RightPlane = objAssembly.CreateReference(Occurrence1, Plane2)		 
				Call Plane1.GetRootPoint(XYZPoints2)
				' Get the top plane of part 1
				Plane3 = Refplanes1.GetTopPlane
				ObjPart1TopPlane = objAssembly.CreateReference(Occurrence1, Plane3)		 
				Call Plane1.GetRootPoint(XYZPoints3)			
				'
				' Part 2
				' Get the front plane of part 2
				Plane4 = Refplanes2.GetFrontPlane
				ObjPart2FrontPlane = objAssembly.CreateReference(Occurrence2, Plane4)
				Call Plane2.GetRootPoint(XYZPoints4)
				' Get the right plane of part 2
				Plane5 = Refplanes2.GetRightPlane
				ObjPart2RightPlane = objAssembly.CreateReference(Occurrence2, Plane5)
				Call Plane2.GetRootPoint(XYZPoints5)			
				' Get the top plane of part 2
				Plane6 = Refplanes2.GetTopPlane
				ObjPart2TopPlane = objAssembly.CreateReference(Occurrence2, Plane6)
				Call Plane2.GetRootPoint(XYZPoints6)
				            
	            ' Create a relation between front planes
	            PlanarRelationFront = objAssembly.Relations3d.AddPlanar(ObjPart1FrontPlane, ObjPart2FrontPlane, False, XYZPoints1, XYZPoints4)
	            ' Create a relation between right planes
	            PlanarRelationRight = objAssembly.Relations3d.AddPlanar(ObjPart1RightPlane, ObjPart2RightPlane, False, XYZPoints2, XYZPoints5)
	            ' Create a relation between front planes
	            PlanarRelationTop = objAssembly.Relations3d.AddPlanar(ObjPart1TopPlane, ObjPart2TopPlane, False, XYZPoints3, XYZPoints6) 			
				End If

				If Occurrence.Subassembly Then 
	 			'For each suboccurrence in subassemblies at first level, search recursively and select parts. 
	 				For Each SubOccurrence In Occurrence.SubOccurrences 
	 				ProcessSubAssembly(SubOccurrence, filename) 
	 				Next 
	 			End If 
 			Next 
End Sub 

'-------------------------------------------------​--------- 
'Process the suboccurrences recursively to select parts. 
'-------------------------------------------------​--------- 
Public shared Sub ProcessSubAssembly(SubOccurrence As SolidEdgeAssembly.SubOccurrence, filename As String) 
		Dim ref As SolidEdgeFramework.Reference = Nothing
		Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
		Dim objApplication As SolidEdgeFramework.Application = Nothing
		Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
		Dim Occurrence As SolidEdgeAssembly.Occurrence = Nothing
		Dim SubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing
		Dim subSubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing 
		Dim Occurrence1 As SolidEdgeAssembly.SubOccurrence = Nothing
		Dim Occurrence2 As SolidEdgeAssembly.Occurrence = Nothing
		Dim objPart1 As SolidEdgePart.PartDocument = Nothing
		Dim objPart2 As SolidEdgePart.PartDocument = Nothing
		Dim ObjPart1FrontPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart1RightPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart1TopPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2FrontPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2RightPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2TopPlane As SolidEdgeFramework.Reference = Nothing
		Dim Refplanes1 As SolidEdgePart.RefPlanes = Nothing
		Dim Refplanes2 As SolidEdgePart.RefPlanes = Nothing
		Dim XYZPoints1(0 To 5) As Double
		Dim XYZPoints2(0 To 5) As Double
		Dim XYZPoints3(0 To 5) As Double
		Dim XYZPoints4(0 To 5) As Double
		Dim XYZPoints5(0 To 5) As Double
		Dim XYZPoints6(0 To 5) As Double
		Dim Plane1 As SolidEdgePart.RefPlane = Nothing
		Dim Plane2 As SolidEdgePart.RefPlane = Nothing
		Dim Plane3 As SolidEdgePart.RefPlane = Nothing
		Dim Plane4 As SolidEdgePart.RefPlane = Nothing
		Dim Plane5 As SolidEdgePart.RefPlane = Nothing
		Dim Plane6 As SolidEdgePart.RefPlane = Nothing
		Dim PlanarRelationFront As SolidEdgeAssembly.PlanarRelation3d = Nothing
		Dim PlanarRelationRight As SolidEdgeAssembly.PlanarRelation3d = Nothing
		Dim PlanarRelationTop As SolidEdgeAssembly.PlanarRelation3d = Nothing 
		
		ref = createTopASMRef(SubOccurrence)
		objAssembly = SubOccurrence.TopLevelDocument
		
		'Select this suboccurrence if matches the name.
			'Go through the occurrences in the assembly 
			'For Each SubOccurrence In Occurrence.SubOccurrences
				'If the part to select is a first level occurrence, i select it directly, there's no need to create a reference. 
				If StrComp(Path2File(SubOccurrence.SubOccurrenceFileName), filename, vbTextCompare) = 0 Then 
				' get a reference to Occurrence1
				Occurrence1 = SubOccurrence.SubOccurrenceDocument
				' get a reference to Occurrence2
				Occurrence2 = occurrences.AddByFilename("H:\Projects\5129 Suspended Coaster\Macro\Solid Edge Files\Plate 2.par")
				' All Occurrences placed through automation are placed "Grounded."
				' You must delete the ground constraint on the second Occurrence before you can place other relationships. 		          	
	            Dim objRelations3d As SolidEdgeAssembly.Relations3d = Occurrence2.Relations3d
		  			For Each objRelation In objRelations3d
		    			Dim relationType As Integer = objRelation.GetType().InvokeMember("Type", Reflection.BindingFlags.GetProperty, Nothing, objRelation, Nothing)
		    			If relationType = SolidEdgeFramework.ObjectType.igGroundRelation3d Then
		      			objRelation.GetType().InvokeMember("Delete", Reflection.BindingFlags.InvokeMethod, Nothing, objRelation, Nothing)
		     			Exit For
		   			 End If
		  			Next
				' get a reference to the Occurence1 document
				objPart1 = Occurrence1.SubOccurrenceDocument
				' get a reference to the Occurence2 document
				objPart2 = Occurrence2.OccurrenceDocument
				'get a reference to the ref planes collection of the first model
				Refplanes1 = objPart1.RefPlanes
				'get a reference to the ref planes collection of the first model
				Refplanes2 = objPart2.RefPlanes	
				'			
				' Part 1
				' Get the front plane of part 1
				Plane1 = Refplanes1.GetFrontPlane
				ObjPart1FrontPlane = Occurrence1.TopLevelDocument.CreateReference(Occurrence1.ThisAsOccurrence, Plane1)		 
				Call Plane1.GetRootPoint(XYZPoints1)
				' Get the right plane of part 1
				Plane2 = Refplanes1.GetRightPlane
				ObjPart1RightPlane = Occurrence1.TopLevelDocument.CreateReference(Occurrence1.ThisAsOccurrence, Plane2)		 
				Call Plane1.GetRootPoint(XYZPoints2)
				' Get the top plane of part 1
				Plane3 = Refplanes1.GetTopPlane
				ObjPart1TopPlane = Occurrence1.TopLevelDocument.CreateReference(Occurrence1.ThisAsOccurrence, Plane3)		 
				Call Plane1.GetRootPoint(XYZPoints3)			
				'
				' Part 2
				' Get the front plane of part 2
				Plane4 = Refplanes2.GetFrontPlane
				ObjPart2FrontPlane = objAssembly.CreateReference(Occurrence2, Plane4)
				Call Plane2.GetRootPoint(XYZPoints4)
				' Get the right plane of part 2
				Plane5 = Refplanes2.GetRightPlane
				ObjPart2RightPlane = objAssembly.CreateReference(Occurrence2, Plane5)
				Call Plane2.GetRootPoint(XYZPoints5)			
				' Get the top plane of part 2
				Plane6 = Refplanes2.GetTopPlane
				ObjPart2TopPlane = objAssembly.CreateReference(Occurrence2, Plane6)
				Call Plane2.GetRootPoint(XYZPoints6)
				            
	            ' Create a relation between front planes
	            PlanarRelationFront = objAssembly.Relations3d.AddPlanar(ObjPart1FrontPlane, ObjPart2FrontPlane, False, XYZPoints1, XYZPoints4)
	            ' Create a relation between right planes
	            PlanarRelationRight = objAssembly.Relations3d.AddPlanar(ObjPart1RightPlane, ObjPart2RightPlane, False, XYZPoints2, XYZPoints5)
	            ' Create a relation between front planes
	            PlanarRelationTop = objAssembly.Relations3d.AddPlanar(ObjPart1TopPlane, ObjPart2TopPlane, False, XYZPoints3, XYZPoints6) 			
				End If 
 			'Next  

				 'If it is a subassembly go through it recursively. 
				 If SubOccurrence.Subassembly Then 
				 For Each subSuboccurrence In SubOccurrence.SubOccurrences 
				 ProcessSubAssembly(subSubOccurrence, filename) 
				 Next 
				 End If 
		End Sub 


'-------------------------------------------------​--------------------------------------------------​--------------------------------- 
'CreateTopASMref: creates a reference for any subpart at any level. This is used for selections. 
'When you select a suboccurrence at any level you are just selecting a reference of a reference of a reference of an occurrence... 
'-------------------------------------------------​--------------------------------------------------​-------------------------------- 
Public shared Function createTopASMRef(ByVal pSubOcc As SolidEdgeAssembly.SubOccurrence) As SolidEdgeFramework.Reference 
 Dim pOcc As SolidEdgeAssembly.Occurrence 
 Dim pRef As SolidEdgeFramework.Reference 
 Dim pSubOccParent As Object 

 'Get the subOccurrence parent. This can be a SubOccurrence or an Occurrence. 
 pSubOccParent = pSubOcc.Parent 


 'If the parent of the suboccurrence is an Occurrence (first level, type igSubAssembly) i just create a simple reference and that's all. 
 If pSubOccParent.Type = SolidEdgeFramework.ObjectType.igSubAssembly Then 
 pRef = pSubOcc.TopLevelDocument.CreateReference(pSubOccParent, pSubOcc.ThisAsOccurrence) 
 Else 
 'But if the parent is a subOccurrence (it's deeper that first level) 
 'I create a reference of the subocurrence related to his immediate parent (as an occurrence of its direct subassembly) 
 pRef = pSubOcc.TopLevelDocument.CreateReference(pSubOccParent.ThisAsOccurrence, pSubOcc.ThisAsOccurrence) 

 'I get the subassembly parent of the original subassembly parent and this way i will be doing a reference of a refrence, etc of the parents 
 pSubOccParent = pSubOccParent.Parent 

 'I use this loop to get the "recursive" reference until i find that the parent is not a SubOccurrence anymore. 
 '(Finally it is a igSubAssembly, which is a first level Occurrence) 
 Do While pSubOccParent.Type = SolidEdgeFramework.ObjectType.igSubOccurrence 
 'Get a refrence to the immediate parent 
 pRef = pSubOccParent.TopLevelDocument.CreateReference(pSubOccParent.ThisAsOccurrence, pRef) 

 'Get the "grandparent", etc. 
 pSubOccParent = pSubOccParent.Parent 
 Loop 

 'Finally i do the last reference for the last step. The Occurrence in the main assembly. 
 pRef = pSubOccParent.TopLevelDocument.CreateReference(pSubOccParent, pRef) 
 End If 

 'The function returns the calculated reference 
 createTopASMRef = pRef 

 'I clean up objects 
 pSubOccParent = Nothing 
 pRef = Nothing 
End Function 

shared Public Function Path2File(ByVal s As String) As String 
 Dim pos As Integer, pos2 As Integer 
 Dim sSep As String 

 pos2 = InStrRev(s, "\") 
 Path2File = Mid$(s, pos2 + 1) 
End Function 
End Class

 

1 REPLY

Re: Create relation between occurrence and suboccurrence - vb.net

Hi again,

 

I have it working! I have the code below for anyone who is interested.

 

Kind regards,

 

Paul Ebbers

 

Option Explicit 

Imports SolidedgeCommunity.Extensions
Imports SolidEdgeGeometry
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.linq
Imports System.text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

'-------------------------------------------------​--------------------------------- 
'When clicking the button, an "trolley.asm" must be open to run the example as is. 
'-------------------------------------------------​--------------------------------- 
Public Class Coordinates
<STAThread()> 
Public Shared Sub Run()	
	Dim objApplication As SolidEdgeFramework.Application = nothing
	Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
	Dim objDocuments As SolidEdgeFramework.Documents = Nothing
	Dim filename As String 
	
	' register with OLE to handle concurrence issues on the current thread.
	SolidEdgeCommunity.OleMessageFilter.Register()
	'connect to a running instance of Solid Edge.
	objApplication = Marshal.GetActiveObject("SolidEdge.Application")
	' Get a reference to the documents collection
	objDocuments = objApplication.Documents
	' get the active document, save it and get it again
	objAssembly = objApplication.ActiveDocument
	'Activate all parts 
	Call objAssembly.ActivateAll 
	
	filename = "Plate 3.par"
	selectPartsAnyLevel(objAssembly, filename) 
	
End Sub

'-------------------------------------------------​--------------------------------------- 
'This routine selects all the parts called "filename" at any level in the main asm 
'-------------------------------------------------​--------------------------------------- 
Public shared Sub selectPartsAnyLevel(objAssembly As SolidEdgeAssembly.AssemblyDocument, filename As String) 
			Dim objApplication As SolidEdgeFramework.Application = Nothing
			Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
			Dim Occurrence As SolidEdgeAssembly.Occurrence = Nothing
			Dim SubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing
			Dim SubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing
			Dim Occurrence1 As SolidEdgeAssembly.Occurrence = Nothing
			Dim Occurrence2 As SolidEdgeAssembly.Occurrence = Nothing
			Dim objPart1 As SolidEdgePart.PartDocument = Nothing
			Dim objPart2 As SolidEdgePart.PartDocument = Nothing
			Dim ObjPart1FrontPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart1RightPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart1TopPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2FrontPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2RightPlane As SolidEdgeFramework.Reference = Nothing
			Dim ObjPart2TopPlane As SolidEdgeFramework.Reference = Nothing
			Dim Refplanes1 As SolidEdgePart.RefPlanes = Nothing
			Dim Refplanes2 As SolidEdgePart.RefPlanes = Nothing
			Dim XYZPoints1(0 To 5) As Double
			Dim XYZPoints2(0 To 5) As Double
			Dim XYZPoints3(0 To 5) As Double
			Dim XYZPoints4(0 To 5) As Double
			Dim XYZPoints5(0 To 5) As Double
			Dim XYZPoints6(0 To 5) As Double
			Dim Plane1 As SolidEdgePart.RefPlane = Nothing
			Dim Plane2 As SolidEdgePart.RefPlane = Nothing
			Dim Plane3 As SolidEdgePart.RefPlane = Nothing
			Dim Plane4 As SolidEdgePart.RefPlane = Nothing
			Dim Plane5 As SolidEdgePart.RefPlane = Nothing
			Dim Plane6 As SolidEdgePart.RefPlane = Nothing
			Dim PlanarRelationFront As SolidEdgeAssembly.PlanarRelation3d = Nothing
			Dim PlanarRelationRight As SolidEdgeAssembly.PlanarRelation3d = Nothing
			Dim PlanarRelationTop As SolidEdgeAssembly.PlanarRelation3d = Nothing 

			'Go through the occurrences in the assembly 
			For Each Occurrence In objAssembly.Occurrences 
				'If the part to select is a first level occurrence, i select it directly, there's no need to create a reference. 
				If StrComp(Path2File(Occurrence.OccurrenceFileName), filename, vbTextCompare) = 0 Then 
				Occurrences = objAssembly.Occurrences
				' get a reference to Occurrence1
				Occurrence1 = occurrences.Item(2).SubOccurrences.Item(1)
				' get a reference to Occurrence2
				Occurrence2 = occurrences.AddByFilename("H:\Projects\5129 Suspended Coaster\Macro\Solid Edge Files\Plate 2.par")
	'			Occurrence2 = occurrences.AddByFilename(Filename)
				' All Occurrences placed through automation are placed "Grounded."
				' You must delete the ground constraint on the second Occurrence before you can place other relationships. 		          	
	            Dim objRelations3d As SolidEdgeAssembly.Relations3d = Occurrence2.Relations3d
		  			For Each objRelation In objRelations3d
		    			Dim relationType As Integer = objRelation.GetType().InvokeMember("Type", Reflection.BindingFlags.GetProperty, Nothing, objRelation, Nothing)
		    			If relationType = SolidEdgeFramework.ObjectType.igGroundRelation3d Then
		      			objRelation.GetType().InvokeMember("Delete", Reflection.BindingFlags.InvokeMethod, Nothing, objRelation, Nothing)
		     			Exit For
		   			 End If
		  			Next
				' get a reference to the Occurence1 document
				objPart1 = Occurrence1.OccurrenceDocument
				' get a reference to the Occurence2 document
				objPart2 = Occurrence2.OccurrenceDocument
				'get a reference to the ref planes collection of the first model
				Refplanes1 = objPart1.RefPlanes
				'get a reference to the ref planes collection of the first model
				Refplanes2 = objPart2.RefPlanes	
				'			
				' Part 1
				' Get the front plane of part 1
				Plane1 = Refplanes1.GetFrontPlane
				ObjPart1FrontPlane = objAssembly.CreateReference(Occurrence1, Plane1)		 
				Call Plane1.GetRootPoint(XYZPoints1)
				' Get the right plane of part 1
				Plane2 = Refplanes1.GetRightPlane
				ObjPart1RightPlane = objAssembly.CreateReference(Occurrence1, Plane2)		 
				Call Plane2.GetRootPoint(XYZPoints2)
				' Get the top plane of part 1
				Plane3 = Refplanes1.GetTopPlane
				ObjPart1TopPlane = objAssembly.CreateReference(Occurrence1, Plane3)		 
				Call Plane3.GetRootPoint(XYZPoints3)			
				'
				' Part 2
				' Get the front plane of part 2
				Plane4 = Refplanes2.GetFrontPlane
				ObjPart2FrontPlane = objAssembly.CreateReference(Occurrence2, Plane4)
				Call Plane2.GetRootPoint(XYZPoints4)
				' Get the right plane of part 2
				Plane5 = Refplanes2.GetRightPlane
				ObjPart2RightPlane = objAssembly.CreateReference(Occurrence2, Plane5)
				Call Plane2.GetRootPoint(XYZPoints5)			
				' Get the top plane of part 2
				Plane6 = Refplanes2.GetTopPlane
				ObjPart2TopPlane = objAssembly.CreateReference(Occurrence2, Plane6)
				Call Plane2.GetRootPoint(XYZPoints6)
				            
	            ' Create a relation between front planes
	            PlanarRelationFront = objAssembly.Relations3d.AddPlanar(ObjPart1FrontPlane, ObjPart2FrontPlane, False, XYZPoints1, XYZPoints4)
	            ' Create a relation between right planes
	            PlanarRelationRight = objAssembly.Relations3d.AddPlanar(ObjPart1RightPlane, ObjPart2RightPlane, False, XYZPoints2, XYZPoints5)
	            ' Create a relation between front planes
	            PlanarRelationTop = objAssembly.Relations3d.AddPlanar(ObjPart1TopPlane, ObjPart2TopPlane, False, XYZPoints3, XYZPoints6) 			
				End If

				If Occurrence.Subassembly Then 
	 			'For each suboccurrence in subassemblies at first level, search recursively and select parts. 
	 				For Each SubOccurrence In Occurrence.SubOccurrences 
	 				ProcessSubAssembly(SubOccurrence, filename) 
	 				Next 
	 			End If 
 			Next 
End Sub 

'-------------------------------------------------​--------- 
'Process the suboccurrences recursively to select parts. 
'-------------------------------------------------​--------- 
Public shared Sub ProcessSubAssembly(SubOccurrence As SolidEdgeAssembly.SubOccurrence, filename As String) 
		Dim ref As SolidEdgeFramework.Reference = Nothing
		Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
		Dim objApplication As SolidEdgeFramework.Application = Nothing
		Dim Occurrences As SolidEdgeAssembly.Occurrences = Nothing
		Dim Occurrence As SolidEdgeAssembly.Occurrence = Nothing
		Dim SubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing
		Dim subSubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing 
		Dim Occurrence1 As SolidEdgeAssembly.Occurrence = Nothing
		Dim Occurrence2 As SolidEdgeAssembly.Occurrence = Nothing
		Dim SubOccurrence1 As SolidEdgeAssembly.SubOccurrence = Nothing
		Dim objPart1 As SolidEdgePart.PartDocument = Nothing
		Dim objPart2 As SolidEdgePart.PartDocument = Nothing
		Dim ObjPart1FrontPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart1RightPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart1TopPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2FrontPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2RightPlane As SolidEdgeFramework.Reference = Nothing
		Dim ObjPart2TopPlane As SolidEdgeFramework.Reference = Nothing
		Dim Refplanes1 As SolidEdgePart.RefPlanes = Nothing
		Dim Refplanes2 As SolidEdgePart.RefPlanes = Nothing
		Dim XYZPoints1(0 To 5) As Double
		Dim XYZPoints2(0 To 5) As Double
		Dim XYZPoints3(0 To 5) As Double
		Dim XYZPoints4(0 To 5) As Double
		Dim XYZPoints5(0 To 5) As Double
		Dim XYZPoints6(0 To 5) As Double
		Dim Plane1 As SolidEdgePart.RefPlane = Nothing
		Dim Plane2 As SolidEdgePart.RefPlane = Nothing
		Dim Plane3 As SolidEdgePart.RefPlane = Nothing
		Dim Plane4 As SolidEdgePart.RefPlane = Nothing
		Dim Plane5 As SolidEdgePart.RefPlane = Nothing
		Dim Plane6 As SolidEdgePart.RefPlane = Nothing
		Dim PlanarRelationFront As SolidEdgeAssembly.PlanarRelation3d = Nothing
		Dim PlanarRelationRight As SolidEdgeAssembly.PlanarRelation3d = Nothing
		Dim PlanarRelationTop As SolidEdgeAssembly.PlanarRelation3d = Nothing 
		
		objAssembly = SubOccurrence.TopLevelDocument
				'If the part to select is a first level occurrence, i select it directly, there's no need to create a reference. 
				If StrComp(Path2File(SubOccurrence.SubOccurrenceFileName), filename, vbTextCompare) = 0 Then 
				' get a reference to Occurrence2
				Occurrence2 = SubOccurrence.TopLevelDocument.Occurrences.AddByFilename("H:\Projects\5129 Suspended Coaster\Macro\Solid Edge Files\Plate 2.par")
				' All Occurrences placed through automation are placed "Grounded."
				' You must delete the ground constraint on the second Occurrence before you can place other relationships. 		          	
	            Dim objRelations3d As SolidEdgeAssembly.Relations3d = Occurrence2.Relations3d
		  			For Each objRelation In objRelations3d
		    			Dim relationType As Integer = objRelation.GetType().InvokeMember("Type", Reflection.BindingFlags.GetProperty, Nothing, objRelation, Nothing)
		    			If relationType = SolidEdgeFramework.ObjectType.igGroundRelation3d Then
		      			objRelation.GetType().InvokeMember("Delete", Reflection.BindingFlags.InvokeMethod, Nothing, objRelation, Nothing)
		     			Exit For
		   			 End If
		  			Next
				' get a reference to the Occurence1 document
				objPart1 = SubOccurrence.SubOccurrenceDocument
				' get a reference to the Occurence2 document
				objPart2 = Occurrence2.OccurrenceDocument
				'get a reference to the ref planes collection of the first model
				Refplanes1 = objPart1.RefPlanes
				'get a reference to the ref planes collection of the first model
				Refplanes2 = objPart2.RefPlanes	
				'			
				' Part 1
				' Get the front plane of part 1
				Plane1 = Refplanes1.GetFrontPlane
				ObjPart1FrontPlane = createTopASMRef(SubOccurrence, Plane1)
				Call Plane1.GetRootPoint(XYZPoints1)
				' Get the right plane of part 1
				Plane2 = Refplanes1.GetRightPlane
				ObjPart1RightPlane = createTopASMRef(SubOccurrence, Plane2)		 
				Call Plane2.GetRootPoint(XYZPoints2)
				' Get the top plane of part 1
				Plane3 = Refplanes1.GetTopPlane
				ObjPart1TopPlane = createTopASMRef(SubOccurrence, Plane3)		 
				Call Plane3.GetRootPoint(XYZPoints3)			
				'
				' Part 2
				' Get the front plane of part 2
				Plane4 = Refplanes2.GetFrontPlane
				ObjPart2FrontPlane = objAssembly.CreateReference(Occurrence2, Plane4)			
				Call Plane2.GetRootPoint(XYZPoints4)
				' Get the right plane of part 2
				Plane5 = Refplanes2.GetRightPlane
				ObjPart2RightPlane = objAssembly.CreateReference(Occurrence2, Plane5)
				Call Plane2.GetRootPoint(XYZPoints5)			
				' Get the top plane of part 2
				Plane6 = Refplanes2.GetTopPlane
				ObjPart2TopPlane = objAssembly.CreateReference(Occurrence2, Plane6)
				Call Plane2.GetRootPoint(XYZPoints6)
				            
	            ' Create a relation between front planes
	            PlanarRelationFront = objAssembly.Relations3d.AddPlanar(ObjPart1FrontPlane, ObjPart2FrontPlane, False, XYZPoints1, XYZPoints4)
	            ' Create a relation between right planes
	            PlanarRelationRight = objAssembly.Relations3d.AddPlanar(ObjPart1RightPlane, ObjPart2RightPlane, False, XYZPoints2, XYZPoints5)
	            ' Create a relation between front planes
	            PlanarRelationTop = objAssembly.Relations3d.AddPlanar(ObjPart1TopPlane, ObjPart2TopPlane, False, XYZPoints3, XYZPoints6) 			
				End If   
			 'If it is a subassembly go through it recursively. 
			 If SubOccurrence.Subassembly Then 
			 For Each subSuboccurrence In SubOccurrence.SubOccurrences 
			 ProcessSubAssembly(subSubOccurrence, filename) 
			 Next 
			 End If 
		End Sub 


'Function that creates a reference of object "obj" that is inside a suboccurrence "pSubOcc" placed at any sublevel inside the main assembly
    Public shared Function createTopASMRef(ByVal pSubOcc As SolidEdgeAssembly.SubOccurrence, ByVal obj As Object) As SolidEdgeFramework.Reference
        Dim pOcc As SolidEdgeAssembly.Occurrence
        Dim pRef As SolidEdgeFramework.Reference

        'We construct the reference of obj recursively to reach the top assembly
        'The first reference to start with is the reference of object "obj" to the immediate containing subassembly.
        'The references are always constructed referring the main top assembly, because we want the relationships are being generated there.
        pRef = pSubOcc.TopLevelDocument.CreateReference(pSubOcc.ThisAsOccurrence, obj)

        While True 'infinite loop
            Try
                'In each loop i get the immediate parent for the suboccurrence in the variable pSubOcc (the subassembly containing the subassembly ... etc)
                pSubOcc = pSubOcc.Parent
            Catch
                'When there is an error is because the immediate parent is not a suboccurrence anymore, but an occurrence of the main top assembly.
                'We have reached the top. pOcc is an Occurrence of the top assembly.
                'We generate the last reference and exit the loop
                pOcc = pSubOcc.Parent
                pRef = pOcc.TopLevelDocument.CreateReference(pOcc, pRef)
                Exit While
            End Try
            'If there is no error is because pSubOcc is still a suboccurrence (a subassembly inside a subassembly...)
            'I generate the reference of the last reference i generated to the object. (finally it is a reference of a reference of a reference...)
            pRef = pSubOcc.TopLevelDocument.CreateReference(pSubOcc.ThisAsOccurrence, pRef)
        End While

        'When i abandone the loop i have got the reference i was looking for. I return it.
        createTopASMRef = pRef

        'Clean it all.
        pSubOcc = Nothing
        pOcc = Nothing
        pRef = Nothing
        GC.Collect()
    End Function
    
	'-------------------------------------------------​-- 
	'Converts a full path into filename without path. 
	'-------------------------------------------------​-- 
	shared Public Function Path2File(ByVal s As String) As String 
		Dim pos As Integer, pos2 As Integer 
		Dim sSep As String 
	
		pos2 = InStrRev(s, "\") 
		Path2File = Mid$(s, pos2 + 1) 
	End Function 
    
	'This was declared as a function because I want to be able to return a filename
	'the filename will be returned as a string
	Public shared Function OpenFile() As String
		'declare a string, this is will contain the filename that we return
		Dim strFileName = ""
		'declare a new open file dialog
		Dim fileDialogBox As New OpenFileDialog() 
		
	 
		'add file filters, this step is optional, but if you notice the screenshot
		'above it does not look clean if you leave it off, I explained in further
		'detail on my site how to add/modify these values
		fileDialogBox.Filter = "Solid Edge Files (*.asm; *.par; *.psm; *.dft; *.pwd)|*.asm; *.par; *.psm; *.dft; *.pwd|" _
			& "Part Documents (*.par)|*.par|" _
			& "Assembly Documents (*.asm)|*.asm|" _		
			& "Sheetmetal Documents (*.psm)|*.psm|" _
			& "Draft Documents (*.dft)|*.dft|" _
			& "Weldment Documents (*.pwd)|*.pwd|" _
			& "All Files(*.*)|"
		'this sets the default filter that we created in the line above, if you don't 
		'set a FilterIndex it will automatically default to 1
		fileDialogBox.FilterIndex = 2
		'this line tells the file dialog box what folder it should start off in first
		'I selected the users my document folder
		'fileDialogBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
	 
		'Check to see if the user clicked the open button
		If (fileDialogBox.ShowDialog() = DialogResult.OK) Then
		    strFileName = fileDialogBox.FileName
		Else
		   MsgBox("You did not select a file!")
		End If
	 
		'return the name of the file
		Return strFileName
	End Function
End Class