Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call examples for dwg_dynapi_common_utf8text? #1030

Open
zenglanmu opened this issue Oct 12, 2024 · 1 comment
Open

call examples for dwg_dynapi_common_utf8text? #1030

zenglanmu opened this issue Oct 12, 2024 · 1 comment

Comments

@zenglanmu
Copy link

I am trying to build a "high-level" python wrapper for this library.
Similar to issuue #991, I was stuck in extracting utf8 string from Dwg_Obj.
For example, I have a Dwg_Layer obj, and want access its name field as utf8 string.
In the c code side, I called dwg_dynapi_common_utf8text function like following:

int isnew = 0;
char* textp;
bool result;
result = dwg_dynapi_common_utf8text(layer_obj, "name", &textp, &isnew, null);

but I got this error
LOG_ERROR ("%s: Invalid common text field %s", __FUNCTION__, fieldname);

Also using uppercase fieldname "NAME" is not work either. Is I missed something important or get utf8 name field is not supported for layer type obj?

@zenglanmu
Copy link
Author

zenglanmu commented Oct 12, 2024

Besides, following is the typemap for wrapper those dynapi utf8 functions which added in dwg.i file , if anyone is interested,

%include "typemaps.i"  // common type mappings
// for support function like dwg_dynapi_entity_utf8text, which have char **restrict textp, int *isnewp as argout
%typemap(in,numinputs=0) (char **restrict textp, int *) (char* tmp1, int tmp2) %{
  $1 = &tmp1;
  $2 = &tmp2;
%}

%typemap(argout) (char **restrict textp, int *) (PyObject* py_textp, PyObject* py_int) %{
  // assuming the func return variable generate by swig is called 'result'
  if(result && *$1){
    py_textp = PyUnicode_FromString(*$1);
  }else{
    py_textp = Py_None;
    Py_INCREF(Py_None);
  }
  py_int = PyInt_FromLong(*$2);
  // we dont care isnew result, only want get string result
  $result = SWIG_Python_AppendOutput($result,py_textp);
%}

%typemap(freearg) (char **restrict textp, int *) %{
  // only free if isnew
  if (*$1 && *$2){
    free (*$1);
  }              
%}
// end of support function like dwg_dynapi_entity_utf8text

Unlike issue #991, it seems the free memory problem had been overcomed by mapping textp and isnew together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant