Geo Location on click _ Forge viewer - autodesk-forge

i'm looking for some guidance on how best to display real world coordinates when i click on the screen or select a model element in the forge viewer.
i have had a look around and done some research but with little success, however i have found this post that helps however the coordinate read out seems to be completely diffent to the cordantes that i would expect to see.
https://github.com/apprentice3d/ForgeViewerExtensions/blob/master/assets/js/extensions/transformationExplorer.js
As you can tell i dont know much about the viewer geo extension, any guidance on this would be great.
Thanks Tom.

If it's enough to get the world coordinates of a point you clicked on in the viewer, you can simply use the viewer's clientToWorld method, for example like so:
viewer.container.addEventListener('click', function (ev) {
const result = viewer.clientToWorld(ev.clientX, ev.clientY);
if (result) {
console.log(result.point);
}
});

i managed to get it to work by using NOP_VIEWER.model.getData().globalOffset
Example
this.infoX.innerText = nodeData.position.x + NOP_VIEWER.model.getData().globalOffset.x;

Related

Is it possible to set snap settings?

I'm using the Autodesk Forge viewer v7 using the Edit2D tool. This question is specifically about the snapping functionality. It is currently working but a bit overzealous... It seems to have all of the possible snaps on (endpoint, along line, intersections, centers, etc.). It also seems to snap to layers that I have hidden.
Is there a way to set the snaps that are on and off? Also is there a way to limit the layers that it snaps to? I was looking for possibly a method that will tell me the object(s) it's trying to snap to and the type of snap and let me determine if that's a viable snap point and return true/false or something like that but I couldn't find one. Is there a way to do this?
EDIT
I did find setSnapFilter but it doesn't seem to do anything. I have this implementation in my code:
// #ts-ignore
e.defaultContext.snapper.sheetSnapper.setSnapFilter((e) => {
console.log(e);
return false;
});
However, I never get a console log happening. I have tried this on both the sheet snapper and the layer snapper and I've tried returning both true and false and none of them are ever called or make any difference to the snapping.
Good catch! The context.snapper.sheetSnapper object does have a setSnapFilter method you can override. Unfortunately, this filter is only invoked in the Snapper#onMouseMove method, and the Edit2D extension bypasses this method. That's why overriding the method has no effect. Let me pass this info to the engineering team for consideration.

load 2D & 3D forge viewers in single web page

I would like to link between elements from the 2D sheet and 3D model, so when I select the element from 2D it should reflect and select (isolate) in the 3D also if I change the color it does the same on both e.g. and the other way around.
so I can use the document browser extensions to open the 2d sheet on 1st viewer and the 3d model on the 2nd viewer:
const firstModel = new Autodesk.Viewing.Private.GuiViewer3D(document.getElementById('MyViewerDiv1'));
const secondModel = new Autodesk.Viewing.Private.GuiViewer3D(document.getElementById('MyViewerDiv2'));
Autodesk.Viewing.Initializer(options1, function() {
viewer1.start();
viewer1.load(...);
});
Autodesk.Viewing.Initializer(options2, function() {
viewer2.start();
viewer2.load(...);
});
if the example above is correct I am still missing how to links both viewers.
I hope someone could help me with this issue
Note that we have a viewer extension that might already give you what you're looking for: https://github.com/Autodesk-Forge/forge-extensions/blob/master/public/extensions/NestedViewerExtension/README.md.
If you want to implement the cross-selection between two viewer instances yourself, you can. Just subscribe to the SELECTION_CHANGED event in one of the viewers, get the selected IDs, and select the same IDs in the other viewer using the usual viewer.select([...]); method.
Btw. regarding your code snippet:
the Autodesk.Viewing.Initializer only needs to be called once per the entire webpage
the Autodesk.Viewing.Private.GuiViewer3D instances should be created after the initializer has done its work

Enabeling the 'perspective with ortho faces' in viewer

I can currently do:
this.viewer.navigation.toOrthographic()
and
this.viewer.navigation.toPerspective()
Is there a way that I could also use the perspective with ortho faces and change the current viewer into that on the go?
I get that there are view_types (https://forge.autodesk.com/en/docs/viewer/v7/reference/globals/VIEW_TYPES/) I could set up on initialize, but i would like to change this option after model load without having to use the view cube.
Thank you all in advance!
If I understood what you are after correctly then it's possible to call shots for viewcube as an extension programmatically (since v7.x and onwards), say for example:
//wait after model is rendered ...
const viewCubeUI = NOP_VIEWER.getExtension("Autodesk.ViewCubeUi")
viewCubeUI.setViewType(Autodesk.Viewing.Private.VIEW_TYPES.PERSPECTIVE_ORTHO_FACES)
See documentation here for details...

forge Viewer - Markup camera change issue

In my viewer, I have a markup plotted using cloud mode and during camera change event, am not able to map the associated label with the markup as it moves on.
To make this work I store coordinates and while camera change event I use worldToClient to find the new coordinates, but this is not working
So problem is with finding original world coordinates. I tried multiple ways as below, but nothing seems to work for me. Much appreciate help on this. Just for an idea on this, am attaching screen-shots of model before and after camera change.
/*** option 1***/
worldCoord = markup.markups[0].generateBoundingBox().max;
/*** option 2***/
var coords = viewer.impl.clientToViewport(pos.x, pos.y);
worldCoord = coords.unproject(viewer.impl.camera)
/*** option 3***/
worldCoord = markup.markups[0].getClientPosition()
/*** option 4***/
worldCoord = viewer.clientToWorld(pos.x,pos.y,true);
/***option 5***/
worldCoord = markup.markups[0].getBoundingRect()
Finally I got it.
We can store layername info with your label and fetch it in your camera change event using below code.
position = markup.svgLayersMap[layerName].markups[0].getClientPosition()
hope this helps someone

Disable snapper on Autodesk Forge Markup

I have problem when try to implement some tools inside MarkUp.Code of Autodesk Forge. When i try to draw an arrow or text in my PDF drawing it always go along with snapper. I've also try to disable it with snapper.clearSnapped() but nothing better.
Do anyone who know the solution for it ?
Thanks a lot !
function DrawText(){
var extension = viewer.getExtension("Autodesk.Viewing.MarkupsCore");
extension.callSnapperMouseDown();
extension.enterEditMode();
extension.snapper.clearSnapped();
extension.snapper.indicator.clearOverlays();
var modeText = new Autodesk.Viewing.Extensions.Markups.Core.EditModeText(extension);
extension.changeEditMode(modeText);
},
Simply remove the extension's reference to the snapper:
ext.snapper=null
You can save a copy of the reference elsewhere so that you'd be able to restore if when needed.