.NET

Reply
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 1 of 13 (84 Views)

AttachExternalReference problem

84 Views, 12 Replies
06-13-2005 04:42 AM
I want use objectDBX to AttachExternalReference ,but always encount fator error,could someone tell me how to solve it?

string[] fileNames = new string[] { "Ref_S_Plan", "Ref_S_Beam", "Ref_S_Foot" };

AxDbDocumentClass axdoc = new AxDbDocumentClass();
string newName;
newName = subFolder + "\\" + fileNames[0] + ".dwg";
axdoc.SaveAs(newName, null);

AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName, null);

AxDbDocumentClass axdoc2 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[2] + ".dwg";
axdoc2.SaveAs(newName, null);

double[] insertPnt = new double[] { 0, 0, 0 };
double scale = 1;
double rotation = 0;
bool bOverlay = false;
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);
}
catch { }

string refFileNameNoext;
refFileNameNoext = fileNames[0];
axdoc1.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null);

refFileNameNoext = fileNames[0];
axdoc2.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null);

try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);
}
catch { }
}
*Albert Szilvasy
Message 2 of 13 (84 Views)

Re: AttachExternalReference problem

06-13-2005 09:26 AM in reply to: netcai
What is the error that you are encountering? On what line?

Albert

wrote in message news:4872847@discussion.autodesk.com...
I want use objectDBX to AttachExternalReference ,but always encount fator
error,could someone tell me how to solve it?

string[] fileNames = new string[] { "Ref_S_Plan", "Ref_S_Beam",
"Ref_S_Foot" };

AxDbDocumentClass axdoc = new AxDbDocumentClass();
string newName;
newName = subFolder + "\\" + fileNames[0] + ".dwg";
axdoc.SaveAs(newName, null);

AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName, null);

AxDbDocumentClass axdoc2 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[2] + ".dwg";
axdoc2.SaveAs(newName, null);

double[] insertPnt = new double[] { 0, 0, 0 };
double scale = 1;
double rotation = 0;
bool bOverlay = false;
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);
}
catch { }

string refFileNameNoext;
refFileNameNoext = fileNames[0];
axdoc1.ModelSpace.AttachExternalReference(subFolder,
refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null);

refFileNameNoext = fileNames[0];
axdoc2.ModelSpace.AttachExternalReference(subFolder,
refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null);

try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);
}
catch { }
}
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 3 of 13 (84 Views)

Re: AttachExternalReference problem

06-13-2005 05:36 PM in reply to: netcai
string[] fileNames = new string[] { "Ref_S_Plan", "Ref_S_Beam", "Ref_S_Foot" };

AxDbDocumentClass axdoc = new AxDbDocumentClass();
string newName;
newName = subFolder + "\\" + fileNames[0] + ".dwg";
axdoc.SaveAs(newName, null);

AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName, null);

AxDbDocumentClass axdoc2 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[2] + ".dwg";
axdoc2.SaveAs(newName, null);

double[] insertPnt = new double[] { 0, 0, 0 };
double scale = 1;
double rotation = 0;
bool bOverlay = false;
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);
}
catch { }

string refFileNameNoext;
refFileNameNoext = fileNames[0];
axdoc1.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null); // this line will cause a fatal error

refFileNameNoext = fileNames[0];
axdoc2.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext, insertPnt,
scale, scale, scale, rotation, bOverlay, null);

try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);
}
catch { }
}
*Albert Szilvasy
Message 4 of 13 (84 Views)

Re: AttachExternalReference problem

06-13-2005 10:30 PM in reply to: netcai
There are too many variables here to actually reproduce the error. What is
the error that you are getting? Does AutoCAD simply vaporizes or does it
display some error?

Albert

wrote in message news:4873818@discussion.autodesk.com...
string[] fileNames = new string[] { "Ref_S_Plan", "Ref_S_Beam",
"Ref_S_Foot" };

AxDbDocumentClass axdoc = new AxDbDocumentClass();
string newName;
newName = subFolder + "\\" + fileNames[0] + ".dwg";
axdoc.SaveAs(newName, null);

AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName, null);

AxDbDocumentClass axdoc2 = new AxDbDocumentClass();
newName = subFolder + "\\" + fileNames[2] + ".dwg";
axdoc2.SaveAs(newName, null);

double[] insertPnt = new double[] { 0, 0, 0 };
double scale = 1;
double rotation = 0;
bool bOverlay = false;
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);
}
catch { }

string refFileNameNoext;
refFileNameNoext = fileNames[0];
axdoc1.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext,
insertPnt,
scale, scale, scale, rotation, bOverlay, null); // this line will cause a
fatal error

refFileNameNoext = fileNames[0];
axdoc2.ModelSpace.AttachExternalReference(subFolder, refFileNameNoext,
insertPnt,
scale, scale, scale, rotation, bOverlay, null);

try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);
}
catch { }
}
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 5 of 13 (84 Views)

Re: AttachExternalReference problem

06-14-2005 02:25 AM in reply to: netcai
I have found the problem.I misunderstanding the first parameter, but a new problem appear, the ExternalReference doesn't attached to axdoc1 and axdoc2,but attached to the current drawing in autocad.
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 6 of 13 (84 Views)

Re: AttachExternalReference problem

06-14-2005 02:55 AM in reply to: netcai
the following is my current code,the problem is line is added to axdoc1 and axdoc2, but External Reference isn't attached to axdoc1 and axdoc2,but attached to current drawing in autocad, it's so strange.
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.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;

axdoc1.ModelSpace.AddLine(new double[] { 0, 0, 0 }, new double[] { 1000, 1000, 0 });
axdoc1.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);
axdoc1.SaveAs(newName1, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);

axdoc2.ModelSpace.AddLine(new double[] { 0, 0, 0 }, new double[] { 1000, 1000, 0 });
axdoc2.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);
axdoc2.SaveAs(newName2, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);

}
*Tony Tanzillo
Message 7 of 13 (84 Views)

Re: AttachExternalReference problem

06-14-2005 05:30 AM in reply to: netcai
Can say if this will work, but it may be
worth a try:

1. Get the managed Database from the AcadDatabase, using
Database.FromAcadDatabase(AxDbDocument.Database)

2. Save the value of HostApplicationServices.WorkingDatabase

3. Set HostApplicationServices.WorkingDatabase to the
Database obtained from the AcadDatabase.

4. Attach the xref

5. Set the value of HostApplicationServices.WorkingDatabase
back to the original value saved previously.


--


wrote in message news:4873960@discussion.autodesk.com...
the following is my current code,the problem is line is added to axdoc1 and axdoc2, but External Reference isn't
attached to axdoc1 and axdoc2,but attached to current drawing in autocad, it's so strange.
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.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;

axdoc1.ModelSpace.AddLine(new double[] { 0, 0, 0 }, new double[] { 1000, 1000, 0 });
axdoc1.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation,
bOverlay, null);
axdoc1.SaveAs(newName1, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc1);

axdoc2.ModelSpace.AddLine(new double[] { 0, 0, 0 }, new double[] { 1000, 1000, 0 });
axdoc2.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation,
bOverlay, null);
axdoc2.SaveAs(newName2, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc2);

}
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 8 of 13 (84 Views)

Re: AttachExternalReference problem

06-14-2005 06:29 PM in reply to: netcai
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;

}
*Tony Tanzillo
Message 9 of 13 (84 Views)

Re: AttachExternalReference problem

06-15-2005 12:31 AM in reply to: netcai
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;

}
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 10 of 13 (84 Views)

Re: AttachExternalReference problem

06-15-2005 01:26 AM in reply to: netcai
tony
I tried as you told, but still occurred a error.
error: Exception occurred: 0xC0000005 (Access Violation)

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.SaveAs(newName, null);
system.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);

AxDbDocumentClass axdoc1 = new AxDbDocumentClass();
string newName1 = subFolder + "\\" + fileNames[1] + ".dwg";
axdoc1.SaveAs(newName1, null);
try
{
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;
axdoc1.ModelSpace.AttachExternalReference(newName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);//a fatal error occurred
axdoc1.SaveAs(newName1, null);
HostApplicationServices.WorkingDatabase = curdb;
}
catch { }

}
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.