Adding UserParameter out of Assembly

Adding UserParameter out of Assembly

schuermann.fabianNKRGP
Explorer Explorer
196 Views
1 Reply
Message 1 of 2

Adding UserParameter out of Assembly

schuermann.fabianNKRGP
Explorer
Explorer

Hello,

 

I want to add a user parameter with a multi value list to every part in an assembly. My code adds the parameter but doesn't add the list. I've tried some things but did not find a solution for this.

Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

Dim oTester As String

    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
	
	For Each oRefDoc In oRefDocs
		
		oDocPathName = oRefDoc.FullDocumentName
		
			If oDocPathName.StartsWith("U:\Paletti Norm")
			
			Else
		
			oPartDoc = ThisApplication.Documents.Open(oDocPathName, True)
			oPartDoc.Activate
			oPartDoc = ThisApplication.ActiveDocument
			oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters			
			
			Try
				
				oTester = oMyParameter.Item("Bauteiltyp")
				oPartDoc.Close(True)					
				
			Catch				
			
			oParameter = oMyParameter.AddByValue("Bauteiltyp", "Standard", UnitsTypeEnum.kTextUnits)
			MultiValue.SetList("Bauteiltyp", "Standard", "ZUKAUF", "ROHSTOFF", "FORMKÖRPER D", "FORMKÖRPER F", "FORMKÖRPER P")
			
			Dim userParams As UserParameters
			Dim param As Parameter 
			
			userParams = oPartDoc.ComponentDefinition.Parameters.UserParameters

			For Each oPara As Parameter In userParams
				
			Dim oName As String = oPara.Name
			
			If oName = "Bauteiltyp_1" Then 
				oPara.Delete
			End If
			Next

			oPartDoc.Close(True)		
			
			End Try
			End If
			

	Next

 

This is the code I am using so far. 

 

0 Likes
197 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor

You can try this simplified version

 

Sub Main()

    Dim asm As AssemblyDocument = ThisDoc.Document
    For Each refDoc As Document In asm.AllReferencedDocuments
        'Skip non-part documents
        If refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For

        AddMultiValueParameterToPart(refDoc, "MultiValueParam", "one", "two", "three")

    Next
End Sub

Sub AddMultiValueParameterToPart(part As PartDocument, paramName As String, ParamArray exprList As String())
    Dim partDef As PartComponentDefinition = part.ComponentDefinition
    Dim textExpressionList = exprList.Select(Function(s) String.Format("""{0}""", s)).ToArray()

    'Ensure parameter by name
    Dim param As UserParameter
    Try
        param = partDef.Parameters.UserParameters(paramName)
    Catch ex As Exception
        param = partDef.Parameters.UserParameters.AddByValue(paramName, exprList(0), "TEXT")
    End Try

    'Set expression list
    param.ExpressionList.SetExpressionList(textExpressionList)
End Sub

 

0 Likes