OBJECT_TREE_CREATED_EVENT in Viewer v7 - autodesk-forge

"Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT" was working with viewer version 6, but when I upgrade viewer version to 7.*, its not working.
I have tried to handle "Autodesk.Viewing.GEOMETRY_LOADED_EVENT", and then call the model.getObjectTree() function, but I get the following error
{instanceTree: null, maxTreeDepth: 0, err: undefined}
How can I handle the object_tree_created event for viewer 7 in my code?

Obviously the model database (which contains object tree data) failed to load (and hence the object tree event was not fired and the object tree was not accessible) and it could have been for incorrect code or networking. Did you get other errors especially network interruptions in your browser's dev tools when loading the model? What is your code to load the model? If loading from Forge did you follow the migration guide and here to use loadDocumentNode?
Autodesk.Viewing.Initializer({ env: 'AutodeskProduction', getAccessToken}, () => {
const viewer = new Autodesk.Viewing.GuiViewer3D(container);
Autodesk.Viewing.Document.load(urn, viewerDocument =>{
var defaultModel = viewerDocument.getRoot().getDefaultGeometry();
viewer.loadDocumentNode(viewerDocument, defaultModel);

Related

Viewing Revit Drafting Views with the Forge API's

It seems to be possible to view and navigate Revit Drafting View(s) graphics using the Forge API's based on how the BIM 360 Document Management web browser interface does it with our own Revit (.rvt) model having published Drafting Views where it list the 2D Drafting Views (thumbnails) in the left panel and the actual detail of the selected Drafting View in the right viewer. We do have one of the more recent Forge API Viewer examples setup and working, and tried modifying some of its code, but is seems to be designed to only work with Models (.rvt) components in the left panel, and its not obvious where and what code needs to be modify to change it to list 2D sheets/views like the Document Manager does. We are having difficulty locating a Forge API example that shows how to do this using the Forge API's and would like to obtain a working example that illustrates how to do this using the Forge API's?
Tried changing the ViewingApplication.bubble.search to include role 2d type view
function onDocumentLoadSuccess(doc) {
// We could still make use of Document.getSubItemsWithProperties()
// However, when using a ViewingApplication, we have access to the
*bubble** attribute,
// which references the root node of a graph that wraps each object from the Manifest JSON.
//var viewables = viewerApp.bubble.search({ 'type': 'geometry' });
var viewables = viewerApp.bubble.search({ 'role': '2d', 'type': 'view' });
if (viewables.length === 0) {
console.error('Document contains no viewables.');
return;
}
The drafting view is kind of 2d role geometry, Therefore you can load it with the same way for the 2D view.
const rootItem = doc.getRoot();
const filter = { type: 'geometry', role: '2d' };
const viewables = rootItem.search( filter );
if( viewables.length === 0 ) {
return onLoadModelError( 'Document contains no viewables.' );
}
// Take the first viewable out as the loading target
const initialViewable = viewables[0];
const loadOptions = {
sharedPropertyDbPath: doc.getPropertyDbPath()
};
viewer.loadDocumentNode(doc, initialViewable.data, modelOptions).then(onItemLoadSuccess).catch(onItemLoadFail);
To show a list of views like the BIM360 Docs, you could load Autodesk.DocumentBrowser extension. It will show viewable items on it, and just click on it to switch. See below snapshot:

Unable to load tensorflowJS model in chrome extension

I am trying to load a trained keras model into web browser using tensorflowjs.
I was able to convert the keras model to tensorflowjs model but unable to load model in chrome extension.
My background.js code to load model
async function app() {
alert('Loading model..');
model = await loadModel("model.json");
alert('Sucessfully loaded model');
}
chrome.runtime.onInstalled.addListener(function(details) {
alert("extension loaded");
chrome.tabs.executeScript(null,
{file:"https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#1.0.0/dist/tf.min.js"});
app();
});
THe url "https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#1.0.0/dist/tf.min.js" is added in permissions key in manifest file.
When i try to laod the extension it fails givind message loadModel is not defined.
Any suggestions on fixing this issue?
loadModel is not defined.
1 - Make sure that the script is loaded in the background process of the tabs
2 - You need to use tf.loadModel() instead of loadModel()

Forge viewer version 6.3.4 doesn't show newly released document browser extension

I upgraded the forge viewer version of my solution to 6.* to utilize the latest released feature "Document browser extension" as it mentions here
This extension doesn't appear for me, please help.
I got it to work after some experimenting.
Here is my workflow in case you still need it.
First, initialize the viewer:
// initialize the viewer
Autodesk.Viewing.Initializer(adOptions, () => {
// when initialized, call loading function
this.loadDocument(encodedUrn);
});
Then, load your document in the function called above:
// load the document from the urn
Autodesk.Viewing.Document.load(
encodedUrn,
this.onDocumentLoadSuccess,
this.onDocumentLoadFailure,
);
In the success callback you can now do the following:
onDocumentLoadSuccess(doc) {
// get the geometries of the document
const geometries = doc.getRoot().search({ type: 'geometry' });
// Choose any of the available geometries
const initGeom = geometries[0];
// and prepare config for the viewer application
const config = {
extensions: ['Autodesk.DocumentBrowser'],
};
// create the viewer application and bind the reference of the viewerContainer to 'this.viewer'
this.viewer = new Autodesk.Viewing.Private.GuiViewer3D(
this.viewerContainer,
config,
);
// start the viewer
this.viewer.start();
// load a node in the fetched document
this.viewer.loadDocumentNode(doc.getRoot().lmvDocument, initGeom);
}
I hope this will make it work for you as well. What helped me was the reference to the loadDocumentNode function in this blog post.

Processing error after upgrade to Version 6.0

With the following function I am trying to load the model into the earlier initialized viewer.
viewer.loadModel("https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM/manifest/urn%3Aadsk.viewing%3Afs.file%3AdXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM%2Foutput%2FRai_04.3ds.svf")
Unfortuneately I get the following error for the function:
viewer3D.js:74844 Error while processing SVF: {"url":"https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM/manifest/urn%253Aadsk.viewing%253Afs.file%253AdXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM%252Foutput%252FRai_04.3ds.svf?domain=http%3A%2F%2Flocalhost%3A3002","httpStatus":400,"httpStatusText":"Bad Request","data":{"url":"https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM/manifest/urn%253Aadsk.viewing%253Afs.file%253AdXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6aW1wbGVuaWFfaW1kL1JhaV8wNC4zZHM%252Foutput%252FRai_04.3ds.svf?domain=http%3A%2F%2Flocalhost%3A3002"}}
This is the way I initialize my viewer:
function onInitialized() { //console.log("viewer inizialized");
var config = Autodesk.Viewing.createViewerConfig();
config.extensions.push('Autodesk.Viewing.ZoomWindow');
config.startOnInitialize = true;
config.theme = 'light-theme';
viewerApp = new Autodesk.Viewing.ViewingApplication('main-viewer');
viewerApp.registerViewer(viewerApp.k3D,Autodesk.Viewing.Private.GuiViewer3D ,config);
viewer = viewerApp.getViewer(config);
viewer.start();
If I use the version 4.1 of the viewer, the model can be loaded this way. Changing to 6.0 the above described error appears.
I would really appreaciate a hint to a solution of my problem!
Thanks a lot in advance!
Cheers,
Felix
With v6 and onwards, call wrapper method Viewer3D.load insteadof .loadModel so that the resource requests made to Forge endpoints can be formed properly:
viewer.load('https://developer.api.autodesk.com/derivativeservice/v2/derivatives/urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6c2IyMzMvd2Fycmlvci4zZHM/output/warrior.3ds.svf');
or simply:
viewer.start(svfUrl)
Code sample: https://jsfiddle.net/dukedhx/9qncbuLt

How to Retrieve Forge Viewer objectTree?

My goal is to highlight a room by adding new geometry to the viewer based on lines I have created in revit like they do here Link
but i can not figure out how to access those lines ids.
I know what they are in revit (element_id) but not how they are mapped as dbid.
Following this Blog Post
I want to access the objectTree in my extension to find out, but it always comes back as undefined.
var tree;
//old way - viewer is your viewer object - undefined
viewer.getObjectTree(function (objTree) {
tree = objTree;
});
//2.5 - undefined
var instanceTree = viewer.model.getData().instanceTree;
var rootId = this.rootId = instanceTree.getRootId();
//- undefined
var objectTree = viewer.getObjectTree();
Can anyone tell me if its still works for them I am using the v2 of the API for the rvt conversion to svf and 2.9 of the viewer3D.js
note I can see a list of dbid if I call this
var model = viewer.impl.model;
var data = model.getData();
var fragId2dbIdArray = data.fragments.fragId2dbId ;
but have no way of mapping back to the Revit element_id
As of version 2.9 this is still working. Here's my console:
Here's a couple of things you can try:
Is viewer undefined? Are you in the correct scope when grabbing the viewer?
The document have to be loaded before you can grab the instance tree. When the document is loaded, an event called Autodesk.Viewing.GEOMETRY_LOADED_EVENT will be fired, then you can start manipulating the instance tree.
Simply do this:
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
var instanceTree = viewer.model.getData().instanceTree;
});
For more structured code, follow this guide to add an extension.
There's a more detailed blog post on which event to listen for. It's still using the old way to get instance tree, though.
Shiya Luo was correct the viewer had not yet finished loading the geometry
in my extentions Load function I added two event listeners and made sure they both fired before trying to access the instanceTree
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
finishedGEOMETRY_LOADED_EVENT = true;
if(finishedGEOMETRY_LOADED_EVENT && finishedOBJECT_TREE_CREATED_EVENT ){
afterModelLoadEvents(viewer);
}
});
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {
finishedOBJECT_TREE_CREATED_EVENT = true;
if(finishedGEOMETRY_LOADED_EVENT && finishedOBJECT_TREE_CREATED_EVENT ){
afterModelLoadEvents(viewer);
}
});