可以指定程序id及其版本以启动特定的AutoCAD版本,例如:
private void addCircleToACAD_Click(object sender, EventArgs e)
{
AcadApplication acadApp = null;
AcadCircle circle = null;
AcadAcCmColor color = null;
try
{
object obj = Marshal.GetActiveObject("AutoCAD.Application.18");
if (obj != null)
{
acadApp = obj as AcadApplication;
double[] cen = new double[] { 0, 0, 0 };
circle = acadApp.ActiveDocument.Database.ModelSpace.AddCircle(cen, 10);
color = acadApp.GetInterfaceObject("Autocad.AcCmColor.18") as AcadAcCmColor;
color.SetRGB(50, 150, 250);
circle.TrueColor = color;
acadApp.ZoomExtents();
}
else
{
MessageBox.Show("AutoCAD is not open or version is not right.");
}
}
catch
{
MessageBox.Show("AutoCAD is not open or version is not right.");
}
finally
{
if (color != null) Marshal.FinalReleaseComObject(color);
if (circle != null) Marshal.FinalReleaseComObject(circle);
if (acadApp != null) Marshal.FinalReleaseComObject(acadApp);
}
}