SAMSON Forum
    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • SAMSON Connect
    • Get SAMSON

    Solved Adding SBAtoms to SBGroupNode

    Modeling
    3
    4
    2809
    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
      Haichao last edited by

      Dear all,

      I am trying to create a group of atoms that are distributed on several chains into a one SBNodeGroup.
      In the following code I added every atom of my document into a SBNodeGroup, however it doesn't work as I cannot "Select group nodes" in the context menu and groupNodesSize is always zero. Am I missing something here?

       SAMSON::beginHolding("adding myModeGroup");
      
        SBPointer<SBNodeGroup> nodeGroup = new SBNodeGroup();
        nodeGroup->setName("myNodeGroup");
      
        SBNodeIndexer allNodes;
        SAMSON::getActiveDocument()->getNodes(allNodes);
        nodeGroup->create();
      
        SB_FOR(SBNode* node, allNodes) {
          if (node->getType() == SBNode::Atom) {
            nodeGroup->addChild(node);
          }
        }
        
        auto groupNodes = nodeGroup->getGroupNodes();;
        
        int allNodesSize = allNodes.size();
        int groupNodesSize = groupNodes->size();
        
        SAMSON::getActiveDocument()->addChild(nodeGroup());
      
        SAMSON::endHolding();```
      
      Thank you,
      Haichao
      1 Reply Last reply Reply Quote 0
      • DmitriyMarin
        DmitriyMarin last edited by DmitriyMarin

        Hi, @Haichao

        You can actually do that using the SBNodeGroup constructor. It may look something like that:

        // get an indexer with all the atoms
        // you can request specific nodes in the getNodes function itself using the Node Specification Language
        SBNodeIndexer nodeIndexer;
        SAMSON::getNodes(nodeIndexer, "node.type atom"); 
        // or use getNodes function for any SBDataGraphNode you want with a predicate
        SAMSON::getActiveDocument()->getNodes(nodeIndexer, SBNode::IsType(SBNode::Atom));
        
        // create a node group based on the nodeIndexer
        SBPointer<SBNodeGroup> nodeGroup = new SBNodeGroup("myNodeGroup", nodeIndexer);
        nodeGroup->create();
        
        if (nodeGroup->isCreated()) {
          SAMSON::getActiveDocument()->addChild(nodeGroup());
        }
        

        Dmitriy,
        The SAMSON Team, https://s-c.io

        1 Reply Last reply Reply Quote 1
        • DmitriyMarin
          DmitriyMarin last edited by DmitriyMarin

          Hi, @Haichao

          You can actually do that using the SBNodeGroup constructor. It may look something like that:

          // get an indexer with all the atoms
          // you can request specific nodes in the getNodes function itself using the Node Specification Language
          SBNodeIndexer nodeIndexer;
          SAMSON::getNodes(nodeIndexer, "node.type atom"); 
          // or use getNodes function for any SBDataGraphNode you want with a predicate
          SAMSON::getActiveDocument()->getNodes(nodeIndexer, SBNode::IsType(SBNode::Atom));
          
          // create a node group based on the nodeIndexer
          SBPointer<SBNodeGroup> nodeGroup = new SBNodeGroup("myNodeGroup", nodeIndexer);
          nodeGroup->create();
          
          if (nodeGroup->isCreated()) {
            SAMSON::getActiveDocument()->addChild(nodeGroup());
          }
          

          Dmitriy,
          The SAMSON Team, https://s-c.io

          1 Reply Last reply Reply Quote 1
          • Stephane
            Stephane last edited by

            For some context: nodes in a group are not children of the group (a node can only have one parent), but only referenced by the group. As a result , the addChild and removeChild functions are not overloaded in SBNodeGroup, and they have the default behavior: they do nothing. The way to create groups is thus as @Dmitriy-Marin suggested :-).

            (This is similar to bonds, which only reference atoms, and are not parents of atoms.)

            1 Reply Last reply Reply Quote 1
            • H
              Haichao last edited by

              Thanks, this solves my problem!

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