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:
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
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:
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
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
Wesley Crihfield
(Not an Autodesk Employee)
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
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.