Author Topic: DRX Command Class  (Read 4213 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
DRX Command Class
« on: May 19, 2008, 08:44:45 AM »
Here is an example of a command class in DRX. One slick feature is that the execute method gets an instance of the working database to party on, It also passes along an instance of an IO class, so you can have ready access to GetXXX()  ,Prompt methods etc.. . Pretty much everything uses smart pointers so you donít have to worry cleaning up.  8-)

Code: [Select]
//++-- class CommandAllOn ---------------------------------------------
class CommandAllOn : public OdStaticRxObject < OdEdCommand >
{
public:
  const OdString localName() const { return globalName(); }
  const OdString groupName() const { return DD_T("DRXLayerIso"); }
  const OdString globalName() const { return OdString("AllOn"); }

  void execute(OdEdCommandContext* pCmdCtx)
  {
    {
      OdDbCommandContextPtr pDbCmdCtx(pCmdCtx);
      OdDbDatabasePtr pDb = pDbCmdCtx->database();

      OdDbObjectId LayerTableId = pDb->getLayerTableId();
      OdDbLayerTablePtr pLayerTable = LayerTableId.safeOpenObject();
      OdDbSymbolTableIteratorPtr pLayerTableIterator = pLayerTable->newIterator();

      for (pLayerTableIterator->start();
          !pLayerTableIterator->done(); 
           pLayerTableIterator->step())
      {

        OdDbLayerTableRecordPtr pTableRecord =
          pLayerTableIterator->getRecord(OdDb::kForWrite);

        if(pTableRecord->isOff())
          pTableRecord->setIsOff(false);

      }
    }
  }
};

Once you have your command class setup, just define it in your entry point class

Code: [Select]
#pragma region EntryPoint

//++-- This is the class that defines our DRX module for Bricscad
class DRXLayerIso : public OdRxModule
{
  //++-- instance of your command
  CommandLiso m_CommandLiso;
  CommandLuiso m_CommandLuiso;
  CommandAllOn m_CommandAllOn;
  CommandAllOff m_CommandAllOff;

//++--
protected:
  DRXLayerIso(){}

  //++--
  void initApp()
  {
    //++-- Reg command
    odedRegCmds()->addCommand(&m_CommandLiso);
    odedRegCmds()->addCommand(&m_CommandLuiso);
    odedRegCmds()->addCommand(&m_CommandAllOn);
    odedRegCmds()->addCommand(&m_CommandAllOff);
  }

  //++--
  void uninitApp()
  {
    //++-- unReg command
    odedRegCmds()->removeCmd(&m_CommandLiso);
    odedRegCmds()->removeCmd(&m_CommandLuiso);
    odedRegCmds()->removeCmd(&m_CommandAllOn);
    odedRegCmds()->removeCmd(&m_CommandAllOff);
  }

//++--
public:
  ~DRXLayerIso(){}
};

ODRX_DEFINE_DYNAMIC_MODULE(DRXLayerIso);

#pragma endregion
Retired

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
Re: DRX Command Class
« Reply #1 on: June 01, 2008, 12:17:46 PM »
Another example of a command class, and getting a selection set  using the OdDbUserIO class. 

Code: [Select]
//++-- class CommandLAYCUR -----------------------------------------------
class CommandLaycur : public OdStaticRxObject < OdEdCommand >
{
public:

  const OdString localName() const { return globalName(); }
  const OdString groupName() const { return DD_T("DRXLayerIso"); }
  const OdString globalName() const { return OdString("LAYCUR"); }

  //++--
  void execute(OdEdCommandContext* pCmdCtx)
  {
    OdDbCommandContextPtr pDbCmdCtx(pCmdCtx);
    OdDbUserIOPtr pDbIO = pDbCmdCtx->dbUserIO();
    OdDbDatabasePtr pDb = pDbCmdCtx->database();

    OdDbObjectId clayerId = pDb->getCLAYER();

    //++-- Get the selection set using the OdDbUserIO class
    //++-- you can iterate through the seletion set or
    //++-- use the objectIdArray() method
    //++-- note: the ids in the array are guaranteed to be valid 

    OdString message = "\nSelect object(s) to be changed to the current layer: ";
    OdDbSelectionSetPtr pSelectionSet = pDbIO->select(message);
    OdDbObjectIdArray objectIdArray = pSelectionSet->objectIdArray();

    for (unsigned int i = 0 ; i < objectIdArray.length() ; i++)
    {
      OdDbEntityPtr pEntity = objectIdArray[i].openObject(OdDb::kForWrite);
      pEntity->setLayer(clayerId);
    }

    pDbIO->putString(_T("\nDone!"));
  }
};


