Forge Viewer rendering pipeline - autodesk-forge

In the latest release of THREE.js, Mesh can implement following callback in order to hook into the rendering pipeline:
this.onBeforeRender = function (renderer, scene, camera ) {
..
}
Is there any way to achieve a similar logic in the current implementation of the viewer?

Unfortunately Engineering just confirmed the closet options we have out of the box the latest Viewer v7 is the OverlayManager - see its doc here:
viewer.overlays.addScene(name)
viewer.overlays.addMesh(mesh, name)
Which obviously isn't exactly what you are after but we will assess the possibilities of adding these hooks to our future versions.

Related

Forge Viewer 7.71 throws errors when removing and recreating custom meshes in ModelViewer

I have an application that adds many custom meshes to the Forge Viewer using the ModelBuilder extension. This application has worked for quite a while but ran into issues with version 7.71 of the viewer.
When graphics are updated in my application, I remove all existing meshes and then recreate them based on new data. With version 7.71 of the viewer, when I recreate the meshes I get many instances the following error in the dev tools console:
Uncaught TypeError: Cannot read properties of undefined (reading 'material')
at FragmentList.getMaterial (FragmentList.js:760:1)
at FragmentList.getMaterialId (FragmentList.js:754:1)
at RenderBatch.js:222:1
at Int32Array.sort (<anonymous>)
at RenderBatch.sortByMaterial (RenderBatch.js:220:1)
at RenderModel.applyVisibility (RenderModel.js:388:1)
at RenderModel.nextBatch (RenderModel.js:360:1)
at RenderScene.reset (RenderScene.js:387:1)
at Array.cmdBeginPhase (Viewer3DImpl.js:1125:1)
at RenderCommandSystem.executeCommandList (Viewer3DImpl.js:847:1)
After these errors, the new meshes are not created.
The release notes don't mention anything about any expected changes here. Looking for guidance from the Autodesk team.
Here is a repo that reproduces the issue:
https://github.com/bencameron/custom-mesh-refresh
I'm told it will take some time to implement the fix (ticket id LMV-6757). In the meantime you could stick with v7.70 or try these workarounds a colleague suggested:
1.) If all of the custom geometry is removed, like it is in the example app, it's probably simpler to remove the model entirely and start with a new one. Here is an (untested) snippet to do that:
// Instead of deleting all geometries individually, simply delete the model and create a new one
viewer.unloadModel(modelBuilder.model);
// drop the reference to the model builder instance, i.e. assign a new instance
modelBuilder = await sceneBuilder.addNewModel({});
// start adding the geometry again...
...
2.) If option #1 doesn't work, or if the user doesn't want to delete all geometries, they could also avoid the crash by running this snippet right after adding new geometries (i.e. every time they add a batch of geometries):
const scenes = modelBuilder.model.getIterator().getGeomScenes();
for (let i = 0; i < scenes.length; ++i) {
scenes[i].numAdded = 0;
}
I would recommend the first option over the second one if applicable.

How to activate 3d view and save the revit model in design automation api for Revit

I am working on a task where I need to accept a template file (.rte) as input and save the resultant revit file in 3d view.
I understand that to activate 3d view, we need UIDocument which is not available when we work in DesignAutomation API.
In an addin project in the past I had implemented it in the following way:
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(View3D));
foreach (View3D v in collector)
{
if (!v.IsTemplate)
{
uiApp.ActiveUIDocument.ActiveView = v;
}
}
So is there any other way to do this in DesignAutomation API.
You definitely have no access whatsoever to the UIDocument in the Design Automation API. It does not even exist. I suggest you just go ahead and generate your Revit model as before and then see what view ends up being saved. Quite possibly it is the 3D view anyway.

How to use the various Forge Viewer transforms

