Inventor DWG to AutoCAD DWG Conversion

Inventor DWG to AutoCAD DWG Conversion

Harshil.JAINS8RVT
Contributor Contributor
2,193 Views
8 Replies
Message 1 of 9

Inventor DWG to AutoCAD DWG Conversion

Harshil.JAINS8RVT
Contributor
Contributor

Hello,

 

I have a requirement to convert from Inventor DWG to AutoCAD DWG. I have tried the code as shown below:-

 

ApplicationAddInsPtr pAddIns = pApplication->GetApplicationAddIns();
TranslatorAddInPtr pTrans;
_bstr_t ClientId = _T("{C24E3AC2-122E-11D5-8E91-0010B541CD80}");
pTrans = pAddIns->GetItemById(ClientId);

DocumentPtr pINVDoc = pApplication->GetActiveDocument();

short bHasSaveOption = pTrans->GetHasSaveCopyAsOptions(pINVDoc, oContext, oOptions);
if (bHasSaveOption)
{
_bstr_t bstrAttribValue("D:\\CAD_DATA\\INV\\tempDWGOut.ini");

VARIANT varProtType;
varProtType.vt = VT_BSTR;
varProtType.bstrVal = bstrAttribValue;
oOptions->PutValue(_T("Export_Acad_IniFile"), varProtType);

_bstr_t bstrTempCADFileName("D:\\CAD_DATA\\INV\\Drawing1_AutoCAD.dwg");
oDataMedium->PutFileName(bstrTempCADFileName);

pTrans->Activate();
pTrans->SaveCopyAs(pINVDoc, oContext, oOptions, oDataMedium);
}

 

But while executing the code I am getting the below error:-

Failed to initialize the 2d translator.

 

Could you please help me to resolve the issue? 

What could be the possible reason for failure?

 

Thanks in Advance

0 Likes
Accepted solutions (2)
2,194 Views
8 Replies
Replies (8)
Message 2 of 9

johnsonshiue
Community Manager
Community Manager

Hi! If you have access to AutoCAD, there is another automated option to consider. You may save the Inventor drawing s as Inventor dwg files. Open the dwg files in AutoCAD. Use EXPORTLAYOUT command to export each sheet as a dwg file. You may use AutoLISP or AutoCAD API to automate it.

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
0 Likes
Message 3 of 9

Harshil.JAINS8RVT
Contributor
Contributor

Thank you for the reply,

 

Here I have Addin Developed In Inventor using C++ technology so the code I have shared is written in C++.

So I would like to know how to convert Inventor DWG to AutoCAD DWG using Inventor c++ API.

And why am I getting errors while trying the given solution in the API help doc?

 

Thanks

0 Likes
Message 4 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

I'm very new to C++ but this works for me:

class ThisRule { 
public:
    CComPtr<Application> ThisApplication;

    void Main() {

        HRESULT Result = NOERROR;

        CComPtr<Document> gDoc;
        Result = ThisApplication->get_ActiveDocument(&gDoc);

        CComPtr<DrawingDocument> doc;
        doc = gDoc;
        
        CComPtr<TranslationContext> context;
        Result = ThisApplication->TransientObjects->CreateTranslationContext(&context);
        context->put_Type(kFileBrowseIOMechanism);

        CComPtr<NameValueMap> options;
        Result = ThisApplication->TransientObjects->CreateNameValueMap(&options);

        CComPtr<DataMedium> dataMedium;
        Result = ThisApplication->TransientObjects->CreateDataMedium(&dataMedium);

        CComPtr<TranslatorAddIn> translator;
        translator = GetDwgTranslator();


        if (translator->HasSaveCopyAsOptions[doc][context][options]) {
            _bstr_t iniFileName = _T("D:\\forum\\\\dwgExport.ini");

            VARIANT varProtType;
            varProtType.vt = VT_BSTR;
            varProtType.bstrVal = iniFileName;

            Result = options->put_Value(_T("Export_Acad_IniFile"), varProtType);
        }

        _bstr_t dwgFileName("D:\\forum\\Drawing1_AutoCAD.dwg");
        Result = dataMedium->put_FileName(dwgFileName);

        Result = translator->SaveCopyAs(doc, context, options, dataMedium);
    }

private:
    CComPtr<TranslatorAddIn> GetDwgTranslator() {
        HRESULT Result = NOERROR;

        _bstr_t ClientId = _T("{C24E3AC2-122E-11D5-8E91-0010B541CD80}");

        CComPtr<ApplicationAddIn> addin;
        Result = ThisApplication->ApplicationAddIns->get_ItemById(ClientId, &addin);

        CComPtr<TranslatorAddIn> translator;
        translator = addin;

        return translator;
    }
};

I call this like this:

ThisRule rule;
rule.ThisApplication = GetInventor();
rule.Main();

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 9

Harshil.JAINS8RVT
Contributor
Contributor

Hello,

 

I have tried shared solution in my code but still I am getting same error at my end.

Error message :-  Failed to initialize the 2d translator.

Could you please tell me the possible reasons of error so that I can investigate more.

 

Here I am not clear about below file as well. Where this file is coming from as this file is not present on my system.

Should I place the file before executing the code or it will be generated after the execution?

D:\\forum\\\\dwgExport.ini

 

Thanks

0 Likes
Message 6 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

the ini file you need to create yourself. you can do it like this:

JelteDeJong_0-1660155401252.png

click next to you see this and click on "Save configuration"

JelteDeJong_1-1660155528849.png

Use the save location in the script.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 7 of 9

Harshil.JAINS8RVT
Contributor
Contributor

Thank you very much for your help,

But while using above method if I have multiple sheets in my drawing all the sheets get exported to different drawing files. Do we have any API setting through which we can export all the sheets in one AutoCAD Drawing?

Thanks in advance!!!

0 Likes
Message 8 of 9

Harshil.JAINS8RVT
Contributor
Contributor
Thank you very much for your help,

But while using above method if I have multiple sheets in my drawing all the sheets get exported to different drawing files. Do we have any API setting through which we can export all the sheets in one AutoCAD Drawing?

Thanks in advance!!!
0 Likes
Message 9 of 9

thomas_iddesign
Observer
Observer

iLogic: DWG Export Rule Re-Exports Unchanged Files Despite Matching Revision Number

Context:

I'm running a custom iLogic rule that exports .dwg drawings to AutoCAD .dwg format based on the model's Revision Number iProperty.

My export rule is designed to:

  • Open a .dwg

  • Grab the referenced model from Sheet 1 View 1

  • Read the Revision Number iProperty from the model

  • Build a filename like DrawingName-REV-<rev>.dwg

  • Export only if that file does not already exist


Problem:

Even though the model’s Revision Number has not changed, the rule keeps exporting the drawing again.

For example:

  • 202400004-A000.iam has REV A (unchanged)

  • 202400004-A001.iam was updated to REV B

  • The rule should only export A001

  • But it re-exports all drawings, including A000

 


What I’ve verified:

  • The drawings reference the correct models (A000.dwg → A000.iam, etc.)

  • The model iProperties are saved and up to date

  • The revision suffix is applied correctly in the exported filename


What I need help with:

  • Is there any caching, model update, or document descriptor issue that might cause Inventor to misreport the referenced model?

  • Could iLogic be resolving Sheet 1 > View 1 incorrectly?

  • Should I be using a different method to read the true referenced model and its iProperties?


Attachments:

  • Screenshot of correct model REV iProperties

  • Exported DWGs still showing REV A

  • Excerpt from my iLogic

Any help to ensure the rule only exports new revisions would be appreciated!

 

Screenshot 2025-08-02 122554.pngScreenshot 2025-08-02 122636.pngScreenshot 2025-08-02 123530.png

 

Sub Main()
    If Not ConfirmProceed() Then Return

    Dim sourceFolder As String = GetFolder("Select folder containing DWG files")
    If sourceFolder = "" Then Return

    Dim exportFolder As String = GetFolder("Select folder to save AutoCAD DWG files")
    If exportFolder = "" Then Return

    Dim configPath As String = "C:\Users\thoma\Documents\$_INDUSTRIAL_DRAFTING_&_DESIGN\INVENTOR_FILES\PRESETS\EXPORT-DWG.ini"
    If Not System.IO.File.Exists(configPath) Then
        MessageBox.Show("Missing DWG config: " & configPath)
        Return
    End If

    Dim files() As String = System.IO.Directory.GetFiles(sourceFolder, "*.dwg")
    Dim successCount As Integer = 0
    Dim skippedCount As Integer = 0
    Dim errorLog As New System.Text.StringBuilder
    Dim exportLog As New System.Text.StringBuilder

    exportLog.AppendLine("DWG Export Log - " & Now.ToString())
    exportLog.AppendLine("")

    For Each file As String In files
        Dim doc As Document = Nothing
        Try
            doc = ThisApplication.Documents.Open(file, False)
            If doc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
                doc.Close(True)
                Continue For
            End If

            Dim baseName As String = System.IO.Path.GetFileNameWithoutExtension(file)
            Dim drawDoc As DrawingDocument = doc
            Dim sheet1 As Sheet = drawDoc.Sheets(1)
            If sheet1.DrawingViews.Count = 0 Then Throw New Exception("No views on Sheet 1")
            Dim baseView As DrawingView = sheet1.DrawingViews(1)

            Dim modelDoc As Document = baseView.ReferencedDocumentDescriptor.ReferencedDocument
            If modelDoc Is Nothing Then Throw New Exception("Referenced model is missing.")

            Dim modelRev As String = ""
            Try
                modelRev = iProperties.Value(modelDoc, "Project", "Revision Number")
            Catch
                modelRev = "NO-REV"
            End Try

            Dim exportName As String = baseName & "-REV-" & SanitizeFileName(modelRev) & ".dwg"
            Dim exportPath As String = System.IO.Path.Combine(exportFolder, exportName)

            exportLog.AppendLine("DWG: " & baseName)
            exportLog.AppendLine(" ↳ Model: " & modelDoc.DisplayName)
            exportLog.AppendLine(" ↳ REV: " & modelRev)

            If System.IO.File.Exists(exportPath) Then
                exportLog.AppendLine(" ⏭ Skipped (same REV already exported)")
                doc.Close(True)
                skippedCount += 1
                Continue For
            End If

            ' Set up translator
            Dim addIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
            Dim context = ThisApplication.TransientObjects.CreateTranslationContext
            context.Type = IOMechanismEnum.kFileBrowseIOMechanism
            Dim options = ThisApplication.TransientObjects.CreateNameValueMap
            If addIn.HasSaveCopyAsOptions(doc, context, options) Then
                options.Value("Export_Acad_IniFile") = configPath
            End If
            Dim oData = ThisApplication.TransientObjects.CreateDataMedium
            oData.FileName = exportPath

            addIn.SaveCopyAs(doc, context, options, oData)
            doc.Close(True)
            successCount += 1
            exportLog.AppendLine(" ✅ Exported: " & exportName)
        Catch ex As Exception
            If Not doc Is Nothing Then doc.Close(True)
            errorLog.AppendLine("ERROR processing " & file & ": " & ex.Message)
        End Try

        exportLog.AppendLine("")
    Next

    ' Write logs
    System.IO.File.WriteAllText(System.IO.Path.Combine(exportFolder, "DWG_Export_Log.txt"), exportLog.ToString())
    If errorLog.Length > 0 Then
        System.IO.File.WriteAllText(System.IO.Path.Combine(exportFolder, "DWG_Export_Errors.txt"), errorLog.ToString())
    End If

    MessageBox.Show("DWG export complete." & vbCrLf & _
        "✔ Files exported: " & successCount & vbCrLf & _
        "⏭ Skipped (already exported at this REV): " & skippedCount & vbCrLf & _
        "⚠ Errors: " & errorLog.Length, "Export Summary")
End Sub

Function GetFolder(prompt As String) As String
    Dim dlg As New FolderBrowserDialog
    dlg.Description = prompt
    If dlg.ShowDialog() <> DialogResult.OK Then Return ""
    Return dlg.SelectedPath
End Function

Function ConfirmProceed() As Boolean
    Dim result = MessageBox.Show("Export DWGs only if target REV file does not exist?", "Confirm Export", MessageBoxButtons.YesNo)
    Return result = DialogResult.Yes
End Function

Function SanitizeFileName(input As String) As String
    For Each c As Char In System.IO.Path.GetInvalidFileNameChars()
        input = input.Replace(c, "_")
    Next
    Return input
End Function
0 Likes