Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Automating Import of AutoCAD Blocks into Inventor Drawing Sheets Using iLogic

1 REPLY 1
Reply
Message 1 of 2
anthony8VA7H
164 Views, 1 Reply

Automating Import of AutoCAD Blocks into Inventor Drawing Sheets Using iLogic

Hi There, 

Short Description and Goal:

I am working on an iLogic rule in Autodesk Inventor designed to automate the process of importing specific CAD blocks from various DWG files into an active drawing sheet at predefined positions. The goal is to streamline the workflow by  placing blocks onto the drawing, mimicking the functionality of the manual "Import AutoCAD Block" feature.

Current Problems:

  1. Block Definitions Not Found: Despite the block names being correct and visible in the manual import process, the script fails to locate the block definitions using SketchedSymbolDefinitions.
  2. Errors During Import: When attempting to import the blocks, the script encounters errors such as "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))".
  3. Unsupported Method: The script attempts to use ImportData, which is not recognized, leading to compilation errors.
  4. Context Issues: Ensuring the script runs within the correct document context and accesses the correct block definitions has proven challenging.
  5. Manual Process Replication: Accurately mimicking the manual import process within the script has been difficult due to API limitations or misuse.
  6. No Block Definitions Found in Script: Debugging information indicates no block definitions found in the DWG file when accessed programmatically, despite being present in manual checks.

Full Script and Further Information:

Here's the latest version of the script and details of the encountered issues:

 

Imports System.Collections.Generic
Imports Inventor
Imports System.IO

Sub Main()
    ' Define mappings between sheet names, block names with their insertion points, and DWG file paths
    Dim pageMappings As New Dictionary(Of String, Tuple(Of List(Of String), List(Of Point2d), String)) From {
        {"Frame:6", Tuple.Create(New List(Of String) From {"P69", "P70", "P71", "P72", "P73"}, 
                                  New List(Of Point2d) From {
                                      ThisApplication.TransientGeometry.CreatePoint2d(10, 10),
                                      ThisApplication.TransientGeometry.CreatePoint2d(20, 10),
                                      ThisApplication.TransientGeometry.CreatePoint2d(30, 10),
                                      ThisApplication.TransientGeometry.CreatePoint2d(40, 10),
                                      ThisApplication.TransientGeometry.CreatePoint2d(50, 10)},
                                  "DWG.dwg")},
        {"Shelf", Tuple.Create(New List(Of String) From {"S1", "S2", "S3", "S4", "S5"}, 
                               New List(Of Point2d) From {
                                   ThisApplication.TransientGeometry.CreatePoint2d(15, 15),
                                   ThisApplication.TransientGeometry.CreatePoint2d(25, 15),
                                   ThisApplication.TransientGeometry.CreatePoint2d(35, 15),
                                   ThisApplication.TransientGeometry.CreatePoint2d(45, 15),
                                   ThisApplication.TransientGeometry.CreatePoint2d(55, 15)},
                               "DWG.dwg")},
        {"Drawer", Tuple.Create(New List(Of String) From {"D1", "D2", "D3", "D4", "D5"}, 
                                New List(Of Point2d) From {
                                    ThisApplication.TransientGeometry.CreatePoint2d(5, 5),
                                    ThisApplication.TransientGeometry.CreatePoint2d(15, 5),
                                    ThisApplication.TransientGeometry.CreatePoint2d(25, 5),
                                    ThisApplication.TransientGeometry.CreatePoint2d(35, 5),
                                    ThisApplication.TransientGeometry.CreatePoint2d(45, 5)},
                                "DWG.dwg")}
    }

    ' Get the active document
    Dim activeDocument As Document = ThisApplication.ActiveDocument
    If activeDocument.DocumentType = kDrawingDocumentObject Then
        Dim drawingDocument As DrawingDocument = activeDocument
        Dim activeSheet As Sheet = drawingDocument.ActiveSheet
        Dim sheetName As String = activeSheet.Name.Trim()

        ' Check if the active sheet name exists in the mappings
        If pageMappings.ContainsKey(sheetName) Then
            Dim blockNames As List(Of String) = pageMappings(sheetName).Item1
            Dim insertionPoints As List(Of Point2d) = pageMappings(sheetName).Item2
            Dim dwgFilePath As String = pageMappings(sheetName).Item3

            ' Check if the DWG file exists
            If System.IO.File.Exists(dwgFilePath) Then
                Try
                    ' Open the DWG file
                    Dim dwgDoc As Document = ThisApplication.Documents.Open(dwgFilePath, False)
                    If dwgDoc.DocumentType = kDrawingDocumentObject Then
                        Dim cadDoc As DrawingDocument = CType(dwgDoc, DrawingDocument)
                        
                        ' Iterate over each block name to import and insert
                        Dim insertionIndex As Integer = 0
                        For Each blockName As String In blockNames
                            Try
                                ' Debug information: Verify the block name
                                MessageBox.Show("Checking block: " & blockName)

                                ' Retrieve block definition
                                Dim blockDef As SketchedSymbolDefinition = Nothing
                                Try
                                    blockDef = cadDoc.SketchedSymbolDefinitions.Item(blockName)
                                    If blockDef IsNot Nothing Then
                                        MessageBox.Show("Block definition found: " & blockName)
                                    End If
                                Catch ex As Exception
                                    MessageBox.Show("Block definition not found during catch: " & blockName & vbCrLf & ex.Message)
                                End Try

                                If blockDef IsNot Nothing Then
                                    ' Insert the block into the active sheet
                                    Dim insertionPoint As Point2d = insertionPoints(insertionIndex)
                                    activeSheet.SketchedSymbols.Add(blockDef, insertionPoint, 0.3) ' Assuming a scale of 0.3
                                    MessageBox.Show("Block inserted: " & blockName)
                                    insertionIndex += 1
                                Else
                                    MessageBox.Show("Block definition not found: " & blockName)
                                End If
                            Catch ex As Exception
                                MessageBox.Show("Failed to import block: " & blockName & vbCrLf & ex.Message)
                            End Try
                        Next
                    End If
                    dwgDoc.Close(False)
                Catch ex As Exception
                    MessageBox.Show("Error opening DWG file: " & dwgFilePath & vbCrLf & ex.Message)
                End Try
            Else
                MessageBox.Show("DWG file not found: " & dwgFilePath)
            End If
        Else
            MessageBox.Show("Sheet name not found in mappings: " & sheetName)
        End If
    Else
        MessageBox.Show("Active document is not a drawing document.")
    End If
End Sub

 

 

1 REPLY 1
Message 2 of 2
WCrihfield
in reply to: anthony8VA7H

Hi @anthony8VA7H.  That is likely the most complicated looking Dictionary I have ever seen. 😉

I think you may be wanting to work with the DrawingDocument.AutoCADBlockDefinitions property, and its properties / methods, instead of the DrawingDocument.SketchedSymbolDefinitions property.  Then, the 'instances' of these definitions are called AutoCADBlocks (or individually AutoCADBlock), instead of SketchedSymbols / SketchedSymbol.

AutoCADBlockDefinitions.AddFromFile Method

AutoCADBlockDefinitionsEnumerator 

Sheet.AutoCADBlocks 

AutoCADBlocks.Add Method 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report