Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Remove duplicate objects from collection and compare to another collection

2 REPLIES 2
Reply
Message 1 of 3
MTheDesigner
527 Views, 2 Replies

Remove duplicate objects from collection and compare to another collection

MTheDesigner
Advocate
Advocate

Right now I am writing some code that automatically finds all the start and end points of line segments to be placed in an ordinate set.

 

I currently have code that creates a set on the left and right side, however, because 2 separate collections are required they sometimes have values that overlap. For example, both sides have a 1 1/2 measurement. I would like to only have one on the left side. 

 

I have been trying to write a function to compare the 2 lists and remove any value that is the same in the second list, but I have had no success getting it to work. Part of the kicker is that the first entry in both lists will be the same and has to avoid deletion.

 

Any help would be appreciated.

 

Public Function CheckForDuplicates (Col1 As ObjectCollection, Col2 As ObjectCollection) As ObjectCollection
	Dim FirstPoint As GeometryIntent
	Dim SecondPoint As GeometryIntent
	Dim oNewCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	For item =1 To Col2.Count
		oNewCol.Add(Col2.Item(item))
	Next
	For i=2 To Col1.Count 
		FirstPoint = Col1.Item(i)
		For j=1 To Col2.Count 
			SecondPoint = Col2.Item(j)
			If FirstPoint.PointOnSheet.X = SecondPoint.PointOnSheet.X Or FirstPoint.PointOnSheet.Y = SecondPoint.PointOnSheet.Y
			 oNewCol.Remove(j)
			End If
		Next
	Next
	
Return oNewCol
End Function

 

0 Likes

Remove duplicate objects from collection and compare to another collection

Right now I am writing some code that automatically finds all the start and end points of line segments to be placed in an ordinate set.

 

I currently have code that creates a set on the left and right side, however, because 2 separate collections are required they sometimes have values that overlap. For example, both sides have a 1 1/2 measurement. I would like to only have one on the left side. 

 

I have been trying to write a function to compare the 2 lists and remove any value that is the same in the second list, but I have had no success getting it to work. Part of the kicker is that the first entry in both lists will be the same and has to avoid deletion.

 

Any help would be appreciated.

 

Public Function CheckForDuplicates (Col1 As ObjectCollection, Col2 As ObjectCollection) As ObjectCollection
	Dim FirstPoint As GeometryIntent
	Dim SecondPoint As GeometryIntent
	Dim oNewCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	For item =1 To Col2.Count
		oNewCol.Add(Col2.Item(item))
	Next
	For i=2 To Col1.Count 
		FirstPoint = Col1.Item(i)
		For j=1 To Col2.Count 
			SecondPoint = Col2.Item(j)
			If FirstPoint.PointOnSheet.X = SecondPoint.PointOnSheet.X Or FirstPoint.PointOnSheet.Y = SecondPoint.PointOnSheet.Y
			 oNewCol.Remove(j)
			End If
		Next
	Next
	
Return oNewCol
End Function

 

2 REPLIES 2
Message 2 of 3
WCrihfield
in reply to: MTheDesigner

WCrihfield
Mentor
Mentor

Hi @MTheDesigner.  I created a Sub routine you can try out to see if it will would work better for you than the Function.  The two input object collections are added ByRef so that it will effect the source objects, instead of creating a new third one, since all we really need is to eliminate duplicates within the second one.

Public Sub EliminateDuplicates(ByRef Col1 As ObjectCollection, ByRef Col2 As ObjectCollection)
	Dim Col1First As GeometryIntent = Col1.Item(1)
	Dim Col2First As GeometryIntent = Col2.Item(1)
	For Each oObj1 In Col1
		If oObj1 Is Col1First Then Continue For
		Dim Col1GInt As GeometryIntent = oObj1
		For Each oObj2 In Col2
			If oObj2 Is Col2First Then Continue For
			Dim Col2GInt As GeometryIntent = oObj2
			If Col1GInt.PointOnSheet.X = Col2GInt.PointOnSheet.X Or _
				Col1GInt.PointOnSheet.Y = Col2GInt.PointOnSheet.Y Then
				Col2.RemoveByObject(oObj2)
			End If
		Next
	Next
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @MTheDesigner.  I created a Sub routine you can try out to see if it will would work better for you than the Function.  The two input object collections are added ByRef so that it will effect the source objects, instead of creating a new third one, since all we really need is to eliminate duplicates within the second one.

Public Sub EliminateDuplicates(ByRef Col1 As ObjectCollection, ByRef Col2 As ObjectCollection)
	Dim Col1First As GeometryIntent = Col1.Item(1)
	Dim Col2First As GeometryIntent = Col2.Item(1)
	For Each oObj1 In Col1
		If oObj1 Is Col1First Then Continue For
		Dim Col1GInt As GeometryIntent = oObj1
		For Each oObj2 In Col2
			If oObj2 Is Col2First Then Continue For
			Dim Col2GInt As GeometryIntent = oObj2
			If Col1GInt.PointOnSheet.X = Col2GInt.PointOnSheet.X Or _
				Col1GInt.PointOnSheet.Y = Col2GInt.PointOnSheet.Y Then
				Col2.RemoveByObject(oObj2)
			End If
		Next
	Next
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3
MTheDesigner
in reply to: WCrihfield

MTheDesigner
Advocate
Advocate
Hey @WCrihfeild, Thanks, your solution worked great. Though I thought that what I was doing was similar. I will have to check my code again to find out why mine failed where yours succeeded.
0 Likes

Hey @WCrihfeild, Thanks, your solution worked great. Though I thought that what I was doing was similar. I will have to check my code again to find out why mine failed where yours succeeded.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report