简体   繁体   中英

Autodesk Forge Viewer and React components

I am trying to embed a Forge Viewer in a React Component. There is an example on the GitHub but it is not quite working for me. It seems there are some missing elements. Is there a way we can get a step by step implementation of the viewer in React?

This is my only missing object at the moment. I have already fetched the model from a bucket, converted it into svf and fetched a urn to be passed to the viewer. Any help out there, possibly without using redux?

You can create a component and a helper:

Helper: viewer-helper.js

import axios from 'axios'
/* global Autodesk, THREE */
const url_base = 'http://localhost:4000/'
// Get token from server
const getToken = async () => {
const {data} = await axios.get(url_base + 'forge/auth');
return data
}

export const initializeViewer = async (urn) => {
const token = await getToken()

const viewerOptions = {
    env: 'AutodeskProduction',
    accessToken: token,
    api: 'derivativeV2',
};
var viewerContainer = document.getElementById('viewerContainer')
var viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerContainer, {})

Autodesk.Viewing.Initializer(viewerOptions, () => {
    viewer.start();
    Autodesk.Viewing.Document.load(`urn:${urn}`, (doc) =>{
        var defaultModel = doc.getRoot().getDefaultGeometry();
        viewer.loadDocumentNode(doc, defaultModel);
    })
  })
}

Component:

import React,{useEffect} from 'react'
import {initializeViewer} from './viewer-helper'

const Viewer = () => {
const urn ='dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6bWFsbGF2ZW50dXJhc2FuanVhbmRlbHVyaWdhbmNob19idWNrZXQvMTU3OTUyNjAwMzkwM19NYWxsJTIwQXZlbnR1cmElMjBTSkxfRXN0cnVjdHVyYXMucnZ0'
useEffect(() => {
    initializeViewer(urn)
}, [])

return (
    <div>
        <div id='viewerContainer'></div>
    </div>
)
}

export default Viewer

App.js

import React from 'react';
import './App.css';
import Viewer from './components/viewer/Viewer'

function App() {
  return (
    <div className="App">
      <Viewer/>
    </div>
  );
}

export default App;

请尝试 React Forge 查看器组件: https : //www.npmjs.com/package/react-forge-viewer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM