Attaching Object Data
The following sample attaches object data to selected objects in a drawing.
It creates a record for each of the objects and adds the data from an existing table to the records. Open rather than attach the drawing that contains the objects.
- Include the necessary
header files:
#include "StdAfx.h"
#include "StdArx.h"
#include <MapArxApi.h>
#include <MapProj.h>
#include <MapODRecord.h>
#include <MapODTable.h>
- Declare variables.
ads_name ss, ename;
long sslen, sscur;
AcDbObjectId eId;
AcMapSession *mapApi = NULL;
AcMapProject *pProj = NULL;
AcMapODContainer *pODCont = NULL;
AcMapODTable *pODTable = NULL;
- Create an AutoCAD Map 3D
session and get the top level objects.
mapApi = AcMapGetSession();
mapApi->GetProject(pProj);
pProj->GetODContainer(pODCont);
- Get the table containing
the data to attach.
pODCont->GetODTable(pODTable, "Zones");
- Use ADS functions to
prompt user to select the objects to which to attach the data, and
process the selections.
acutPrintf("\nAttach data to which object(s)?");
if (acedSSGet(NULL, NULL, NULL, NULL, ss) != RTNORM) {
acutPrintf("\nNo objects selected.\n");
acedSSFree(ss);
return;
}
- Use an ADS function to
determine the number of objects selected in step 5.
acedSSLength(ss, &sslen);
- Loop through the objects
selected in step 5, getting the entity name of the next object,
and converting it to an object ID, which you pass to AcMapODTable::AddRecord.
Within the for loop, you create the AcMapODTableRecord using the
new operator, thereby allocating memory for each record added. Remember
to delete each pointer to AcMapODTableRecord within the loop, as
shown here.
for (sscur = 0; sscur < sslen; sscur++)
{
acedSSName(ss, sscur, ename);
acdbGetObjectId(eId, ename);
AcMapODTableRecord *pTabRec = NULL;
pTabRec = new AcMapODTableRecord();
pODTable->AddRecord(*pTabRec, eId);
acutPrintf ("\nOD Record added to object.");
if (pTabRec) delete pTabRec;
}
- Delete the memory allocated
by calling AcMapODContainer::GetODTable in step 4, and free the
memory allocated for the selection set in step 5.
delete pODTable;
acedSSFree(ss);