Inventor.Application m_inventorApp = null;
// Try to get an active instance of Inventor
try
{
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
// If not active, create a new Inventor session
if (m_inventorApp == null)
{
Type inventorAppType = System.Type.GetTypeFromProgID("Inventor.Application");
m_inventorApp = System.Activator.CreateInstance(inventorAppType) as Inventor.Application;
}
Document doc = m_inventorApp.ActiveDocument;
if (doc != null && doc.DocumentType.Equals(DocumentTypeEnum.kPartDocumentObject))
{
PartDocument pDoc = (PartDocument)doc;
//TranslatorAddIn oDWGAddIn = (TranslatorAddIn)m_inventorApp.ApplicationAddIns.get_ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}");// dwg
TranslatorAddIn oDWGAddIn = (TranslatorAddIn)m_inventorApp.ApplicationAddIns.get_ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}");// dxf
if (!oDWGAddIn.Activated)
{
oDWGAddIn.Activate();
}
TranslationContext oContext = m_inventorApp.TransientObjects.CreateTranslationContext();
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism;
NameValueMap oOptions = m_inventorApp.TransientObjects.CreateNameValueMap();
DataMedium oDataMedium = m_inventorApp.TransientObjects.CreateDataMedium();
if (oDWGAddIn.get_HasSaveCopyAsOptions(m_inventorApp.ActiveDocument, oContext, oOptions))
{
string sPlugInPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
sPlugInPath = System.IO.Path.Combine(sPlugInPath, " ").Trim();
string sExportFileSettings = @"C:\Users\ragman\Documents\Inventor\MyConfig2015.ini";
if (System.IO.File.Exists(sExportFileSettings))
{
oOptions.Value["Export_Acad_IniFile"] = sExportFileSettings;
}
}
oOptions.Value["Solid"] = true;
String sSavePath = @"D:\Sketch-true.dwg";
oDataMedium.FileName = sSavePath;
if (System.IO.File.Exists(sSavePath))
{
System.IO.File.Delete(sSavePath);
}
try
{
oOptions.Value["Sketch"] = true; // Output sketches.
oDWGAddIn.SaveCopyAs(m_inventorApp.ActiveDocument, oContext, oOptions, oDataMedium);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
sSavePath = @"D:\Sketch-false.dwg";
oDataMedium.FileName = sSavePath;
if (System.IO.File.Exists(sSavePath))
{
System.IO.File.Delete(sSavePath);
}
try
{
oOptions.Value["Sketch"] = false; // Output sketches.
oDWGAddIn.SaveCopyAs(m_inventorApp.ActiveDocument, oContext, oOptions, oDataMedium);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}