- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
We are creating an application where the drafters have created templates with blocks and we read data from a few sources and populate the block attributes with the correct data. To do this I have a separate windows exe app that gets all of the data and writes it to a JSON file then the AutoCAD command reads the JSON file and builds the drawing as required. To date, we have had single references to unique block definitions in each drawing. So, I've been able to search for the block def, get all references (only one), and then select the first item from the collection (of length 1).
But, what happens when I have more than one block reference to the same definition? How do I know it's the one I want? For instance, let's say I know that x, y, z should be in the block in the upper left and that a, b, and c data should be in the block in the lower right. I used relative coordinates in the example, but just as an example, I'd like something that can robustly determine the "correct" block for the data I have grabbed.
I've dug into this a bit and the best I can come up with is to have the drafters run the LIST command on a block and get me the HANDLE, which I could use to identify that block, but I'm unfamiliar with handles in AutoCAD and if they change as the drafters make changes to the drawings, or if they change each time I open a drawing.
Looking for advice here.
Below is some of the code that I am using, which I have received some help with already. These functions are inside of a class that has the database and transaction as instance variables. As you can see, I go and get all block references, but I also assume that I only get 1 back. If I get more than one I have no idea how to deal with it.
public void ProcessBlock(AcadBlockData block)
{
BlockReference br = GetAcadBlockReference(block.Name);
if (br != null)
{
if(block.Name == "VALVE_BODY")
{
SetDynamicPropertyValue(br, "Visibility1", block.Attributes["VALVE_TYPE"]);
}
ProcessBlockRefAttributes(br, block.Attributes);
}
}
private BlockReference GetAcadBlockReference(string blockName)
{
ObjectIdCollection ids = GetAllBlockReferences(blockName);
return ids.Count > 0 ? (BlockReference)tr.GetObject(ids[0], OpenMode.ForWrite) : null;
}
private ObjectIdCollection GetAllBlockReferences(string blockName)
{
var blockReferenceIds = new ObjectIdCollection();
var bt = GetBlocktable();
if (bt.Has(blockName))
{
var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
foreach (ObjectId id in btr.GetBlockReferenceIds(true, false))
{
blockReferenceIds.Add(id);
}
foreach (ObjectId anonymousBlockId in btr.GetAnonymousBlockIds())
{
var anonymousBtr = (BlockTableRecord)tr.GetObject(anonymousBlockId, OpenMode.ForRead);
foreach (ObjectId id in anonymousBtr.GetBlockReferenceIds(true, false))
{
blockReferenceIds.Add(id);
}
}
}
return blockReferenceIds;
}
Solved! Go to Solution.