.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and am running into problems. I've got the application and document open, no problem.
Thanks,
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Get the database
Get the layout dictionary id
Then step through all the layouts in the dictionary checking the objects to
make sure they are blocks, and match the name you are looking for.
--
Tim
"A blind man lets nothing block his vision."
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's
attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and
am running into problems. I've got the application and document open, no
problem.
Thanks,
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Plenty of folks here know how, but few if any are
going to make the significant effort required to
write a detailed example for you.
So, rather than ask if anyone knows how, just post
whatever code you have that you're having problems
with, and someone should be able to give you some
ideas on what's wrong and what you need to do.
If you're just starting out with C# and haven't done
much with it, then please make that clear as well.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and am running into problems. I've got the application and document open, no problem.
Thanks,
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
-----
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
---
private void somefunction()
{
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRec
foreach (ObjectId bId in btr)
{
using (Entity ent = (Entity)trans.GetObject(bId, OpenMode.ForRead, false))
{
if (ent.GetRXClass().Name.ToString() == "AcDbBlockReference")
{
BlockReference br = (BlockReference)ent;
BlockTableRecord blkObj = (BlockTableRecord)trans.GetObject(br.BlockTableRec
if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == "TITLEBLK"))
{
Autodesk.AutoCAD.DatabaseServices.AttributeCollect
foreach (ObjectId attId in attcol)
{
AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite);
switch (attRef.Tag)
{
case "DWG_NO":
attRef.TextString = "My Number";
break;
case "SHEET_NUMBER":
attRef.TextString = "My Sheet";
break;
case "REVISION_NUMBER":
attRef.TextString = "My Rev";
break;
}
string str = ("\n Updated Attribute Tag: " + attRef.Tag + " Attribute String: " + attRef.TextString + "\n");
ed.WriteMessage(str);
}
}
}
}
} trans.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("Unexpected Error: " + ex.ToString());
}
}
}
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
which API you wanted to use.
You can use the same ActiveX API used in VBA
with C# or, you can use the managed ObjectARX
API (a much bigger leap).
So, if you meant to use the managed API,
the basic direction would be:
1. Get a filtered selection set containing
the block references you're interested in.
You can specify one or more block names
for the filter criteria (SelectionSetFilter
class).
2. Iterate through the selection set, and
open each BlockReference object, and
then examine its AttributesCollection
looking for the attributes of interest,
and operate on them.
That's the general direction, but there's still
a lot of knowledge required to do it using the
managed API, that can only be gained by
following the examples and using whatever
learning materials you can find.
When you actually attempt to do this with
the managed API, if you run into problems,
then just post the code you have and then,
we can more easily give you more specific
direction.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Thanks Tony and Tim. I am new to AutoCad, but not C#. Like I stated before, I have this working in a VBA form thru AutoCad, but just want to convert to C#. I'll check out the labs to see if I can get some leads there on which direction to go. I didn't really want a detailed tutorial with examples, but was just looking for some specific direction or ideas on some literature.
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
want to get the name of a block, don't forget dynamic blocks.
http://through-the-interface.typepad.com/
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's
attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and
am running into problems. I've got the application and document open, no
problem.
Thanks,
John
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Re: retrieving block info using c#
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hello All, When I use the code as posted I get the attributes repeated for every instance of the block. I only want them once. I tried changing the way I retrieve the instance to btr by indexing into the blockTable as such; bt[blockName], however that approach never gets me an AcDbBlockReference. I have years of c# under my belt, but I'm new to AutoCAD.NET. Thanks in advance.
try
{
string BName = lstBlockName.SelectedItem.ToString();
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
//using the following with a foreach repeats the attributes for each instance of the block
// BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord
BlockTableRecord btr = GetBtr(tr, BName, bt);
foreach (ObjectId bId in btr)
{
using (Entity ent = (Entity)tr.GetObject(bId, OpenMode.ForRead, false))
{
if (ent.GetRXClass().Name.ToString() == "AcDbBlockReference")
{
BlockReference br = (BlockReference)ent;
BlockTableRecord blkObj = (BlockTableRecord)tr.GetObject(br.BlockTableRecord
if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == BName))
{
