add parameter to all childrent part and all Sub-Assembly code

add parameter to all childrent part and all Sub-Assembly code

tamnn.designer
Contributor Contributor
276 Views
2 Replies
Message 1 of 3

add parameter to all childrent part and all Sub-Assembly code

tamnn.designer
Contributor
Contributor

Hi Sir,

i am doing the stupid way. I used 2 code.

1st. Add all parameter from external rule into the main assembly and Sub-Assembly

2nd: I copied all parameter from the current main assembly into all children part.

So that lead to Parameter from Assemblies = Parts. That is not good for me later. I would choose which parameter will add into Assemblies, and which parameters will go all children parts.

 

Finally: One code combination 2 code by only 1 code but choose the 2 destination

- I would want all parameter in this code input to all parts.

- Only 3 pars Xcubic, Ycubic and Zcubic input to main assembly and sub-assembly only "

 

Thank a lot and best regards

0 Likes
277 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

You can try this:

Sub Main()

    If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
    Dim doc As AssemblyDocument = ThisDoc.FactoryDocument

    Dim paramsData As New List(Of List(Of Object))
    paramsData.Add(New List(Of Object) From {"extent_dim_X", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"extent_dim_Y", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"extent_dim_T", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"part_area", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"bend_number", "1", UnitsTypeEnum.kUnitlessUnits})
    paramsData.Add(New List(Of Object) From {"Xcubic", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"Ycubic", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"Zcubic", "1", UnitsTypeEnum.kMillimeterLengthUnits})
    paramsData.Add(New List(Of Object) From {"thread_hole_number", "1", UnitsTypeEnum.kUnitlessUnits})

    EnsureParamsExist(doc, paramsData)

    Dim allSubAsms As List(Of Document) = doc.AllReferencedDocuments.Cast(Of Inventor.Document).Where(Function(d) d.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject).ToList
    If allSubAsms Is Nothing OrElse allSubAsms.Count = 0 Then Exit Sub

    For Each oSubAsm In allSubAsms
        EnsureParamsExist(oSubAsm, paramsData)
    Next

    Dim allSubParts As List(Of Document) = doc.AllReferencedDocuments.Cast(Of Inventor.Document).Where(Function(d) d.DocumentType = DocumentTypeEnum.kPartDocumentObject).ToList
    If allSubParts Is Nothing OrElse allSubAsms.Count = 0 Then Exit Sub

    For Each part In allSubParts
        Try
            CopyUserParameters(doc, part)
        Catch ex As Exception
            MsgBox("Problems while making parameter copies to part: " & part.DisplayName)
        End Try
    Next

    If doc.RequiresUpdate Then doc.Update2(True)
End Sub

Public Sub CopyUserParameters(sourceDoc As AssemblyDocument, targetDoc As PartDocument)
    Dim sourceParameters As UserParameters = sourceDoc.ComponentDefinition.Parameters.UserParameters
    Dim targetParameters As UserParameters = targetDoc.ComponentDefinition.Parameters.UserParameters
    Dim targetParametersList As List(Of UserParameter) = targetDoc.ComponentDefinition.Parameters.UserParameters.Cast(Of UserParameter).ToList()

    For Each source As UserParameter In sourceParameters
        If (targetParametersList.Contains(source)) Then
            Dim newParameter = targetParameters.Item(source.Name)
            newParameter.Expression = source.Expression
            newParameter.ExposedAsProperty = True

        Else
            Dim newParameter = targetParameters.AddByExpression(source.Name, source.Expression, source.Units)
            newParameter.ExposedAsProperty = True
            ' iLogicVb.UpdateWhenDone = True
        End If
    Next
End Sub

Sub EnsureParamsExist(oDoc As Document, oParamsData As List(Of List(Of Object)))

    If oDoc Is Nothing Or (oParamsData Is Nothing OrElse oParamsData.Count = 0) Then Exit Sub
    If oDoc.IsModifiable = False Then Exit Sub
    Dim oUParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
    For Each MainEntry In oParamsData
        If MainEntry.Count = 0 Then Continue For
        Dim oUParam As UserParameter = Nothing
        Try
            oUParam = oUParams.Item(MainEntry.First)
        Catch
            oUParam = oUParams.AddByExpression(MainEntry.First, MainEntry.Item(1), MainEntry.Last)
        End Try

        Try
            If Not oUParam.ExposedAsProperty Then oUParam.ExposedAsProperty = True
            Dim oCPF As CustomPropertyFormat = oUParam.CustomPropertyFormat
            oCPF.Units = oDoc.UnitsOfMeasure.GetStringFromType(MainEntry.Last)
            oCPF.Precision = CustomPropertyPrecisionEnum.kThreeDecimalPlacesPrecision
            oCPF.ShowLeadingZeros = True
            oCPF.ShowTrailingZeros = True
        Catch
        End Try
    Next
End Sub

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 3

tamnn.designer
Contributor
Contributor

Thank you very much for your support.

It's worked with No. 1 as bellow. No 2. I just want to copy only 3 parameters NOT all parameter.

 

Finally: One code combination 2 code by only 1 code but choose the 2 destination

1.  I would want all parameter in this code input to all parts.

2.  Only 3 pars Xcubic, Ycubic and Zcubic input to main assembly and sub-assembly only "

0 Likes