VBA/iLogic Rule to Rename Model Parameter NAMES

VBA/iLogic Rule to Rename Model Parameter NAMES

bionorica2015
Enthusiast Enthusiast
2,172 Views
18 Replies
Message 1 of 19

VBA/iLogic Rule to Rename Model Parameter NAMES

bionorica2015
Enthusiast
Enthusiast

Hello!

I have this code written by me:

Option Explicit
Public Sub Rename()
Dim i As Integer
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartCompDef As PartComponentDefinition
Set oPartCompDef = oDoc.ComponentDefinition

For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
Dim param As String
param = "d" & i
Dim pName As String
pName = oPartCompDef.Parameters.ModelParameters(i).Name
pName = param
oDoc.Update2 (True)
Next i

Else
Exit Sub
End If

End Sub

 

Снимок.JPG

I want to automatically rename model parameter names by numeric order: d1, d2, d3, ...

 

Please help with it - to write it correctly so it worked

Thanks

0 Likes
2,173 Views
18 Replies
Replies (18)
Message 2 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

You should first write all parameter names to a temporary name. Otherwise, if you try to rename parameter d14 to d8 and another parameter named d8 already exist, it will fail.

Option Explicit ON

Dim i As Integer
Dim oDoc As PartDocument=ThisDoc.Document

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	
	For i =  1 To oPartCompDef.Parameters.ModelParameters.Count
		oPartCompDef.Parameters.ModelParameters(i).Name = "tmpd" & i.ToString
	Next i
	For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
		oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString 
	Next i
End If

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 3 of 19

bionorica2015
Enthusiast
Enthusiast

hank you very much that You answered.

There is a little error:

I need model parameter renamed to d1, d2, d3, ....

But with Your code(I changed some , because it didnt work) I get tmpd1, tmpd2, ...

 

Here is code now:

Option Explicit
Public Sub Rename()
Dim i As Integer
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartCompDef As PartComponentDefinition
Set oPartCompDef = oDoc.ComponentDefinition

For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
oPartCompDef.Parameters.ModelParameters(i).Name = "tmpd" & i
Next i

For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
Dim param As String
param = "t" & i
Dim pName As String
pName = oPartCompDef.Parameters.ModelParameters(i).Name
pName = param
Next i

End If

End Sub

 

 

How can I solve this?

0 Likes
Message 4 of 19

A.Acheson
Mentor
Mentor

This post link appears in 6 other posts and many have no connection to your requested help. Please refrain from duplicated posting as it will have the opposite effect. 

 

If it is an ilogic related question, please use the inventor ilogic forum as it will likely get a quicker and more target response there. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 5 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

Your code does exact the same my code does. It only splits it in 4 more lines of code. I can not reproduce the error. I've added a feedback, so you are informed in case of an error and maybe this helps to find a reason.

Option Explicit ON

Dim i As Integer
Dim stmpString As String="tmp" 
Dim oDoc As PartDocument=ThisDoc.Document

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	
	Try
		For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
			oPartCompDef.Parameters.ModelParameters(i).Name = stmpString & i.ToString
		Next i
	Catch
		MsgBox("Parameter " & oPartCompDef.Parameters.ModelParameters(i).Name & " could not be renamed to " & stmpString & i & vbCrLf & "Please check results.", MsgBoxStyle.Critical,"Rename Model Parameters")
	end try
	
	Try
		For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
			oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString 
		Next i
	Catch
		MsgBox("Parameter " & oPartCompDef.Parameters.ModelParameters(i).Name & " could not be renamed to " & "d" & i & vbCrLf & "Please check results.", MsgBoxStyle.Critical,"Rename Model Parameters")
	End Try

End If

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 6 of 19

bionorica2015
Enthusiast
Enthusiast

tOO many errors occured

Here is screen:

Снимок.JPG

0 Likes
Message 7 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

You are aware this is iLogic code? Your screenshot shows VBA editor.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 8 of 19

bionorica2015
Enthusiast
Enthusiast

Yes. it is my mistake that I didnt say you. Yes, I am writing vba code for renaming model params.

This code works:

Option Explicit
Sub Rname()

Dim i As Integer
Dim tmp As String
tmp = "tmp"
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartCompDef As PartComponentDefinition
Set oPartCompDef = oDoc.ComponentDefinition

For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
oPartCompDef.Parameters.ModelParameters(i).Name = tmp & i
Next i

For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i
Next i

End If
End Sub

 

 

 

Now new problem was born. How can I rename model and reference params at one time?

I forgot abot it

Sorry

0 Likes
Message 9 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

Why did you write iLogic to your thread title if you ask VBA only?

Anyway, you can rename reference parameters the same way. You loop through the ReferenceParameter collection instead the ModelParameter collection. As a reference parameter can not have the same name as a model parameter, you can first start numbering of reference parameters at count of model parameters +1.

I'm getting sometimes an error 5 while renaming reference parameters.

 

 

