c# .net export to dwg format

c# .net export to dwg format

Anonymous
Not applicable
3,035 Views
2 Replies
Message 1 of 3

c# .net export to dwg format

Anonymous
Not applicable
Hello, new to the forums here. I have a quick question here regarding saving an Inventor Drawing with multiple sheets to an AutoCad DWG file. I'm attempting to only save the current sheet to dwg file but have only been successful in saving all the sheets in the drawing. Here's the chunk of code in question: foreach (Inventor.ApplicationAddIn oApp in Instance.ApplicationAddIns) { if (oApp.DisplayName == "Translator: DWG") { // Found the dwg translator now setup translator and save // file off as a 2004 autocad file. Inventor.TranslatorAddIn oTranslator = (Inventor.TranslatorAddIn)oApp; Inventor.TranslationContext oContext = Instance.TransientObjects.CreateTranslationContext(); Inventor.NameValueMap oOptions = Instance.TransientObjects.CreateNameValueMap(); Inventor.DataMedium oMedium = Instance.TransientObjects.CreateDataMedium(); oContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism; oOptions.set_Value("Export_Acad_IniFile", PATH+"export.ini"); oOptions.set_Value("Sheet_Range",Inventor.PrintRangeEnum.kPrintCurrentSheet); oMedium.FileName = SAVE_PATH + "export.dwg" oTranslator.SaveCopyAs(Drawing, oContext, oOptions, oMedium); } } The only information I've been able to search down so far on this is related to the PDF translator within Inventor. For that translator the line oOptions.set_Value("Sheet_Range",Inventor.PrintRangeEnum.kPrintCurrentSheet); should cause the Current sheet to be exported only; however, as I indicated earlier I'm still getting all the sheets in the current idw file to be exported as separate dwg files. Is it a different value that needs to be populated for the DWG translator? Any help would be greatly appreciated. Alan
0 Likes
3,036 Views
2 Replies
Replies (2)
Message 2 of 3

YuhanZhang
Autodesk
Autodesk

Hi Alan,

 

Could you re-format your post above, specially you can use the Insert Code command in Rich Text edit mode to format your code, then it would be looking beautiful.

 

The translator does not support to specify sheet range to export to DWG format. A possible solution here I think is to copy the sheet you want to export to a new drawing document and then export that document(remember to delete the default sheet in that drawing document). The Sheet.CopyTo can be used to copy a sheet to another drawing document.

 

Hope this helps.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 3

Anonymous
Not applicable

New I should have gone back after the fact and looked at the post again.

 

Here's a repost of the code using the insert code method.

 

 

string sSaveFile;
Inventor.TranslatorAddIn oDWGAddIn;

oDWGAddIn = (Inventor.TranslatorAddIn)Instance.ApplicationAddIns.get_ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}");

Inventor.TranslatorAddIn oTranslator = (Inventor.TranslatorAddIn)oDWGAddIn;

Inventor.TranslationContext oContext = Instance.TransientObjects.CreateTranslationContext();
Inventor.NameValueMap oOptions = Instance.TransientObjects.CreateNameValueMap();
Inventor.DataMedium oMedium = Instance.TransientObjects.CreateDataMedium(); oContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism; oOptions.set_Value("Export_Acad_IniFile", PATH+"export.ini"); sSaveFile = "SR" + oDieData.PartNumber() + sPNsuffix; oMedium.FileName = SAVE_PATH + sSaveFile; oTranslator.SaveCopyAs(Drawing, oContext, oOptions, oMedium); // Since I was unable to figure out how to limit the export to
// the active sheet only. Because of this delete the _Sheet_2.dwg
// file and rename the _Sheet_1.dwg file to the desired name.
// Note that you must check for the existance of the target
// filename prior to the renaming operation or an unhandled
// exception will the thrown.
// A cleaner method would be to utilize a try / catch. if(File.Exists(SAVE_PATH + sSaveFile + "_Sheet_2.dwg")) File.Delete(SAVE_PATH + sSaveFile + "_Sheet_2.dwg");
if(File.Exists(SAVE_PATH + sSaveFile)) File.Delete(SAVE_PATH + sSaveFile);
if(File.Exists(SAVE_PATH + sSaveFile + "_Sheet_1.dwg")) File.Move(SAVE_PATH + sSaveFile + "_Sheet_1.dwg", SAVE_PATH + sSaveFile);

 

Above is the code I settled in on until I heard a response.  As you can see from the comments in the code I went ahead and performed the export on the original file and simply deleted the file I didn't need (in this chase the _Sheet_2.dwg file) and subsequently renamed the file I wanted to the desired filename outside of Inventor via code. Not sure which way would actually perform faster (the method suggested by Rocky above or this method).

 

Thanks for the suggestion Rocky.

 

0 Likes