Automating data entry into Autodesk Inventor with Excel




In the world of CAD and design, efficiency and accuracy are paramount. Automation in Autodesk Inventor using Excel data can significantly streamline workflows, particularly in repetitive tasks like data entry. This guide provides a detailed explanation of how to automate the process of inputting data into Inventor using a sample C# or VB code script.

Understanding the Code

The provided code snippet is written in C# and VB, and is intended to be used with Autodesk Inventor. It automates the task of updating Inventor part files (.ipt) with data extracted from an Excel sheet.

Key Components of the Code

  1. Excel Worksheet Initialization:
    • The script starts by accessing an active Excel worksheet, allowing the program to read data from it.
  2. Setting Up File Search Parameters:
    • It defines a folder to search (C:\Temp\) and a file type (.ipt) to look for, focusing on Inventor part files.
  3. Looping Through Excel Rows:
    • The for loop iterates over rows in the Excel sheet (up to 5 rows in this case), reading data for each row.
    • You can adjust the numbers to suit your document
  4. Extracting Data from Excel:
    • The script reads file names, properties, and parameters from specific columns in Excel (columns A, B, and C).
    • Feel free to repeat the same format if you need more properties changed
  5. Finding and Opening Inventor Documents:
    • It searches for files in the specified folder that match the file names obtained from Excel and opens the corresponding Inventor document.
  6. Updating Inventor Document Properties and Parameters:
    • The code updates specific properties and parameters in the Inventor document based on the Excel data.
    • To find out more about property sets scroll down to Understanding Property sets
  7. Saving Changes:
    • After updating, the document is saved and closed.

Code

C#
// Assuming _Excel and _invApp are already defined and initialized
// 1. Excel Worksheet Initialization
Worksheet oSheet = _Excel.ActiveSheet;

// 2. Setting Up File Search Parameters
string serchFolder = "C:\\Temp\\";
string fileType = ".ipt";

// 3. Looping Through Excel Rows
for (int i = 1; i <= 5; i++)
{
    // 4. Extracting Data from Excel
    string fileName = oSheet.Range["A" + i].Value;
    string Property1 = oSheet.Range["B" + i].Value;
    double Parameter1 = oSheet.Range["C" + i].Value;

    // 5. Finding and Opening Inventor Documents
    string[] files = System.IO.Directory.GetFiles(serchFolder,  fileName + fileType, System.IO.SearchOption.AllDirectories);
    if (files.Length != 1 )
    {
        System.Windows.Forms.MessageBox.Show("Could not find exact match for " + fileName);
        return;
    }

    PartDocument oDoc = (PartDocument)_invApp.Documents.Open(files[0]);

    // 6. Updating Inventor Document Properties and Parameters
    // All property sets in Inventor
    PropertySet invSumaryInfo = oDoc.PropertySets["Inventor Summary Information"];
    PropertySet invDocSumaryInfo = oDoc.PropertySets["Inventor Document Summary Information"];
    PropertySet invDesignInfo = oDoc.PropertySets["Design Tracking Properties"];
    PropertySet invUserDefinedInfo = oDoc.PropertySets["Inventor User Defined Properties"];

    // Set our new property
    invDesignInfo["Description"].Value = Property1;

    // Set our new perameter
    oDoc.ComponentDefinition.Parameters["d1"].Value = Parameter1;

    // 7. Saving Changes
    oDoc.Update();
    oDoc.Save();
    oDoc.Close();
}
VB
'Assuming _Excel and _invApp are already defined and initialized
'1. Excel Worksheet Initialization
Dim oSheet As Worksheet = _Excel.ActiveSheet

'2. Setting Up File Search Parameters
Dim serchFolder As String = "C:\Temp\"
Dim fileType As String = ".ipt"

'3. Looping Through Excel Rows
For i As Integer = 1 To 5

    '4. Extracting Data from Excel
    Dim fileName As String = oSheet.Range("A" & i).Value
    Dim Property1 As String = oSheet.Range("B" & i).Value
    Dim Parameter1 As Double = oSheet.Range("C" & i).Value

    '5. Finding and Opening Inventor Documents
    Dim files As String() = System.IO.Directory.GetFiles(serchFolder, fileName + fileType, IO.SearchOption.AllDirectories)
    
    If Not files.Length = 1 Then
        MessageBox.Show("Could no find exact match for " + fileName)
        Return
    End If

    Dim oDoc As PartDocument = _Invapp.Documents.Open(files(0))

    '6. Updating Inventor Document Properties and Parameters
    'All property sets in Inventor
    Dim invSumaryInfo As PropertySet = oDoc.PropertySets("Inventor Summary Information")
    Dim invDocSumaryInfo As PropertySet = oDoc.PropertySets("Inventor Document Summary Information")
    Dim invDesignInfo As PropertySet = oDoc.PropertySets("Design Tracking Properties")
    Dim invUserDefinedInfo As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")

    'Set our new property
    invDesignInfo("Description").Value = Property1

    'Set our new perameter
    oDoc.ComponentDefinition.Parameters("d1").Value = Parameter1

    '7. Saving Changes
    oDoc.Update()
    oDoc.Save()
    oDoc.Close()
Next

How to Use This Script

To effectively use this script:

  1. Prepare Your Excel Sheet: Ensure your Excel sheet is structured correctly, with the file names, properties, and parameters in the right columns.
  2. Connect the Script with Excel & Inventor: Add references to the applications with the variables ‘_Excel‘ and ‘_invApp‘ as shown here. Note: both Excel and Inventors API include the object Application, make sure to use the correct version for each program.
  3. Customize the Script: Modify the script to match the exact structure of your Excel sheet and the specific requirements of your Inventor files.
  4. Run the Script in a Compatible Environment: This script should be run in an environment where both Autodesk Inventor and Excel are accessible, typically within a .NET application that references Inventor API and Excel interop assemblies.

Benefits of Automation in Inventor

  • Efficiency: Automates repetitive tasks, significantly reducing the time spent on manual data entry.
  • Accuracy: Reduces the likelihood of human error in data transcription.
  • Scalability: Easily update multiple documents, making it ideal for large projects.

Transferring Data from Autodesk Inventor to Excel

Extending the Automation Script

While the primary focus of the provided script is to automate the process of updating Inventor documents using data from Excel, it can also be adapted to perform the reverse: extracting data from Inventor files and populating an Excel spreadsheet. This modification involves reading properties and parameters from Inventor documents and writing them into an Excel sheet.

Key Modifications for Data Transfer

To adapt the script for transferring data from Inventor to Excel, several key changes are required:

  1. Reading Data from Inventor Documents:
    • Instead of setting properties in Inventor based on Excel data, the script needs to read these properties from the Inventor documents.
  2. Writing Data to Excel:
    • Establish a connection to an Excel sheet and write the extracted data into specific cells.
  3. Looping Through Inventor Files:
    • The script should loop through a set of Inventor files in a specified directory, similar to the original script, but this time to read data from them.

Example Code Snippet

Here’s a basic example of how the script can be modified:

C#
// Assuming _Excel and _invApp are already defined and initialized
Worksheet oSheet = _Excel.ActiveSheet;
string searchFolder = "C:\\Temp\\";
string fileType = ".ipt";

// Serch for all ipt files in the folder
string[] inventorFiles = System.IO.Directory.GetFiles(searchFolder, "*" + fileType, System.IO.SearchOption.AllDirectories);

// run for each file found
for (int i = 0; i < inventorFiles.Length; i++)
{
    PartDocument oDoc = (PartDocument)_invApp.Documents.Open(inventorFiles[i]);

    // Example: Reading properties from Inventor
    PropertySet invDesignInfo = oDoc.PropertySets["Design Tracking Properties"];
    string propertyValue = invDesignInfo["Description"].Value.ToString();

    // Example: Reading a parameter
    double parameterValue = oDoc.ComponentDefinition.Parameters["d1"].Value;

    // Writing to Excel
    oSheet.Range["A" + (i + 1)].Value = System.IO.Path.GetFileNameWithoutExtension(inventorFiles[i]);
    oSheet.Range["B" + (i + 1)].Value = propertyValue;
    oSheet.Range["C" + (i + 1)].Value = parameterValue;

    oDoc.Close();
}
VB
' Assuming _Excel and _invApp are already defined and initialized
Dim oSheet As Worksheet = _Excel.ActiveSheet
Dim searchFolder As String = "C:\Temp\"
Dim fileType As String = ".ipt"

