Introduction
This session focuses on technologies that can be used to view and access DWF content downstream from Autodesk’s design products. Yesterday’s session focused on DWG – we’re going to take the model containing 3D solids that we created during that session and publish it to DWF, adding custom metadata. We can then look at how to harness this data in lightweight applications, whether to access it non-graphically or to view it.
Firstly, why are we using 3D solids in this example? The choice was somewhat arbitrary – the point is really to demonstrate the ability to access properties of objects stored in a DWG file without AutoCAD running – but it does suit our overall purpose for a few reasons:
- As 3D entities, 3D solids allow us to evaluate the 3D capabilities of the viewing technologies we’re looking at.
- While they have precise geometric properties (such as volume), as DWF is not a double-precision format we’re going to focus on accessing data of a less precision-oriented nature: the solids’ materials.
A quick word on the programming technology used in this demonstration. The code samples are in a mixture of languages: the AutoCAD .NET sample that adds metadata is in C#, the downstream DWF Toolkit sample is in C++ (for the DWF Toolkit component) and in VB.NET (for the user interface), while the Design Review and Freewheel samples are in HTML.
These handouts, along with the sample projects demonstrated within them, have been posted to my blog: http://blogs.autodesk.com/through-the-interface. Only significant portions of code will be highlighted in this document, and even those should not need to be typed back in. :-)
Creating DWFx files
In AutoCAD 2008, the PUBLISH command creates DWF files. In the future, however, Autodesk will increasingly publish to the DWFx format, one which more based on industry (particularly Microsoft) standard technology. For more information on DWFx, search for it on the Autodesk website, and search for XPS on Microsoft’s website.
To create DWFx files from AutoCAD 2008 today, you need to download and install the DWFx Printer Driver, which can then be selected from the PLOT dialog:
http://usa.autodesk.com/adsk/servlet/ps/dl/item?siteID=123112&id=9124615&linkID=9240618
AutoCAD’s PLOT mechanism does support multiple sheet printing, but won’t let you plot both 2D and 3D sheets to the same file (unlike the PUBLISH command, which does).
The other technique for creating DWFx files is to save DWFs from Design Review 2008 as DWFx. This approach has the benefit of including any 3D and metadata content in the resultant DWFx file.
Publishing DWF and plotting DWFx from AutoCAD
Now we’re going to take a quick look at the contents of DWF and DWF files. For the purpose of this example, we’ll simply PUBLISH our 2D layout to DWF and PLOT it using the DWFx driver to DWFx.
Let's launch PUBLISH and remove the modelspace sheet, and then hit the “Publish” button, selecting a DWF file as the destination (in our case solids-Layout1.dwf):
Figure 1 – publishing a single-sheet DWF file from AutoCAD
Now we launch the PLOT command and select the DWFx driver, using it to PLOT our 2D layout to a DWFx file (in our case solids-Layout1.dwfx):
Figure 2 – plotting a single-sheet DWFx file from AutoCAD
So now we have two files, solids-Layout1.dwf & solids-Layout1.dwfx. To look at their contents, we rename them to .zip and open them with WinZIP (both DWF and DWFx are package formats which are implemented as ZIP files of different types of data):
Figure 3 – the contents of our DWF file
The interesting piece of our 2D DWF file is the .w2d file. If we extract it and open it in Notepad, we can see it’s contains both ASCII and binary information:
Figure 4 – the contents of the W2D stream of our DWF
The big change between DWF and DWFx is the move away from the W2D format (which harks back to the days of the Whip! format) and adopting the more modern XML-based XPS format.
Let’s take a look at our DWFx file, now:
Figure 5 – the contents of our DWFx file
We can see straight away that there’s more to it: to start with there are more files included (and the DWFx file is 16KB while our DWF file is 8KB, but the ratio shifts as the files get larger – the overhead in this example is high relative to the amount of content we’re storing). The interesting file is the selected XML file – let’s load that into Internet Explorer to view it:
Figure 6 – the contents of the W2X stream of our DWFx
This XML content defines the 2D geometry in our DWFx file, and is read by the XPS Viewer when we load it.
That’s it for our brief look at the DWF and DWFx formats. We could certainly go into greater depth – especially when it comes to the storage of metadata and 3D, but this is left as an exercise for the reader.
Publishing additional metadata to DWF files from AutoCAD
Now we’re going to take a look at the more advanced capabilities of the PUBLISH command in AutoCAD. Our DWG file contains 3D content in the modelspace and 2D content in its paperspace layout, and we ideally want a DWF file containing both. Not only that, but we’d like to attach metadata to both our 2D and 3D geometry.
We’re going to use an AutoCAD .NET module to attach this information. Here’s the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Publishing;
namespace DWFMetadata
{
public class SolidsPublisher : IExtensionApplication
{
private bool mbPublishingTo3D = false;
public SolidsPublisher() {}
public void Initialize()
{
// Register for publishing events
Publisher pb = Application.Publisher;
pb.AboutToBeginPublishing +=
new AboutToBeginPublishingEventHandler(
OnAboutToBeginPublishing
);
pb.BeginEntity +=
new BeginEntityEventHandler(
OnBeginEntity
);
pb.BeginSheet +=
new BeginSheetEventHandler(
OnBeginSheet
);
}
public void Terminate()
{
// Unregister from publishing events
Publisher pb = Application.Publisher;
pb.AboutToBeginPublishing -=
new AboutToBeginPublishingEventHandler(
OnAboutToBeginPublishing
);
pb.BeginEntity -=
new BeginEntityEventHandler(
OnBeginEntity
);
pb.BeginSheet -=
new BeginSheetEventHandler(
OnBeginSheet
);
}
// Although an empty handler, responding to
// this events allows the subsequent ones
// to fire
void OnAboutToBeginPublishing(
object sender,
AboutToBeginPublishingEventArgs e
){}
// Check whether we're publishing to 2D or 3D
private void OnBeginSheet(
object sender,
PublishSheetEventArgs e
)
{
mbPublishingTo3D = e.PublishingTo3DDwf;
}
// This event does the real work
private void OnBeginEntity(
object sender,
PublishEntityEventArgs e
)
{
try
{
Solid3d solid = e.Entity as Solid3d;
if (solid != null)
{
if (mbPublishingTo3D)
{
// Adding 3D metadata is easy
e.Add3DDwfProperty(
"Material",
"Name",
solid.Material
);
e.Add3DDwfProperty(
"Identity",
"Handle",
solid.Handle.ToString()
);
}
else
{
// 2D is more complicated...
ObjectIdCollection objIds =
new ObjectIdCollection();
System.Int32 nodeId =
e.GetEntityNode(
e.Entity.ObjectId,
objIds
);
if (nodeId <= 0)
{
// Create node with next sequential number
// and a unique name
nodeId =
e.GetNextAvailableNode();
string strUnique =
e.UniqueEntityId;
string nodeName =
"ASDK" + strUnique;
DwfNode node =
new DwfNode(nodeId, nodeName);
// Create a string property in
// the Material category
EPlotProperty matprop =
new EPlotProperty(
"Name",
solid.Material
);
matprop.Category = "Material";
matprop.Type = "string";
// Create a string property in
// the Identity category
EPlotProperty handprop =
new EPlotProperty(
"Handle",
solid.Handle.ToString()
);
handprop.Category = "Identity";
handprop.Type = "string";
// Create a property bag, containing
// the properties and references
EPlotPropertyBag bag =
new EPlotPropertyBag();
// This should set the ID of the property bag,
// to be used in the property definition
bag.Id = nodeName;
// Add it to the object/instance references
bag.References.Add(nodeName);
// The properties themselves needs adding
bag.Properties.Add(matprop);
bag.Properties.Add(handprop);
// Add properties and relationships to the DWF
e.AddPropertyBag(bag);
e.AddNodeToMap(
e.Entity.ObjectId,
objIds,
nodeId
);
e.AddPropertiesIds(bag, node);
}
}
}
}
catch (System.Exception ex)
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("\nException: " + ex.Message);
}
}
}
}
Once we build this into a managed assembly we can simply load it with NETLOAD – the module initialization takes care of adding the event handlers into the publishing system, so there’s no need to call a command.
Now let’s launch the PUBLISH command and create our DWF file.
AutoCAD’s PUBLISH command allows us to generate DWF files that contain both 2D and 3D data (this is not possible using PLOT – it has to be PUBLISH). All we need to do is set the Model sheet to be a “3D DWF”:
Figure 7 – setting 3D content in AutoCAD’s PUBLISH dialog
Once we hit the “Publish” button and select our DWF file (solids.dwf, as that’s the name our embedded viewing sample will look for later).
We can now load the DWF file in Design Review and take a look at the 3D and 2D content.
Figure 8 – 3D DWF content with metadata in Design Review
We can see that as we select a spherical object in the model, we can see the associated metadata in the “Properties” view on the left. The key thing for this demo is that we have identical “identity” metadata attached to the 2D content, so we can relate the two views.
Figure 9 – 2D DWF content with metadata in Design Review
Sure enough, we see the same “material” and ‘identity” metadata associated with objects in both 2D and 3D content of the DWF.
Before we move on to using the DWF Toolkit to mine the content, let’s quickly Save As DWFx from Design Review 2008 and see the capabilities of the XPS Viewer.
Figure 10 – Save As DWFx from Design Review 2008
The file created (solids.dwfx) is actually about the same size as the equivalent DWF file (solids.dwf), but that’s also because the 3D and metadata content is implemented in the same way (as XPS doesn’t currently support either).
When we load it into the XPS Viewer (by browsing to it from Internet Explorer, assuming you’ve installed the XPS Viewer from http://www.microsoft.com/whdc/xps/viewxps.mspx or are using Vista, which has it pre-installed), you see a basic interface for 2D content:
Figure 11 – using the XPS Viewer to view our DWFx file
Note that our initial 3D sheet is replaced by one pointing us at the Design Review download location, as the XPS Viewer doesn’t currently support 3D content.