By Wayne Brill
You can use "Autodesk Internal DWG Translator" to create a draft view from an AutoCAD drawing. This approach allows you to have the user set the options by displaying the File wizard dialog or by using preset options in an ini file.
Following are VBA and VB.NET examples that demonstrate the "Autodesk Internal DWG Translator".
Note: This ZIP file has the DWGtoInventor.INI file used in the code snippet.
VBA
Public Sub ImportDWG()
' Calls a function that uses the DWG translator
' directly. This allows you to specify some more
' options. In this case it's specifying the
' ini file to use that contains all of the
' various options.
Dim DwgName As String
DwgName = "C:\temp\test1.dwg"
Call DWGIn_TranslatorAddIn(DwgName)
End Sub
Public Sub DWGIn_TranslatorAddIn(DWGFilename As String)
' Set a reference to the DWG translator add-in.
Dim oDWGAddIn As TranslatorAddIn
Dim i As Long
For i = 1 To ThisApplication.ApplicationAddIns.count
On Error Resume Next
If ThisApplication.ApplicationAddIns(i).AddInType = _
kTranslationApplicationAddIn Then
If Err Then
Debug.Print Err.Description
Err.Clear
End If
Debug.Print ThisApplication.ApplicationAddIns(i).DisplayName
' you can also using the CLSID for DWG Translator -
' {C24E3AC2-122E-11D5-8E91-0010B541CD80}"
If ThisApplication.ApplicationAddIns(i).Description = _
"Autodesk Internal DWG Translator" Then
Set oDWGAddIn = ThisApplication.ApplicationAddIns.Item(i)
Exit For
End If
End If
Next i
If oDWGAddIn Is Nothing Then
MsgBox "The DXF add-in could not be found."
Exit Sub
End If
' Check to make sure the add-in is activated.
If Not oDWGAddIn.Activated Then
oDWGAddIn.Activate
End If
Dim trans As TransientObjects
Set trans = ThisApplication.TransientObjects
Dim map As NameValueMap
Set map = trans.CreateNameValueMap
Dim context As TranslationContext
Set context = trans.CreateTranslationContext
context.Type = kFileBrowseIOMechanism
Dim file As DataMedium
Set file = trans.CreateDataMedium
file.FileName = DWGFilename
Dim b As Boolean
'you can show the options dialog...
'Call oDWGAddIn.ShowOpenOptions(file, context, map)
'or specify an existing ini file that has the
'saved configuration....
Call map.Add("Import_Acad_IniFile", _
"C:\temp\DWGtoINVENTOR.ini")
'Open the .dwg file
Dim doc As Document
oDWGAddIn.Open file, context, map, doc
End Sub
VB.NET
Public Sub ImportDWG()
' Calls a function that uses the DWG translator
' directly. This allows you to specify some more
' options. In this case it's specifying the
' ini file to use that contains all of the
' various options.
Dim DwgName As String
DwgName = "C:\Temp\Test1.dwg"
DWG_In(DwgName)
End Sub
Public Sub DWG_In(ByVal DWGFilename As String)
Dim m_inventorApp As Inventor.Application _
= Nothing
Dim m_quitInventor As Boolean = False
' Get an active instance
Try
m_inventorApp = System.Runtime. _
InteropServices.Marshal. _
GetActiveObject("Inventor.Application")
Catch ex As Exception
MessageBox.Show("Start an Inventor Session")
Return
End Try
' If not active, create a new Inventor session
' starting Inventor this way could result in
' Inventor.exe not getting shut down properly
'If m_inventorApp Is Nothing Then
' Dim inventorAppType As Type = System.Type. _
' GetTypeFromProgID _
' ("Inventor.Application")
' m_inventorApp = System.Activator. _
' CreateInstance(inventorAppType)
' m_inventorApp.Visible = True
' m_quitInventor = True
'End If
' Set a reference to the DWG translator add-in.
Dim oDWGAddIn As TranslatorAddIn = Nothing
Dim i As Long
For i = 1 To m_inventorApp.ApplicationAddIns.Count
Try
If m_inventorApp.ApplicationAddIns(i). _
AddInType = ApplicationAddInTypeEnum. _
kTranslationApplicationAddIn Then
'If Err() Then
' Debug.Print(Err.Description)
' Err.Clear()
'End If
Debug.Print(m_inventorApp. _
ApplicationAddIns _
(i).DisplayName)
' you can also using the CLSID for
' DWG() Translator()
'{C24E3AC2-122E-11D5-8E91-0010B541CD80}
If m_inventorApp. _
ApplicationAddIns(i). _
Description = _
"Autodesk Internal DWG Translator" _
Then
oDWGAddIn = m_inventorApp. _
ApplicationAddIns.Item(i)
Exit For
End If
End If
Catch ex As Exception
End Try
Next i
If oDWGAddIn Is Nothing Then
MsgBox _
("The DXF add-in could not be found.")
Exit Sub
End If
' Check to make sure the add-in is activated.
If Not oDWGAddIn.Activated Then
oDWGAddIn.Activate()
End If
Dim trans As TransientObjects
trans = m_inventorApp.TransientObjects
Dim map As NameValueMap
map = trans.CreateNameValueMap
Dim context As TranslationContext
context = trans.CreateTranslationContext
context.Type = IOMechanismEnum. _
kFileBrowseIOMechanism
Dim file As DataMedium
file = trans.CreateDataMedium
file.FileName = DWGFilename
Dim b As Boolean
'you can show the options dialog...
'Call oDWGAddIn.ShowOpenOptions
' (file, context, map)
'or specify an existing ini file that has
' the saved configuration....
Call map.Add("Import_Acad_IniFile", _
"C:\temp\DWGtoINVENTOR.ini")
'Open the .dwg file
Dim doc As Document = Nothing
oDWGAddIn.Open(file, context, map, doc)
End Sub