Find xref object layer

ralstogj
Collaborator
Collaborator

Find xref object layer

ralstogj
Collaborator
Collaborator

Hi

 

I am trying to write an app to check all xref objects are on the correct layer according to our office standards. In a project folder that contains a number of dwgs.

 

Typical we want an xref object inserted on a layer X-FileName.

 

I have gotten the code at the following link up an running that lets me select a folder and then loops thru the dwgs in the folder and finds the xrefs in each dwg. 

https://forums.autodesk.com/t5/net/net-c-updating-xref-path-without-opening-the-drawing/td-p/3566486

 

I am now stuck at what I need to insert to select the xref object and find the layer it is inserted on.

Is someone able to provide a sample of the code demonstrating now one would find the xref layer.

Regards

Justin Ralston
http://c3dxtreme.blogspot.com/
0 Likes
Reply
522 Views
1 Reply
Reply (1)

ralstogj
Collaborator
Collaborator

modified some code from a blog post to find the xref object in the database and layer.

[CommandMethod("XP_FindxrefsLayer")]
public void ChangeXref()
// this code is modified from this blog post
//http://through-the-interface.typepad.com/through_the_interface/2015/01/modifying-the-contents-of-an-...
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

if (doc == null)
return;
var ed = doc.Editor;
var db = doc.Database;

// Get the database associated with each xref in the
// drawing and change all of its circles to be dashed

using (var tr = db.TransactionManager.StartTransaction())

{
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
var ms =(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

// Loop through the contents of the modelspace
foreach (var id in ms)
{
// We only care about BlockReferences
var br =tr.GetObject(id, OpenMode.ForRead) as BlockReference;

if (br != null)
{
// Check whether the associated BlockTableRecord is
// an external reference

var bd =(BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);

if (bd.IsFromExternalReference)

{
// If so, get its Database and call the function
// to change the linetype of its Circles
String xreflayer =br.Layer;
String xrefname = br.Name;

ed.WriteMessage("\nthe xref is inserted on the layer" + xreflayer);
ed.WriteMessage("\nthe xref name is " + xrefname);
}
}
}
tr.Commit();
}

Regards

Justin Ralston
http://c3dxtreme.blogspot.com/
0 Likes