• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Posts: 92
    Registered: ‎06-04-2007

    retrieving block info using c#

    359 Views, 9 Replies
    08-08-2007 01:01 PM
    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
    Please use plain text.
    Mentor
    Posts: 2,504
    Registered: ‎02-17-2004

    Re: retrieving block info using c#

    08-08-2007 01:52 PM in reply to: johnlap
    How I would do it (not all the experienced)
    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."


    wrote in message news:5684044@discussion.autodesk.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
    Please use plain text.
    *Tony Tanzillo

    Re: retrieving block info using c#

    08-08-2007 02:34 PM in reply to: johnlap
    >> does anybody know how to

    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

    wrote in message news:5684044@discussion.autodesk.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
    Please use plain text.
    Valued Contributor
    Posts: 92
    Registered: ‎06-04-2007

    Re: retrieving block info using c#

    08-08-2007 03:00 PM in reply to: johnlap
    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
    Please use plain text.
    Distinguished Contributor
    Posts: 1,691
    Registered: ‎12-15-2003

    Re: retrieving block info using c#

    08-08-2007 03:45 PM in reply to: johnlap
    BlockRef & AttRef OR BlockDef & AttDef?
    Please use plain text.
    Active Member
    Posts: 6
    Registered: ‎07-16-2007

    Re: retrieving block info using c#

    08-09-2007 11:31 AM in reply to: johnlap
    Below is a similiar function to one where I update our drawing TitleBlock(block name "TITLEBLK"). this function updates the attributes "DWG_NO", "SHEET_NUMBER","REVISION_NUMBER"

    -----
    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[BlockTableRecord.PaperSpace], OpenMode.ForRead);
    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.BlockTableRecord, OpenMode.ForRead);

    if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == "TITLEBLK"))
    {

    Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcol = br.AttributeCollection;
    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());
    }
    }

    }
    Please use plain text.
    *Tony Tanzillo

    Re: retrieving block info using c#

    08-09-2007 02:09 PM in reply to: johnlap
    You said you wanted to convert to C#, but say
    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

    wrote in message news:5684250@discussion.autodesk.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
    Please use plain text.
    Distinguished Contributor
    Posts: 172
    Registered: ‎11-19-2003

    Re: retrieving block info using c#

    08-13-2007 10:57 PM in reply to: johnlap
    Look at this blog, maybe you can find something you will need. And if you
    want to get the name of a block, don't forget dynamic blocks.
    http://through-the-interface.typepad.com/

    schrieb im Newsbeitrag news:5684044@discussion.autodesk.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
    Please use plain text.
    Distinguished Contributor
    Posts: 1,222
    Registered: ‎10-14-2004

    Re: retrieving block info using c#

    08-14-2007 10:35 AM in reply to: johnlap
    Great blog. I loved the self-portraits! LOL
    Please use plain text.
    New Member
    Posts: 1
    Registered: ‎05-12-2010

    Re: retrieving block info using c#

    05-26-2010 01:39 PM in reply to: johnlap

    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.ModelSpace], OpenMode.ForRead);

    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, OpenMode.ForRead);

    if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == BName))

    {

    Please use plain text.