ObjectARX
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[ACAD2013] Save and retrieve variables in DWG

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
gatti.massimo
1155 Views, 8 Replies

[ACAD2013] Save and retrieve variables in DWG

Hi,

I need to save and retrieve some variables from a dwg file.(int, float,strings)

 

I know there are lots of way to store data in autocad and the main way are xdata and dictionaries.

 

To attach my data to the autocad entities I use dictionaries, but now I need to store data at dwg level for data related to the drawing.

 

I think I should create a fake autocad entity with a dictionary attacched to it and put that entity in the drawing in a programmatic way, but the user may remove it, I don't know.

 

I'm looking for an easy way to store persistent data in the dwg.

 

Thank you for your time.

 

Have a good day!

8 REPLIES 8
Message 2 of 9

What about XRecord and Named Object Dictionary? Named Object Dictionary allow to have data dwg level:

 

17-01-2014 12-40-32.png

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 9

Hi,

 

it could be good!

 

I have just to persist the scale factor! as your example!

 

Where can I find a piece of code to save and retrieve those data?

 

Thanks for your help!

 

Massimo

Message 4 of 9

ObjectARX SDK:  \samples\database\xrecord_dg

createXrecord() and listXrecord() functions

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 5 of 9
dgorsman
in reply to: gatti.massimo

Its exactly the same as working with Dictionaries and XRecords attached to drawing entities, except you are adding them to the Named Object Dictionary.  I'd recommend encapulating your content in a dedicated dictionary to make management easier.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 6 of 9

You can use this code as a example:

 

//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
 
