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?
给我你的怀抱2017-05-19 10:12:06
Yes, you can achieve it like this:
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 );
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 );