- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Inventor experts!
I am here again with another ask for help.
I have searched in this forum and got the code below to create a point in center of gravity.
Option Explicit On
' Check to make sure a part or assembly document is active.
Dim doc As Document = ThisDoc.Document
If doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject AndAlso doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show(" This rule can only be run in a part or assembly.", "WorkPoint At Mass Center")
Exit Sub
End If
Dim massProps As MassProperties
Dim workPoints As workPoints
If (doc.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
Dim partDoc As PartDocument = doc
massProps = partDoc.ComponentDefinition.MassProperties
workPoints = partDoc.ComponentDefinition.WorkPoints
Else
Dim assemDoc As AssemblyDocument = doc
massProps = assemDoc.ComponentDefinition.MassProperties
workPoints = assemDoc.ComponentDefinition.WorkPoints
End If
' Get the Center of Mass.
Dim oCenterOfMass As Point = massProps.CenterOfMass
' Check to see if a work point for center of mass already exists.
' This uses the name of the work feature to identify it.
Const workPointName As String = "Shipping Center Of Gravity"
Dim oWorkPoint As WorkPoint
Try
oWorkPoint = workPoints.Item(workPointName)
Catch
oWorkPoint = Nothing
End Try
If (oWorkPoint IsNot Nothing) Then
If (doc.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
Dim oFixedDef As FixedWorkPointDef = oWorkPoint.Definition
oFixedDef.Point = oCenterOfMass
Else
Dim oFixedDef As AssemblyWorkPointDef = oWorkPoint.Definition
oFixedDef.Point = oCenterOfMass
End If
doc.Update
Else
' Create a new workpoint at the location of the center of mass.
oWorkPoint = workPoints.AddFixed(oCenterOfMass)
' Rename the work point.
oWorkPoint.Name = workPointName
End If
This center of gravity point is created as workfeature and works 100% in case there is only one solid in the file.
But in my real situation I have more than one solid in the ipt file (solid1, solid2, solid3 …) and for this case, this code doesn’t work.
My goal at this moment is to get help to adapt this code to became possible to add center of gravities points in several solids, by selecting one solid each time.
My idea for this solution, would be a rule that shows a message box, asking for select which solid the center of gravity should be placed in.
I am available to provide any other information required.
Thanks in advance for all colleagues that can help.
Best regards,
Solved! Go to Solution.