javascript - 如何调整对象的大小 - Autodesk Forge Viewer

标签 javascript autodesk-forge autodesk-viewer autodesk viewer

如何更改对象的大小??

我需要改变物体的高度

例如,我们需要改变门或窗帘的高度

on this code my object disappears

let change = function () {

    const viewer = oViewer;
    const model = viewer.model;

    const frags = [
        123,
        361,
    ];

    for(let i in frags){

        let fragId = frags[i];

        // Get mesh with frag id
        let mesh = viewer.impl.getRenderProxy(model, fragId);

        // Selection ID
        let dbId = 1280; // viewer.getSelection()[0]

        model.getData().instanceTree.enumNodeFragments(dbId, fragId => {
            mesh.scale.x += 0.5;
            // mesh.scale.y = 5;
            // mesh.scale.z = 5;

            model.getFragmentList().setMesh(fragId, mesh, true);
            viewer.impl.invalidate(true);
        });
    }
};

The following code resizes objects but becomes too large and cannot be scaled

const viewer = oViewer;
const model = viewer.model;

viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectedCallback);

function onSelectedCallback(event) {

    const fragId = event.fragIdsArray[0];

    if (typeof fragId == 'undefined') {return;}

    const fragIdsArray = (Array.isArray(fragId) ? fragId : [fragId]);

    fragIdsArray.forEach(function (subFragId) {

        let mesh = viewer.impl.getRenderProxy(model, subFragId).clone();

        mesh.scale.y += 0.2;
        mesh.scale.x += 0.2;
        mesh.scale.z += 0.2;

        model.getFragmentList().setMesh(subFragId, mesh, true);
        viewer.impl.invalidate(true);
    });
}

最佳答案

我找到了解决方案并为其编写了脚本

let transform = new function () {

    let _self = this;

    this.fragId = null;
    this.proxy = null;
    this.viewer = oViewer;
    this.model = this.viewer.model;

    this.setFragId = function (fragId) {
        this.fragId = fragId;
        this.proxy = this.viewer.impl.getFragmentProxy(this.model, this.fragId);
        this.proxy.getAnimTransform();
    };

    this.update = function(){
        this.proxy.updateAnimTransform();
        this.viewer.impl.sceneUpdated(true);
    };

    this.scaleX = function (num) {
        this.proxy.scale.x = num + 1;
        this.update();
    };

    this.scaleY = function (num) {
        this.proxy.scale.y = num + 1;
        this.update();
    };

    this.scaleZ = function (num) {
        this.proxy.scale.z = num + 1;
        this.update();
    };

    this.positionX = function (num) {
        this.proxy.position.x = num;
        this.update();
    };

    this.positionY = function (num) {
        this.proxy.position.y = num;
        this.update();
    };

    this.positionZ = function (num) {
        this.proxy.position.z = num;
        this.update();
    };

};

for find frag Ids you can use follow code

let selection = new function () {

    this.viewer = oViewer;

    let _self = this;

    this.ids = function () {
        return this.viewer.getSelection();
    };

    this.count = function () {
        return this.viewer.getSelectionCount();
    };

    // Mesh Object
    this.mesh = new function () {
        this.all = function () {
            if (_self.count() === 0) return {};

            let meshes = _self.viewer.impl.selectionMeshes;
            let output = [];

            for (let index in meshes) {
                output.push(meshes[index]);
            }

            return output;
        };

        this.fragIds = function(){
            let meshes = this.all();
            let ids = [];
            meshes.forEach(function(mesh){
                ids.push(mesh.fragId);
            });
            return ids;
        };

        this.first = function () {
            return this.all()[0];
        };

        this.last = function () {
            return this.all().reverse()[0];
        }
    };

};

如何使用?

  • 用鼠标选择你的元素
  • 打开浏览器控制台
  • 键入 selection.mesh.fragIds()//[11]
  • 输入transform.setFragId(11)
  • 现在您可以更改比例和位置:)

    transform.scaleX(number);
    
    transform.scaleY(number);
    
    transform.scaleZ(number);
    
    
    transform.positionX(number);
    
    transform.positionY(number);
    
    transform.positionZ(number);
    

    关于javascript - 如何调整对象的大小 - Autodesk Forge Viewer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57970267/

    相关文章:

    javascript - 将预加载 img 转换为 iframe

    autodesk-forge - 选择要在 Autodesk Forge Viewer 中显示的特定 3D View

    node.js - SVF 模型衍生品下载为(几乎)空的 ZIP 文件 (Autodesk Forge)

    javascript - Autodesk Forge Viewer - 从 "viewer.model.search"和 "viewer.getSelection()"获取不同的值 dbId

    javascript - 伪造查看器调用焦点方法

    javascript - 使用 NaN 时允许输入小数点

    javascript - 缓慢地自动滚动到 div 的底部

    javascript - Angular-UI Shift 按钮按键

    java - 向 Autodesk 进行身份验证

    autodesk-forge - Autodesk Forge 查看器 PDF 扩展