Import Parameter from Part/Assembly

Import Parameter from Part/Assembly

Anonymous
Not applicable
1,040 Views
5 Replies
Message 1 of 6

Import Parameter from Part/Assembly

Anonymous
Not applicable

Hi all,

 

I am trying to acheive copying parameter from one part/assembly to another part/assembly document and this is what i came out with: 

 

Private Sub CopyParamToTarget(SourceFile As String, TargetFile As String, ByRef ParamList As ListBox)
        'Declares the variables to be used in this sub
        Dim d_target As Inventor.Document = Nothing, d_source As Inventor.Document = Nothing
        Dim t_transaction As Inventor.Transaction = Nothing

        Try
            d_source = _ia_app.Documents.Open(SourceFile, False)    'Opens the source file
            d_target = _ia_app.Documents.Open(TargetFile, True)     'Opens the target file

            'Starts inventor transaction on the target file, enabling user to undo this command
            t_transaction = _ia_app.TransactionManager.StartTransaction(CType(d_target, Inventor._Document), "Import param")

            'Checks the type of documents of both the source and target file; Is there is an easier way out???
            'If target file is of type part
            If TypeOf d_target Is Inventor.PartDocument Then
                Dim pd_target As Inventor.PartDocument = CType(d_target, Inventor.PartDocument)

                If TypeOf d_source Is Inventor.PartDocument Then
                    Dim pd_source As Inventor.PartDocument = CType(d_source, Inventor.PartDocument)

                    For Each str_item As String In ParamList.Items
                        Dim bool_found As Boolean = False

                        For Each p_param As Inventor.Parameter In pd_source.ComponentDefinition.Parameters.ModelParameters
                            If p_param.Name = str_item Then
                                pd_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                bool_found = True
                                Exit For
                            End If
                        Next

                        If Not bool_found Then
                            For Each p_param As Inventor.Parameter In pd_source.ComponentDefinition.Parameters.UserParameters
                                If p_param.Name = str_item Then
                                    pd_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                ElseIf TypeOf d_source Is Inventor.AssemblyDocument Then
                    Dim ad_source As Inventor.AssemblyDocument = CType(d_source, Inventor.AssemblyDocument)

                    For Each str_item As String In ParamList.Items
                        Dim bool_found As Boolean = False

                        For Each p_param As Inventor.Parameter In ad_source.ComponentDefinition.Parameters.ModelParameters
                            If p_param.Name = str_item Then
                                pd_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                bool_found = True
                                Exit For
                            End If
                        Next

                        If Not bool_found Then
                            For Each p_param As Inventor.Parameter In ad_source.ComponentDefinition.Parameters.UserParameters
                                If p_param.Name = str_item Then
                                    pd_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                End If
            ElseIf TypeOf d_target Is Inventor.AssemblyDocument Then
                Dim ad_target As Inventor.AssemblyDocument = CType(d_target, Inventor.AssemblyDocument)

                If TypeOf d_source Is Inventor.PartDocument Then
                    Dim pd_source As Inventor.PartDocument = CType(d_source, Inventor.PartDocument)

                    For Each str_item As String In ParamList.Items
                        Dim bool_found As Boolean = False

                        For Each p_param As Inventor.Parameter In pd_source.ComponentDefinition.Parameters.ModelParameters
                            If p_param.Name = str_item Then
                                ad_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                bool_found = True
                                Exit For
                            End If
                        Next

                        If Not bool_found Then
                            For Each p_param As Inventor.Parameter In pd_source.ComponentDefinition.Parameters.UserParameters
                                If p_param.Name = str_item Then
                                    ad_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                ElseIf TypeOf d_source Is Inventor.AssemblyDocument Then
                    Dim ad_source As Inventor.AssemblyDocument = CType(d_source, Inventor.AssemblyDocument)

                    For Each str_item As String In ParamList.Items
                        Dim bool_found As Boolean = False

                        For Each p_param As Inventor.Parameter In ad_source.ComponentDefinition.Parameters.ModelParameters
                            If p_param.Name = str_item Then
                                ad_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                bool_found = True
                                Exit For
                            End If
                        Next

                        If Not bool_found Then
                            For Each p_param As Inventor.Parameter In ad_source.ComponentDefinition.Parameters.UserParameters
                                If p_param.Name = str_item Then
                                    ad_target.ComponentDefinition.Parameters.UserParameters.AddByExpression(str_item, p_param.Expression, p_param.Units)
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            If Not t_transaction Is Nothing Then t_transaction.End()
        Finally
            If Not d_source Is Nothing Then d_source.Close(True)
            ParamList.MoveItems(lb_sourceParams, True)
            If Not t_transaction Is Nothing Then t_transaction.End()
        End Try
    End Sub
    'Ends Sub CopyParamToTarget

For something this simple, the code is kindda long. How can I make the code shorter?

I did another version for Inventor VBA in which is quite similar to this length as well . . .

Tried using ComponentDefinition instead but it does not have access to the Parameters list.

0 Likes
Accepted solutions (1)
1,041 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

I structured the code abit... guess I'll stick to this for now...

 

