6 May 2015

Viewer API: search DWG Solids by Layer

Yesterday I went for a cool meetup on Android, was interesting to get a fresh air on languages: Kotlin. The basic idea is to have a safer language where we (developers) have less worries with programming problems, such as null pointers. It's not released yet, but may worth looking at.

Also, I spent my lottery luck wining a nice book on Angular JS, now I'll have a good motivation to learn it. Expect some posts soon...

Angular_js_book

Back to this post topic, the idea is from a drawing (.dwg) uploaded to Autodesk View and Data API, search for entities of a specific type, then search again by a specific property. In this sample, the idea is search for Solids and the by Layer name.

To do this, we need to open all components on the viewer, starting from the root, the iterate through each children. Below is the code sample using Viewer v1 (there is a newer version)

function checkTree() {
  viewer.getObjectTree(function (rootComponent) {
    checkChildren(rootComponent.children);
  });
}

function checkChildren(children) {
  if (children == undefined) return;
  if (!Array.isArray(children)) return;
  children.forEach(function (component) {
    if (Array.isArray(component.children))
      checkChildren(component.children);
    if (!component.hasOwnProperty('fragIds')) return;

    if (component.name.indexOf('Solid') >= 0) {
      var fragIdsArray = (Array.isArray(component.fragIds)
        ? component.fragIds : [component.fragIds]);
      var mesh = viewer.impl.getRenderProxy(
        viewer, fragIdsArray[0]);
      viewer.getProperties(mesh.dbId, function (props) {
        props.properties.forEach(function (prop) {
          if (prop.displayName.indexOf('Layer') >= 0) {


            // Do something here
          }
        });
      });
    }
  });
}

 

Tags:

Related Article

Posted By

Augusto Goncalves

Developer Advocate at Autodesk since 2008, working with both desktop and web/cloud apps using top technologies, like C#, JavaScript, NodeJS and any other that can solve problems and improve workflows. See my samples on Github and follow me on Twitter for updates.