we have used forge Viewer to display the BIM models. But if we click any equipment listed in the Tree Selecion, In the Forge Viewer equipment not Zooming.
Kindly help us to get the selected equipment Zooming in the Forge Viewer. Kindly share the sample code for our reference.
We have Used Below code:
Autodesk.Viewing.Initializer(options, function () {
// Get the viewer div
const viewerDiv = document.getElementById('forgeViewer');
// Initialize the viewer object
const viewer = new Autodesk.Viewing.GuiViewer3D(viewerDiv);
viewer.start();
// Ensure event listeners are added only once
viewer.addEventListener(Autodesk.Viewing.TOOLBAR_CREATED_EVENT, onToolbarCreated);
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, Loadedevent);
// Initialize the AggregatedView
const view = new Autodesk.Viewing.AggregatedView();
view.init(viewerDiv, options3d);
// Use the same viewer instance
const aggregatedViewer = view.viewer;
// Set custom profile settings
const customProfileSettings = {
settings: {
reverseMouseZoomDir: true,
reverseHorizontalLookDirection: true,
selectionMode: 1 // enum value, 0 is default 'First Object' is 1
}
};
const customProfile = new Autodesk.Viewing.Profile(customProfileSettings);
viewer.setProfile(customProfile);
// Optimize viewer settings
viewer.setQualityLevel(false, false); // Disable ambient occlusion and antialiasing
viewer.setGroundShadow(false); // Disable ground shadow
viewer.setGroundReflection(false); // Disable ground reflection
viewer.setProgressiveRendering(true); // Enable progressive rendering
// Load all manifests
const tasks = models.map(md => loadManifest(md.urn, aggregatedViewer));
Promise.all(tasks)
.then(docs => Promise.resolve(docs.map(doc => {
const bubbles = doc.getRoot().search({ type: 'geometry', role: '3d' });
const bubble = bubbles[0];
if (!bubble) return null;
return bubble;
})))
.then(bubbles => view.setNodes(bubbles));
});
// loadManifest function
function loadManifest(documentId, viewer) {
debugger;
return new Promise((resolve, reject) => {
const onDocumentLoadSuccess = (doc) => {
doc.downloadAecModelData(() => resolve(doc));
// New code for handling selection changes
viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, (event) => {
if (!event.selections || event.selections.length <= 0 || event.selections.length > 1) return;
const selSet = event.selections[0];
const dbIds = selSet.dbIdArray;
const model = selSet.model;
const dbId = dbIds[0];
model.getProperties(dbId, function (data) {
if (FromPage == '') {
if (FromSelection == '') {
if (Count === 0) {
let instanceTree = model.getInstanceTree();
let parentId = instanceTree.getNodeParentId(dbId);
var objectid = parentId + 1;
if (objectid === PreviousParentId) {
Count = 1;
selectionchangecount = 0;
} else {
viewer.select(parentId, model, Autodesk.Viewing.SelectionType.OVERLAYED);
// or viewer.setAggregateSelection([ { ids: [parentId ], model, selectionType: Autodesk.Viewing.SelectionType.OVERLAYED } ])
Count = 1;
selectionchangecount = 0;
}
PreviousParentId = parentId;
} else {
itemobject = dbId;
selectionchangecount = selectionchangecount + 1;
if (selectionchangecount == fileselectioncount) {
Count = 0;
selectionchangecount = 0;
}
}
} else {
selectionchangecount = selectionchangecount + 1;
if (selectionchangecount == filecount) {
FromSelection = '';
selectionchangecount = 0;
}
itemobject = dbId;
}
} else {
FromPage = '';
itemobject = dbId;
}
});
});
};
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, reject);
});
}
need Solution for Zooming the equipment.
New contributor
E.M Pradeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.