Sample Annotation Object Creation

The code sample below implements many of the significant functions which can be used for annotation scaling. For further information, please check the ObjectARX Reference Guide.

void CAnnotationApp::adskAnnotationScalingaddAnnoEnt()
{
 AcGePoint3d insPnt;
 // pick a point on screen for the object
 int res = acedGetPoint(NULL, _T("\nPick point for object : "), asDblArray(insPnt));
 // if ok
if (res == RTNORM)
  {
    Acad::ErrorStatus es;
    // lets carry out the process to try and create a custom scale of 1:22
    // first, get the current drawing
    AcDbDatabase *dwg = curDoc()->database();
    
    // create my annotation scale object, may as well set it to the current scale in case my scale is already set
    AcDbAnnotationScale *myAnnotationScale = dwg->cannoscale();
    // also create one on the stack so we don't have to worry about deleting it later (addContext makes a copy of the passed pointer)
    AcDbAnnotationScale scale; 
    // what scale are we set too?
    AcDbAnnotationScale *curScale = dwg->cannoscale();
    AcString curScaleName;
    // get the name of the scale
    curScale->getName(curScaleName);
    // if we are not already set to my 1:22 scale then we need to add it
    if (_tcscmp(curScaleName, _T("MyScale 1:22")))
    {
      // next get the objectContextManager
      AcDbObjectContextManager *contextManager = dwg-
>objectContextManager();      // if ok
      if (contextManager)
      {
        // now get the Annotation Scaling context collection (named ACDB_ANNOTATIONSCALES_COLLECTION)
        AcDbObjectContextCollection* const contextCollection = contextManager->contextCollection(ACDB_ANNOTATIONSCALES_COLLECTION);
        // if ok
        if (contextCollection) 
        {
          // if it doesn't exist, then lets set it up
          myAnnotationScale = &scale;
          myAnnotationScale->setName(_T("MyScale 1:22"));
          myAnnotationScale->setPaperUnits(1);
          myAnnotationScale->setDrawingUnits(22);          
          // lets check to see if we already have this scale context in the context manager
          bool alreadyHasContext = contextCollection->hasContext(_T("MyScale 1:22"));
          // if not
          if (!alreadyHasContext)
          {
            // add the new context
            es = contextCollection->addContext(myAnnotationScale);
          }
          // now set the current dwg annotation scale to the newly created scale context 1:22, not a requirement
          es = dwg->setCannoscale(myAnnotationScale);
        }
      }
    }
    // next, create a new instance of my custom object, this is derived from AcDbText so that means the 
    // Annotation Context framework is already implemented for us
    AcDbObjectPointer<asdkMyAnnotativeObject> ent;
    ent.create();
    // set the properties for it
    ent->setDatabaseDefaults();
    ent->setPosition(insPnt);
    // set the AcDbText height, because we are derived from AcDbText
    // the Annotation framework is already in place
    ent->setHeight(1);
    ent->setTextString(_T("AcDbText Derived Annotative Text - Scales automatically"));
    // now set my own Text height, this will show how to utilise the Annotation
    // framework - see the worldDraw/viewportDraw of the custom entity
    ent->setMyTextHeight(1);
    ent->setMyTextString(_T("My Custom Object Text - code implemented to handle scaling Framework"));
    // now add to the current space, open it for write
    AcDbBlockTableRecordPointer curSpace(curDoc()->database()->currentSpaceId(), AcDb::kForWrite);
    // if ok
    if (curSpace.openStatus() == Acad::eOk)
      curSpace->appendAcDbEntity(ent);
    // now add to the entity
    AcDbAnnotativeObjectPE *annotationPE = ACRX_PE_PTR(ent, AcDbAnnotativeObjectPE);
    // if ok
    if (annotationPE)
    {
      // now get the Object Context PEX
      AcDbObjectContextInterface *objectContextInterface = ACRX_PE_PTR(ent, AcDbObjectContextInterface);
      // if ok
      if (objectContextInterface) 
      {
        // set it to be annotative
        es = annotationPE->setAnnotative(ent, true);
        // add My scale context to the list of contexts        es = objectContextInterface->addContext(ent, *myAnnotationScale);
      }
      // all done!
    }
  }
}