How to set a model active from a MDL application?

Hi, There,

There seems to be only function to get the active model, i.g.  mdlModelRef_getActive. How can I set a model active?

Any suggestion will be greatly appreciated.

Ken

  • Hi, Michael,

    Thanks for your reply. I found this function in the document. It took me quite a while to figure out how to do it. The following is my code to set a model of a given name active:

    Public int activateModel

    (

       char *modelName  // the name of the model

    )

    {

       char            cellName[MAX_CELLNAME_LENGTH];

       char            cellDscr[MAX_CELLNAME_LENGTH];

       MSWChar         mdlName[MAX_CELLNAME_LENGTH];

       int             rs = SUCCESS;

       DgnIndexIteratorP dgnIdxIterP;

       DgnIndexItemP dgnIdxItemP;

       DgnModelRefP            modelRef;

       ModelID                 iModelID;

       DgnFileObjP     fObjP = mdlDgnFileObj_getMasterFile();

       /* Creates an iterator object that is used to process all models in the DGN file */

       dgnIdxIterP = mdlModelIterator_create(fObjP);

       // Create a model iterator: each cell is a model in a cell lib

    dgnIdxIterP = mdlModelIterator_create (fObjP);

    mdlModelIterator_setAcceptCellsOnly (dgnIdxIterP, TRUE);

    /* Advances the iterator object through each model */

    dgnIdxItemP = mdlModelIterator_getNext (dgnIdxIterP);

    while (dgnIdxItemP)

       {   // Get model name, i.e. the cell name.

    mdlModelItem_getName (dgnIdxItemP, mdlName, MAX_CELLNAME_LENGTH);

    mdlCnv_convertUnicodeToMultibyte (mdlName, -1, cellName, MAX_CELLNAME_LENGTH);

    strpspc (cellName);

           if ( 0 == strcmp ( modelName, cellName ))

           {   // found the model

               iModelID = mdlModelItem_getModelID(dgnIdxItemP);

               mdlModelRef_createWorking (&modelRef, fObjP,  iModelID, TRUE, TRUE);

               if ( SUCCESS != mdlModelRef_activateAndDisplay (modelRef) )

                   rs = ERROR;

               else

                   break;

           }

    dgnIdxItemP = mdlModelIterator_getNext (dgnIdxIterP);

       }

       return rs;

    }   // end of activateModel()

    Any better suggestion?

    Ken

  • You don't need to iterate the DGN file …

    /** --------------------------------------------------------------------+
    |
    | function name: models_activate |
    |
    | description: Activate the named model |
    |
    | returns: TRUE on success |
    |
    +--------------------------------------------------------------------- */

    BoolInt    models_activate
    (
    LPCWSTR    modelName  //  => Activate this model
    )
    {
      BoolInt  activated  = FALSE;
      MSWChar  activeName  [MAX_MODEL_NAME_LENGTH];
      mdlModelRef_getModelName (ACTIVEMODEL, activeName);
      if (0 == wcscmp ((MSWChar*)modelName, activeName))
      {
        activated = TRUE;
      }
      else
      {
        DgnFileObjP dgnFileObj = mdlModelRef_getDgnFile (ACTIVEMODEL);
        DgnModelRefP modelRef = NULL;
        if (SUCCESS == mdlModelRef_createWorkingByName (&modelRef, dgnFileObj, modelName, TRUE, FALSE)
          &&
          SUCCESS == mdlModelRef_activateAndDisplay (modelRef))
        {
          // The following test is implemented in an unpublished function. It's
    // included because of uncertainty about the lifetime of a modelRef in
    // some earlier versions of MicroStation
          //ASSERT (mdlModelRef_modelsAreSame (modelRef, ACTIVEMODEL));

          ASSERT (models_areSame (modelRef, ACTIVEMODEL));
          activated = models_areSame (modelRef, ACTIVEMODEL);
        }
        FREE_WORKINGMODEL (modelRef);
      }
    return activated;
    }

     
    Regards, Jon Summers
    LA Solutions

  • and just in case you will need the model from a different file, those 4 lines are the 'easy' way, if you don't need the modelref for anything else

    sprintf(command, "newfile \"%s\"", dgnName);

    mdlInput_sendKeyin(command, TRUE, INPUTQ_EOQ, NULL);

    sprintf(command, "model active \"%s\"", modelName);

    mdlInput_sendKeyin(command, TRUE, INPUTQ_EOQ, NULL);

    HTH Michael