Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

bug ?

9 REPLIES 9
Reply
Message 1 of 10
TONELLAL
485 Views, 9 Replies

bug ?

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

 

9 REPLIES 9
Message 2 of 10
YuhanZhang
in reply to: TONELLAL

Can you check that if it fails what the oCompDef.MassProperties.CenterOfMass really returns? Whether an initialized point it returns or not?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 10
TONELLAL
in reply to: YuhanZhang

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".

Message 4 of 10
YuhanZhang
in reply to: TONELLAL

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.

 

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 5 of 10
TONELLAL
in reply to: YuhanZhang

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".

Message 6 of 10
YuhanZhang
in reply to: TONELLAL

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?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 7 of 10
TONELLAL
in reply to: YuhanZhang

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.

Message 8 of 10
YuhanZhang
in reply to: TONELLAL

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.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 9 of 10
TONELLAL
in reply to: YuhanZhang

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.

Message 10 of 10
YuhanZhang
in reply to: TONELLAL

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.

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report