Adding SBAtoms to SBGroupNode



  • 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


  • 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());
    }
    


  • 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.)



  • Thanks, this solves my problem!


Log in to reply
 

Looks like your connection to SAMSON Connect - Forum was lost, please wait while we try to reconnect.