Retired

ahlzl

  • Guest
Re: DRX Command Class
« Reply #2 on: June 05, 2008, 11:22:30 PM »
great Daniel !

I download DrxSDK_2.04, but don't find DrxWizards.msi, How do I run your samples? Must I set VC2005 again?

thanks !!!

I'm stupid

VC2005 + ARX2009 + DrxSDK_2.04 + CAD2009
« Last Edit: June 05, 2008, 11:29:38 PM by ahlzl »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
Re: DRX Command Class
« Reply #3 on: June 05, 2008, 11:27:02 PM »
great Daniel !

I download DrxSDK_2.04, but find't DrxWizards.msi, How run your samples? I must again set VC2005?

thanks !!!

I'm stupid

VC2005 + ARX2009 + DrxSDK_2.04 + CAD2009

Iím using VS2005, DRXSDK_2.0.5 and Iíve been running the apps on Bricscad 8.2.XXX
I donít think Autodesk products support DRX at this time  :wink:
Retired

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
Re: DRX Command Class
« Reply #4 on: June 05, 2008, 11:36:31 PM »
Here is the beginnings of a template project, if you want to have a look
$(DRXINC)// drx include folder
$(DRXLIB)// drx VC8 lib folder
$(BRISDS) // Bricscad /API/SDS folder

Retired

ahlzl

  • Guest
Re: DRX Command Class
« Reply #5 on: June 06, 2008, 12:23:25 AM »
Daniel, thank you very much !

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7096
  • AKA Daniel
Re: DRX Command Class
« Reply #6 on: June 20, 2008, 12:33:38 AM »
DRX deepCloneObjects Example

Code: [Select]
class CommandCloneWars: public OdStaticRxObject < OdEdCommand >
{

private:
  OdDbUserIOPtr pDbIO;
  OdDbDatabasePtr pDb;

public:
  const OdString localName() const { return globalName(); }
  const OdString groupName() const { return DD_T("DRXGLOBAL"); }
  const OdString globalName() const { return OdString("DOIT"); }

  //++-- get our new selection set
  OdDbObjectIdArray SelectEntities()
  {
    OdString message = "\nSelect Object(s) to Clone: ";
    OdDbSelectionSetPtr pSelectionSet = pDbIO->select(message);
    return pSelectionSet->objectIdArray();
  }

  //++-- Clone entities
  OdDbObjectIdArray DeepClone(OdDbObjectIdArray ids)
  {
    OdDbObjectIdArray newIds;
    OdDbObjectId spaceId = pDb->getModelSpaceId();
    OdDbIdMappingPtr pIdMap = OdDbIdMapping::createObject();

    pDb->deepCloneObjects(ids,spaceId,*pIdMap);

    OdDbIdMappingIterPtr pMapIter = pIdMap->newIterator();
    for(pMapIter->start() ;!pMapIter->done(); pMapIter->next())
    {
      OdDbIdPair idPair;
      pMapIter->getMap(idPair);
      newIds.append(idPair.value());
    }
    return newIds;
  }

  //++-- doit
  void execute(OdEdCommandContext* pCmdCtx)
  {

    OdDbCommandContextPtr pDbCmdCtx(pCmdCtx);
    pDbIO = pDbCmdCtx->dbUserIO();
    pDb = pDbCmdCtx->database();

    OdDbObjectIdArray selectedIds = SelectEntities();
    OdDbObjectIdArray clonedIds = DeepClone(selectedIds);

    OdString basePointMessage = "\nEnter Base Point: ";
    OdGePoint3d basePoint = pDbIO->getPoint(basePointMessage);

    OdString PlopPointMessage = "\nEnter Second Point: ";
    OdGePoint3d plopPoint = pDbIO->getPoint(PlopPointMessage,OdEd::kGptRubberBand);

    OdDbObjectId spaceId = pDb->getModelSpaceId();

    OdGeMatrix3d matrix = OdGeMatrix3d::translation
      (plopPoint.asVector() - basePoint.asVector());

    for(unsigned int i = 0 ; i < clonedIds.length() ; i++)
    {
      OdDbObjectPtr pOb = clonedIds[i].openObject(OdDb::kForRead);

      if(pOb->isKindOf(OdDbEntity::desc()))
      {
        OdDbEntityPtr pEnt = pOb;

        if(pEnt->ownerId() == spaceId )
        {
          pEnt->upgradeOpen();
          pEnt->transformBy(matrix);
        }

      }
    }
  }
};
Retired