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
- Excel Worksheet Initialization:
- The script starts by accessing an active Excel worksheet, allowing the program to read data from it.
- 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.
- It defines a folder to search (
- 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
- The
- 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
- 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.
- 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
- Saving Changes:
- After updating, the document is saved and closed.
Code
// 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();
}
'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:
- Prepare Your Excel Sheet: Ensure your Excel sheet is structured correctly, with the file names, properties, and parameters in the right columns.
- 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 objectApplication
, make sure to use the correct version for each program. - Customize the Script: Modify the script to match the exact structure of your Excel sheet and the specific requirements of your Inventor files.
- 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:
- 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.
- Writing Data to Excel:
- Establish a connection to an Excel sheet and write the extracted data into specific cells.
- 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:
// 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();
}
' 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:
- Inventor Summary Information: This set contains basic document summary information.
- Inventor Document Summary Information: Holds additional summary details specific to the document.
- Design Tracking Properties: Includes properties used for tracking the design and its changes.
- 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
Name | Type |
---|---|
Appearance | String |
Authority | String |
Catalog Web Link | String |
Categories | String |
Checked By | String |
Cost | Decimal |
Cost Center | String |
Creation Time | DateTime |
Date Checked | DateTime |
Defer Updates | Boolean |
Density | Double |
Description | String |
Design Status | Long |
Designer | String |
Document SubType | String |
Document SubType Name | String |
Engineer | String |
Engr Approved By | String |
Engr Date Approved | DateTime |
External Property Revision Id | String |
Flat Pattern Area | Double |
Flat Pattern Defer Update | Boolean |
Flat Pattern Length | Double |
Flat Pattern Width | Double |
Language | String |
Last Updated With | String |
Manufacturer | String |
Mass | Double |
Material | String |
Material Identifier | String |
Mfg Approved By | String |
Mfg Date Approved | DateTime |
Parameterized Template | Boolean |
Part Icon | IPictureDisp |
Part Number | String |
Part Property Revision Id | String |
Project | String |
Proxy Refresh Date | DateTime |
Sheet Metal Area | String |
Sheet Metal Length | String |
Sheet Metal Rule | String |
Sheet Metal Width | String |
Size Designation | String |
Standard | String |
Standard Revision | String |
Standards Organization | String |
Stock Number | String |
SurfaceArea | Double |
Template Row | String |
User Status | String |
Valid MassProps | Long |
Vendor | String |
Volume | Double |
Weld Material | String |
Inventor Summary Information
Name | Type |
---|---|
Author | String |
Comments | String |
Keywords | String |
Last Saved By | String |
Revision Number | String |
Subject | String |
Thumbnail | IPictureDisp |
Title | String |
Inventor Document Summary Information
Name | Type |
---|---|
Category | String |
Company | String |
Manager | String |
Leave a Reply