Private Sub CopyParamToTarget(SourceFile As String, TargetFile As String, ByRef ParamList As ListBox)
        'Declares the variables to be used in this sub
        Dim d_target As Inventor.Document = Nothing, d_source As Inventor.Document = Nothing
        Dim t_transaction As Inventor.Transaction = Nothing

        Try
            d_source = _ia_app.Documents.Open(SourceFile, False)    'Opens the source file
            d_target = _ia_app.Documents.Open(TargetFile, True)     'Opens the target file

            'Starts inventor transaction on the target file, enabling user to undo this command
            t_transaction = _ia_app.TransactionManager.StartTransaction(CType(d_target, Inventor._Document), "Import param")

            'If target file is of type part
            If TypeOf d_target Is Inventor.PartDocument Then
                Dim pd_target As Inventor.PartDocument = CType(d_target, Inventor.PartDocument)

                If TypeOf d_source Is Inventor.PartDocument Then
                    Dim pd_source As Inventor.PartDocument = CType(d_source, Inventor.PartDocument)

                    CopyParam(pd_target.ComponentDefinition.Parameters.UserParameters, pd_source.ComponentDefinition.Parameters.ModelParameters,
                              pd_source.ComponentDefinition.Parameters.UserParameters, ParamList)
                ElseIf TypeOf d_source Is Inventor.AssemblyDocument Then
                    Dim ad_source As Inventor.AssemblyDocument = CType(d_source, Inventor.AssemblyDocument)

                    CopyParam(pd_target.ComponentDefinition.Parameters.UserParameters, ad_source.ComponentDefinition.Parameters.ModelParameters,
                              ad_source.ComponentDefinition.Parameters.UserParameters, ParamList)
                End If
            ElseIf TypeOf d_target Is Inventor.AssemblyDocument Then
                Dim ad_target As Inventor.AssemblyDocument = CType(d_target, Inventor.AssemblyDocument)

                If TypeOf d_source Is Inventor.PartDocument Then
                    Dim pd_source As Inventor.PartDocument = CType(d_source, Inventor.PartDocument)

                    CopyParam(ad_target.ComponentDefinition.Parameters.UserParameters, pd_source.ComponentDefinition.Parameters.ModelParameters,
                              pd_source.ComponentDefinition.Parameters.UserParameters, ParamList)
                ElseIf TypeOf d_source Is Inventor.AssemblyDocument Then
                    Dim ad_source As Inventor.AssemblyDocument = CType(d_source, Inventor.AssemblyDocument)

                    CopyParam(ad_target.ComponentDefinition.Parameters.UserParameters, ad_source.ComponentDefinition.Parameters.ModelParameters,
                              ad_source.ComponentDefinition.Parameters.UserParameters, ParamList)
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            If Not t_transaction Is Nothing Then t_transaction.End()
        Finally
            If Not d_source Is Nothing Then d_source.Close(True)
            ParamList.MoveItems(lb_sourceParams, True)
            If Not t_transaction Is Nothing Then t_transaction.End()
        End Try
    End Sub
    'Ends Sub CopyParamToTarget

    Private Sub CopyParam(ByRef TargetUserParam As Inventor.UserParameters, ByRef SourceModelParam As Inventor.ModelParameters, ByRef SourceUserParam As Inventor.UserParameters, ByRef ParamList As ListBox)
        'For each param names to bo copied
        For Each str_i As String In ParamList.Items
            Dim bool_found As Boolean = False

            'Looks for the parameter in the model parameter
            For Each p_param As Inventor.Parameter In SourceModelParam
                If p_param.Name = str_i Then    'If parameter is found then
                    'Avoids copying the parameter if target file already contains the current parameter
                    If Not TargetUserParam.Contains(str_i) Then TargetUserParam.AddByExpression(p_param.Name, p_param.Expression, p_param.Units)
                    bool_found = True
                    Exit For
                End If
            Next

            'If paramater is not found in the model parameter then tries to look into the use parameters
            If Not bool_found Then
                For Each p_param As Inventor.Parameter In SourceUserParam
                    If p_param.Name = str_i Then    'If parameter is found then
                        'Avoids copying the parameter if target file already contains the current parameter
                        If Not TargetUserParam.Contains(str_i) Then TargetUserParam.AddByExpression(p_param.Name, p_param.Expression, p_param.Units)
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub
    'Ends Sub CopyParam

 

0 Likes
Message 3 of 6

philippe.leefsma
Alumni
Alumni
Accepted solution

Hi AlexSu,

 

Everything depends in which context you are invoking that method, for example you are checking if your document is part or assembly. If you are sure it is either one or the other, you could just use late binding and write:

 

    Dim parameters As parameters
    Set parameters = doc.ComponentDefinition.parameters

 

Is your goal writting the smallest code possible or a code that cannot fail? You are passing a ListBox as input to your method, which I find a little weird. Another approach would be to open first the source document, check that all parameters exists, then invoke another method with inputs (target, List(Of Parameters)). Also what would happen if the expression of the copied parameter was referencing another parameter still in the source document? Is it a scenario that you consider? If yes, then the code would get more complex...

 

I hope it helps.

 

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 6

Anonymous
Not applicable

Hi Philippe,

 

Thank you! That "Set parameters = doc.ComponentDefinition.parameters" is just what I was looking for....

Now, the only other issue is to get VB.net to accept late bindings....

 

This is a project in VB.net that prompts the user to select the source and target file, both of which I set the filters to *.ipt or *.iam, to limit possibilities of other file types. This way, I can safely assume and use the late bindings then....

 

This then lists all the available parameters in a listbox, for the user to select which parameter he/she wants to copy... I did not go as far as to check if the parameter in an expression exists or not in the target document. I leave that to the user to take care of that Smiley Very Happy

 

Thanks again!

0 Likes
Message 5 of 6

philippe.leefsma
Alumni
Alumni

I don't get what you mean by "now, the only other issue is to get VB.net to accept late bindings...." that's built-in feature in the language...

 

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi Philippe,

 

Sorry should have mentioned it earlier... I have the habbit of leaving "Option Strict" ON, and it does not like late bindings at all... Smiley Happy

0 Likes