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

C# find Block References by Name

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
8450 Views, 5 Replies

C# find Block References by Name

I have been try and trying and pulling my hair out, but I cannot figure out how to iterate through modlespace and find all the block REFERANCES with a particular name.

Could anyone please post an example of how to accomplish this, or at least a nudge in the right direction.
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

Hi Techno,

try this out,

AcadDB.BlockTable BT = (AcadDB.BlockTable)trans.GetObject(db.BlockTableId,AcadDB.OpenMode.ForRead);
foreach (AcadDB.ObjectId btrID in BT)
{
AcadDB.BlockTableRecord BTR = (AcadDB.BlockTableRecord)trans.GetObject(btrID, AcadDB.OpenMode.ForRead);
if (BTR.Name.ToUpper() == blockName.ToUpper())
{
blockId = BTR.ObjectId;
AcadDB.BlockTableRecord modelSpaceRecord = (AcadDB.BlockTableRecord)trans.GetObject(BT[AcadDB.BlockTableRecord.ModelSpace],AcadDB.OpenMode.ForWrite);
AcadGeom.Point3d pt = new Autodesk.AutoCAD.Geometry.Point3d(4,4,0);
AcadDB.BlockReference blockRef = new Autodesk.AutoCAD.DatabaseServices.BlockReference(pt, blockId);
ed.WriteMessage("BlockRef :" + blockRef);
modelSpaceRecord.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
}
}
Message 3 of 6
Anonymous
in reply to: Anonymous

I think that attached code will be useful for you.
Try it.
P.S.: There is another way:
[code]
[CommandMethod("CalcBlocks1")]
static public void CalcBlocks1()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
PromptResult res = ed.GetString("\nType name of block: ");
if (res.Status == PromptStatus.OK)
{
TypedValue [] flt =
{
new TypedValue(0, "INSERT"),
new TypedValue(2, res.StringResult),
new TypedValue(410, "Model")
};
PromptSelectionResult rs = ed.SelectAll(new SelectionFilter(flt));
if (rs.Status == PromptStatus.OK && rs.Value.Count > 0)
{
ed.WriteMessage("\nNumber of blocks with name <{0}> in Model Space is {1}",res.StringResult,rs.Value.Count);
}
else
{
ed.WriteMessage("\nNo blocks with name <{0}>",res.StringResult);
}
}
}
[/code]
Message 4 of 6
Anonymous
in reply to: Anonymous

Thanks to both of you for your help, I really appreciate it!
I will go through both examples and then see if I have anymore questions.
Once again
Thank you.
Message 5 of 6
Anonymous
in reply to: Anonymous

Also see the GetBlockReferenceIds method of the
BlockTableRecord object.

You probably don't have to iterate through any space to
get all contained references for a given block. You can get
the IDs of all references from the aforementioned method,
and compare the BlockId or OwnerID property value of each,
to the id of the block table record for the desired space.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5203965@discussion.autodesk.com...
I have been try and trying and pulling my hair out, but I cannot figure out how to iterate through modlespace and find all the block REFERANCES with a particular name.

Could anyone please post an example of how to accomplish this, or at least a nudge in the right direction.
Message 6 of 6
Anonymous
in reply to: Anonymous

"You can get
the IDs of all references from the aforementioned method [GetBlockReferenceIds],
and compare the BlockId or OwnerID property value of each,
to the id of the block table record for the desired space."

I'm a bit late to join the party.. I know..
The example about looping through and using the Name property seems close to what the question was about; though I'm not clear why creating a new BlockReference, at some point that one might not even be interested in inserting at, is required? Any other way to get a BlockReference without that bit of 'cheating' ?

However, you mentioned
"and compare the BlockId or OwnerID property value of each,
to the id of the block table record for the desired space."

Aren't those Id's managed by the API or AutoCAD, whereas the Name property on BlockTableRecord : SymbolTableRecord, is what's available for developers to use?
E.g, if we change the quantity of a given part No, the part No would be used to locate all blocks by Name that need to be updated owing to this change in quantity.
How would the comparison of BlockId against "id of block table record for the desired space" be useful in such a case? Or should the understanding be that BlockId and Name property serve the same purpose? Thanks.

Rico.

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