Hi,
I am trying to delete all unused derived parameters. I feel like they might slow me down and give unnecessary updates.
I tried to do this by code but I get a error.. Any Ideas what could be wrong? The error is thrown by the oDerivedParameter.Delete method.
For Each oDerivedParameter As DerivedParameter In DerivedParameterTable.DerivedParameters If Not oDerivedParameter.InUse Then oDerivedParameter.Delete() Next
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of an invocation.
Hi Jef,
This is a blog I did on that very topic a few years ago. Its old iLogic code, and haven't tested it in several years, but it used to work fine 🙂
http://classic.cadlinecommunity.co.uk/Blogs/Blog.aspx?ScoId=352228f8-6b7a-447f-a6bd-5b94590d3175&ret...
Thanks,
Luke
The problem has to do with which methods are available for "DerivedParameter" objects (which are part of the
Document.ComponentDefinition.Parameters.DerivedParameterTables(i).DerivedParameters
collection) as opposed to "DerivedPartEntity" objects (which are part of the
Document.ComponentDefinition.ReferenceComponents.DerivedPartComponents(i).Definition.Parameters
collection) -- both of which are different ways of accessing derived parameters.
The "InUse" method is only available for "DerivedParameter" and regular old "Parameter" objects, while the "IncludeEntity" method is only available for "DerivedPartEntity" objects.
You need to iterate through all "DerivedPartEntitity" parameter objects, check if a parameter of the same name is "InUse", and if it's not then use the "IncludeEntity" method on the "DerivedPartEntity" to remove it.
Another thing I've found is that for some reason you have to set the derived part component definition back to itself in order for the "IncludeEntity" method to take effect.
The following code should work for you (bear in mind there is no error handling):
Dim oDoc As PartDocument = ThisDoc.Document For Each oDerivedPartComponent As Inventor.DerivedPartComponent In oDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents Dim oDerivedPartDefinition As Inventor.DerivedPartDefinition = oDerivedPartComponent.Definition For Each oParameterEntity As Inventor.DerivedPartEntity In oDerivedPartDefinition.Parameters If Not Parameter.Param(oParameterEntity.ReferencedEntity.Name).InUse Then oParameterEntity.IncludeEntity = False End If Next 'Derived parameter oDerivedPartComponent.Definition = oDerivedPartDefinition Next 'Derived part component
@Anonymous's code essentially does the same thing but with much more extensive proper checks, error handling, and user-friendly messages. However, one interesting thing I noticed about his code is it does not in any way check if a derived parameter is used before trying to delete it. The code simply tries to delete every derived parameter, and somehow Inventor is smart enough to not error out, and keep the parameter anyway, if it's used, which is pretty neat.
So the if-statement to check if the parameter is "InUse" really isn't necessary, and the code above could simply be:
Dim oDoc As PartDocument = ThisDoc.Document For Each oDerivedPartComponent As Inventor.DerivedPartComponent In oDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents Dim oDerivedPartDefinition As Inventor.DerivedPartDefinition = oDerivedPartComponent.Definition For Each oParameterEntity As Inventor.DerivedPartEntity In oDerivedPartDefinition.Parameters oParameterEntity.IncludeEntity = False Next 'Derived parameter oDerivedPartComponent.Definition = oDerivedPartDefinition Next 'Derived part component
Oh and as for the ".delete" method, I'm not sure why that doesn't work but I'm assuming it's intended for normal User parameters, and it's for some reason "available" for use on Derived parameters, but doesn't work due to the fact that you can't really "delete" a derived parameter, you can only un-include it from the Derive operation. Hence the need to access the parameter via the DerivedPartDefinition object and use the IncludeEntity method.
Hi,
I'm using VBA, try this :
Public Sub RemoveUnusedDeriveParam()
Dim oApp As Application
Dim oPD As PartDocument
Dim oPCD As PartComponentDefinition
Dim oDPC As DerivedPartComponent
Dim oPs As Parameters
Dim oMPs As ModelParameters
Dim oMP As ModelParameter
Dim oUPS As UserParameters
Dim oUP As UserParameter
Dim oDeriveParam As Parameter
Dim NewDPCDef As DerivedPartDefinition
Dim oBjCol As ObjectCollection
Set oApp = ThisApplication
Set oPD = oApp.ActiveDocument
Set oPCD = oPD.ComponentDefinition
Set oPs = oPCD.Parameters
Set oMPs = oPs.ModelParameters
Set oUPS = oPs.UserParameters
Set oDPC = oPCD.ReferenceComponents.DerivedPartComponents.Item(1)
Set NewDPCDef = oDPC.Definition
Set oBjCol = oApp.TransientObjects.CreateObjectCollection
For Each oDeriveParam In oDPC.Parameters
If oDeriveParam.InUse = True Then
Call oBjCol.Add(oDeriveParam)
End If
Next
For Each Item In oBjCol
i = 0
For Each oDeriveParam In oDPC.Parameters
i = i + 1
If Item.Name = oDeriveParam.Name Then
NewDPCDef.Parameters.Item(i).IncludeEntity = True
Else
NewDPCDef.Parameters.Item(i).IncludeEntity = False
End If
Next
Next
oDPC.Definition = NewDPCDef
End Sub
I was getting no results until I tried setting the definition to itself after the change. It worked, but crashed with some kind of badly-described error. So I put it in a Try Catch block, and that got it through - changing ONE parameter only. After that, any attempt to access the objects causes another exception right away, including changing another parameter's IncludeEntity. So I can only change one parameter this way! Not sure why it's necessary to set the definition to itself anyhow. But if you have any ideas for me, I'm all ears.
Dim baseName As String = "C:\Users\Don\Documents\Inventor\Speaker Measurement Platform\Base Part.ipt" Dim docDer As Document = ThisDoc.Document Dim docBase As Document = GetDoc(baseName) ' Get information from derived document Dim cdDerived As PartComponentDefinition = docDer.ComponentDefinition ' Find base documents Dim rcsDerived As ReferenceComponents = cdDerived.ReferenceComponents Dim dpcs As DerivedPartComponents = rcsDerived.DerivedPartComponents ' Iterate base documents Dim dpcBase As DerivedPartComponent Dim descBase As DocumentDescriptor For Each dpcBase In dpcs descBase = dpcBase.ReferencedDocumentDescriptor Logger.Debug(" " & descBase.FullDocumentName) If descBase.FullDocumentName = baseName Then Exit For Next dpcBase If descBase.FullDocumentName <> baseName Then Logger.Debug(baseName & " not found.") Exit Sub End If ' Got an error doing docDer.Update too. ' This is the line that causes update to error: Dim dpd As DerivedPartDefinition = dpcBase.Definition If dpd Is Nothing Then Logger.Debug("dpd Is Nothing") End If ' Didn't work either: 'dpd.IncludeAllParameters = True 'exit sub ' Works, but is system unstable after? Could it do two params? Dim dpes As DerivedPartEntities = dpd.Parameters Dim dpe As DerivedPartEntity For Each dpe In dpes Try If dpe.ReferencedEntity.Name = "ParamToPass" Or _ dpe.ReferencedEntity.Name = "PassThisToo" Then 'Logger.Debug(" Found! Val was " & dpe.IncludeEntity) dpe.IncludeEntity = True dpcBase.Definition = dpd Logger.Debug(" Changed! Val is " & dpe.IncludeEntity)
' Exit For End If Catch End Try Next dpe End Sub
I did find a workaround: duplicating all the code from the line:
Dim dpd As DerivedPartDefinition = dpcBase.Definition
to the end of the subroutine, just changing the parameter name. It derived both parameters. Obviously I can do that in a loop, so it's not too bad. But not sure why I'm having these errors.
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DerivedPartEntity.get_IncludeEntity()
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Try this:
Public Sub x() Dim oderivedparameter As DerivedParameter Dim DerivedParameterTable As DerivedParameterTable 'DerivedParameterTable = ThisDocument.ComponentDefinition.Parameters.ParameterTables For Each oderivedparameter In ThisDocument.ComponentDefinition.Parameters.ParameterTables If oderivedparameter.InUse = False Then oderivedparameter.Delete Next End Sub
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Can't find what you're looking for? Ask the community or share your knowledge.