EKL Find Duplicate Parts

EKL Find Duplicate Parts

// EKL to find duplicate parts
// ASSUMPTION: "duplicate" = same VPMReference + equal global position matrix
// ASSUMPTION: editor root is the loaded physical product


// Get occurence of editor root
Let RootRef (VPMReference)
RootRef = GetEditorRoots("VPMReference").GetItem(1)
Let occRoot (ProductOccurrence)
occRoot = RootRef->ListOccurrences(RootRef).GetItem(1)

// Get everything under the root that is a 3DPart. MODIFY IF NEEDED for other objects
let all3DPartOccurrences (List)
all3DPartOccurrences = occRoot.Query( "ProductOccurrence", "x.Reference.GetAttributeString( 'V_usage' ) == '3DPart'" )

// Get list of all the unique reference objects that the 3D Parts point to
let uniqueParts (List)
if all3DPartOccurrences.Size() > 0
{
    uniqueParts = all3DPartOccurrences.Extract( "ProductOccurrence", "VPMReference", "y = x.Reference" )
    uniqueParts.RemoveDuplicates()
}

Let partRef (VPMReference)
for partRef inside uniqueParts
{
	// for a part reference, get all of its occurences under the root
	let partOccurences (List)
	partOccurences = partRef->ListOccurrences(RootRef)
	
	Let occ (ProductOccurrence)
	Let posMatrices (List)
	for occ inside partOccurences
	{
		// for each occurence, get its position matrix (GLOBAL, NOT LOCAL)
		Let inst (VPMInstance)
		set inst = occ
		Let M(matrix)
		M = inst.PositionMatrix
		
		// round values to 1e-06 mm (position matrix is 3x4)
		Let i,j (Integer)
		for i=1 while i<=3
		{
			for j=1 while j<=4	
			{
				M.Set(i, j, round(M.Get(i,j), "mm", 6))
			}
		}
		
		// check for duplicate position matrices before appending	
		Let prevM (Matrix)
		for prevM inside posMatrices
		{
			if M == prevM 
			{
				// multi-highlight duplicate items and notify
				AddToSelection(occ, false)		
				Notify("#", occ.Name)
			}	
		}
		posMatrices.Append(M)
	}
}