// (C) Copyright 2008 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//
/////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AcmRevisionList.h"

void createRevList() {
  //create a descriptor object with default settings
  AcMgRevisionListDescriptor* pRevListDescriptor = pAcMgRevListMgr->NewDescriptor();  //AddRef() is called internally
  if ( !pRevListDescriptor) {
    return;
  }

  //set some settings in descriptor object which will be used to create the new revision list
  pRevListDescriptor->setPosition(AcGePoint3d(3,3,0));
  AcMgRevisionListAttribMap* pAttributesMap = NULL;
  pRevListDescriptor->Attributes(pAttributesMap);
  pAttributesMap->SetAt(REVLINE_ATTRIB_NR, _T(/*MSG0*/"1"));
  pAttributesMap->SetAt(REVLINE_ATTRIB_NAME, _T(/*MSG0*/"Autodesk"));
  pRevListDescriptor->setAttributes(pAttributesMap);

  //create the new revision list with specified settings
  AcDbObjectId revListId = pAcMgRevListMgr->CreateRevisionList(pRevListDescriptor);

  //release the descriptor object
  pAttributesMap->Release();
  pRevListDescriptor->Release();
}

void deleteRevList() {
  //retrieve the revision list collection object 
  AcMgRevisionLists* pRevListArray = NULL;
  if (!pAcMgRevListMgr->GetRevisionLists(pRevListArray)) {  //AddRef() is called internally
    return;
  }

  //return when no revision list
  if (!pRevListArray->Count()) {
    pRevListArray->Release();
    return;
  }

  //delete the first revision list in the collection object.
  AcDbObjectId revListId;
  AcMgRevisionList* pRevList = pRevListArray->item(0);  //AddRef() is called internally
  pRevList->RevListId(revListId);
  if (revListId.database() == acdbHostApplicationServices()->workingDatabase()) {
    pAcMgRevListMgr->DeleteRevisionList(revListId);    
  }

  //decrease the ref count
  pRevList->Release();
  pRevListArray->Release();
}

void modifyRevLine() {
  //retrieve the revision list collection object 
  AcMgRevisionLists* pRevListArray = NULL;
  if (!pAcMgRevListMgr->GetRevisionLists(pRevListArray)) {  //AddRef() is called internally
    return;
  }

  //return when no revision list
  if (!pRevListArray->Count()) {
    pRevListArray->Release();
    return;
  }

  //retrieve the first revision list object in the collection object
  AcMgRevisionList* pRevList = pRevListArray->item(0);  //AddRef() is called internally
  if (!pRevList->totalItem()) {
    pRevList->Release();
    pRevListArray->Release();
    return;
  }
  
  //choose the first item(revision line)
  AcMgRevisionListAttribMap* pAttributeMap = NULL;
  pRevList->item(0,pAttributeMap);

  //change the user name attribute
  pAttributeMap->SetAt(REVLINE_ATTRIB_NAME, _T(/*MSG0*/"Your Name"));

  //apply the change
  pRevList->set(0,pAttributeMap);

  //release the objects
  pAttributeMap->Release();
  pRevList->Release();
  pRevListArray->Release();
}

void addRevLine() {
  //retrieve the revision list collection object 
  AcMgRevisionLists* pRevListArray = NULL;
  if (!pAcMgRevListMgr->GetRevisionLists(pRevListArray)) {  //AddRef() is called internally
    return;
  }

  //return when no revision list
  if (!pRevListArray->Count()) {
    pRevListArray->Release();
    return;
  }

  //retrieve the first revision list object in the collection object
  AcMgRevisionList* pRevList = pRevListArray->item(0);  //AddRef() is called internally
  int iItemCount = pRevList->totalItem();
  if (!iItemCount) {
    pRevList->Release();
    pRevListArray->Release();
    return;
  }

  //choose the last item(revision line) of this revision list.
  AcMgRevisionListAttribMap* pAttributeMap = NULL;
  pRevList->item(iItemCount-1,pAttributeMap);

  //add a new item 
  AcString strNumber;
  pAttributeMap->GetAt(REVLINE_ATTRIB_NR, strNumber);
  strNumber.format(_T("%d"), _ttoi(strNumber.kACharPtr())+1);
  pAttributeMap->SetAt(REVLINE_ATTRIB_NR, strNumber);
  pRevList->add(pAttributeMap);

  //release the objects
  pAttributeMap->Release();
  pRevList->Release();
  pRevListArray->Release();
}