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

VB Block Question

4 REPLIES 4
Reply
Message 1 of 5
lvthunder
292 Views, 4 Replies

VB Block Question

I wrote a program that takes a list of drawings and generates a quantities Excel sheet based on a block.  The problem I have is unless I purge I don't get the correct number of blocks.  I don't load the drawing in the editor since I just need the database.  Is there a way to tell as it's going through the blocks if it is actually in the drawing (something that woldn't purge) so I can skip the ones that aren't?

4 REPLIES 4
Message 2 of 5
jeff
in reply to: lvthunder

If GetBlockReferenceIds returns any references then you know it is inserted in BlockTableRecord, or if Database.Purge(BlockidinCollection) returns the id then it can be purged.

Using GetBlockReferenceIds and getting the number of references if dynamic block then something like

 

        public static ObjectIdCollection GetAllBlockReferenceIds(this BlockTableRecord btr, Transaction trx, bool directOnly, bool forceValidity)
        {
            if (trx == null)
            {
                throw new NoActiveTransactionException("No active Transaction");
            }

            ObjectIdCollection blockReferenceIds = btr.GetBlockReferenceIds(directOnly, forceValidity);

         if (!btr.IsDynamicBlock) return blockReferenceIds;
         foreach (ObjectId id in btr.GetAnonymousBlockIds())
         {
             BlockTableRecord record = trx.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
             blockReferenceIds.Add(record.GetBlockReferenceIds(directOnly, forceValidity));

         }
         return blockReferenceIds;
        }

 

 

oh I guess you will need

 

        public static void Add(this ObjectIdCollection thisIds, ObjectIdCollection ids)
        {
            foreach (ObjectId id in ids)
            {
                thisIds.Add(id);
            }
        }

        public static void Add(this ObjectIdCollection thisIds, IEnumerable<ObjectId> ids)
        {
            foreach (ObjectId id in ids)
            {
                thisIds.Add(id);
            }
        }



        public static ObjectId[] ToArray(this ObjectIdCollection ids)
        {
            ObjectId[] idsArray = new ObjectId[ids.Count];
            ids.CopyTo(idsArray, 0);
            return idsArray;
        }

 

If your just wanting a count of blocks in model or what ever BlockTableRecord you can do something like

var currentSpaceIds = btr.GetAllBlockReferenceIds(true, false).Where<DBObject>(dbo => dbo.OwnerId == Db.CurrentSpaceId);

 And update to check if ModelSpaceId or Id is a layout, etc.....

You can also find your answers @ TheSwamp
Message 3 of 5
dgorsman
in reply to: lvthunder

You can only purge block definitions, not block references, so this could be a logical issue on your part.  Are you iterating the definitions to get a list of all inserted block references?  Is the counter reset properly when it finds no references for a given definition?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 5
lvthunder
in reply to: dgorsman

I'm using this to get the ID's of the blocks.

 

    Public Function GetBRefIDs(ByVal DatabaseIn As Database, ByVal BlockName As String) As ObjectIdCollection
        Dim retOIDColl As New ObjectIdCollection
        Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
            Dim myBT As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
            If myBT.Has(BlockName) = False Then
                Return Nothing
            End If
            Dim myBTR As BlockTableRecord = myBT(BlockName).GetObject(OpenMode.ForRead)
            For Each myObjID As ObjectId In myBTR.GetBlockReferenceIds(True, True)
                retOIDColl.Add(myObjID)
            Next
            Dim another As New ObjectIdCollection
            For Each childID As ObjectId In myBTR.GetAnonymousBlockIds
                Dim myChildBTR As BlockTableRecord = childID.GetObject(OpenMode.ForRead)
                For Each myChildBRefID As Object In myChildBTR.GetBlockReferenceIds(True, True)
                    retOIDColl.Add(myChildBRefID)
                Next
            Next
        End Using
        Return retOIDColl
    End Function

 Then I read the attributes in those blocks and get the counts I need to output to the spreadsheet.

 

There are two different blocks I use to get the information I need.  In some drawings there is a block defined, but not necessarily inserted anywhere.  So if I to purge the drawing in AutoCAD before I run the program I get the right answer.  If I don't it doesn't work.

Message 5 of 5
Balaji_Ram
in reply to: lvthunder

Hi,

 

Do you mean the number of block references of a certain block definition is incorrect if you do not purge the drawing ?

In that case, can you please try using "db.CloseInput(True)" just after it is read from the file ?

 

If that does not help, can you please share a non-confidential drawing and the block name that gives incorrect results ?

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

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