check assembly for custom properties, and list all unik string

check assembly for custom properties, and list all unik string

Darkforce_the_ilogic_guy
Advisor Advisor
340 Views
3 Replies
Message 1 of 4

check assembly for custom properties, and list all unik string

Darkforce_the_ilogic_guy
Advisor
Advisor

I have a code that find all custom properties (Raw Material) .   in an assembly and it parts. but I only want to have each properties one time for each unik vaule/ string

 

I want to create a list of all type of material that are use in the assembly

 

 

any go way to do this ?

 

Class BOMEXport
Shared oMaterial As String
Shared oMaterialName As String
Sub main()
	oMaterial = ""
	oMaterialName = ""
PossibleIssueReport()

End Sub 

Sub PossibleIssueReport()
	
	
	Row2 = 3
	Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM

oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
oBOM.StructuredViewDelimiter = "."

oBOM.PartsOnlyViewEnabled = True

Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")

Call ExportToExcel(oBOMView.BOMRows)


End Sub 



Public Function ExportToExcel(brows As BOMRowsEnumerator)
	
	Dim brow As BOMRow
For Each brow In brows
	
	Dim rDoc As Document
rDoc = brow.ComponentDefinitions.Item(1).Document



Dim docPropertySet1 As PropertySet
docPropertySet1 = rDoc.PropertySets.Item("User Defined Properties")
Try
	oMaterialName = ""
	If docPropertySet1.Item("Raw Material").Value = "3916030054" Then
		
		oMaterialName = "RHP 50x30x4"
	End If 
	
	If docPropertySet1.Item("Raw Material").Value = "3916050106" Then
		
		oMaterialName = "RHP 100x50x6"
	End If 
	
	
	oMaterial  = (oMaterial +  vbLf  + docPropertySet1.Item("Raw Material").Value) + " " + oMaterialName
	
	Catch
		
	End Try
	
	
	Next
	msgbox (oMaterial)
	
	
	End Function
	

End Class
0 Likes
Accepted solutions (1)
341 Views
3 Replies
Replies (3)
Message 2 of 4

J-Camper
Advisor
Advisor
Accepted solution

I think a NameValueMap is what you want.  It throws an error when trying to add a Name that already exists.  With a Try/Catch you can build a list for each unique material:

 

Class BOMEXport
Shared oMaterial As String
Shared oMaterialName As String
Shared OutputMap As NameValueMap
Sub main()
	oMaterial = ""
	oMaterialName = ""
	OutputMap = ThisApplication.TransientObjects.CreateNameValueMap
PossibleIssueReport()


End Sub 

Sub DisplayOutputMap()
	Dim Message As String
	For j = 1 To OutputMap.Count 
		Logger.Trace(OutputMap.Name(j) & " : " & OutputMap.Value(OutputMap.Name(j))) 'Set Log Level to Trace for selectable output
		Message += vbLf & OutputMap.Name(j) & " : " & OutputMap.Value(OutputMap.Name(j))
	Next
	MsgBox (Message)'Modified Output
End Sub

Sub PossibleIssueReport()
	
	
	Row2 = 3
	Dim oDoc As AssemblyDocument
	oDoc = ThisApplication.ActiveDocument
	Dim oBOM As BOM
	oBOM = oDoc.ComponentDefinition.BOM

	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewDelimiter = "."

	oBOM.PartsOnlyViewEnabled = True

	Dim oBOMView As BOMView
	oBOMView = oBOM.BOMViews.Item("Structured")

	Call ExportToExcel(oBOMView.BOMRows)


End Sub 



Public Function ExportToExcel(brows As BOMRowsEnumerator)
	
	Dim brow As BOMRow
	For Each brow In brows
	
		Dim rDoc As Document
		rDoc = brow.ComponentDefinitions.Item(1).Document



		Dim docPropertySet1 As PropertySet
		docPropertySet1 = rDoc.PropertySets.Item("User Defined Properties")
		Try
			oMaterialName = ""
			If docPropertySet1.Item("Raw Material").Value = "3916030054" Then
				
				oMaterialName = "RHP 50x30x4"
			End If 
			
			If docPropertySet1.Item("Raw Material").Value = "3916050106" Then
				
				oMaterialName = "RHP 100x50x6"
			End If 
			
			Try
				OutputMap.Add(oMaterialName, docPropertySet1.Item("Raw Material").Value)
			Catch 
				'Name is already taken. Add handling if you want to modify value to indicate multiple instances
			End Try
			
			
			oMaterial  = (oMaterial +  vbLf  + docPropertySet1.Item("Raw Material").Value) + " " + oMaterialName
			
		Catch
			
		End Try
	
	Next
	DisplayOutputMap()'Modified Output first
	MsgBox (oMaterial)'Current
		
	End Function
	

End Class

 

Let me know if you have any questions or if this is not working as intended.

Message 3 of 4

Darkforce_the_ilogic_guy
Advisor
Advisor

it fix one problem.  but I now I have 2 other problem.

 

First the 2 text does not line up very vel.  Second  it would be nice to sort them. 

 

bt_0-1646893865935.png

 

 

 

0 Likes
Message 4 of 4

J-Camper
Advisor
Advisor

@Darkforce_the_ilogic_guy,

I wish there was a sort method on the namevaluemap.  I'm not sure if this is the easiest/best way to sort it, but it think it works in this situation.  Since the Value Object is a String in your application, you can put the namevaluemap into a List(Of String) with a known separator String, Sort the List, then fill the namevaluemap back with the sorted list. 

 

As for the Offset text you are seeing, I'm only putting " : " between the name/value in the message box so I don't know where all those extra spaces are coming from.  Do they exist in the iLogic Log or just the message box?

 

In any case I added a sub routine to sort the namevaluemap [for Values of type string, or can be converted to string only].  here is the new sub and a modified Display sub to call the Sort sub:

Sub AlphaSortMap()
	Dim SortingList As New List(Of String)
	Dim seperator As String = " <|> "
	
	For j = 1 To OutputMap.Count 
		SortingList.Add(OutputMap.Name(j) & seperator & OutputMap.Value(OutputMap.Name(j)).ToString)
	Next
	
	SortingList.Sort()
	OutputMap.Clear()
	
	For Each s As String In SortingList
		OutputMap.Add(Left(s, s.IndexOf(seperator)), Right(s, s.Length-s.IndexOf(seperator)-seperator.Length))
	Next
End Sub

Sub DisplayOutputMap()
AlphaSortMap()
	Dim Message As String
	For j = 1 To OutputMap.Count 
		Logger.Trace(OutputMap.Name(j) & " : " & OutputMap.Value(OutputMap.Name(j))) 'Set Log Level to Trace for selectable output
		Message += vbLf & OutputMap.Name(j) & " : " & OutputMap.Value(OutputMap.Name(j))
	Next
	MsgBox (Message)'Modified Output
End Sub

 

Let me know if you have any questions or if this is not working as intended

0 Likes