Need some help to create Ilogic for "Check iproperties in part and rename partnumber part in *.iam" please

Need some help to create Ilogic for "Check iproperties in part and rename partnumber part in *.iam" please

ballnr
Advocate Advocate
2,239 Views
13 Replies
Message 1 of 14

Need some help to create Ilogic for "Check iproperties in part and rename partnumber part in *.iam" please

ballnr
Advocate
Advocate

I have an assembly make from multibody part.

 

04.jpg

 

Bom show like this

 

05.jpg

 

I need to have Ilogic to check

If iproperties "vol", "area" & "Iall" have a same value, please rename to the same partnumer.

Look like this picture.

 

06.jpg

Please help or suggest.

0 Likes
Accepted solutions (2)
2,240 Views
13 Replies
Replies (13)
Message 2 of 14

FINET_Laurent
Advisor
Advisor

Is it necessary to have 4 different part files for the same part ? If not you can place the part 4 times in the assembly..

 

You can't have :

 

solid1:1

solid1:1

solid1:1

solid1:1

 

By placing the part 4 times you could have

 

solid1:1

solid1:2

solid1:3

solid1:4

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 14

ballnr
Advocate
Advocate

1. I need to looking the same shape on multibody part.

 

2. When I try to replace part that is many work to do If I have 200 multibody part used in assembly.Because is not correct position after you replace.

 

3.Why I need to change partnumber, because I need to count part in bom drawing and easy to used "Rename browser node"

0 Likes
Message 4 of 14

FINET_Laurent
Advisor
Advisor

My go whould be :

 

- Create a custom iProperty "PartName" and name it Part1

- Next I whould pick a part which whould have the reference shape (volume, aera,..)

- Then go through all part in the assembly and compare them with the reference part.

- If they have the same iPorperties then change the iProperty "Part name" to Part1

- If they don't : do nothing.

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 5 of 14

ballnr
Advocate
Advocate

Thanks for your suggest.

0 Likes
Message 6 of 14

JhoelForshav
Mentor
Mentor

Hi @ballnr 

Try this 🙂

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oBOM As BOM = oAsm.ComponentDefinition.BOM
	oBOM.PartsOnlyViewEnabled = True
	Dim oView As BOMView = oBOM.BOMViews(oBOM.BOMViews.Count)
	Dim oHandled As New List(Of PartComponentDefinition)
	For Each oRow As BOMRow In oView.BOMRows
		If oHandled.Contains(oRow.ComponentDefinitions(1)) Then Continue For
		Dim oDef As PartComponentDefinition = oRow.ComponentDefinitions(1)
		For Each oOtherRow As BOMRow In oView.BOMRows
			If oOtherRow IsNot oRow AndAlso oHandled.Contains(oOtherRow.ComponentDefinitions(1)) = False
				Dim oDef2 As PartComponentDefinition = oOtherRow.ComponentDefinitions(1)
				If HasSameProperties(oDef, oDef2)
					oDef2.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = _
					oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
					oHandled.Add(oDef2)
				End If
			End If
		Next
	Next
End Sub
Function HasSameProperties(Def1 As PartComponentDefinition, Def2 As PartComponentDefinition) As Boolean
	Dim doc1 As PartDocument = Def1.Document
	Dim doc2 As PartDocument = Def2.Document

	Dim oCustomprops1 As PropertySet = doc1.PropertySets.Item("Inventor User Defined Properties")
	Dim oCustomprops2 As PropertySet = doc2.PropertySets.Item("Inventor User Defined Properties")
	Try
		Dim oString1 As String = oCustomprops1.Item("vol").Value & "¤" & _
		oCustomprops1.Item("area").Value & "¤" & _
		oCustomprops1.Item("Iall").Value

		Dim oString2 As String = oCustomprops2.Item("vol").Value & "¤" & _
		oCustomprops2.Item("area").Value & "¤" & _
		oCustomprops2.Item("Iall").Value

		If oString1 = oString2
			Return True
		Else
			Return False
		End If
	Catch
		Return False
	End Try
End Function
0 Likes
Message 7 of 14

ballnr
Advocate
Advocate

Thank you very much.

When I run on first time. Error coming

07.jpg

08.jpg

 

when I run second time no error but the result some thing wrong.

 

09.jpg

0 Likes
Message 8 of 14

JhoelForshav
Mentor
Mentor

@ballnr 

That's strange... It works for me.

forum.PNG

 

Seems like there's some row that where the code fails to get the componentdefinition for some reason in your case...

What does the result look like if you run it like this?

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oBOM As BOM = oAsm.ComponentDefinition.BOM
	oBOM.PartsOnlyViewEnabled = True
	Dim oView As BOMView = oBOM.BOMViews(oBOM.BOMViews.Count)
	Dim oHandled As New List(Of PartComponentDefinition)
	For Each oRow As BOMRow In oView.BOMRows
		Try
			If oHandled.Contains(oRow.ComponentDefinitions(1)) Then Continue For
			Dim oDef As PartComponentDefinition = oRow.ComponentDefinitions(1)
			For Each oOtherRow As BOMRow In oView.BOMRows
				If oOtherRow IsNot oRow AndAlso oHandled.Contains(oOtherRow.ComponentDefinitions(1)) = False
					Dim oDef2 As PartComponentDefinition = oOtherRow.ComponentDefinitions(1)
					If HasSameProperties(oDef, oDef2)
						oDef2.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = _
						oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
						oHandled.Add(oDef2)
					End If
				End If
			Next
		Catch
		End Try
	Next

