Saturday, September 26, 2009

Extracting 3D DWF Model Properties Using Autodesk Design Review API and AutoCAD VBA

A while ago I came across a situation where I had to extract line lists, valve lists, equipment lists and fittings lists from a DWF file. The DWF file was converted from a PDS model review file (DRI) using NavisWorks. After a few hours research on Autodesk Design Review API, I could extract all the required lists with a few lines of code. Ofcourse, with the help of fast dying AutoCAD VBA.

As the code makes use of CExpressViewerContol, you need to place it inside a form before running the code. You may use any COM compliant development environment in place of AutoCAD VBA. But I would like to stick to AutoCAD VBA till its last breath due to ease of use.

Sub ExtractDwfProps()
'### Extracts properties from model components of a 3D DWF file
'### By zoomharis@gmail.com
'### Date: 09/09/09

'### Based on Autodesk Design Review 2010 API

'### DWF specific references
' -----------------------
'### AdCommon 1.0 Type Library
'### ECompositeViewer 1.0 Type Library
'### ExpressViewerDll 1.0 Type Library

'### DWF specific controls
' ---------------------
'### CExpressViewerContol

On Error Resume Next
Dim oECV As ECompositeViewer.IAdECompositeViewer
Dim oSec As ECompositeViewer.IAdSection
Dim oEnt As AdCommon.IAdObject
Dim oProp As AdCommon.IAdProperty
Dim oCol As AdCommon.CAdCollection
Dim oCont As ECompositeViewer.IAdContent
Dim strDwfLoc As String
Dim strPropName As String
Dim strPropValue As String
'## Let me use a sample 3D dwf file
strDwfLoc = "C:\Dwf\3DModel.dwf"
strPropName = ""
strPropValue = ""
'## Open the dwf file in the viewer
CExpressViewerContol1.SourcePath = strDwfLoc
Set oECV = CExpressViewerContol1.ECompositeViewer
'## Iterate through the dwf model
For Each oSec In oECV.Sections
Set oCont = oSec.Content
Set oCol = oCont.Objects(0)
For Each oEnt In oCol
For Each oProp In oEnt.Properties
strPropName = oProp.Name
strPropValue = oProp.value
Next
'## Write code here to apply conditions to filter the list
'## and send the extracted info into a text or excel file.
'## Then clear the property name and property value strings
strPropName = ""
strPropValue = ""
Next
Next
Set oProp = Nothing
Set oEnt = Nothing
Set oCol = Nothing
Set oCont = Nothing
Set oSec = Nothing
Set oECV = Nothing
End Sub

I have stripped down some of the code portion as it was specific my purpose. This is mostly in a general form and you may need to add/modify wherever necessary in order to run it in your system.