Here is another example code you can try out in an iLogic rule. It does mostly the same task, but includes the name of the WorkPoint while recording the data, and writes the name of the WorkPoint in the first column in the Excel sheet, then its coordinates in the next 3 columns after that. It also divides the overall task up into 3 parts (Main, Get Data, & Write Data), using 3 code routines. It must have a 'Sub Main' area, if it will have other routines, and the main area gets the WorkPoints from the current document, then calls the other two routines to run. The second routine extracts all the data from the WorkPoints, and puts it into a Dictionary. Then the third routine writes that data to Excel. There are lots of ways this type of code could be laid-out. I have not tested it yet though, since I have no use for it myself, and don't have a good example model for it to work on. Plus I am leaving for the day. Let me know if it works OK for you.
AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Sub Main
'get current document
Dim oDoc As Inventor.Document = ThisDoc.Document
'check which type of document we got...if not a part or assembly, then exit rule
If (Not TypeOf oDoc Is PartDocument) AndAlso (Not TypeOf oDoc Is AssemblyDocument) Then
MessageBox.Show("This code only works for a Part or Assembly.", "Wrong Document Type", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return 'this exit the code routine, and the rule in this case
End If
'get the ComponentDefinition (where all geometry & coordinate system exists)
'this is getting a generic version of that type of object
Dim oCD As Inventor.ComponentDefinition = oDoc.ComponentDefinition
'get the WorkPoints collection for this document
Dim oWPoints As Inventor.WorkPoints = oCD.WorkPoints
'call the custom Function defined after this routine to run
'it is designed to return a Dictionary of entries, where each entry has two parts
'Each entry's Key is the name of the WorkPoint
'each entry's Value is an array of Double type values (the Point's 3 coordinates)
Dim oPointCoordsData As Dictionary(Of String, Double()) = GetWorkPointsData(oWPoints)
'now we should check the returned data, to see if it is valid
If (oPointCoordsData Is Nothing) OrElse (oPointCoordsData.Count = 0) Then
'if not, then let user know, before exiting this code routine
MessageBox.Show("No data was collected from the WorkPoints!", "No Data Collected", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return 'exit this code routine
End If
'<<< now we can focus on the task of writing that data to Excel >>>
'call a custom Sub routine defined below to write that data to Excel
WriteCoordsDataToExcel(oPointCoordsData)
End Sub
Function GetWorkPointsData(WPoints As Inventor.WorkPoints) As Dictionary(Of String, Double())
'create a collection type variable to hold names and point coordinates data
'this is a Dictionary, which is a 2-factor list (each entry has 2 parts)
'each entry has a 'Key' and a 'Value'
'the Key here will be a String (text), the name of the WorkPoint
'the Value will be an array of Double values, the X,Y,Z coordinates of the Point
Dim oPointCoordsData As New Dictionary(Of String, Double())
'iterate through all WorkPoints, in their 'natural' order (oldest existing to newest)
For i As Integer = 1 To WPoints.Count
'get the WorkPoint at this Index
Dim oWPoint As Inventor.WorkPoint = WPoints.Item(i)
'get the transient mathematical Point object from it
Dim oPoint As Inventor.Point = oWPoint.Point
'create variable to supply to next method
'this is an array of Double type values (Double is a decinal type numerical value)
Dim oCoords() As Double = {}
'this method sets the value of previous variable
oPoint.GetPointData(oCoords)
'check if the Dictionary already contains an entry for this WorkPoint
If Not oPointCoordsData.ContainsKey(oWPoint.Name) Then
'if not, then add one, with its name, and its coordinates
oPointCoordsData.Add(oWPoint.Name, oCoords)
End If
Next 'i 'this is where it goes to the next Index of WorkPoints, if there are any
'return the collected data to the proceedure that called this Function to run
Return oPointCoordsData
End Function
Sub WriteCoordsDataToExcel(PointCoordsData As Dictionary(Of String, Double()))
'create an instance of the Excel.Application object
Dim oExcel As Object = CreateObject("Excel.Application")
'make Excel visible
oExcel.Visible = True
'create a new Excel Workbook
Dim oWB As Object = oExcel.Workbooks.Add()
'create a new Excel Worksheet
Dim oWS As Object = oWB.Worksheets.Add()
Dim iRow As Integer = 1
Dim iCol As Integer = 1
'write column headers/labels
oWS.Cells(iRow, 1) = "Name"
oWS.Cells(iRow, 2) = "X"
oWS.Cells(iRow, 3) = "Y"
oWS.Cells(iRow, 4) = "Z"
'start iterating through the data that we collected earlier, in its natural order
For i As Integer = 0 To PointCoordsData.Count - 1
iRow += 1
'get the entry at this Index
Dim oEntry As KeyValuePair(Of String, Double()) = PointCoordsData.ElementAt(i)
'get the name of the WorkPoint from the entry
Dim sName As String = oEntry.Key
'get the coordinates of the Point from the entry
Dim oCoords() As Double = oEntry.Value
'write the WorkPoint's name to the first column in the row
oWS.Cells(iRow, 1) = sName
'write the X coordinate to the second column
oWS.Cells(iRow, 2) = oCoords(0)
'write the Y coordinate to the third column
oWS.Cells(iRow, 3) = oCoords(1)
'write the Z coordinate to the fourth column
oWS.Cells(iRow, 4) = oCoords(2)
Next 'i 'go to next entry in the Dictionary
'negate the variables, without closing the Workbook or Excel
oWS = Nothing
oWB = Nothing
oExcel = Nothing
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)