//-----------------------------------------------------------------------------
#define szRDS _RXST("")
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CDWGVariablesApp : public AcRxArxApp {
public:
  CDWGVariablesApp () : AcRxArxApp () {}
  virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
    AcRx::AppRetCode retCode = AcRxArxApp::On_kInitAppMsg (pkt) ;
    return (retCode) ;
  }
 
  virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
    AcRx::AppRetCode retCode = AcRxArxApp::On_kUnloadAppMsg (pkt) ;
    return (retCode) ;
  }
 
  virtual void RegisterServerComponents () {
  }
 
  static AcDbObjectId GetOrCreateVariableXrecordId(ACHAR *appNameACHAR *varNameAcDbDatabase *db = NULL)
  {
    AcDbObjectId idXrecid;
    if (!dbdb = acdbCurDwg();
    AcDbObjectId idNOD = db->namedObjectsDictionaryId();
    AcDbObjectPointer<AcDbDictionarypNOD(idNOD,AcDb::kForRead);
    if (pNOD.openStatus() == Acad::eOk) {
      AcDbObjectPointer<AcDbDictionarypDict;
      AcDbObjectPointer<AcDbXrecordpXrec;
      if (pNOD->has(appName)) { 
        if (pNOD->getAt(appNameid) == Acad::eOk) {
           if (pDict.open(id,AcDb::kForWrite) == Acad::eOk) {
             if (pDict->has(varName)) {
               pDict->getAt(varNameidXrec);
             } else {
               pXrec.create(); pDict->setAt(varNamepXrecidXrec);
             }
           }
        }
      } else {
        pDict.create();
        if (pNOD->upgradeOpen() == Acad::eOk && pNOD->setAt(appNamepDictid) == Acad::eOk) {
          pXrec.create(); pDict->setAt(varNamepXrecidXrec);
        }
      }
    }
    return idXrec;
  }
 
  static Acad::ErrorStatus SetVariable(ACHAR *appNameACHAR *varNameint nAcDbDatabase *db = NULL)
  {
    AcDbObjectId idXrecord = GetOrCreateVariableXrecordId(appNamevarNamedb);
    if (idXrecord.isNull()) return Acad::eInvalidObjectId;
    AcDbObjectPointer<AcDbXrecordpXrec(idXrecord,AcDb::kForWrite);
    if (pXrec.openStatus() == Acad::eOk) {
      resbuf *rb = acutBuildList(AcDb::kDxfInt16n, 0);
      Acad::ErrorStatus es = pXrec->setFromRbChain(*rb);
      acutRelRb(rb);
      return es;
    } 
    return Acad::eNullHandle;
  }
  static Acad::ErrorStatus SetVariable(ACHAR *appNameACHAR *varNamedouble dAcDbDatabase *db = NULL)
  {
    AcDbObjectId idXrecord = GetOrCreateVariableXrecordId(appNamevarNamedb);
    if (idXrecord.isNull()) return Acad::eInvalidObjectId;
    AcDbObjectPointer<AcDbXrecordpXrec(idXrecord,AcDb::kForWrite);
    if (pXrec.openStatus() == Acad::eOk) {
      resbuf *rb = acutBuildList(AcDb::kDxfReald, 0);
      Acad::ErrorStatus es = pXrec->setFromRbChain(*rb);
      acutRelRb(rb);
      return es;
    }
    return Acad::eNullHandle;
  }
  static Acad::ErrorStatus SetVariable(ACHAR *appNameACHAR *varNameACHAR *strAcDbDatabase *db = NULL)
  {
    AcDbObjectId idXrecord = GetOrCreateVariableXrecordId(appNamevarNamedb);
    if (idXrecord.isNull()) return Acad::eInvalidObjectId;
    AcDbObjectPointer<AcDbXrecordpXrec(idXrecord,AcDb::kForWrite);
    if (pXrec.openStatus() == Acad::eOk) {
      resbuf *rb = acutBuildList(AcDb::kDxfTextstr, 0);
      Acad::ErrorStatus es = pXrec->setFromRbChain(*rb);
      acutRelRb(rb);
      return es;
    }
    return Acad::eNullHandle;
  }
 
  static resbuf *GetVariable(ACHAR *appNameACHAR *varNameAcDbDatabase *db = NULL)
  {
    resbuf *rb = NULL;
    AcDbObjectId idXrecord = GetOrCreateVariableXrecordId(appNamevarNamedb);
    if (idXrecord.isNull()) return rb;
    AcDbObjectPointer<AcDbXrecordpXrec(idXrecord,AcDb::kForWrite);
    if (pXrec.openStatus() == Acad::eOk) {
      pXrec->rbChain(&rb);
    }
    return rb;
  }
 
  //////////////////////////////////////////////////////////////////////////
  //  Lisp Function:
  //    (setdwgvariable "appname" "varname" varvalue)
  //  For example:
  //    (setdwgvariable "MassimoVars" "ScaleFactor" 1.0)
  //////////////////////////////////////////////////////////////////////////
  static int ads_setdwgvariable () {
    ACHAR *appName = NULL, *varName = NULL;
    resbuf *args = acedGetArgs (), *rb = args;
    if (rb && rb->restype == RTSTR) {
      appName = rb->resval.rstring;
      rb = rb->rbnext;
    } else {
      acutPrintf(_T("\nsetdwgvariable: invalid first argument 'appName'"));
      return RSRSLT ;
    }
 
    if (rb && rb->restype == RTSTR) {
      varName = rb->resval.rstring;
      rb = rb->rbnext;
    } else {
      acutPrintf(_T("\nsetdwgvariable: invalid second argument 'varName'"));
      return RSRSLT ;
    }
 
    if (rb) {
      switch (rb->restype)
      {
      case RTSHORT:
        SetVariable(appName,varName,rb->resval.rint);
        break;
      case RTREAL:
        SetVariable(appName,varName,rb->resval.rreal);
        break;
      case RTSTR:
        SetVariable(appName,varName,rb->resval.rstring);
        break;
      }
    } else {
      acutPrintf(_T("\nsetdwgvariable: invalid third argument 'varValue'"));
      return RSRSLT ;
    }
    return RSRSLT ;
  }
 
  //////////////////////////////////////////////////////////////////////////
  //  Lisp Function:
  //    (getdwgvariable "appname" "varname")
  //  For example:
  //    (getdwgvariable "MassimoVars" "ScaleFactor")
  //////////////////////////////////////////////////////////////////////////
  static int ads_getdwgvariable () {
    ACHAR *appName = NULL, *varName = NULL;
    resbuf *args = acedGetArgs (), *rb = args;
    if (rb && rb->restype == RTSTR) {
      appName = rb->resval.rstring;
      rb = rb->rbnext;
    } else {
      acutPrintf(_T("\nsetdwgvariable: invalid first argument 'appName'"));
      return RSRSLT ;
    }
 
    if (rb && rb->restype == RTSTR) {
      varName = rb->resval.rstring;
      rb = rb->rbnext;
    } else {
      acutPrintf(_T("\nsetdwgvariable: invalid second argument 'varName'"));
      return RSRSLT ;
    }
 
    rb = GetVariable(appName,varName);
 
    if (rb) {
      switch (rb->restype)
      {
      case AcDb::kDxfInt16:
        acedRetInt(rb->resval.rint);
        break;
      case AcDb::kDxfReal:
        acedRetReal(rb->resval.rreal);
        break;
      case AcDb::kDxfText:
        acedRetStr(rb->resval.rstring);
        break;
      }
    } else {
      acutPrintf(_T("\nsetdwgvariable: no saved variable with name '%s'"), varName);
      return RSRSLT ;
    }
    return RSRSLT ;
  }
  
} ;
 
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CDWGVariablesApp)
ACED_ADSSYMBOL_ENTRY_AUTO(CDWGVariablesApp, setdwgvariable, true)
ACED_ADSSYMBOL_ENTRY_AUTO(CDWGVariablesApp, getdwgvariable, true)
 

 

 

Results:

 

2014-01-19_4-12-47.png

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 7 of 9
mjwelch
in reply to: Alexander.Rivilis

I am curious as to how this dialog with the symbol tables, dictionaries, and database tabs was generated?  Is is an AutoCAD command?

 

Thanks, Mark

Message 8 of 9
owenwengerd
in reply to: mjwelch

That is the ArxDbg ObjectARX SDK sample.


mjwelch wrote:

I am curious as to how this dialog with the symbol tables, dictionaries, and database tabs was generated?  Is is an AutoCAD command?

 

Thanks, Mark




--
Owen Wengerd
ManuSoft
Message 9 of 9

is it Possible to save some XML file as string......

 

I try creating APP and Variable and try to save a small string...

but I go the below AutoCAD message..

.acmess.PNG

 

Please help me to get this....

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost