Using a specified drawing template when opening a DXF file

Using a specified drawing template when opening a DXF file

dinomyar
Enthusiast Enthusiast
937 Views
6 Replies
Message 1 of 7

Using a specified drawing template when opening a DXF file

dinomyar
Enthusiast
Enthusiast

We will create a DXF file from a flat pattern using the DataIO class. Then we need to open the DXF file and possible reorient it, scale it, or remove unneeded edges. we do this using the TranslatorAddIn with the following code.

 

CComPtr<ApplicationAddIns> addins;

Result = m_pApplication->get_ApplicationAddIns(&addins);

CComBSTR appid = /*NOXLATE*/_T("{C24E3AC4-122E-11D5-8E91-0010B541CD80}");

CComPtr<ApplicationAddIn> addin;

Result = addins->get_ItemById(appid, &addin);

CComQIPtr<TranslatorAddIn> transaddin(addin);

CComPtr<DataMedium> datamed;

transobj->CreateDataMedium(&datamed);

datamed->put_FileName(dxffilename);

CComPtr<TranslationContext> context;

transobj->CreateTranslationContext(&context);

context->PutType(kFileBrowseIOMechanism);

CComPtr<IDispatch> newdocdisp;

transaddin->Open(datamed, context, namemap, &newdocdisp);

CComQIPtr<Document> newdoc(newdocdisp);

if(!newdoc)

return false;

 

The problem is that when it opens the DXF file it uses the default drawing template, that may contain borders, title blocks, etc. Is there any way to specify the template to be used so we can use an empty one? Barring that, is there any way to see if it is empty, and if not empty it?

 

0 Likes
Accepted solutions (1)
938 Views
6 Replies
Replies (6)
Message 2 of 7

HermJan.Otterman
Advisor
Advisor

from the Inventor programming/API help is this code:

 

Public Sub PublishDXF()
    ' Get the DXF translator Add-In.
    Dim DXFAddIn As TranslatorAddIn
    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")

    'Set a reference to the active document (the document to be published).
    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument

    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism

    ' Create a NameValueMap object
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    ' Create a DataMedium object
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

    ' Check whether the translator has 'SaveCopyAs' options
    If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then

        Dim strIniFile As String
        strIniFile = "C:\tempDXFOut.ini"

        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If

    'Set the destination file name
    oDataMedium.FileName = "c:\tempdxfout.dxf"

    'Publish document.
    Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

 

 

I think you can set in the .ini file your template...

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 3 of 7

dinomyar
Enthusiast
Enthusiast

I have already tried this. I use this to set units and file versions. There is nothing in the help that says how to specify a template file for DXF importing. I tried guessing a few things but nothing works. I was hoping that someone had figured it out or that Autodesk could tell me if it is possible.

0 Likes
Message 4 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support

@dinomyar,

 

Hoping that below idea in VBA code could help.

 

Public Sub PublishDXF()
    ' Get the DXF translator Add-In.
    Dim DXFAddIn As TranslatorAddIn
    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")

    'Set a reference to the active document (the document to be published).
    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument

    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism

    ' Create a NameValueMap object
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    ' Create a DataMedium object
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

    ' Check whether the translator has 'SaveCopyAs' options
    If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then

        Dim strIniFile As String
        strIniFile = "C:\tempDXFOut.ini"

        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile

oOptions.Value("Template") = "C:\Temp\Template.dxf"
End If 'Set the destination file name oDataMedium.FileName = "c:\tempdxfout.dxf" 'Publish document. Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 7

dinomyar
Enthusiast
Enthusiast

I have tried this and it does not seem to work. Still uses the template specified in the project file.

 

One of the differences is that I am opening a DXF file into a idw file not saving a DXF file.

 

The following is a snippet of the code used

 

CComPtr<NameValueMap> namemap;

CComPtr<TransientObjects> transobj;

m_pApplication->get_TransientObjects(&transobj);

transobj->CreateNameValueMap(&namemap);

TCHAR mapfile[_MAX_PATH + 1];

SetupInvImportFile(mapfile, PartInfo); // Sets the units in inifile

CComVariant mfile(mapfile);

namemap->Add(CComBSTR(/*NOXLATE*/_T("Import_Acad_IniFile")), mfile);

CComBSTR templatename = _T("C:\\Users\\Public\\Documents\\Autodesk\\Inventor 2018\\Templates\\standard.idw");

CComVariant templatenameVar(templatename);

namemap->Add(CComBSTR(/*NOXLATE*/_T("Template")), templatenameVar); 

CComPtr<ApplicationAddIns> addins;

Result = m_pApplication->get_ApplicationAddIns(&addins);

CComBSTR appid = /*NOXLATE*/_T("{C24E3AC4-122E-11D5-8E91-0010B541CD80}");

CComPtr<ApplicationAddIn> addin;

Result = addins->get_ItemById(appid, &addin);

CComQIPtr<TranslatorAddIn> transaddin(addin);

CComPtr<DataMedium> datamed;

transobj->CreateDataMedium(&datamed);

datamed->put_FileName(dxffilename);

CComPtr<TranslationContext> context;

transobj->CreateTranslationContext(&context);

context->PutType(kFileBrowseIOMechanism);

CComPtr<IDispatch> newdocdisp;

transaddin->Open(datamed, context, namemap, &newdocdisp);

 

0 Likes
Message 6 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@dinomyar,

 

Drawing Templates path can be changed using FileOptions Inventor API. For more details, refer the below forum discussion.

 

https://forums.autodesk.com/t5/inventor-customization/open-file-from-default-templates/td-p/3072510

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 7

fsanchou
Advocate
Advocate

Hi,

@chandra.shekar.g

 

Is it possible to select a template for Flat Pattern to DXF export?

[oCompDef.DataIO.WriteDataToFile(sOut, sDXFFullFileName)]

 

Thanks,

0 Likes