Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
ejaL9K8H
in reply to: ejaL9K8H

I found a solution, but it is only working if i Run it as 2 separately codes:

 

Firstly the ID number is updated:

' Update ID Number
' Item Number = ID Number

doc = ThisDoc.Document
Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition
Dim oBOM As BOM = oAssyDef.BOM

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
' Set the Row Merge Settings
oBOM.SetPartNumberMergeSettings(False)

Dim oBOMRow As BOMRow

For Each oBOMRow In oBOMView.BOMRows
    'Set a reference to the primary ComponentDefinition of the row
    Dim oCompDef As ComponentDefinition
    oCompDef = oBOMRow.ComponentDefinitions.Item(1)
    
    Dim CompFullDocumentName As String = oCompDef.Document.FullDocumentName
    Dim CompFileNameOnly As String
    Dim index As Integer = CompFullDocumentName.LastIndexOf("\")
    
    CompFileNameOnly = CompFullDocumentName.Substring(index + 1)
	
	Try
	ItemNumber = oBOMRow.ItemNumber
	
 	Parameter(CompFileNameOnly, "ID_Number") = ItemNumber
	Catch
	End Try


Next
iLogicVb.UpdateWhenDone = True

 

Secondly an Asset Tag is made, which updates the all asset models:

AddReference "Autodesk.Factory.PublicAPI.dll"
Imports Autodesk.Factory.PublicAPI.Currency.v2

Sub Main()

' set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Set up a variable for the component cccurrence
Dim oOcc As ComponentOccurrence, factoryInstance As IAssetInstance

'Access the Browser and Activate the "Bim Content" Browser Pane
Dim oBrowserPanes = ThisApplication.ActiveDocument.BrowserPanes
Dim Model_Browser_Pane As BrowserPane = oBrowserPanes.Item("Model")
Dim oOccNode As BrowserNode

For Each oOcc In oAsmCompDef.Occurrences
	 Try
 If (Not IsFactoryInstance(ThisApplication.ActiveDocument, oOcc, factoryInstance)) Then
  MessageBox.Show(oOcc.Name & " is NOT a Factory Asset", "Title")
 Else 
  oOccNode = Model_Browser_Pane.TopNode.BrowserNodes.Item(oOcc.Name)
  oOccNode.DoSelect
  ChangeAssetTag(ThisApplication.ActiveDocument, oOcc, factoryInstance)
 End If
	Catch
  	End Try

Next

End Sub

'Use this Function To see If the models are Assets
Function IsFactoryInstance(layoutDoc As Document, oOcc As ComponentOccurrence, ByRef factoryInstance As IAssetInstance)
 Dim instances = Autodesk.Factory.PublicAPI.API.Instance.GetAssetInstances(layoutDoc)
 found = False
 For Each instance In instances
  If instance.NativeObject.Equals(oOcc) Then
   factoryInstance = instance
   found = True
   Exit For
  End If
 Next
 Return found
End Function

'The public subroutine will allow the editing of Asset Tags
Public Sub ChangeAssetTag(layoutDoc As Document, oOcc As ComponentOccurrence, Asset_Instance As IAssetInstance)
For Each grp In Asset_Instance.PropertyGroups
 If grp.Name = "AssetTag" Then
  For Each prop In grp.Properties
   If prop.Name = "Asset Tag" Then
	    New_Value = Parameter(oOcc.Name, "ID_Number") & " - " & iProperties.Value(oOcc.Name, "Project" ,"Part Number") 
    	prop.Value = New_Value
   End If
  Next
 End If
Next

'After changed some value, you need To update the instance
Dim ins() As IAssetInstance={Asset_Instance}
Autodesk.Factory.PublicAPI.API.Instance.UpdateAssetInstances(layoutDoc,ins)
End Sub

 

Is it possible to merge these two codes and improve them?

I would like the code to save computer capacity and work faster.

 

Thanks in Advance