hyrd / jsc3d

Automatically exported from code.google.com/p/jsc3d
0 stars 0 forks source link

Problems to show/hide models loaded from some STL files #118

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I'm loading some STL files from an Array as follow: components=[row;idpart; 
descriprion; stl_filename], and I wanna use components['row',jr] like 
mesh.internalID to show/hide components and  components['description',jr] to 
label on checkbox, but loader.loadFromUrl(stl_filename@components) seems do not 
respect the arrey order and the result is not good, show/hide part of different 
description respect to the checkbox label.
I tryied to assign a proprieties like 'part_name' or 'part_id' on loaded files, 
but due to my limited knoledge I didn't found any solutions.

Can you help me please???

Original issue reported on code.google.com by 27vii...@gmail.com on 4 Oct 2014 at 10:41

GoogleCodeExporter commented 8 years ago
Yes, the file requests are asynchronous and the responses may come in arbitrary 
order.

I guess you are using a group of loader objects to request STL models from 
multiple sources, with the loader.onload callbacks overridden for adding them 
into one scene when done. In this case, since JavaScript allows adding 
arbitrary properties to an existing object, we can just assign an unique ID to 
a new mesh object as it has been loaded:

  ...
  var loader = ...;
  loader.onload = function(scene) {
    if (scene.getChildren().length > 0) {
      // an STL model contains only one mesh object
      var mesh = scene.getChildren()[0];
      // we assign an self-defined ID to the newly loaded mesh object
      mesh.userId = anUniqueIdString;
      // now add this mesh into the scene for display
      ...
    }
  };
  loader.loadFromUrl(url);
  ...

Now each mesh has a self-defined new property 'userId', we just utilize them to 
identify mesh objects in our application.

Original comment by Humu2...@gmail.com on 5 Oct 2014 at 2:29

GoogleCodeExporter commented 8 years ago
I tried to insert the suggestions on my code. There are some errors that I do 
not know eliminate. Bellow my code.
1- at row: loader.onload = function(scene,idx_file){}, in function idx_file is 
lost
2- setobject() not function

Thank

----------------------

        var canvas = document.getElementById('cv');
        viewer = new JSC3D.Viewer(canvas);

        theScene = new JSC3D.Scene;

        var numOfLoaded = 0;

      viewer.setParameter('InitRotationX', 0);
      viewer.setParameter('InitRotationY', 0);
      viewer.setParameter('InitRotationZ', 0);
      viewer.setParameter('ModelColor', '#FF0000');
      viewer.setParameter('BackgroundImageUrl', 'images/bg_3D_viewer.jpg');
      viewer.setParameter('Definition', 'standard');
      viewer.setParameter('RenderMode', 'flat');
      viewer.setParameter('Renderer', 'webgl');
      viewer.setParameter('ProgressBar', 'on');
      viewer.init();

      // table components
      tbl_components[0]=[id1,'obj_desc_1','file1.stl'];
      tbl_components[1]=[id2,'obj_desc_2','file2.stl'];
      tbl_components[2]=[id3,'obj_desc_3','file3.stl'];
      tbl_components[3]=[id4,'obj_desc_4','file4.stl'];
      tbl_components[4]=[id5,'obj_desc_5','file5.stl'];

      var num_comp=tbl_components.length-1;
      for (var jr=0; jr<num_comp; jr++) {

           var loader = new JSC3D.StlLoader;

           idx_file=tbl_components[jr,0];

           loader.onload = function(scene,idx_file) {
                                  if (scene.getChildren().length > 0){
                                      var mesh = scene.getChildren()[0];
                                      // set object_id linked to tbl_components
                                      mesh.userId = idx_file;
                                      theScene.addChild(mesh);
                                      mesh.setMaterial(new JSC3D.Material('material', 0, components_color[idx_file]));
                                  }

                                  if (++numOfLoaded ==components.length ) { 
                                      viewer.replaceScene(theScene);

                                      for(obj in viewer.scene.children) { 
                                        idx_file=obj.userId
                                        obj_desc=tbl_components[idx_file,1];
                                        // display checkbox (show/hide) - obj_desc
                                        shtml+="<input type='checkbox' value='" + obj + "' checked onclick='setobject(this);'/>" + obj_desc + "<br />";
                                      };
                                      div_objects_list.innerHTML = shtml;

                                  }
                          };

              loader.loadFromUrl(tbl_components[jr,2]);

      }

      viewer.update();

////

function setobject(self) { 
    viewer.scene.children[self.value].visible = self.checked; 
}

------------------------------------------

Original comment by 27vii...@gmail.com on 5 Oct 2014 at 8:01

GoogleCodeExporter commented 8 years ago
The callback loader.onload only takes a single parameter. You cannot pass in 
your IDs in that way. Try to move your implementation of the onload function to 
a closure instead:

  for (var jr=0; jr<num_comp; jr++) {
    ...
    var idx_file=tbl_components[jr][0];
    loader.onload = (function(userId) {
      return function onload(scene) {
        if (...) {
          ...
          // set object_id linked to tbl_components
          mesh.userId = userId;
          console.info('ID for this mesh is ' + mesh.userId);
          ...
        }
        ...
      };
    })(idx_file);
  }
  ...

This makes use of closures to bind mesh objects to correct user-defined IDs. 
You can refer to this article 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures if you 
are not familiar with either the syntax or the usage of JavaScript closure 
feature.

Besides, maybe you have to check your codes a second time. I guess it contains 
more than one mistakes.

Original comment by Humu2...@gmail.com on 5 Oct 2014 at 10:03