Recently wrote a tool to convert a CAD file in a specified directory into a picture format in bulk.
You first need to add the corresponding reference:
In the AutoCAD2008 environment corresponding to the AutoCAD type library and autocad/objectdbx Common 17.0 type library These two references,
The author because is AutoCAD2010, only found the AutoCAD Type Library did not find the second, after Baidu learned to add to the file "C:\Program Files\Common Files\autodesk shared\ Axdb18enu.tlb "is a reference to it.
Initialize AutoCAD, depending on the version, the corresponding ProgramID is also different:
1 private static acadapplication _app = null;
1 #regionInitializing CAD Programs2 #regionCAD ProgramID Table3 //AutoCAD 20044 //R165 //autocad.application.166 //AutoCAD7 //R16.8 //autocad.application.16.9 //AutoCAD 2006Ten //R16. One //autocad.application.16. A //AutoCAD - //R17 - //autocad.application.17 the //AutoCAD - //R17. - //autocad.application.17. - //AutoCAD + //R17. - //autocad.application.17. + //AutoCAD A //R18 at //autocad.application.18 - //AutoCAD - //R18. - //autocad.application.18. - #endregion - stringProgramID ="autocad.application.18.0"; in Try - { to //if CAD is already open, get it directly +_app =(acadapplication) marshal.getactiveobject (programid); - } the Catch * { $ TryPanax Notoginseng { - //Create a new instance the_app = (acadapplication)Newacaddocument (). Application; + } A Catch(Exception ex) the { + return; - } $ } $ #endregion
The CAD file is then loaded and converted to a picture format, which requires closing the corresponding CAD file after the conversion is completed:
1Acaddocument doc =NULL;2 stringDestPath =string. Empty;3 Try4 {5Doc = _app. Documents.Open (Pfile.fullname,true);6Doc. SetVariable ("SDI",0);7Doc. SetVariable ("Filedia",0);8Doc. SetVariable ("Rasterpreview",1);9Doc. SetVariable ("Backgroundplot",0);TenDoc. Activelayout.configname ="Publishtoweb JPG.PC3"; OneDoc. Activelayout.usestandardscale =true; ADoc. Activelayout.standardscale =Acplotscale.acscaletofit; -Doc. Activelayout.plottype =acplottype.acextents; -Doc. Activelayout.centerplot =true; theDoc. Activelayout.plotrotation =acplotrotation.ac0degrees; -Doc. Activelayout.plottype =Autodesk.AutoCAD.Interop.Common.AcPlotType.acExtents; -Doc. Plot.quieterrormode =true; -DestPath = Path.Combine (PFile.Directory.FullName, Path.getfilenamewithoutextension (pfile.name) +". jpg"); +Doc. Plot.plottofile (DestPath,"Publishtoweb JPG.PC3"); - + } A Catch(System.Exception e) at { - return false; - } - finally - { - if(Doc! =NULL) Doc. Close (false); in}
The CAD application needs to be closed after all processing is complete:
#region Close the CAD program try{ _app. Quit ();} Catch (Exception ex) { return;} #endregion
C # CAD Batch conversion to picture