Search model items within a volume and apply transformation

By Xiaodong Liang

If you want to search the model items within a volume, the bounding box could help you. Bounding Box is the extents which aligns 3D Axis. It Identifies a cuboid-shaped bounded area in 3D space.

  • ModelGeometry.BoundingBox:  extents of a model item
    – ModelItemCollection.BoundingBox: maximum extents of all items contained in the collection

The code below searches the model items whose bounding box are within the specific box and transforms them. The code is tested with <Navisworks Simulate/Manage> Samplesgatehouse gatehouse_pub.nwd. Its units is millimeters. The bounding box uses the units of the document Modal.Units. While the UI uses meter.

using Autodesk.Navisworks.Api;
using ComApiBridge = Autodesk.Navisworks.Api.ComApi;
using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;
private void transObjWithBox()
{
   // get current document
   Document oDoc =
      Autodesk.Navisworks.Api.Application.ActiveDocument;
 
   //create the box (volume) you want to check
   BoundingBox3D oNewBox = new BoundingBox3D(
              new Point3D(-1000, -1000, 0),
                    new Point3D(1000, 1000, 1000));
 
   // in the first model, check the items whose boxes are 
   // within the specified  box.
   IEnumerable items =
      oDoc.Models[0].RootItem.DescendantsAndSelf.
             Where(x =>
             oNewBox.Contains(x.BoundingBox())
            );
 
  // Select the items in the model that are 
  // contained in the collection
  oDoc.CurrentSelection.CopyFrom(items);
 
  // create a collection to store the items
  ModelItemCollectionoModelColl =new ModelItemCollection();
  oModelColl.CopyFrom(items);
 
 //transform the objects. Currently this needs COM API
 ComApi.InwOpState10oState =ComApiBridge.ComApiBridge.State;
 ComApi.InwOpSelection oSel =
     ComApiBridge.ComApiBridge.ToInwOpSelection(oModelColl);
 
 // create transform matrix
 ComApi.InwLTransform3f3 oTrans =
       (ComApi.InwLTransform3f3)oState.ObjectFactory
        (ComApi.nwEObjectType.eObjectType_nwLTransform3f,
       null, null);
 ComApi.InwLVec3f oVec =
      (ComApi.InwLVec3f)oState.ObjectFactory
      (ComApi.nwEObjectType.eObjectType_nwLVec3f,
       null, null);
 oVec.SetValue(5000, 5000, 0);
 oTrans.MakeTranslation(oVec);
 
 // transform
 oState.OverrideTransform(oSel, oTrans);
}

Comments

4 responses to “Search model items within a volume and apply transformation”

  1. Can I get the same code in .NET (preferable C#)?

  2. xiaodong Avatar
    xiaodong

    Hi,
    this is C# code already. Did you meet any difficulty?
    Regards,
    Xiaodong

  3. Thanks. The top part of the code runs fine. (before transformation).After “oDoc.CurrentSelection.CopyFrom(items);”
    I am actually looking to print the collection in a text file but only the values in the properties of the items. Like suppose I only want to print the item (from collection) if it has a property category “Entity Handle” and contains Property “Value”, and if its true I will print the number contained in Value.
    I am able to write the displaynames, boundingbox.min.x etc but how can I write the values of properties?

  4. It’s Ok. I figured it out

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading