Ejemplo n.º 1
0
bool EditableModelItemImpl::loadModelFile(const std::string& filename)
{
    BodyPtr newBody;

    MessageView* mv = MessageView::instance();
    mv->beginStdioRedirect();

    bodyLoader.setMessageSink(mv->cout(true));
    newBody = bodyLoader.load(filename);

    mv->endStdioRedirect();
    
    if(newBody){
        newBody->initializeState();
        newBody->calcForwardKinematics();
        Link* link = newBody->rootLink();
        
        AbstractBodyLoaderPtr loader = bodyLoader.lastActualBodyLoader();
        VRMLBodyLoader* vloader = dynamic_cast<VRMLBodyLoader*>(loader.get());
        if (vloader) {
            // VRMLBodyLoader supports retriveOriginalNode function
            setLinkTree(link, vloader);
        } else {
            // Other loaders dont, so we wrap with inline node
            VRMLProtoInstance* proto = new VRMLProtoInstance(new VRMLProto(""));
            MFNode* children = new MFNode();
            VRMLInlinePtr inl = new VRMLInline();
            inl->urls.push_back(filename);
            children->push_back(inl);
            proto->fields["children"] = *children;
            // first, create joint item
            JointItemPtr item = new JointItem(link);
            item->originalNode = proto;
            self->addChildItem(item);
            ItemTreeView::instance()->checkItem(item, true);
            // next, create link item under the joint item
            LinkItemPtr litem = new LinkItem(link);
            litem->originalNode = proto;
            litem->setName("link");
            item->addChildItem(litem);
            ItemTreeView::instance()->checkItem(litem, true);
        }
        for (int i = 0; i < newBody->numDevices(); i++) {
            Device* dev = newBody->device(i);
            SensorItemPtr sitem = new SensorItem(dev);
            Item* parent = self->findItem<Item>(dev->link()->name());
            if (parent) {
                parent->addChildItem(sitem);
                ItemTreeView::instance()->checkItem(sitem, true);
            }
        }
    }

    return (newBody);
}
void H3DViewerPopupMenus::OnTreeViewAddChildNode( wxCommandEvent& event ) {
  wxTreeItemId id = treeview_dialog->TreeViewTree->GetSelection();
  
  H3DViewerTreeViewDialog::TreeIdMap::iterator ni = treeview_dialog->node_map.find( id.m_pItem );
  if( ni == treeview_dialog->node_map.end() ) {
    wxMessageBox( wxT("Selected tree item is not a node"),
                  wxT("Error"),
                  wxOK | wxICON_EXCLAMATION);
  }

  vector< wxString > node_fields;

  Node *selected_node = (*ni).second.get();
  H3DNodeDatabase *db = H3DNodeDatabase::lookupNodeInstance( selected_node );
  for( H3DNodeDatabase::FieldDBConstIterator i = db->fieldDBBegin();
       db->fieldDBEnd() != i; ++i ) {
    Field *f = i.getField( selected_node ); 
    if( SFNode *sfnode = dynamic_cast< SFNode * >( f ) ) {
      if( sfnode->getAccessType() == Field::INPUT_ONLY ||
          sfnode->getAccessType() == Field::INPUT_OUTPUT ) {
        node_fields.push_back( wxString(sfnode->getName().c_str(),wxConvUTF8) );
      } 
    } else if( MFNode *mfnode = dynamic_cast< MFNode * >( f ) ) {
      if( mfnode->getAccessType() == Field::INPUT_ONLY ||
          mfnode->getAccessType() == Field::INPUT_OUTPUT ) {
        node_fields.push_back( wxString(mfnode->getName().c_str(), wxConvUTF8) );
      }
    }
  }
  
  if( node_fields.empty() ) {
    wxMessageBox( wxT("Selected node does not have a SFNode or MFNode field."),
                  wxT("Error"),
                  wxOK | wxICON_EXCLAMATION);
    return;
  }

  string field_to_change;

  if( node_fields.size() > 1 ) {
    wxString *choices = new wxString[ node_fields.size() ];
    for( unsigned int i = 0; i < node_fields.size(); ++i ) {
      choices[i] = node_fields[i];
    }
    wxSingleChoiceDialog *choice_dialog = new wxSingleChoiceDialog ( this,
                                                                     wxT("Add/replace node in which field.."),
                                                                     wxT(""),
                                                                     node_fields.size(),
                                                                     choices );
    delete [] choices;

    if (choice_dialog->ShowModal() == wxID_OK) {
      field_to_change = std::string( node_fields[ choice_dialog->GetSelection() ].mb_str() );
    } else {
      return;
    }
  }

  wxTextEntryDialog *node_name_dialog = 
    new wxTextEntryDialog(this, 
                          wxT("Enter the name of the node type you want to use" ),
                          wxT("Add/replace node" ) );
  if (node_name_dialog->ShowModal() == wxID_OK) {
    Node *new_node = H3DNodeDatabase::createNode( std::string(node_name_dialog->GetValue().mb_str()) );
    if( !new_node ) {
      wxMessageBox( wxT("No such node type exists: " + node_name_dialog->GetValue()),
                    wxT("Error"),
                    wxOK | wxICON_EXCLAMATION);
    } else {
      Field *f = selected_node->getField( field_to_change );
      SFNode *sfnode = dynamic_cast< SFNode * >( f );
      MFNode *mfnode = dynamic_cast< MFNode * >( f );
       
      if( sfnode ) {
        AutoRef< Node > old_node( sfnode->getValue() );
        try { 
          sfnode->setValue( new_node );
        } catch (...) {
          sfnode->setValue( old_node.get() );
          wxMessageBox( wxT("Invalid node type for field"),
                        wxT("Error"),
                        wxOK | wxICON_EXCLAMATION);
        }
      } else if( mfnode ) {
        try { 
          mfnode->push_back( new_node );
        } catch (...) {
          wxMessageBox( wxT("Invalid node type for field"),
                        wxT("Error"),
                        wxOK | wxICON_EXCLAMATION);
        }
      }
    }
  } 
}