Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    R16 iterators [SOLVED]

    SDK Help
    0
    10
    744
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      On 21/10/2014 at 08:49, xxxxxxxx wrote:

      User Information:
      Cinema 4D Version:   R16 
      Platform:   Windows  ;   
      Language(s) :     C++  ;

      ---------
      Hi,

      I'm having trouble with R16 iterators.
      I must first confess that I don't use iterators in C++ because I think it's very ugly looking code. But I would like to force myself to use them more often.

      This code yells at me that <BaseArray<Int> is not a template.
      I do have #include "c4d_misc.h" in my code. Do I need to write my own template?
      How do I make this work?

          maxon::BaseArray<Int> myarr;  
        
        for (maxon::AutoIterator<BaseArray<Int> > it(myarr); it; ++it)  
        {  
            //... do something with *it or it->  
        }
      

      Can someone also please post an example how to use an R16 iterator to iterate the objects in the hierarchy?

      Thanks,
      -ScottA

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 21/10/2014 at 08:54, xxxxxxxx wrote:

        It's similar to the STD iterators.

        for (maxon::BaseArray<Int>::Iterator it = myarr.Begin(); it != myarr.End(); ++it)
        {
            // ...
        }
        

        I prefer to use auto for iterators.

        for (auto it = myarr.Begin(); it != myarr.End(); ++it)
        {
            // ...
        }
        

        There's also a FOREACH macro in the SDK. I think you use it like this

        FOREACH (it, myarr)
        {
            // ...
        }
        
        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 21/10/2014 at 09:59, xxxxxxxx wrote:

          Thanks Niklas.
          This is what I have working so far:

              //Create an array and fill it with some data  
            maxon::BaseArray<Int> myarr;      
            for(Int32 i = 0; i < 10; i++){ myarr.Append(i); }  
            
            //Iterate through the array using an iterator  
            //The iterator 'it' is the value of each array element  
            //for(maxon::BaseArray<Int>::Iterator it = myarr.Begin(); it != myarr.End(); ++it)  
            for(auto it = myarr.Begin(); it != myarr.End(); ++it)  
            {  
                auto value = *it;      
                GePrint(String::IntToString(value));  
            
                if (*it == 5) { GePrint("found value of 5"); break; }          
            }  
            
            FOREACH(it, myarr)  
            {  
                GePrint("ForEach macro results: " + String::IntToString(*it));  
            }
          

          Now I just need to figure out how to iterate objects.

          -ScottA

          P.S. @ MAXON: Please document these things better with examples like the one I just posted

          1 Reply Last reply Reply Quote 0
          • H
            Helper
            last edited by

            On 21/10/2014 at 10:09, xxxxxxxx wrote:

            What do you mean with "how to iterate objects"?

            1 Reply Last reply Reply Quote 0
            • H
              Helper
              last edited by

              On 21/10/2014 at 10:38, xxxxxxxx wrote:

              Something like this:

                
                    FOREACH(obj, MyAtomArray)  
                    {  
                        GePrint(obj->GetName());  
                    }  
              

              I know how to do this with an AtomArray and a standard for() loop.
              But I'd like to use the new FOREACH and/or iterators instead.

              -ScottA

              1 Reply Last reply Reply Quote 0
              • H
                Helper
                last edited by

                On 21/10/2014 at 12:04, xxxxxxxx wrote:

                The AtomArray has no built-in iterator capabilities. You have to use GetIndex().

                1 Reply Last reply Reply Quote 0
                • H
                  Helper
                  last edited by

                  On 21/10/2014 at 12:11, xxxxxxxx wrote:

                  Well. That's just an example.
                  Is there no way to use the new iterators for iterating object hierarchies?

                  -ScottA

                  1 Reply Last reply Reply Quote 0
                  • H
                    Helper
                    last edited by

                    On 21/10/2014 at 12:13, xxxxxxxx wrote:

                    Sorry, forget this post. I wasn't aware you were already a few steps further... 😞

                    Hi Scott,

                    I'm not quite sure, as you seemed to already have it. Here's your example with PolygonObject:

                    maxon::BaseArray<PolygonObject*> polyarr;
                    for (Int32 i = 0; i < 10; i++) { polyarr.Append(PolygonObject::Alloc(8, 6)); }
                      
                    //Iterate through the array using an iterator
                    //The iterator 'it' is the value of each array element
                    for (auto it = polyarr.Begin(); it != polyarr.End(); ++it) {
                      auto pPObj = *it;
                      GePrint(String::IntToString(pPObj->GetPolygonCount()));
                      
                      if (pPObj->GetPolygonCount() == 6) { GePrint("polyarr found possible cube"); break; }
                    }
                      
                    FOREACH(it, polyarr)
                    {
                      GePrint("polyarr ForEach macro results: " + String::IntToString((*it)->GetPolygonCount()));
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • H
                      Helper
                      last edited by

                      On 21/10/2014 at 12:36, xxxxxxxx wrote:

                      Anyway, I'll add iterators on our ever growing todo list of documentation topics.

                      Regarding your question: I don't think so (Niklas may correct me, if I'm mistaken). In order to use the iterator, the class to iterate needs to have one defined. Like BaseArray[URL-REMOVED]


                      [URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.

                      1 Reply Last reply Reply Quote 0
                      • H
                        Helper
                        last edited by

                        On 24/10/2014 at 04:57, xxxxxxxx wrote:

                        That's correct. Only the new array classes support iterators, the old ones don't. That's one of the many advantages the new classes have, and one more reason to use them whenever possible.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post