[vtkusers] casting vtkVariant to vtkStringArray - how?

Tanaka Simon tanakas at gmx.ch
Wed Nov 5 05:45:44 EST 2014


for the records:

i found a way to extract the data from a vtkVariantArray:

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkStringArray.h>
#include <vtkVariantArray.h>
#include <string>

int main(int, char *[])
{
     unsigned int N = 10;

     vtkSmartPointer<vtkPoints> points =
             vtkSmartPointer<vtkPoints>::New();
     vtkSmartPointer<vtkStringArray> stringAttribute_extract =
             vtkSmartPointer<vtkStringArray>::New();
     vtkSmartPointer<vtkVariantArray> variantArray =
             vtkSmartPointer<vtkVariantArray>::New();

     variantArray->SetNumberOfComponents(1);
     variantArray->SetName("my_variant_array");

     // create some points:
     for (unsigned int k=0; k<N; ++k) {
         points->InsertNextPoint((double)k, 0.0, 0.0);
     }

     // create vtkStringArray's of different size for each point:
     for (unsigned int k=0; k<N; ++k) {
         vtkSmartPointer<vtkStringArray> stringAttribute =
                 vtkSmartPointer<vtkStringArray>::New();
         for (unsigned int i=0; i<k+1; ++i) {
             std::string s1;
             s1 = (char)(97+i);
             stringAttribute->InsertNextValue(s1.c_str());
         }
variantArray->InsertNextValue(vtkVariant(stringAttribute));
     }

     // show content:
     for (int i=0; i<variantArray->GetNumberOfValues(); ++i) {
         stringAttribute_extract = 
(vtkStringArray*)variantArray->GetValue(i).ToArray();

         for (int j=0; j<stringAttribute_extract->GetNumberOfValues(); 
++j) {
std::cout<<stringAttribute_extract->GetValue(j)<<" ";
         }
         std::cout<<"\n";
     }

     return EXIT_SUCCESS;
}


best wishes
simon

On 05.11.2014 00:48, Tanaka Simon wrote:
> Dear VTK users,
>
> I have:
>
> myVtkVariantArray->InsertNextValue( vtkVariant(myVtkStringArray) );
>
> how can i get back the myVtkStringArray? something like this does not 
> work:
>
> myVtkStringArray_extract = myVtkVariantArray->getValue(...)
>
>
>
> please have a look at the short example (attached).
>
> thanks!
> simon
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20141105/4bf80c98/attachment.html>


More information about the vtkusers mailing list