search

Home  >  Q&A  >  body text

javascript - How to rotate and move a widget?

Is there a way to move or rotate a component after selecting it? For example, after selecting it, rotate it by an angle along a certain axis, or after selecting it, move the component to another position?

PHPzPHPz2806 days ago530

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:12:06

    Yes, you can achieve it like this:

    1. Rotate 180 degrees along the Y axis (the following is a code example for ES2015):

    class RotateExt extends Autodesk.Viewing.Extension {
      constructor( viewer, options ) {
        super();
      }
    
       load() {
        viewer.addEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
        return true;
       }
    
       unload() {
        viewer.removeEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
        return true;
       }
       
      /**!
       * 关键函数
       */
       onSelectionChanged = ( event ) => {
           
        const quaternion = new THREE.Quaternion();
        // 设置旋转量 - 依 Y 轴旋转构件 180 度
        quaternion.setFromAxisAngle( new THREE.Vector3( 0,1,0 ), Math.PI );
    
        const model = event.model;
        const fragIdsArray = event.fragIdsArray;
    
        fragIdsArray.forEach( ( fragId, idx ) => {
          const fragProxy = this.viewer.impl.getFragmentProxy( model, fragId );
    
          fragProxy.getAnimTransform();
    
          const position = new THREE.Vector3( fragProxy.position.x, fragProxy.position.y, fragProxy.position.z );
          position.applyQuaternion( quaternion );
    
          fragProxy.position = position;
          fragProxy.quaternion.multiplyQuaternions( quaternion, fragProxy.quaternion );
    
          if( idx === 0 ) {
            const euler = new THREE.Euler();
            euler.setFromQuaternion( fragProxy.quaternion, 0 );
          }
    
          fragProxy.updateAnimTransform();
        });
        
        this.viewer.impl.sceneUpdated( true );
      };
    }
    
    Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.Viewing.Extension.RotateTool', RotateExt );
    1. Move -100 units along the X-axis (the following is a code example for ES2015):

    class TranslateExt extends Autodesk.Viewing.Extension {
      constructor( viewer, options ) {
        super();
      }
    
       load() {
        viewer.addEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
        return true;
       }
    
       unload() {
        viewer.removeEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
        return true;
       }
    
      /**!
       * 关键函数
       */
       onSelectionChanged = ( event ) => {
        
        // 设置移动量 - 向 X 轴移动 -100 单位
        const offset = new THREE.Vector3( -100, 0 , 0 );
    
        const model = event.model;
        const fragIdsArray = event.fragIdsArray;
    
        fragIdsArray.forEach( ( fragId, idx ) => {
          const fragProxy = this.viewer.impl.getFragmentProxy( model, fragId );
    
          fragProxy.getAnimTransform();
    
          const position = new THREE.Vector3(
            fragProxy.position.x + offset.x,
            fragProxy.position.y + offset.y,
            fragProxy.position.z + offset.z
          );
    
          fragProxy.position = position;
    
          fragProxy.updateAnimTransform();
        });
        
        this.viewer.impl.sceneUpdated( true );
      };
    }
    
    Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.Viewing.Extension.TranslateTool', TranslateExt );

    reply
    0
  • Cancelreply