简体   繁体   English

如何在对象上调整大小-Autodesk Forge Viewer

[英]How to resize on object - Autodesk Forge Viewer

How can i change size of on object ?? 如何更改对象的大小?

i need to change height of on object 我需要更改物体上的高度

For example, we need to change the height of a door or curtain 例如,我们需要更改门或窗帘的高度

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);
    });
}

Consider setting the scale of objects: 考虑设置对象的比例:

mesh.scale.set(x,y,z)

See usage reference here 在这里查看用法参考

I found the solution and wrote the script for it 我找到了解决方案并为其编写了脚本

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 对于查找碎片ID,您可以使用以下代码

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];
        }
    };

};

How to use ? 如何使用 ?

  • Select your element with mouse 用鼠标选择您的元素
  • Open browser console 打开浏览器控制台
  • Type selection.mesh.fragIds() // [11] 类型selection.mesh.fragIds() // [11]
  • Type transform.setFragId(11) 类型transform.setFragId(11)
  • Now You Can Change Scale and position :) 现在您可以更改比例和位置:)

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

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM