I have AutoDesk 2014 and VS2012 installed. I already have the dlls mentioned here and also tried this but not worked. I really need to know how to export those files to images, jpg, png,.. using C# code. Thanks!
The DLLs and code you mentioned are used to create plugins for AutoCAD.
You can create images (PNG or other) using a code like this: http://through-the-interface.typepad.com/through_the_interface/2007/04/taking_a_snapsh.html
But you may need to get started with the API, see a basic tutorial at http://www.autodesk.com/myfirstautocadplugin
To meet the requirements of your post, you can opt for third party plugins (which allow you to export DWGs to PNG, JPG etc) and associate the chosen plugin with your Visual Studio Solution to allow you to export DWGs to PNG, JPG etc ... However, under Autodesk's point of view, the recommendation will always be to consume the API for you to develop plugins and / or achieve your requirements through the API; I particularly prefer to go and spoil the manufacturer's native solutions and then think about using a third-party solution. It is worth mentioning that I am a Developer and I develop plugins for AutoCAD software but I am not from Autodesk and I do not win today to defend this point of view.
The only aspects that are separators in your choice is to know if the DWGs are linked from Database Servers or not. And, in case DWGs are independent of Database Server, if the third-party plugin is free or you have to pay to take advantage of the features you need.
Here is an example code I use to drive AutoCAD through an external application (Console Application Project); as Autodesk develops its products using also COM interfaces, allows us developers to consume the features intrinsic to Autodesk Softwares that can be executed by the external application. At the code bellow, open the AutoCAD application by your Program ID and iterate throw all DWG files using a native command named JPGOUT.
class Program
{
public static void Main(string[] args)
{
AcadApplication acAppComObj = null;
//Query your Regedit Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD to get the correctly suffix that specifies the version
const string strProgId = "AutoCAD.Application.20";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch
{
// If an instance of AutoCAD is not created then message and exit
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application
if (null != acAppComObj)
{
try
{
int i = 0;
AcadState appState = app.GetAcadState();
while (!appState.IsQuiescent)
{
if (i == 120)
{
Environment.Exit(-1);
}
// Wait .25s
Thread.Sleep(250);
i++;
}
app.Visible = true;
var docs = app.Documents;
docs.Add("acadiso.dwt");
}
catch (COMException err)
{
if (err.ErrorCode.ToString() == "-2147417846")
{
Thread.Sleep(5000);
}
}
catch (Exception ex)
{
throw new Exception("Falha durante a obtenção do documento ativo.", ex);
}
}
else
{
throw new Exception("Erro to open first document.");
}
// Open AutoCAD project file, use this code if all DWGs is associated with a AutoCAD Project with Server Database
#region ' Open Project '
acDocComObj.SendCommand("FILEDIA","0");
acDocComObj.SendCommand("-OPENPROJECT", "C:\\\\Users\\<username>\\Documents\\ProjectFolder\\Project.xml");
acDocComObj.SendCommand("FILEDIA","1");
#endregion
string[] dwgFiles = //To do: add here the rule that list all full path DWG files
AcadDocuments docs = app.Documents;
foreach(string dwgPath in dwgFiles)
{
docs.Open(dwgPath, true);
Thread.Sleep(3000);
AcadDocument acadDoc = acAppComObj.ActiveDocument;
acDocComObj.SendCommand("FILEDIA","0");
acadDoc.SendCommand("JPGOUT ", "C:\\\\Users\\<username>\\Images\\" + Path.GetFileName(dwgPath) + ".jpg");
acDocComObj.SendCommand("FILEDIA","1");
}
}
}
Premise to use this source code example (this was tested and is in using):
a) Have an AutoCAD product installed (if you do not have a license and will use a student version to download the 2018 version because the 2019 has license-based encryption to open the DWG will always throw an exception);
b) Create a Visual Studio project of type Console Application with build in x64 processing architecture;
c) Add the references "C: \ ProgramFiles \ Autodesk \ AutoCAD 20XX \ Autodesk.AutoCAD.Interop.dll" and "C: \ ProgramFiles \ Autodesk \ AutoCAD 20XX \ Autodesk.AutoCAD.Interop.Common.dll";
Thats all.
来源:https://stackoverflow.com/questions/29159304/how-to-export-dwg-files-to-images-using-autocad-api-with-c