Message 1 of 6

Not applicable
06-24-2013
07:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.