如何为Autodesk Viewer实现PDF按标准纸型缩放的扩展功能
Autodesk Viewer PDF缩放扩展:实现scalePdfToSize函数
要实现PDF缩放到指定纸型的逻辑,核心是获取当前PDF图纸的原始尺寸、计算适配目标纸型的缩放因子,并调整Viewer相机让图纸匹配目标尺寸显示。以下是完整实现方案:
关键说明
- Autodesk Viewer中PDF图纸的尺寸单位为英寸,需先将毫米单位的纸型尺寸转换为英寸(1英寸 = 25.4毫米)
- 需确保PDF图纸已完全加载,避免获取尺寸时出现空值
- 保持PDF宽高比,避免拉伸变形
修改后的完整扩展代码
替换原扩展中的scalePdfToSize、load、unload方法,补充必要逻辑:
// ******************************************* // PDF Scaling Extension // ******************************************* class PdfScalingExtension extends Autodesk.Viewing.Extension { constructor(viewer, options) { super(viewer, options); this.paperSizes = { A0: { width: 841, height: 1189, text: "A0 - 841mm x 1189mm" }, A1: { width: 594, height: 841, text: "A1 - 594mm x 841mm" }, A2: { width: 420, height: 594, text: "A2 - 420mm x 594mm" }, A3: { width: 297, height: 420, text: "A3 - 297mm x 420mm" }, A4: { width: 210, height: 297, text: "A4 - 210mm x 297mm" }, A5: { width: 148, height: 210, text: "A5 - 148mm x 210mm" }, A6: { width: 105, height: 148, text: "A6 - 105mm x 148mm" }, A7: { width: 74, height: 105, text: "A7 - 74mm x 105mm" }, A8: { width: 52, height: 74, text: "A8 - 52mm x 74mm" }, A9: { width: 37, height: 52, text: "A9 - 37mm x 52mm" }, A10: { width: 26, height: 37, text: "A10 - 26mm x 37mm" }, }; // 存储事件监听句柄,用于卸载时清理 this.onGeometryLoaded = () => { if (this.viewer.model.is2D()) { console.log("PDF图纸已加载完成,可进行缩放操作"); } }; } async load() { // 监听模型加载完成事件,确保能获取到图纸信息 this.viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, this.onGeometryLoaded); return true; } unload() { // 移除事件监听和工具栏控件,避免内存泄漏 this.viewer.removeEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, this.onGeometryLoaded); if (this._group) { this.viewer.toolbar.removeControl(this._group); this._group = null; } return true; } onToolbarCreated() { // Create a new toolbar group this._group = this.viewer.toolbar.getControl("PdfScalingExtensionsToolbar"); if (!this._group) { this._group = new Autodesk.Viewing.UI.ControlGroup("PdfScalingExtensionsToolbar"); this.viewer.toolbar.addControl(this._group); } // Paper Size Dropdown let paperSizeDropdown = new Autodesk.Viewing.UI.ComboButton("PaperSizeDropdown"); paperSizeDropdown.setToolTip("Select Paper Size"); paperSizeDropdown.icon.classList.add("fas", "fa-file-alt"); paperSizeDropdown.container.style.color = "white"; // Set the color of dropdown text to white this._group.addControl(paperSizeDropdown); // Add paper size options to dropdown for (let size in this.paperSizes) { let option = new Autodesk.Viewing.UI.Button(size); option.setToolTip(this.paperSizes[size].text); option.text = this.paperSizes[size].text; // Set the text of the button to the paper size name option.onClick = (ev) => { this.scalePdfToSize(this.paperSizes[size]); }; paperSizeDropdown.addControl(option); } } scalePdfToSize(size) { // 校验当前是否为2D PDF图纸 if (!this.viewer.model || !this.viewer.model.is2D()) { console.warn("当前不是2D PDF图纸,无法执行缩放操作"); return; } // 1. 将目标纸型尺寸从毫米转换为Viewer内部使用的英寸单位 const targetWidthIn = size.width / 25.4; const targetHeightIn = size.height / 25.4; // 2. 获取当前PDF图纸的原始尺寸(单位:英寸) const sheet = this.viewer.model.getSheet(); if (!sheet) { console.warn("无法获取当前图纸信息,请确保图纸已加载完成"); return; } const originalWidth = sheet.width; const originalHeight = sheet.height; // 3. 计算适配缩放因子(取宽高比例的最小值,避免图纸超出目标纸型范围) const scaleX = targetWidthIn / originalWidth; const scaleY = targetHeightIn / originalHeight; const scaleFactor = Math.min(scaleX, scaleY); // 4. 计算目标视图范围,基于原始尺寸和缩放因子 const targetBounds = new THREE.Box3( new THREE.Vector3(0, 0, 0), new THREE.Vector3(originalWidth * scaleFactor, originalHeight * scaleFactor, 0) ); // 5. 调整相机适配目标范围,保持图纸居中显示 this.viewer.navigation.fitBounds(true, targetBounds); console.log(`已缩放PDF至目标纸型: ${size.width}mm x ${size.height}mm,缩放因子: ${scaleFactor.toFixed(4)}`); } } Autodesk.Viewing.theExtensionManager.registerExtension("PdfScalingExtension", PdfScalingExtension);
逻辑说明
- 单位转换:将纸型的毫米尺寸转为Viewer内部使用的英寸单位,确保计算精度
- 图纸尺寸获取:通过
model.getSheet()获取当前PDF的原始尺寸 - 缩放因子计算:取宽、高方向缩放比例的最小值,保证图纸完整适配目标纸型且不变形
- 相机适配:调用
navigation.fitBounds()让Viewer自动调整相机,将图纸缩放到目标范围并居中显示
内容的提问来源于stack exchange,提问作者Vibhav Joshi
相关产品推荐
相关产品推荐