End Sub
Function HasSameProperties(Def1 As PartComponentDefinition, Def2 As PartComponentDefinition) As Boolean
	Dim doc1 As PartDocument = Def1.Document
	Dim doc2 As PartDocument = Def2.Document

	Dim oCustomprops1 As PropertySet = doc1.PropertySets.Item("Inventor User Defined Properties")
	Dim oCustomprops2 As PropertySet = doc2.PropertySets.Item("Inventor User Defined Properties")
	Try
		Dim oString1 As String = oCustomprops1.Item("vol").Value & "¤" & _
		oCustomprops1.Item("area").Value & "¤" & _
		oCustomprops1.Item("Iall").Value

		Dim oString2 As String = oCustomprops2.Item("vol").Value & "¤" & _
		oCustomprops2.Item("area").Value & "¤" & _
		oCustomprops2.Item("Iall").Value

		If oString1 = oString2
			Return True
		Else
			Return False
		End If
	Catch
		Return False
	End Try
End Function

 

0 Likes
Message 9 of 14

ballnr
Advocate
Advocate

For you picture that is a good result.

 

But for my computer that is different from your picture.

My inventor version is

10.jpg

I will try to restart and test again

0 Likes
Message 10 of 14

ballnr
Advocate
Advocate

I try your last Ilogic.

 

No error on first time. But result is the same picture.

 

11.jpg

Please suggest.

0 Likes
Message 11 of 14

JhoelForshav
Mentor
Mentor
Accepted solution

It seems as if my last reply disappeared...

Does this way of comparing the values work better for you maybe?

 

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oBOM As BOM = oAsm.ComponentDefinition.BOM
	oBOM.PartsOnlyViewEnabled = True
	Dim oView As BOMView = oBOM.BOMViews(oBOM.BOMViews.Count)
	Dim oHandled As New List(Of PartComponentDefinition)
	For Each oRow As BOMRow In oView.BOMRows
			If oHandled.Contains(oRow.ComponentDefinitions(1)) Then Continue For
			Dim oDef As PartComponentDefinition = oRow.ComponentDefinitions(1)
			For Each oOtherRow As BOMRow In oView.BOMRows
				If oOtherRow IsNot oRow AndAlso oHandled.Contains(oOtherRow.ComponentDefinitions(1)) = False
					Dim oDef2 As PartComponentDefinition = oOtherRow.ComponentDefinitions(1)
					If HasSameProperties(oDef, oDef2)
						oDef2.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = _
						oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
						oHandled.Add(oDef2)
					End If
				End If
			Next
	Next

End Sub
Function HasSameProperties(Def1 As PartComponentDefinition, Def2 As PartComponentDefinition) As Boolean
	Dim doc1 As PartDocument = Def1.Document
	Dim doc2 As PartDocument = Def2.Document

	Dim oCustomprops1 As PropertySet = doc1.PropertySets.Item("Inventor User Defined Properties")
	Dim oCustomprops2 As PropertySet = doc2.PropertySets.Item("Inventor User Defined Properties")
	Try
		If DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("vol").Value), CDblAny(oCustomprops2.Item("vol").Value)) _
		AndAlso DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("area").Value), CDblAny(oCustomprops2.Item("area").Value)) _
		AndAlso DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("Iall").Value), CDblAny(oCustomprops2.Item("Iall").Value)) _
		Then Return True
	Catch
		MsgBox("Failed to compare values!")
		Return False
	End Try
End Function
0 Likes
Message 12 of 14

ballnr
Advocate
Advocate

Thank you very much for your help.

The last Ilogic, I run firsttime have some error.

When second time the result ok.

I think may be have some problems on my computer.

I will re check on another computer.

0 Likes
Message 13 of 14

JhoelForshav
Mentor
Mentor
Accepted solution

@ballnr 

Since the problem seems to be obtaining the componentdefinition from the BOMrow on the first try, maybe going through referenced documents is a better approach for you?

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oList As New List(Of Document)
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject _
			AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc).Count > 0

			For Each oOtherDoc As Document In oAsm.AllReferencedDocuments
				If oList.Contains(oOtherDoc) = False _
					AndAlso oOtherDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject _
					AndAlso oOtherDoc IsNot oRefDoc _
					AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oOtherDoc).Count > 0
					If HasSameProperties(oRefDoc, oOtherDoc)
						oOtherDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = _
						oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
						oList.Add(oOtherDoc)
					End If
				End If
			Next
		End If
	Next
End Sub
Function HasSameProperties(doc1 As PartDocument, doc2 As PartDocument) As Boolean

	Dim oCustomprops1 As PropertySet = doc1.PropertySets.Item("Inventor User Defined Properties")
	Dim oCustomprops2 As PropertySet = doc2.PropertySets.Item("Inventor User Defined Properties")
	Try
		If DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("vol").Value), CDblAny(oCustomprops2.Item("vol").Value)) _
		AndAlso DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("area").Value), CDblAny(oCustomprops2.Item("area").Value)) _
		AndAlso DoubleForEquals.IsEqual(CDblAny(oCustomprops1.Item("Iall").Value), CDblAny(oCustomprops2.Item("Iall").Value)) _
		Then Return True
	Catch
		MsgBox("Failed to compare values!")
		Return False
	End Try
End Function
0 Likes
Message 14 of 14

ballnr
Advocate
Advocate

The last Ilogic, That is perfect.

 

Thank you very much.

0 Likes