Below are the various transforms I have found so far using NOP_VIEWER.model.getData().
I'm using the transforms to bring a position into viewer space, and I haven't been able to find any good documentation describing what they all do. My hope here is that this question can help by providing some documentation of the role of these transforms and how/when to use them.
The model originally comes from Revit.
GlobalOffset (Vector3)
placementWithOffset (Matrix4) - seems to be just the inverse of GlobalOffset as a matrix?
placementTransform (Matrix4) - undefined in all models I've tested, I've seen some hints that this is a user defined matrix.
refPointTransform (Matrix4)
Also, there are some transforms in the NOP_VIEWER.model.getData().metadata. These may be Revit specific:
metadata.georeference.positionLL84 (Array[3]) - this is where the model's GPS coords are stored
metadata.georeference.refPointLMV (Array[3]) - no idea what this is, and it has huge and seemingly random values on many models. For example, on my current model it is [-17746143.211481072, -6429345.318822183, 27.360225423452952]
metadata.[custom values].angleToTrueNorth - I guess this is specifying whether the model is aligned to true or magnetic north?
metadata.[custom values].refPointTransform - (Array[12]) - data used to create the refPointTransform matrix above
Can someone help by documenting what these transforms do?
Related: Place a custom object into viewer space using GPS coords
As an alternative solution, the Viewer works with extensions. The Autodesk.Geolocation extension provides a few methods to handle the data structure you mentioned:
Load extension:
let geoExt;
NOP_VIEWER.loadExtension('Autodesk.Geolocation').then((e) => {geoExt = e});
Or get already loaded extension:
let geoExt = NOP_VIEWER.getLoadedExtensions()['Autodesk.Geolocation']
Then use the methods to convert the coordinates
geoExt.lmvToLonLat
geoExt.lonLatToLmv
Here is a quick article on it.
You may .activate() the extension to see additional information on the model geo location.

Forge viewer Document.load throws exception on mobile devices

We are using Forge viewer version 3.2.x.
For Autodesk.Viewing.Document the function load(documentId,onSuccessCallback,onErrorCallback,accessControlProperties) the parameter accessControlProperties is listed as required (see Autodesk Forge Documentation). Not providing this parameter still works on Chrome / Firefox, but I discovered that missing the parameter causes problems on mobile devices. However, for this parameter there seems to be no example or specification available.
Maybe the problem is not the missing parameter itself but something else...? Getting the following exception in browser console:
Uncaught TypeError: Cannot read property 'id' of null
at PinchRecognizer.recognizeWith (viewer3D.js?v=3.2.*:2300)
at Array.forEach (<anonymous>)
at each (viewer3D.js?v=3.2.*:923)
at invokeArrayArg (viewer3D.js?v=3.2.*:903)
at PinchRecognizer.recognizeWith (viewer3D.js?v=3.2.*:2294)
at new Autodesk.Viewing.GestureHandler (viewer3D.js?v=3.2.*:16042)
at GuiViewer3D.Viewer3D.createControls (viewer3D.js?v=3.2.*:44685)
at GuiViewer3D.Viewer3D.initialize (viewer3D.js?v=3.2.*:44758)
at GuiViewer3D.initialize (viewer3D.js?v=3.2.*:50852)
at GuiViewer3D.Viewer3D.start (viewer3D.js?v=3.2.*:44555)
Using Viewer.loadDocument(documentId,onDocumentLoad,onLoadFailed,accessControlProperties) is not an option, because we must load multiple models into the viewer.
Apologies for the late response, we have a backlog in the queue due to the IFC issue this week.
According to my experience, the accessControlProperties is not a required parameter for using ViewingApplication.loadDocument(). Could you consider providing a reproducible case demonstrating this issue to forge.help#autodesk.com? I'm glad to help you check what happened.
A non-confidential reproducible case contains following items:
A short exact description of what you are trying to achieve.
The behavior you observe versus what you expect, and why this is a problem.
Some complete yet minimal sample models to run a test in. For example, Revit sample models here: https://knowledge.autodesk.com/support/revit-products/getting-started/caas/CloudHelp/cloudhelp/2016/ENU/Revit-GetStarted/files/GUID-61EF2F22-3A1F-4317-B925-1E85F138BE88-htm.html.
A complete yet minimal Forge viewer app that can run and debugged.
Detailed step-by-step instructions for reproducing the issue, e.g. which element to pick, what command to launch etc.
Thanks for your reply, Eason - isolating the issue from our whole application was quite difficult, but made me finally find the problem myself:
We had a conflict with another JavaScript library that is included in our minified js file. The GestureRecognizer classes are named similar as in the camunda library which we are using for modelling bpmn processes. Solved it by excluding that library from the global js and include it only where it is needed, same as how we are dealing with forge library.

Active viewer model for miltimodel mode

Then several models are loaded in viewer, only one of them is "active". I mean that Object tree is shown for this model and select(),isolate() etc methods are related to it. I try to use following method to set "active" model:
function setActiveModel(model) {
var instanceTree = model.getData().instanceTree;
viewer.modelstructure.setModel(instanceTree);
}
But object tree doesn't change.
Questions are:
That is correct way to change viewer "active" model?
How can I get current "active" model in viewer?
Here is a patched version that supports multi-model switching. It is a drop-in replacement, simply include the file after the viewer3D.js script:
MultiModelStructurePanel.js
Multi-models will be supported soon in a future version of the API.