Author Topic: Get block references in layouts only  (Read 404 times)

0 Members and 1 Guest are viewing this topic.

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Get block references in layouts only
« on: March 24, 2011, 11:05:28 PM »
Hi

The BlockTableRecord.GetBlockReferencesIds() method returns all inserted references, even nested in other blocks.

A way to get only references inserted in layout is to filter the ObjectIdCollection returned by GetBlockReferencesIds() with the reference owner.

The following F# and C# snippets define a function/method which require as arguments the database and the block name.
They return respectively an ObjectId array and an ObjectIdCollection which contain all block references directly inserted in layouts (even dynamic/anonymous blocks).

C#
Code: [Select]
        private ObjectIdCollection GetReferences(Database db, string bName)
        {
            ObjectIdCollection result = new ObjectIdCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt =
                    (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(bName))
                {
                    BlockTableRecord btr =
                        (BlockTableRecord)tr.GetObject(bt[bName], OpenMode.ForRead);
                    foreach (ObjectId refId in btr.GetBlockReferenceIds(true, false))
                    {
                        BlockReference br =
                            (BlockReference)tr.GetObject(refId, OpenMode.ForRead);
                        BlockTableRecord owner =
                            (BlockTableRecord)tr.GetObject(br.OwnerId, OpenMode.ForRead);
                        if (owner.IsLayout)
                            result.Add(br.ObjectId);
                    }
                    foreach (ObjectId id in btr.GetAnonymousBlockIds())
                    {
                        BlockTableRecord anon =
                            (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                        foreach (ObjectId refId in anon.GetBlockReferenceIds(true, false))
                        {
                            BlockReference br =
                                (BlockReference)tr.GetObject(refId, OpenMode.ForRead);
                            BlockTableRecord owner =
                                (BlockTableRecord)tr.GetObject(br.OwnerId, OpenMode.ForRead);
                            if (owner.IsLayout)
                                result.Add(br.ObjectId);
                        }
                    }
                }
                tr.Commit();
            }
            return result;
        }

F#
Code: [Select]
let GetBlockReferences (db : Database) (bName : string) =
    use tr = db.TransactionManager.StartTransaction()
    let bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) :?> BlockTable
    if bt.Has(bName) then
        let btr = tr.GetObject(bt.[bName], OpenMode.ForRead) :?> BlockTableRecord
        btr.GetAnonymousBlockIds()
        |> Seq.cast<_>
        |> Seq.map (fun id -> tr.GetObject(id, OpenMode.ForRead) :?> BlockTableRecord)
        |> Seq.append (Seq.singleton btr)
        |> Seq.collect (fun b ->
            b.GetBlockReferenceIds(true, false)
            |> Seq.cast<_>
            |> Seq.filter (fun id ->
                let br = tr.GetObject(id, OpenMode.ForRead)
                let owner = tr.GetObject(br.OwnerId, OpenMode.ForRead) :?> BlockTableRecord
                owner.IsLayout))
        |> Seq.toArray
    else Array.empty

Offline Sam

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • prefered language: VB
    • Prog expertise: Beginner
    • View Profile
Re: Get block references in layouts only
« Reply #1 on: April 11, 2011, 01:36:24 PM »
I am looking for AutoCAD.NET Programmer

below is the job description

please let me know if interested or would you able to do that

email me at saleem@ventures-me.com mentioning the Tags

thanks



Key Skills: ASP.NET (VB) 2008/2010, SQL Server 2005/2008, Strong OOPS concept, Web Services, AutoCAD.NET

Job description
The candidate will be responsible for integrating a web based medical equipment planning application with AutoCAD. (Based on the data send from the application the AutoCAD should be opened with the blocks loaded. When a block is added in the application it should get reflected in the AutoCAD and vice versa)