NP CAD Page | Articles | Russian | Download

A.Rivilis. Migration Advice - AcString Type

This is a continuation of the theme: migration of ARX applications to AutoCAD 2007.
In ObjectARX there is a special class (data type) AcString declared in AcString.h of versions 2006 and 2007 (in ObjectARX 2005 or earlier there is no such a type). Text string declaration when using the class looks like the following one:

AcString str;

This data type is very much alike CString type (from the MFC library) and std::string (from the standard library, STL) but is different because it allows to operate both on strings containing char symbols and on string containing wchar_t symbols. The AcString class has constructors creating strings from char type and wchar_t type, e.g.:

AcString sa("String of char");
AcString sw(_T("String of wchar_t"));

With the help of a corresponding constructor one can create an empty string or a string initialized with a symbol of type char or of type wchar_t.
AcString type assists in conversion between char and wchar_t types, e.g.:

const char *pStrA1 = sa.kszPtr();

or

const char *pStrA2 = sa; // implicitely (const char *)sa; is called
const wchar_t *pStrW1 = sa.kwszPtr();
const wchar_t *pStrW2 = sa; // implicitely (const wchar_t *)sa; is called

Moreover the class has operators/functions for string concatenation, substring search and selection, getting string length, formatting, conversion to number and so on. Using AcString type allows to write the code that in overwhelming majority of cases will work in AutoCAD 2006 as well as in AutoCAD 2007 and next versions.
As an example, the following expression will work in both versions of AutoCAD:

acedPrompt (AcString("\nPrinting a string"));

You must be careful only while calling the function with a variable number of arguments (e.g. acutPrintf when number of arguments is greater than one, as well as acedCommand, acutBuildList) because compilator cannot understand what type is to be passed. In these cases it is more preferable to use methods AcString::kszPtr() and AcString::kwszPtr() for explicit clarification of data type. On basis of this class it is convenient to make macros for passing required type to functions, e.g.:

#ifndef INC_UNI_H
    #include <AcString.h>
#define INC_UNI_H
  #if defined(ACADR17)
 // Conversion from UNICODE to ANSI
    #define W2C(x) ((const char    *)AcString(x))
 // Conversion from ANSI to UNICODE
    #define C2W(x) ((const wchar_t *)AcString(x))
  #else
    #define W2C(x) (x)
    #define C2W(x) (x)
  #endif
#endif

If for preprocessor in ObjectARX 2006 project the ACADR16 identifier is set or in ObjectARX 2007 project the ACADR17 identifier is set then the lines

char *str1 = "\nPrinting a string <%s>";
char *str2 = "---my string---";
acutPrintf(C2W(str1), C2W(str2));

will be compiled rightly and will work in both versions of AutoCAD.
Similarly the following expression will be compiled without errors and will be normally executed:

acedCommand (RTSTR, C2W("_.LINE"), RTSTR, C2W("0,0"), RTSTR, C2W("10,10"), RTNONE);

(Translation: N.Poleshchuk)


NP CAD Page | Articles | Russian | Download