Hello Everyone,
I came across this great post about renaming features in a part using iLogic (code below is from the solution of this post.) I was hoping to take it a step further to include other features, iFeatures and sketches. Adjusting the code to update other features is pretty straight forward but, I am running into issues when I try to get iFeatures and/or sketches to rename. I think this is due to my limited knowledge of code.
Here is the Code I'm working with;
oDoc = ThisApplication.ActiveDocument oBodies = oDoc.ComponentDefinition.SurfaceBodies 'look at the bodies For Each oBody In oBodies i = 1 'look at all the features that make up the body For Each oFeature In oBody.AffectedByFeatures 'look at only extrude features If oFeature.Type = ObjectTypeEnum.kExtrudeFeatureObject Then 'rename feature using solid name 'and pad the number with zero if under 10 oFeature.Name = oBody.Name _ & " - Ext. " & If(i < 10, "0" + CStr(i), CStr(i)) i = i + 1 End If Next Next
In the code above, I think I need to change "oFeature" and ObjectTypeEnum.kExtrudeFeatureObject. However, I don't know what I need to change this to.
Any idea how I can make this work?
Thanks,
Paul
Solved! Go to Solution.
Hello Everyone,
I came across this great post about renaming features in a part using iLogic (code below is from the solution of this post.) I was hoping to take it a step further to include other features, iFeatures and sketches. Adjusting the code to update other features is pretty straight forward but, I am running into issues when I try to get iFeatures and/or sketches to rename. I think this is due to my limited knowledge of code.
Here is the Code I'm working with;
oDoc = ThisApplication.ActiveDocument oBodies = oDoc.ComponentDefinition.SurfaceBodies 'look at the bodies For Each oBody In oBodies i = 1 'look at all the features that make up the body For Each oFeature In oBody.AffectedByFeatures 'look at only extrude features If oFeature.Type = ObjectTypeEnum.kExtrudeFeatureObject Then 'rename feature using solid name 'and pad the number with zero if under 10 oFeature.Name = oBody.Name _ & " - Ext. " & If(i < 10, "0" + CStr(i), CStr(i)) i = i + 1 End If Next Next
In the code above, I think I need to change "oFeature" and ObjectTypeEnum.kExtrudeFeatureObject. However, I don't know what I need to change this to.
Any idea how I can make this work?
Thanks,
Paul
Solved! Go to Solution.
Solved by clutsa. Go to Solution.
Yes, you do need to change the code to account for every kind of feature you'll come across in the model tree before you can rename it. Typically you have to get down through the component definition, then the collection of features you want to look at, then the specific feature out of that collection, then you can get to the property that stores the name you see in the browser.
Are you only looking to rename specific features in the model tree? Otherwise it'll take a while if you want to account for everything Inventor has to offer.
Right now that code is only looking at Surface Bodies, you'll have to do a loop for every kind of feature you want to rename.
Yes, you do need to change the code to account for every kind of feature you'll come across in the model tree before you can rename it. Typically you have to get down through the component definition, then the collection of features you want to look at, then the specific feature out of that collection, then you can get to the property that stores the name you see in the browser.
Are you only looking to rename specific features in the model tree? Otherwise it'll take a while if you want to account for everything Inventor has to offer.
Right now that code is only looking at Surface Bodies, you'll have to do a loop for every kind of feature you want to rename.
You were close... try this out
oDoc = ThisApplication.ActiveDocument oFeatures = oDoc.ComponentDefinition.Features i = 1 For Each oFeature In oFeatures 'look at all the features that make up the body 'look at only extrude features If oFeature.Type = ObjectTypeEnum.kExtrudeFeatureObject Then 'rename feature using solid name 'and pad the number with zero if under 10 oFeature.Name = oFeature.SurfaceBodies(1).Name _ & " - Ext. " & If(i < 10, "0" + CStr(i), CStr(i)) i = i + 1 End If Next
You were close... try this out
oDoc = ThisApplication.ActiveDocument oFeatures = oDoc.ComponentDefinition.Features i = 1 For Each oFeature In oFeatures 'look at all the features that make up the body 'look at only extrude features If oFeature.Type = ObjectTypeEnum.kExtrudeFeatureObject Then 'rename feature using solid name 'and pad the number with zero if under 10 oFeature.Name = oFeature.SurfaceBodies(1).Name _ & " - Ext. " & If(i < 10, "0" + CStr(i), CStr(i)) i = i + 1 End If Next
Scratch what I said earlier, I wasn't aware that you could go right to the name without going through specific feature collections.
Scratch what I said earlier, I wasn't aware that you could go right to the name without going through specific feature collections.
Clusta, that looks great. Thank you. What would this code look like to rename Holes, iFeatures and/or Sketches?
Clusta, that looks great. Thank you. What would this code look like to rename Holes, iFeatures and/or Sketches?
I got this far but I have to do may actual job now I guess. I'm close on the sketch part but not all sketches have dependents so you'll have to find a way around that (I'm sure a Try/Catch could do it)
oDoc = ThisApplication.ActiveDocument oFeatures = oDoc.ComponentDefinition.Features oSketches = oDoc.ComponentDefinition.Sketches ext = 1 iFeat = 1 'same pattern for other feature counts here For Each oFeature In oFeatures 'look at all the features that make up the body Select Case oFeature.Type Case = ObjectTypeEnum.kExtrudeFeatureObject oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (ext < 10, "0" + CStr(ext), CStr(ext)) ext = ext + 1 Case = ObjectTypeEnum.kiFeatureObject oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat)) iFeat = iFeat + 1 'Case = ObjectTypeEnum.k... 'same pattern as above End Select Next For Each oSketch In OSketches If oSketch.Type = ObjectTypeEnum.kPlanarSketchObject Then For Each Dependent In oSketch.Dependents ' If Not Dependent.Definition Is Nothing Then ' oSketch.Name = Dependent.Definition.Parent.Name & " - Sketch" ' End If Next End If Next
I got this far but I have to do may actual job now I guess. I'm close on the sketch part but not all sketches have dependents so you'll have to find a way around that (I'm sure a Try/Catch could do it)
oDoc = ThisApplication.ActiveDocument oFeatures = oDoc.ComponentDefinition.Features oSketches = oDoc.ComponentDefinition.Sketches ext = 1 iFeat = 1 'same pattern for other feature counts here For Each oFeature In oFeatures 'look at all the features that make up the body Select Case oFeature.Type Case = ObjectTypeEnum.kExtrudeFeatureObject oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (ext < 10, "0" + CStr(ext), CStr(ext)) ext = ext + 1 Case = ObjectTypeEnum.kiFeatureObject oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat)) iFeat = iFeat + 1 'Case = ObjectTypeEnum.k... 'same pattern as above End Select Next For Each oSketch In OSketches If oSketch.Type = ObjectTypeEnum.kPlanarSketchObject Then For Each Dependent In oSketch.Dependents ' If Not Dependent.Definition Is Nothing Then ' oSketch.Name = Dependent.Definition.Parent.Name & " - Sketch" ' End If Next End If Next
Thank you Clusta! that works perfect. from there I was able to make everything I needed. Sketches would be nice but, know the variables, I figured it would be more work that it was worth.
I would recommend editing the code you have though. Although iFeatures are renamed, they are renamed as Ext.
What you have
oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Should be
oFeature.Name = oFeature.SurfaceBodies(1).Name & " - iFeat. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Thanks again for your help
Thank you Clusta! that works perfect. from there I was able to make everything I needed. Sketches would be nice but, know the variables, I figured it would be more work that it was worth.
I would recommend editing the code you have though. Although iFeatures are renamed, they are renamed as Ext.
What you have
oFeature.Name = oFeature.SurfaceBodies(1).Name & " - Ext. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Should be
oFeature.Name = oFeature.SurfaceBodies(1).Name & " - iFeat. " & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Thanks again for your help
when I use this rule for the first time everything works perfectly if after I do other processing and I use the rule again it gives me the following error:
Errore alla riga 11 nella regola: Regola0, nel documento: PivotconSA.ipt
Errore non specificato. (Eccezione da HRESULT: 0x80004005 (E_FAIL))
unspecified error
How can I fix?
when I use this rule for the first time everything works perfectly if after I do other processing and I use the rule again it gives me the following error:
Errore alla riga 11 nella regola: Regola0, nel documento: PivotconSA.ipt
Errore non specificato. (Eccezione da HRESULT: 0x80004005 (E_FAIL))
unspecified error
How can I fix?
Hi @ldellaversano. It is very difficult to determine what may be going wrong just based on the information you provided. If the error happens again, can you look at the 'More Info' tab of the error message that shows, and translate that to English for us. If you just take a screenshot of the error message, it will still be in be in your native language, and some of us will not be able to read it or translate it. Usually there is a line of text within the 'More Info' tab that indicates what it was trying to do when the error was encountered, and we can use that to determine where in your code the error occurred. But even that may not help if we can not see your code. I suspect, based on your description that this may be a simple matter of needing to update the document after making some edits before trying to change the feature names again, but it is very difficult to judge on so little information. Please include as much information as possible.
Wesley Crihfield
(Not an Autodesk Employee)
Hi @ldellaversano. It is very difficult to determine what may be going wrong just based on the information you provided. If the error happens again, can you look at the 'More Info' tab of the error message that shows, and translate that to English for us. If you just take a screenshot of the error message, it will still be in be in your native language, and some of us will not be able to read it or translate it. Usually there is a line of text within the 'More Info' tab that indicates what it was trying to do when the error was encountered, and we can use that to determine where in your code the error occurred. But even that may not help if we can not see your code. I suspect, based on your description that this may be a simple matter of needing to update the document after making some edits before trying to change the feature names again, but it is very difficult to judge on so little information. Please include as much information as possible.
Wesley Crihfield
(Not an Autodesk Employee)
This is the other info:
System.Runtime.InteropServices.COMException (0x80004005): Errore non specificato. (Eccezione da HRESULT: 0x80004005 (E_FAIL))
in Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object o, Type& objType, String name, Object[] args, String[] paramnames, Boolean OptimisticSet, CallType UseCallType)
in Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType)
in Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments)
in ThisRule.Main() in regola: Regola0, nel documento PivotconSA.ipt:riga 11
in Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
in iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
This is my code:
oDoc = ThisApplication.ActiveDocument
oFeatures = oDoc.ComponentDefinition.Features
ext = 1
iFeat = 1
'same pattern for other feature counts here
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kExtrudeFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kRevolveFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kFilletFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kChamferFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
Thanks thousand
This is the other info:
System.Runtime.InteropServices.COMException (0x80004005): Errore non specificato. (Eccezione da HRESULT: 0x80004005 (E_FAIL))
in Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object o, Type& objType, String name, Object[] args, String[] paramnames, Boolean OptimisticSet, CallType UseCallType)
in Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType)
in Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments)
in ThisRule.Main() in regola: Regola0, nel documento PivotconSA.ipt:riga 11
in Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
in iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
This is my code:
oDoc = ThisApplication.ActiveDocument
oFeatures = oDoc.ComponentDefinition.Features
ext = 1
iFeat = 1
'same pattern for other feature counts here
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kExtrudeFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kRevolveFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kFilletFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
For Each oFeature In oFeatures 'look at all the features that make up the body
Select Case oFeature.Type
Case = ObjectTypeEnum.kChamferFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (ext < 10, "0" + CStr(ext), CStr(ext))
ext = ext + 1
Case = ObjectTypeEnum.kiFeatureObject
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
iFeat = iFeat + 1
'Case = ObjectTypeEnum.k...
'same pattern as above
End Select
Next
Thanks thousand
Hi @ldellaversano. Here is a modified version of your code that I just created for you, but have not tested yet. It looked to me like the problem you were likely encountering is that the code was trying to name another feature exactly the same as an already existing feature, which I don't think Inventor allows in the same document. It is very easy to do, so it is difficult to avoid with a rule like this. And I still can not guarantee that you will not encounter the same issue while using this version, but this version is a bit shorter and easier to look at.
Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oFeatures As Object = oDoc.ComponentDefinition.Features
'must be different counter for each different type of feature
Dim iExt As Integer = 1
Dim iRev As Integer = 1
Dim iFill As Integer = 1
Dim iCham As Integer = 1
Dim iFeat As Integer = 1
For Each oFeature As PartFeature In oFeatures
Select Case oFeature.Type
Case ObjectTypeEnum.kExtrudeFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (iExt < 10, "0" + CStr(iExt), CStr(iExt))
Catch
End Try
iExt = iExt + 1
Case ObjectTypeEnum.kRevolveFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (iRev < 10, "0" + CStr(iRev), CStr(iRev))
Catch
End Try
iRev = iRev + 1
Case ObjectTypeEnum.kFilletFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (iFill < 10, "0" + CStr(iFill), CStr(iFill))
Catch
End Try
iFill = iFill + 1
Case ObjectTypeEnum.kChamferFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (iCham < 10, "0" + CStr(iCham), CStr(iCham))
Catch
End Try
iCham = iCham + 1
Case ObjectTypeEnum.kiFeatureObject
'an iFeature can contain multiple other types of features
'Dim oIFeature As Inventor.iFeature = oFeature
'Dim oIFeatDef As iFeatureDefinition = oIFeature.iFeatureDefinition
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Other" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Catch
End Try
iFeat = iFeat + 1
End Select
Next
Wesley Crihfield
(Not an Autodesk Employee)
Hi @ldellaversano. Here is a modified version of your code that I just created for you, but have not tested yet. It looked to me like the problem you were likely encountering is that the code was trying to name another feature exactly the same as an already existing feature, which I don't think Inventor allows in the same document. It is very easy to do, so it is difficult to avoid with a rule like this. And I still can not guarantee that you will not encounter the same issue while using this version, but this version is a bit shorter and easier to look at.
Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oFeatures As Object = oDoc.ComponentDefinition.Features
'must be different counter for each different type of feature
Dim iExt As Integer = 1
Dim iRev As Integer = 1
Dim iFill As Integer = 1
Dim iCham As Integer = 1
Dim iFeat As Integer = 1
For Each oFeature As PartFeature In oFeatures
Select Case oFeature.Type
Case ObjectTypeEnum.kExtrudeFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Es" & If (iExt < 10, "0" + CStr(iExt), CStr(iExt))
Catch
End Try
iExt = iExt + 1
Case ObjectTypeEnum.kRevolveFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Riv" & If (iRev < 10, "0" + CStr(iRev), CStr(iRev))
Catch
End Try
iRev = iRev + 1
Case ObjectTypeEnum.kFilletFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Racc" & If (iFill < 10, "0" + CStr(iFill), CStr(iFill))
Catch
End Try
iFill = iFill + 1
Case ObjectTypeEnum.kChamferFeatureObject
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Sm" & If (iCham < 10, "0" + CStr(iCham), CStr(iCham))
Catch
End Try
iCham = iCham + 1
Case ObjectTypeEnum.kiFeatureObject
'an iFeature can contain multiple other types of features
'Dim oIFeature As Inventor.iFeature = oFeature
'Dim oIFeatDef As iFeatureDefinition = oIFeature.iFeatureDefinition
Try
oFeature.Name = oFeature.SurfaceBodies(1).Name & "_Other" & If (iFeat < 10, "0" + CStr(iFeat), CStr(iFeat))
Catch
End Try
iFeat = iFeat + 1
End Select
Next
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.