简体   繁体   中英

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

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]
  • Type 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);
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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