Hello,
I wrote a little add-in to add a workpoint at the center of mass of the part or assembly. It functions perfectly on parts, but on assembly sometimes it fucntions sometimes not : I have an error : "Unspecified error : HRESULT : 0x80004005 (E_FAIL)" when creating the workpoint. Where is the bug ?
Sub
WorkPointAtMassCenter()
'Create a workpoint at the center of mass of the active document
' Set a reference to the active document.
Dim oDoc As Inventor.Document
oDoc = g_inventorApplication.ActiveDocument
Dim oCompDef AsComponentDefinition
oCompDef = oDoc.componentdefinition
' Get the Center of Mass.
Dim oCenterOfMass As Inventor.Point
oCenterOfMass = oCompDef.MassProperties.CenterOfMass
OnErrorResumeNext
Dim oWorkPoint As Inventor.WorkPoint
oWorkPoint = oCompDef.WorkPoints.item("Center Of Mass")
If Err.Number = 0 Then oWorkPoint.Delete() 'If the workpoint already exist, delete it
' Create a new workpoint at the location of the center of mass.
oWorkPoint = oCompDef.WorkPoints.AddFixed(oCenterOfMass) 'the error occurs on this line
' Rename the work point.
oWorkPoint.Name ="Center Of Mass"
' End If
OnErrorGoTo 0
EndSub
Can you check that if it fails what the oCompDef.MassProperties.CenterOfMass really returns? Whether an initialized point it returns or not?
After the line : oCenterOfMass = oCompDef.MassProperties.CenterOfMass
oCenterOfMass contains the right values for X, Y and Z, but oCompDef.MassProperties.CenterOfMass is in error : "COM error : no child available".
This looks wierd, if the oCompDef.MassProperties.CenterOfMass returns an error, the failure should happens at the line: oCenterOfMass = oCompDef.MassProperties.CenterOfMass. But if you have the oCenterOfMass with right value I think the AddFixed should succeed. Can you double check that which line the error really happens? And can you add below line above the oCenterOfMass = oCompDef.MassProperties.CenterOfMass:
Dim oUpdateMassProperties As ControlDefinition oUpdateMassProperties = g_inventorApplication.CommandManager.ControlDefinitions("AppUpdateMassPropertiesCmd") oUpdateMassProperties.Execute()
The code will help to update the mass properties, so the oCompDef.MassProperties.CenterOfMass then will return correct values. Please check if it helps.
I add the code for Massproperties update.
There is an error is on the line :
oCenterOfMass = oCompDef.MassProperties.CenterOfMass
I've put a breakpoint on the line, with 2 spies, on oCenterOfMass and on oCompDef.MassProperties.CenterOfMass.
Before the line is executed, I have :
oCenterOfMass = nothing
oCompDef.MassProperties.CenterOfMass = <COM type> "Erreur : no child available"
oCompDef is ok.
oCompDef.MassProperties is in error : "Erreur : no child available"
After the line is executed, I have :
oCenterOfMass = Inventor.point
x=...
y=...
z=...
oCompDef.MassProperties.CenterOfMass = <COM type> "Erreur : no child available"
Another error on when creating the workpoint :
oWorkPoint = oCompDef.WorkPoints.AddFixed(oCenterOfMass)
oCenterOfMass is right.
oCompDef is defined and seems to be right.
oCompDef.WorkPoints is in error : "No child available".
Looks like the MassProperties(oCompDef.MassProperties) does not work well there. Can you open the assembly which causes the code error, and open its iProperties dialog, and see if the Physical->General Properties->Center of Gravity has proper values? And if the Update button is enabled, click it and save the document, then try the code with the assembly to see if it works?
I open the assembly : the mass properties need to be updated.
Cancel the dialog box without updating.
Run the add-in: "Unspecifief error : HRESULT Exception : 0x80004005 (E_FAIL)
iProperties > mass properties are up to date, so the code to update mas properties functions. Ne need to update.
Run the add-in again : same error.
Is it OK for you to upload the adin and the data which can reproduce the problem so we can debug it? Please simiplify the addin as possible as you can, and don't upload any confidential data.
I post the solution. It add a tab "Utilities" in part or seembly environment.
You can test it with standard sample files :
On a part file, no problem.
On Engine MKII, no problem.
On piping sample "Buffer prep skid.iam" you have the problem.
Sorry for a little late to response. I tried your project and it works fine on my machine, I don't see any problem here for the code. I used the same data as you said to test.
But if you still see the same problem, can you try to cast the oCompDef to PartComponentDefinition or AssemblyComponentDefinition according to the document type instead of the ComponentDefinition. Please let me if it works.