Viewing Revit Drafting Views with the Forge API's - autodesk-forge

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:

Related

How to open on a specific page a PDF File on Forge Viewer?

I need to open a PDF file from BIM 360 Docs on the Viewer selecting a specific page. I'm currently opening the PDF on the Viewer but I don't know how to select a page.
Adding on to my comment, you can get a list of all available viewables when using the onDocumentLoadSucces callback. This callback is triggered after initting the viewer and loading the first Urn(model) into the viewer. You can look into more functionality on bubble nodes in the documentation
Example in angular/typescript:
private onDocumentLoadSucces(viewerDocument: Autodesk.Viewing.Document) {
// Default viewable
let defaultModel = viewerDocument.getRoot().getDefaultGeometry();
// list of all viewables in this model
this.viewables = viewerDocument.getRoot().search({'type':'geometry'});
// We load in the default viewable here but we could choose to load any viewable
// in this.viewables
this.viewer.loadDocumentNode(viewerDocument, defaultModel);
console.log('model changed');
}
I assume you are using the PDF extension to directly load and view PDF. If so, you can tell it the page number in the loadModel call. For example:
viewer.loadModel( pdf, {page:2});
Here's the whole function:
function initializeViewer( pdf ) {
var options = {
env: "Local",
useADP: false
}
Autodesk.Viewing.Initializer(options, () => {
viewer = new Autodesk.Viewing.Private.GuiViewer3D(document.getElementById('viewer3D'));
viewer.setTheme("light-theme");
viewer.start();
if (!pdf) return;
viewer.loadExtension('Autodesk.PDF').then( () => {
viewer.loadModel( pdf, {page:2});
});
});
};
The docs mention this here:
https://forge.autodesk.com/en/docs/viewer/v7/reference/Extensions/PDFExtension/
and there is also an example I used to test from a blog post here:
https://forge.autodesk.com/blog/fast-pdf-viewingmarkup-inside-forge-viewer
hope it helps

How to get AEC model data in autodesk forge?

I want to add new Autodesk Forge Minimap3DExtension to my forge viewer, but i can't get AECModelData with doc.downloadAecModelData().
I try to get data with this code is it right?
var viewables = viewerApp1.bubble.search({'type':'geometry'});
if (viewables.length === 0) {
console.error('Document contains no viewables.');
return;
}
//explicitly call downloadAecModelData of viewer document.
doc.downloadAecModelData()
}````
I also try to get data with bubbleNode, but got only errors.
Only RVT files from Revit 2018 and later will produce the AecModelData. Please check your RVT file version. If it is supported, you can use either way below to get AecModelData
// From document
await doc.downloadAecModelData()
// From bubble
await Autodesk.Viewing.Document.getAecModelData(viewer.model.getDocumentNode())

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.

Unable to retrieve frag IDs in autodesk forge viewer

I am currently looking at trying to get a very simple solution working as a proof of concept before going any further with forge. I have gone through the basic application quick start guide and the basic extension guide and the viewer it self works perfectly with a file I created using Autodesk pipe 3D (tried both as a dwg file and a dwfx).
All I am trying to do is change the colour of the model, all the guides I could find all use similar methods but they all require you to get the list of FragIDs to apply a material
var it = viewer.model.getData().instanceTree;
var allDbIds = Object.keys(it.nodeAccess.dbIdToIndex);
for (var i=0; i<allDbIds.length; i++) {
var dbid = allDbIds[i];
var fragIds = []
try {
it.enumNodeFragments(dbid, function(fragId){
fragIds.push(fragId)
})
}catch(error) {
console.error(error);
}
fragIds.forEach(function(fragId) {
//code to actually change colour
})
It manages to get the dbid fine and brings back many however when it tries to get the frag ids it doesnt bring back anything. It does not break it just brings back nothing but am having trouble finding where to actually look in to why or where to even start
First thing would be to define the fragIds array outside of the for loop, so it's not getting overridden at each iteration.
You can use the following code to set a specific material to a fragId:
fragIds.forEach((fragId) => {
model.getFragmentList().setMaterial(
fragId, material)
})
Take a look at this thread for more info how the topic:
Forge Viewer THREE.MeshLambertMaterial

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