// Serch for all ipt files in the folder
Dim inventorFiles As String() = System.IO.Directory.GetFiles(searchFolder, "*" & fileType, System.IO.SearchOption.AllDirectories)

// run for each file found
For i As Integer = 0 To inventorFiles.Length - 1

    Dim oDoc As PartDocument = CType(_invApp.Documents.Open(inventorFiles(i)), PartDocument)

    ' Example: Reading properties from Inventor
    Dim invDesignInfo As PropertySet = oDoc.PropertySets("Design Tracking Properties")
    Dim propertyValue As String = invDesignInfo("Description").Value.ToString()

    ' Example: Reading a parameter
    Dim parameterValue As Double = oDoc.ComponentDefinition.Parameters("d1").Value

    ' Writing to Excel
    oSheet.Range("A" & (i + 1).ToString()).Value = System.IO.Path.GetFileNameWithoutExtension(inventorFiles(i))
    oSheet.Range("B" & (i + 1).ToString()).Value = propertyValue
    oSheet.Range("C" & (i + 1).ToString()).Value = parameterValue

    oDoc.Close()
Next

Considerations and Benefits

  • Flexibility in Data Management: This modification allows for a two-way flow of data, making it easier to manage and document your Inventor projects.
  • Data Analysis and Reporting: Extracted data in Excel can be used for further analysis or generating reports.
  • Customization: You can customize the script further to extract different types of data based on your project needs.

Understanding Property Sets in Autodesk Inventor

Overview of Inventor Property Sets

Autodesk Inventor organizes document properties into four distinct property sets. These sets categorize properties based on their function and usage, enabling users to manage and access document information efficiently.

The Four Property Sets:

  1. Inventor Summary Information: This set contains basic document summary information.
  2. Inventor Document Summary Information: Holds additional summary details specific to the document.
  3. Design Tracking Properties: Includes properties used for tracking the design and its changes.
  4. Inventor User Defined Properties: Initially blank, this set allows users to create custom properties as needed.

The initial code snippet in our guide illustrates how to access each of these property sets, demonstrating the process of retrieving and updating their respective properties.

Properties in the Fixed Sets

While the ‘Inventor User Defined Properties’ set is customizable and empty by default, the first three sets comprise a range of fixed properties. Below is a table listing the properties contained within these three fixed sets:

Design Tracking Properties
NameType
AppearanceString
AuthorityString
Catalog Web LinkString
CategoriesString
Checked ByString
CostDecimal
Cost CenterString
Creation TimeDateTime
Date CheckedDateTime
Defer UpdatesBoolean
DensityDouble
DescriptionString
Design StatusLong
DesignerString
Document SubTypeString
Document SubType NameString
EngineerString
Engr Approved ByString
Engr Date ApprovedDateTime
External Property Revision IdString
Flat Pattern AreaDouble
Flat Pattern Defer UpdateBoolean
Flat Pattern LengthDouble
Flat Pattern WidthDouble
LanguageString
Last Updated WithString
ManufacturerString
MassDouble
MaterialString
Material IdentifierString
Mfg Approved ByString
Mfg Date ApprovedDateTime
Parameterized TemplateBoolean
Part IconIPictureDisp
Part NumberString
Part Property Revision IdString
ProjectString
Proxy Refresh DateDateTime
Sheet Metal AreaString
Sheet Metal LengthString
Sheet Metal RuleString
Sheet Metal WidthString
Size DesignationString
StandardString
Standard RevisionString
Standards OrganizationString
Stock NumberString
SurfaceAreaDouble
Template RowString
User StatusString
Valid MassPropsLong
VendorString
VolumeDouble
Weld MaterialString
Inventor Summary Information
NameType
AuthorString
CommentsString
KeywordsString
Last Saved ByString
Revision NumberString
SubjectString
ThumbnailIPictureDisp
TitleString
Inventor Document Summary Information
NameType
CategoryString
CompanyString
ManagerString

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *