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

Detaching all XRefs

0 REPLIES 0
Reply
Message 1 of 1
Ontario70
264 Views, 0 Replies

Detaching all XRefs

Hi there,

for an automatic proccessing of over 6.000(!) DWGs all drawings need to get ripped off their XRefs.

I tried the following, using the Database.DetachXRef method:

(-------- Code starts --------)
public void DetachXrefs()
{
FolderBrowserDialog fldFolder = new FolderBrowserDialog();
if (fldFolder.ShowDialog() == DialogResult.OK)
{
string[] DWGs = Directory.GetFiles(fldFolder.SelectedPath, "*.dwg");
DocumentCollection dcDocMan = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
for (int i = 0; i < DWGs.Length; i++)
{
Document actDWG = dcDocMan.Open(DWGs[\i]);
Database docDB = actDWG.Database;
using (Transaction trDWG = docDB.TransactionManager.StartTransaction())
{
XrefGraph dbXRGraph = docDB.GetHostDwgXrefGraph(true);
for (int j = 0; j < dbXRGraph.NumNodes; j++)
{
try
{
XrefGraphNode xnGraphNode = dbXRGraph.GetXrefNode(i);
DetachChildren(docDB, trDWG, xnGraphNode.BlockTableRecordId);
}
catch //(System.Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
trDWG.Commit();
}
docDB.SaveAs(DWGs[\i], DwgVersion.AC1015);
actDWG.CloseAndDiscard();
}
}
}

private void DetachChildren(Database DB, Transaction trDWG, ObjectId xnGraphId)
{
BlockTableRecord btr = (BlockTableRecord)trDWG.GetObject(xnGraphId, OpenMode.ForRead);
BlockTableRecordEnumerator itIterator = btr.GetEnumerator();

while (itIterator.MoveNext())
{
BlockReference brXRefBlock = (BlockReference)itIterator.Current.GetObject(OpenMode.ForRead);
BlockTableRecord nBTR = (BlockTableRecord)trDWG.GetObject(brXRefBlock.BlockTableRecord, OpenMode.ForRead);
if (nBTR.IsFromExternalReference)
{
DB.DetachXref(nBTR.ObjectId);
}
}
}
(-------- Code ends --------)

But

"BlockTableRecord btr = (BlockTableRecord)trDWG.GetObject(xnGraphId, OpenMode.ForRead);" in DetachChildren

returns an "eInvalidObjectId", the ObjectId is =0. Although the drawing definitly contains (ghosted) XRefs, I can see them in XRef manager.

What's wrong?

Any other suggestion about stripping XRefs?

Thx
Ontario

Edited by: Ontario70 on Feb 2, 2010 2:33 PM:

Indexes [\i] are intentionally done for preventing this forum editor from using [italics]... Edited by: Ontario70 on Feb 2, 2010 2:35 PM
0 REPLIES 0

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