如何使用纯Javascript Autodesk

时间:2018-06-12 11:11:05

标签: javascript autodesk-forge autodesk-viewer autodesk autodesk-data-management

我正在尝试使用纯Javascript在离线模式下在查看器中显示2d文件。我已经使用https://extract.autodesk.io/上传并提取了dwg。 提取的文件包含许多json.gz文件和一个文件夹。在此文件夹中,它具有清单,元数据(json.gz文件)和一个.f2d文件

我已将此文件位置提供给我的查看器选项

var docs = [{ "path": "./{foldername}/primaryGraphics.f2d", "name": "2D view" }];
var options = { 'docid': docs[0].path, env: 'Local' };

我的观看者初始化是

viewer = new Autodesk.Viewing.Private.GuiViewer3D(document.getElementById('MyViewerDiv'), {}); 
Autodesk.Viewing.Initializer(options, function () {
        viewer.initialize();
        viewer.loadModel(options.docid);
});

它在查看器中显示错误消息"我们无法显示您要查找的项目。它可能尚未处理...."并给我错误代码为5(指定类型无效)。

请帮忙。

1 个答案:

答案 0 :(得分:1)

请确保您已完全下载DWG的所有已提取的可查看气泡,并且您要加载的模型的路径是正确的,因为错误代码5代表NETWORK_FILE_NOT_FOUND

我刚从blocks_and_tables_-_metric.dwg使用以下代码段对此AutoCAD Sample Files进行了测试,效果正常。

var options = {
    env: 'Local',
};

var doc = { 'rootFolder': 'Model', 'path': '29c9e407-f76f-a1c0-0972-dcb5b496fff9_f2d/primaryGraphics.f2d', 'name': '2D view' };

var viewerDiv = document.getElementById( 'MyViewerDiv' );
var viewer = new Autodesk.Viewing.Private.GuiViewer3D( viewerDiv );


Autodesk.Viewing.Initializer(options, function() {
    if( viewer.initialize() != 0 ) return console.error( 'Failed to initialize viewer' );

    var basePath = getCurrentBaseURL();
    var modelFolderPath = basePath + doc.rootFolder + '/';
    var modelFilePath = modelFolderPath + doc.path;
    var modelOptions = {
        sharedPropertyDbPath: modelFolderPath
    };

    viewer.loadModel( modelFilePath, modelOptions, onLoadModelSuccess, onLoadModelError );
});

function getCurrentBaseURL() {
    var basePath = '';
    var lastSlash = document.location.href.lastIndexOf( '/' );

    if( lastSlash != -1 )
        basePath = document.location.href.substr( 0, lastSlash + 1 );

    return basePath;
}

/**
 * viewer.loadModel() success callback.
 * Invoked after the model's SVF has been initially loaded.
 * It may trigger before any geometry has been downloaded and displayed on-screen.
 */
function onLoadModelSuccess( model ) {
    console.log( 'onLoadModelSuccess()!' );
    console.log( 'Validate model loaded: ' + ( viewer.model === model ) );
    console.log( model );
}

/**
 * viewer.loadModel() failure callback.
 * Invoked when there's an error fetching the SVF file.
 */
function onLoadModelError( viewerErrorCode ) {
    console.error( 'onLoadModelError() - errorCode:' + viewerErrorCode );
}

提取的blocks_and_tables_-_metric.dwg模型的文件结构如下所示: enter image description here

我使用的2D模型的文件结构是: enter image description here