Search code examples
autodesk-forgeautodesk-viewer

Transform control produces gaps in the model


I'm referencing the code here to move a model. However, I found the distance between the transfrom mesh and the object I moved, even though I used a bounding box. The larger the object, the greater the distance between the transforms. enter image description here

my code

_onControlsChange(ev) {
    if (this.selectedModel) {
      const posTransform = this._controls.position;
      let tr = this.selectedModel.getPlacementTransform();
      tr.elements[12] = posTransform.x;
      tr.elements[13] = posTransform.y;
      tr.elements[14] = posTransform.z;
      this.selectedModel.setPlacementTransform(tr);

      this._viewer.impl.invalidate(true, true, true);
    }
  }

_onSelectionChange(ev) {
    const selSet = ev.selections;
    const firstSel = selSet[0];

    if (firstSel) {
      const listdbIds = firstSel.dbIdArray;
      const dbidItem = listdbIds[0];
      const typeModel = dbidItem.toString().slice(0, 3);
      if (typeModel !== "500") return;

      const model = firstSel.model;
      this.selectedModel = model;
      let dbIds = firstSel.dbIdArray;
      let firstDbId = dbIds[0];
      const instanceTree = model.getData().instanceTree;
      const fragList = model.getFragmentList();
      let bounds = new THREE.Box3();
      instanceTree.enumNodeFragments(
        firstDbId,
        (fragId) => {
          let box = new THREE.Box3();
          fragList.getWorldBounds(fragId, box);
          bounds.union(box);
        },
        true
      );

      const position = bounds.getCenter();
      this._controls.setPosition(position);
      this._controls.visible = true;
    } else {
      this._controls.visible = false;
      this.selectedModel = null;
    }
  }

Solution

  • I've updated the transform tool in the GitHub branch so that you can now configure it to move either individual objects, or entire models. See the commit https://github.com/petrbroz/forge-simple-viewer-nodejs/commit/a72ea8fa3782bf7458ff8e381f8ff578809462e7.

    To configure the tool to move the entire model, initialize the extension like so:

    viewer.loadExtension('TransformExtension', { mode: TransformToolMode.MODEL });