Private Sub Rename()
    
    If Not ThisApplication.ActiveDocument.DocumentType = kPartDocumentObject Then
        Call MsgBox("This function is for part files only. Exit", VbMsgBoxStyle.vbCritical, "Rename model parameters")
        Exit Sub
    End If
    
    Dim s As String
    
    Dim i As Integer
    Dim j As Integer
    Dim stmpString As String
    stmpString = "tmp"
    Dim sString As String
    sString = "d"
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    If oDoc.DocumentType = kPartDocumentObject Then
        Dim oPartCompDef As PartComponentDefinition
        Set oPartCompDef = oDoc.ComponentDefinition
        
        For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
            oPartCompDef.Parameters.ModelParameters(i).Name = stmpString & CStr(i)
        Next i
        
        For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
            oPartCompDef.Parameters.ModelParameters(i).Name = sString & CStr(i)
        Next i
        
        For i = oPartCompDef.Parameters.ModelParameters.Count + 1 To oPartCompDef.Parameters.ModelParameters.Count + oPartCompDef.Parameters.ReferenceParameters.Count
            j = j + 1
            oPartCompDef.Parameters.ReferenceParameters(j).Name = stmpString & CStr(i)
        Next i
        
        oDoc.Update
        
        j = 0
        For i = oPartCompDef.Parameters.ModelParameters.Count + 1 To oPartCompDef.Parameters.ModelParameters.Count + oPartCompDef.Parameters.ReferenceParameters.Count
            j = j + 1
            oPartCompDef.Parameters.ReferenceParameters(j).Name = sString & CStr(i)
        Next i
        
    End If

End Sub

 

 

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 10 of 19

bionorica2015
Enthusiast
Enthusiast

After i=100 an error occurs. In one part document when i=150 , in other i=100.(Call to undefined argument)

0 Likes
Message 11 of 19

bionorica2015
Enthusiast
Enthusiast

How can I skip error "Variable name is alraedy in use"?

When I have more than 1000 variables. I can not know is this variable name used in others.

 

0 Likes
Message 12 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

As I wrote in my last post, sometimes an Error 5 appears. I have no idea why, cause there is no visible reason to me. I tried different ways, but all failed. Sometimes the error disappeared when overwrite the failing parameter name by hand, sometimes that makes no difference.

 

In VBA the simpliest way is to use "On Error Resume Next" statement. This will ignore all errors and continue. You can check the Err.Number property to see if there was an error. Reset Error counter with Err.Clear and return to normal program behaviour with "On Error GoTo 0".

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 13 of 19

bionorica2015
Enthusiast
Enthusiast

Hello, Krieg!

Thanks a lot for Your code:

 

 

Option Explicit ON

Dim i As Integer
Dim oDoc As PartDocument=ThisDoc.Document

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	
	For i =  1 To oPartCompDef.Parameters.ModelParameters.Count
		oPartCompDef.Parameters.ModelParameters(i).Name = "tmpd" & i.ToString
	Next i
	For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
		oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString 
	Next i
End If

 

 

It works. 

In the file which I am sending You now (file name: Detail_rename.ipt) error occurs and it fails.

Why it happens? What is solution in this case?

Thanks! 

0 Likes
Message 14 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

As stated before, this is one of the errors that sometimes occur and I can't explain why. The parameter is correct renamed to a temporary name in first loop. Rename to the final name in second loop fails. You could try to catch this errors and display a message at end of program. After renaming the parameter manually to "d9" (in this case), running the rule again finishes without further error messages.

Wish I had a real solution, but did not found. 😞

 

Option Explicit ON

Dim i As Integer
Dim oDoc As PartDocument=ThisDoc.Document
Dim bErr As Boolean

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	Try
		For i =  1 To oPartCompDef.Parameters.ModelParameters.Count
			oPartCompDef.Parameters.ModelParameters(i).Name = "tmpd" & i.ToString
		Next i
		For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
			oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString 
		Next i
	Catch ex As Exception
		bErr = True
	End Try

	If bErr Then
		MsgBox("Error occured, please check manually", MsgBoxStyle.Critical, "iLogic")
	End If
End If

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 15 of 19

A.Acheson
Mentor
Mentor

This is still a mystery. Here is a work around, if you add a user parameter named as per the error bound model parameter then delete you can then rename the model parameter by code. 

'Option Explicit ON

