castro nelson
castro nelson

Reputation: 1

Save and retrive in forge viewer

I am using forge viewer for displaying AutoCAD files. Also using the drawing tool over viewer based on the sample source.

I will draw the area by using box or sphere draw tools.

I need to save the current viewer including box or sphere area which I was marked over viewer and when again loading same file the area which has been marked that should be bind default.

How it is possible please help me

Suggest any way to implement this scenario.

Thanks in advance.

Upvotes: 0

Views: 175

Answers (1)

João Martins
João Martins

Reputation: 688

You can do that with 2 steps.

First, taking advantage of Object3D.toJSON() method.

Let's summarize in a sample where we generate a JSON object from our mesh:

//here we create a BoxGeometry
let geom = new THREE.BufferGeometry().fromGeometry(new THREE.BoxGeometry(100,100,100));
let phongMaterial = new THREE.MeshPhongMaterial({
  color: new THREE.Color(1, 0, 0)
});
let mesh = new THREE.Mesh(geom, phongMaterial);

if (!viewer.overlays.hasScene("CustomScene")) {
  viewer.overlays.addScene("CustomScene");
}
viewer.overlays.addMesh(mesh, "CustomScene");
viewer.impl.sceneUpdated(true);

//here we generate the JSON from the mesh and download it
let jsonObject = JSON.stringify(mesh.toJSON())
download(jsonObject, 'Box.json', 'text/plain');

download function can be found here.

The next step is about generating the box from the saved JSON.

For that, we'll use ObjectLoader.parse method.

And again, we can summarize in the code below:

//here we read the JSON object from our generated file
var request = new XMLHttpRequest();
request.open("GET", "js/Box.json", false);
request.send(null)
var my_JSON_object = JSON.parse(request.responseText);

//here we generate the mesh
let mesh = new THREE.ObjectLoader().parse(my_JSON_object);

if (!viewer.overlays.hasScene("CustomScene")) {
  viewer.overlays.addScene("CustomScene");
}
viewer.overlays.addMesh(mesh, "CustomScene");
viewer.impl.sceneUpdated(true);

Refer here for the function to read objects from JSON file.

Upvotes: 0

Related Questions