I don't know if this is your problem but in your code,
you do this:
Database curdb = HostApplicationServices.WorkingDatabase;
Database db = Database.FromAcadDatabase(axdoc1.Database);
HostApplicationServices.WorkingDatabase = db;
//...
axdoc1.ModelSpace.AttachExternalReference(newName,
fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);
axdoc1.SaveAs(newName1, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
When you call ReleaseComObject(axdoc1), the Database that it
wraps is destroyed while it is the current 'WorkingDatabase',
which is almost certainly going to blow up.
Try restoring the WorkingDatabase back to curdb before
you let go of axdoc1.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
wrote in message news:4875028@discussion.autodesk.com...
thanks tony,I do as you suggested,but it still doesn't work for me , a fatal error will appeare when attacth external reference.
private static void CreateRefDrawing(string subFolder)
{
string[] fileNames = new string[] { "Ref_S_Plan", "Ref_S_Beam", "Ref_S_Foot" };
AxDbDocumentClass axdoc = new AxDbDocumentClass();
string newName = subFolder + "\\" + fileNames[0] + ".dwg";
axdoc.ModelSpace.AddLine(new double[] { 0, 0, 0 }, new double[] { 1000, 1000, 0 });
axdoc.SaveAs(newName, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);
AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
string newName1 = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName1, null);
AxDbDocumentClass axdoc2 = new AxDbDocumentClass();
string newName2 = subFolder + "\\" + fileNames[2] + ".dwg";
axdoc2.SaveAs(newName2, null);
double[] insertPnt = new double[] { 0, 0, 0 };
double scale = 1;
double rotation = 0;
bool bOverlay = false;
Database curdb = HostApplicationServices.WorkingDatabase;
Database db = Database.FromAcadDatabase(axdoc1.Database);
HostApplicationServices.WorkingDatabase = db;
// the following line will cause a fatal error,
axdoc1.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay,
null);
axdoc1.SaveAs(newName1, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
db = Database.FromAcadDatabase(axdoc2.Database);
HostApplicationServices.WorkingDatabase = db;
axdoc2.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay,
null);
axdoc2.SaveAs(newName1, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);
HostApplicationServices.WorkingDatabase = curdb;
}