AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

No easy way to count object data records in a table?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
fieldguy
1075 Views, 2 Replies

No easy way to count object data records in a table?

fieldguy
Advisor
Advisor

Is there no easy way to get the count of records in any given object data table?  I would like to inform the user that there are empty tables.  Do I have to do something like create an array of booleans?

 

I have found the sdk example with create table, add, show, delete record but they use tables.getobjectrecords with an objectid.  I'd rather not try all of the objects just to determine that the table is empty.   

0 Likes

No easy way to count object data records in a table?

Is there no easy way to get the count of records in any given object data table?  I would like to inform the user that there are empty tables.  Do I have to do something like create an array of booleans?

 

I have found the sdk example with create table, add, show, delete record but they use tables.getobjectrecords with an objectid.  I'd rather not try all of the objects just to determine that the table is empty.   

2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: fieldguy

Anonymous
Not applicable
Accepted solution

fieldguy,

Easy? very relative... 😉

 

It appears to me all records for all tables are in the same _location_ thusly I don't think there is an alternative to iterating the records.

Maybe something like this ...

 

public int do_getRecordCount(string forTableName)
{
	int rtnval = 0;

	using (DocumentLock docLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
	{
		Tables tables = HostMapApplicationServices.Application.ActiveProject.ODTables;
		using (Transaction tm = doc.TransactionManager.StartTransaction())
		{
			BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, false);
			BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], DatabaseServices.OpenMode.ForRead, false);
			Records rS = tables.GetObjectRecords(0, btr.ObjectId, Autodesk.Gis.Map.Constants.OpenMode.OpenForRead, false);
			if (rS.Count > 0)
			{
				foreach (Record r in rS)
				{
					if (r.TableName.ToUpper() == forTableName.ToUpper())
						rtnval++;
					
				}//foreach Record
			}//if rS
			tm.Abort(); //not really necessary in this case 
		}//Transaction
	}//DocumentLock
	return rtnval;
}

 

You probably do not have to lock the document but that would depend on what else you are doing. Certainly not necessary for this.

 

r,

dennis

0 Likes

fieldguy,

Easy? very relative... 😉

 

It appears to me all records for all tables are in the same _location_ thusly I don't think there is an alternative to iterating the records.

Maybe something like this ...

 

public int do_getRecordCount(string forTableName)
{
	int rtnval = 0;

	using (DocumentLock docLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
	{
		Tables tables = HostMapApplicationServices.Application.ActiveProject.ODTables;
		using (Transaction tm = doc.TransactionManager.StartTransaction())
		{
			BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, false);
			BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], DatabaseServices.OpenMode.ForRead, false);
			Records rS = tables.GetObjectRecords(0, btr.ObjectId, Autodesk.Gis.Map.Constants.OpenMode.OpenForRead, false);
			if (rS.Count > 0)
			{
				foreach (Record r in rS)
				{
					if (r.TableName.ToUpper() == forTableName.ToUpper())
						rtnval++;
					
				}//foreach Record
			}//if rS
			tm.Abort(); //not really necessary in this case 
		}//Transaction
	}//DocumentLock
	return rtnval;
}

 

You probably do not have to lock the document but that would depend on what else you are doing. Certainly not necessary for this.

 

r,

dennis

Message 3 of 3
fieldguy
in reply to: Anonymous

fieldguy
Advisor
Advisor

Easy? very relative... 😉

 

Agreed.  I was looking for tables.count or table.count which do not exist.  These are odd structures IMO. Why do I have to use gettablenames as a string collection just to get a count of the number of tables?

Your answer works very well. Thanks. I did not realize I could get all the records at once using the blocktablerecord "ModelSpace". 

 

vb code - requires Imports acadapps = Autodesk.AutoCAD.ApplicationServices and

Imports acaddbs = Autodesk.AutoCAD.DatabaseServices

Public Shared Function getodrecordcount(ByRef findtable As String) As Integer
        Dim returnval As Integer = 0
        Dim doc As acadapps.Document = acadapps.Application.DocumentManager.MdiActiveDocument
        Dim db As acaddbs.Database = doc.Database
        Dim tables As ObjectData.Tables = HostMapApplicationServices.Application.ActiveProject.ODTables
        Using doclock As acadapps.DocumentLock = doc.LockDocument
            Using mytx As acaddbs.Transaction = db.TransactionManager.StartTransaction
                'Open the Block table for read
                Dim acBlkTbl As acaddbs.BlockTable = mytx.GetObject(db.BlockTableId, acaddbs.OpenMode.ForRead)
                'Open the Block table record Modelspace for read
                Dim acBlkTblRec As acaddbs.BlockTableRecord = mytx.GetObject(acBlkTbl(acaddbs.BlockTableRecord.ModelSpace), _
                    acaddbs.OpenMode.ForRead)
                Dim odrecords As ObjectData.Records = tables.GetObjectRecords(0, acBlkTblRec.ObjectId, Constants.OpenMode.OpenForRead, False)
                If odrecords.Count > 0 Then
                    For Each odrec As ObjectData.Record In odrecords
                        If odrec.TableName.ToUpper = findtable.ToUpper Then
                            returnval += 1
                        End If
                    Next
                End If

                mytx.Abort()

            End Using 'transaction
        End Using 'document lock       
        Return returnval
    End Function

0 Likes

Easy? very relative... 😉

 

Agreed.  I was looking for tables.count or table.count which do not exist.  These are odd structures IMO. Why do I have to use gettablenames as a string collection just to get a count of the number of tables?

Your answer works very well. Thanks. I did not realize I could get all the records at once using the blocktablerecord "ModelSpace". 

 

vb code - requires Imports acadapps = Autodesk.AutoCAD.ApplicationServices and

Imports acaddbs = Autodesk.AutoCAD.DatabaseServices

Public Shared Function getodrecordcount(ByRef findtable As String) As Integer
        Dim returnval As Integer = 0
        Dim doc As acadapps.Document = acadapps.Application.DocumentManager.MdiActiveDocument
        Dim db As acaddbs.Database = doc.Database
        Dim tables As ObjectData.Tables = HostMapApplicationServices.Application.ActiveProject.ODTables
        Using doclock As acadapps.DocumentLock = doc.LockDocument
            Using mytx As acaddbs.Transaction = db.TransactionManager.StartTransaction
                'Open the Block table for read
                Dim acBlkTbl As acaddbs.BlockTable = mytx.GetObject(db.BlockTableId, acaddbs.OpenMode.ForRead)
                'Open the Block table record Modelspace for read
                Dim acBlkTblRec As acaddbs.BlockTableRecord = mytx.GetObject(acBlkTbl(acaddbs.BlockTableRecord.ModelSpace), _
                    acaddbs.OpenMode.ForRead)
                Dim odrecords As ObjectData.Records = tables.GetObjectRecords(0, acBlkTblRec.ObjectId, Constants.OpenMode.OpenForRead, False)
                If odrecords.Count > 0 Then
                    For Each odrec As ObjectData.Record In odrecords
                        If odrec.TableName.ToUpper = findtable.ToUpper Then
                            returnval += 1
                        End If
                    Next
                End If

                mytx.Abort()

            End Using 'transaction
        End Using 'document lock       
        Return returnval
    End Function

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report