Dim i As Integer
Dim oDoc As PartDocument = ThisDoc.Document

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oPartCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
	Try
		For i =  1 To oPartCompDef.Parameters.ModelParameters.Count
			oPartCompDef.Parameters.ModelParameters(i).Name = "temp" & i.ToString
			Logger.Error( i & "-" & oPartCompDef.Parameters.ModelParameters(i).Name)
		Next i
		Catch 
		MessageBox.Show("Error setting to temp", "Title")
	End Try
	
		For i = 1 To oPartCompDef.Parameters.ModelParameters.Count
			Try
			oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString 
			Logger.Info(oPartCompDef.Parameters.ModelParameters(i).Name)
			Catch ex As Exception
			Logger.Error(oPartCompDef.Parameters.ModelParameters(i).Name)
			
			Try
			Dim a As UserParameter = oPartCompDef.Parameters.UserParameters.AddByValue("d" & i.ToString, 1, UnitsTypeEnum.kUnitlessUnits)
			a.Delete
			Catch
				MessageBox.Show("Error doing work around", "Title")
			End Try
			
			Try
			oPartCompDef.Parameters.ModelParameters(i).Name = "d" & i.ToString  
			Catch
				MessageBox.Show("Error setting Parameter", "Title")
			End Try
			End Try
		Next i
End If

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 16 of 19

WCrihfield
Mentor
Mentor

Hi @bionorica2015.  After opening your part and seeing what was going on with the parameter names, I can see that there are a couple of parameter names that seem to rename OK to the temporary name, but then do not want to rename back to a 'd' name.  So I simply incorporated a GoTo loop in there in a couple places, to make it try again with the next incremental integer, which then worked.  I generally do not recommend a scenario like that in more complex situations though, because it might lead to an endless loop.

Here is the code I wrote for this situation that seemed to work just fine, as far as I could tell.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oPDoc As PartDocument = ThisDoc.Document
Dim oMPs As ModelParameters = oPDoc.ComponentDefinition.Parameters.ModelParameters
oTrans = ThisApplication.TransactionManager.StartTransaction(oPDoc, "Rename Parameters")
Dim i As Integer = 0
For Each oMP As ModelParameter In oMPs
	TryAgain1 :
	i = i + 1
	Dim oNewName As String = "tmp" & i
	Try
		oMP.Name = oNewName
	Catch
		Logger.Error("Failed to rename '" & oMP.Name & "' to '" & oNewName & "'")
		GoTo TryAgain1
	End Try
Next
oPDoc.Update
i = 0
For Each oMP As ModelParameter In oMPs
	TryAgain2 :
	i = i + 1
	Dim oNewName As String = "d" & i
	Try
		oMP.Name = oNewName
	Catch
		Logger.Error("Failed to rename '" & oMP.Name & "' to '" & oNewName & "'")
		GoTo TryAgain2
	End Try
Next
oPDoc.Update
oTrans.End

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 17 of 19

bionorica2015
Enthusiast
Enthusiast

Thanks, but If You see my code is in VBA.

There is no try catch try again syntactic in vba.

How can we do that in vba?

0 Likes
Message 18 of 19

WCrihfield
Mentor
Mentor

OK.  Here is the VBA version.  I just commented out the 'Logger' lines, because they are iLogic only lines, and this is VBA.  I didn't think you would want to see a message box pop-up for each potential error if you may be processing a thousand parameters.  I suppose you could access the iLogic Add-In's automation resource from VBA, if you really wanted to, with some extra code, then gain access to its 'LogControl' object, but that seemed like too much extra code for now in this simple task.

Sub RenameAllModelParams()
    If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
        Call MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
        Exit Sub
    End If
    Dim oPDoc As PartDocument
    Set oPDoc = ThisApplication.ActiveDocument
    Dim oMPs As ModelParameters
    Set oMPs = oPDoc.ComponentDefinition.Parameters.ModelParameters
    Dim oMP As ModelParameter
    Dim oTrans As Transaction
    Set oTrans = ThisApplication.TransactionManager.StartTransaction(oPDoc, "Rename Parameters")
    Dim i As Integer
    i = 0
    Dim oNewName As String
    For Each oMP In oMPs
TryAgain1:
        i = i + 1
        oNewName = "tmp" & i
        On Error Resume Next
        oMP.Name = oNewName
        If Err <> 0 Then
            'Logger.Error ("Failed to rename '" & oMP.Name & "' to '" & oNewName & "'")
            GoTo TryAgain1
        End If
        On Error GoTo 0
    Next
    oPDoc.Update
    i = 0
    For Each oMP In oMPs
TryAgain2:
        i = i + 1
        oNewName = "d" & i
        On Error Resume Next
        oMP.Name = oNewName
        If Err <> 0 Then
            'Logger.Error ("Failed to rename '" & oMP.Name & "' to '" & oNewName & "'")
            GoTo TryAgain2
        End If
        On Error GoTo 0
    Next
    oPDoc.Update
    oTrans.End
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 19 of 19

cadman777
Advisor
Advisor

I'm a hacker at best, so not the guy to ask to write code for you.

Is it possible that the code is still seeing two Parameters of the same name?

Maybe you can rename all the parameters by adding any letter and number to the end of it so you can have infinite renames (instead of totally renaming each Parameter)?

@WCrihfield@A.Acheson and @Ralf_Krieg have helped me in the past, and I learned this form some of the help they gave.

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes