The structure for containing data in an object data table is a table record (AcMapOdTableRecord object).
The actual data in the record is contained in instances of the AcMapValue class, one value for each field (column) in the record. You read the value of a record using one form of AcMapODTableRecord::Value, and you add new data or modify the existing value, using the other form, which is the non-const override of the function.
The following sample shows how to add new data or modify the current data in the object records of selected objects. Before performing this procedure, create a table named Zones. Next, attach data. Data attached to selected objects consists of two columns: a zoning code column called Code and an Approved By column.
- Include the necessary
header files:
#include "StdAfx.h"
#include "StdArx.h"
#include <MapODColumn.h>
#include <MapArxApi.h>
#include <MapODDefinition.h>
#include <MapProj.h>
#include <MapODRecord.h>
#include <MapODIterator.h>
- Define a constant for
input of the record values and declare variables.
#define USR_STRG_LENGTH 133
int retCode = FALSE;
ads_name ename;
ads_point spt;
char zoneCode [USR_STRG_LENGTH] = "";
char approveCode [USR_STRG_LENGTH] = "";
AcMapSession *mapApi = NULL;
AcMapProject *pProj = NULL;
AcMapODContainer *pODCont = NULL;
AcMapODRecordIterator *pIter = NULL;
AcDbObjectId eId;
AcMapODTableRecord record;
- Create an AutoCAD Map 3D
session and get the top level objects.
mapApi = AcMapGetSession();
mapApi->GetProject(pProj);
pProj->GetODContainer(pODCont);
- Use ADS functions to
prompt user to select an object, and convert the entity name of
the selected object to an object ID for initializing the iterator
in the next step.
retCode = acedEntSel(NULL, ename, spt);
if (retCode != RTNORM) {
acutPrintf("\nNo object selected.\n");
return;}
acdbGetObjectId(eId, ename);
- Get a record iterator
and initialize it for writing. Using the iterator in a for loop,
traverse the records of the object selected in step 4, getting the
table associated with each record.
pODCont->GetObjectODRecordIterator (pIter);
pIter->Init (eId, AcMap::kOpenForWrite, Adesk::kFalse);
for (; pIter->IsDone() == Adesk::kFalse; pIter->Next())
{
pIter->GetRecord (record);
pODCont->GetODTable (pTable, record.ODTableName());
acutPrintf("\n*** OD Table %s.", record.ODTableName());
- Loop through the columns
of each record, and print the name of the column, which in this
example is either Code or Approved By.
for (int i = 0; i < record.Count(); i++)
{
AcMapODTableDefinition tableDef = pTable->Definition();
AcMapODColumnDefinition Column;
tableDef.GetColumn(Column, i);
acutPrintf("\n%-15s", Column.Name());
- Print the value at the
column of the record and get a pointer, colname, to the column.
AcMapValue &val = record.Value(i);
acutPrintf(" %s", ((const char *)val));
colname = Column.Name();
- Create an AcMapValue
variable and assign the value entered by the user to it. Then, assign
the AcMapValue variable to the value of the record using the AcMapODTableRecord::Value
function. Update the record using the record iterator.
if(!strcmp(colname, "Code")){
acedGetString(TRUE,"\nNew zoning code:", zoneCode);
AcMapValue v = zoneCode;
record.Value(i) = v;
pIter->UpdateRecord(record);
}
- Using the same paradigm
as step 9, get and update the record's Approved by column.
if(!strcmp(colname, "Approved")){
acedGetString(TRUE,"\nApproved by:", approveCode);
AcMapValue v = approveCode;
record.Value(i) = v;
pIter->UpdateRecord(record);
}
- End the for loop started
in step 7 and delete the pointer to the table created with AcMapODContainer::GetODTable.
End the for loop started in step 6 and delete the iterator created
in step 5.
}//end for
if (pTable) delete pTable;
} //end for
if (pIter) delete pIter;