Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Autodesk Inventor VBA Sample: Generating a Drawing

Here's a basic example of using VBA in Autodesk Inventor to generate a drawing from an existing 3D model. This script assumes that you have an existing part or assembly file and want to create a drawing with standard views.


### Autodesk Inventor VBA Sample: Generating a Drawing

#### Step 1: Open Autodesk Inventor and access the VBA editor
1. Open Autodesk Inventor.
2. Press `Alt + F11` to open the VBA editor.

#### Step 2: Insert a new module
1. In the VBA editor, go to `Insert` > `Module` to add a new module.

#### Step 3: Copy the VBA code

Here’s the VBA code to create a new drawing with standard views (Top, Front, and Right) from an existing part or assembly:

```vba
Sub CreateDrawingFromPart()
    ' Declare Inventor application and document objects
    Dim invApp As Application
    Set invApp = ThisApplication
    
    ' Check if a document is open
    If invApp.Documents.Count = 0 Then
        MsgBox "Please open a part or assembly before running this macro.", vbExclamation
        Exit Sub
    End If
    
    ' Get the active document (assuming it's a part or assembly)
    Dim invDoc As Document
    Set invDoc = invApp.ActiveDocument
    
    ' Ensure it's a part or assembly document
    If Not (TypeOf invDoc Is PartDocument Or TypeOf invDoc Is AssemblyDocument) Then
        MsgBox "This macro only works with part or assembly documents.", vbExclamation
        Exit Sub
    End If
    
    ' Create a new drawing document
    Dim invDrawingDoc As DrawingDocument
    Set invDrawingDoc = invApp.Documents.Add(kDrawingDocumentObject, _
        invApp.FileManager.GetTemplateFile(kDrawingDocumentObject, kMetricDrawingDocument))
    
    ' Get the active sheet in the drawing
    Dim invSheet As Sheet
    Set invSheet = invDrawingDoc.ActiveSheet
    
    ' Create a base view of the part/assembly
    Dim invBaseView As DrawingView
    Set invBaseView = invSheet.DrawingViews.AddBaseView(invDoc, _
        ThisApplication.TransientGeometry.CreatePoint2d(10, 10), _
        1, kFrontViewOrientation, _
        kHiddenLineRemovedDrawingViewStyle, Nothing, Nothing)
    
    ' Add additional projected views
    Dim invTopView As DrawingView
    Set invTopView = invSheet.DrawingViews.AddProjectedView(invBaseView, _
        ThisApplication.TransientGeometry.CreatePoint2d(10, 15), kTopViewOrientation)
    
    Dim invRightView As DrawingView
    Set invRightView = invSheet.DrawingViews.AddProjectedView(invBaseView, _
        ThisApplication.TransientGeometry.CreatePoint2d(15, 10), kRightViewOrientation)
    
    ' Save the drawing
    Dim drawingFilePath As String
    drawingFilePath = invDoc.FullFileName
    drawingFilePath = Replace(drawingFilePath, ".ipt", ".idw")
    drawingFilePath = Replace(drawingFilePath, ".iam", ".idw")
    invDrawingDoc.SaveAs(drawingFilePath, False)
    
    MsgBox "Drawing generated and saved as " & drawingFilePath, vbInformation
End Sub
```

### Code Explanation:

- **Application and Document Objects**: The script starts by declaring the `Application` and `Document` objects to interact with Inventor and the currently open document.
- **Document Check**: It checks if there’s an open document and ensures it’s either a part (`.ipt`) or assembly (`.iam`) file.
- **New Drawing Creation**: A new drawing document is created using the metric template. The active sheet in the drawing is accessed.
- **Base and Projected Views**: A base view is added to the drawing, positioned at coordinates (10, 10), and scaled to 1:1. The script then adds top and right projected views based on the base view.
- **Saving the Drawing**: The script saves the drawing with the same name as the part/assembly but with the `.idw` extension.

### Running the Code:

1. **Open a Part or Assembly**: Open a part (`.ipt`) or assembly (`.iam`) file in Inventor.
2. **Run the Macro**: In the VBA editor, go to `Run` > `Run Sub/UserForm` or press `F5`.
3. **View the Drawing**: The macro will generate a drawing and save it in the same directory as the part or assembly file.

This script serves as a basic example and can be expanded with additional features like custom view positioning, different scales, or annotations.

caa August 12 2024 127 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam