.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AttachExternalReference problem

12 REPLIES 12
Reply
Message 1 of 13
netcai
326 Views, 12 Replies

AttachExternalReference problem

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 { }
}
12 REPLIES 12
Message 2 of 13
Anonymous
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 { }
}
Message 3 of 13
netcai
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 { }
}
Message 4 of 13
Anonymous
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 { }
}
Message 5 of 13
netcai
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.
Message 6 of 13
netcai
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);

}
Message 7 of 13
Anonymous
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);

}
Message 8 of 13
netcai
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;

}
Message 9 of 13
Anonymous
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;

}
Message 10 of 13
netcai
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 { }

}
Message 11 of 13
netcai
in reply to: netcai

I tried another mothod ,and it work fine,so I think it's a bug in objectdbx.
by the way ,could someone know how to attach external reference with arx, and how to close a docment with arx.

the following is my modified codes ,it works fine.


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);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(axdoc);

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;

try
{
//// the following comment line will cause a fatal error

//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);
//HostApplicationServices.WorkingDatabase = curdb;

string refName = ".\\" + fileNames[0] + ".dwg";
AcadDocument doc1 = ActiveX.AcadApp.Documents.Open(newName1, false, null);
doc1.ModelSpace.AttachExternalReference(refName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);
doc1.Close(true, newName1);

AcadDocument doc2 = ActiveX.AcadApp.Documents.Open(newName2, false, null);
doc2.ModelSpace.AttachExternalReference(refName, fileNames[0], insertPnt, scale, scale, scale, rotation, bOverlay, null);
doc2.Close(true, newName2);
}
catch { }

}
Message 12 of 13
netcai
in reply to: netcai

Is my judgement correct? has External reference a bug in objectdbx?
Message 13 of 13
Anonymous
in reply to: netcai

This was logged in our bug tracking database. Nobody looked into it yet so I
can't confirm or deny. It looks suspeciously like a bug.

Albert

wrote in message news:4881355@discussion.autodesk.com...
Is my judgement correct? has External reference a bug in objectdbx?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost