.NET

Reply
Active Member
Jbrauer
Posts: 6
Registered: ‎10-15-2002
Message 1 of 4 (340 Views)

Find Block in Xref

340 Views, 3 Replies
03-28-2012 03:47 PM

Enyone know how to find a given block inside a Xref.

I can run trough the entir xref object and finde the block - by name -  

but is it posibel to search direct like searching in the main drawing.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,071
Registered: ‎06-29-2007
Message 2 of 4 (327 Views)

Re: Find Block in Xref

03-29-2012 12:22 AM in reply to: Jbrauer

Hi,

 

>> to search direct like searching in the main drawing

What is a "direct search" for you? ....and with "Block" you mean "BlockReferece" or "BlockTableRecord"?

Assuming you mean searching for BlockReferences by using a selectionset ==> no, because you can only find top-level-elements with a selectionset (elements placed in Modelspace or Paperspace), you can not find block-in-block with selectionsets, so you can't find block-in-xref.

 

What should be possible is to search for the BlockTableRecord within the database of your XRef, if you got it you can then use the function .GetBlockReferenceIds.

 

HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Member
Jbrauer
Posts: 6
Registered: ‎10-15-2002
Message 3 of 4 (276 Views)

Re: Find Block in Xref

06-14-2012 02:14 PM in reply to: Alfred.NESWADBA

according to the thread "GetBlockReferenceIds Problems" provide GetBlockReferenceIds my not with a list of blockreferences in the drawing for a specifik block - as I have found out by trying.

 

Is there another smart way to get direct access to a block in an xref. like a search in the master drawing ??

 

I have drawings with hundreds of blocke inside several Xrefs, and I need to find one specificblock inside each xref - inside the block I nead to read some attribute values. 
It takes awful long to run through all blocke in each xref to find the right block. 
Via the XrefDatabase I  can find out if blocken in block table, but not if it is inserted in the drawing

 
 
 



 

 

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 4 of 4 (258 Views)

Re: Find Block in Xref

06-15-2012 09:04 AM in reply to: Jbrauer

Have you had a look at how xref blocks appear in the database using the MgdDbg tool. You can get this plugin here: http://adndevblog.typepad.com/autocad/2012/04/dwg-debugger-mgddbg-app-for-autocad-20122013.html

 

I looked into it and it seems that attribute block definitions come into your main drawing with names in an XrefFileName|BlockDefinitionName format. You will see that it uses a vertical pipe symbol to seperate the xref name from the block name.

 

So say you have an xref drawing named "XREF01.dwg" which contains a block table record with attributes named "MyAttributeBlock", then when you xref it into your main drawing you will get a block table record named "XREF01|MyAttributeBlock".

 

So here is my suggestion. How about you only access block table records with names that end with a pipe and your block definition name. You can then easily call GetBlockReferenceIds() to get the block references and their attribute values.

 

Here's what I mean in code...

 

[CommandMethod("XrefAttributes")]
public static void XrefAttributes()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;
	Editor ed = doc.Editor;

	using (Transaction tr = db.TransactionManager.StartTransaction())
	{
		BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
		foreach (ObjectId btrId in bt)
		{
			BlockTableRecord btr =
				(BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

			if (btr.Name.EndsWith("|MyAttBlock"))
			{
				ObjectIdCollection ids = btr.GetBlockReferenceIds(true, false);
				foreach (ObjectId id in ids)
				{
					BlockReference br =
						(BlockReference)tr.GetObject(id, OpenMode.ForRead);

					AttributeCollection attc = br.AttributeCollection;
					foreach (ObjectId attId in attc)
					{
						AttributeReference attr =
							(AttributeReference)tr.GetObject(
							attId,
							OpenMode.ForRead
							);

						ed.WriteMessage("\n" + attr.TextString);
					}
				}
			}
		}

		tr.Commit();
	}
}

 

The only other option I can think of is to get the xref file names using the XrefGraph and XrefNode classes, and then load them into side databases for-read to search for your data. Try searching "side database" at the following blog for further details on this other technique: http://through-the-interface.typepad.com/

 

Art

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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