By Xiaodong Liang
(Cross-posted to the Cloud & Mobile DevBlog )
Cloud & mobile (C&M) is an exciting technology. My colleagues have created many excellent tutorials on various aspects that can be used to integrate Autodesk products with C&M. I believe some Navisworks developers are thinking the applications to connect Navisworks with C&M. Recently, I started to try my hands on it and created a first demo. In addition, my colleague Simon Bee in the engineer team has delivered another demo on the DevCamp in June this year. I will post them in a series as a start for the practice of Navisworks.
Here is the first post that introduces my sample.
Target
The diagram below illustrates what the sample targets to.
The figures above refer to the two pictures in the links:
www.uimaker.com/uimakerhtml/uidesign/uiphone/2011/1114/25549.htm
http://it.southcn.com/9/2009-09/11/content_5759707_2.htm
In the office, we open the Navisworks model, switch to each saved viewpoints, export to the image and upload it to the cloud. When we are working on-site, we can use mobile to connect with the cloud, download the images and view the viewpoints that we are interested in. Actually, you can apply this demo with other Autodesk products such as Inventor, Revit, uploading the latest list of viewpoints and check them anywhere with the mobile. I hope this tiny demo would be helpful for you to get started with Simon’s demo : ) – that is more interesting!
In this practice, we use Amazon cloud and the services S3. At the mobile end, it is Android. Please make sure you have the following environments:
- Navisworks 2013 on your machines in the office
- Account of Amazon cloud and can have S3 service
- Visual Studio 2010
- Amazon SDK for Visual Studio
- Eclipse
- Android SDK for Eclipse (ADT Plugin)
- Amazon SDK for Eclipse
- A mobile of Android. This is just for the real test. In our developing, the emulator AVD (Android Virtual Device) is used.
We will not introduce in detail on how to setup these environments. If you are new to cloud and mobile technology, I would suggest you get started with the tutorials on Android Development and Amazon Cloud .
Creating Navisworks .NET plug-in
First we need to create a .NET plug-in of Navisworks, adding a button in Add-Ins tab to execute our command. Simply, create a class from AddInPlugin. Add the basic references of Navisworks API. The references are required:
- Autodesk.Navisworks.Api: for plug-in, saved viewpoints
- Autodesk.Navisworks.ComApi : for export image
- Autodesk.Navisworks.Interop.ComApi: for export image
- AWSSDK: for cloud of Amazon.
Navisworks 2013 has exposed the .NET API for viewpoints and saved viewpoints. These are some posts on the relevant topic on AEC blog. However, we still need to use COM interop to access the ability of exporting image. In this demo, we iterate the collection of each save viewpoint, export the current viewpoint to the image.
After that, the image will be uploaded to the cloud. We will firstly check if a bucket named “nw_demo_test” exists. If not, create one. To category the saved viewpoints from different files, we create a folder for each model in the bucket.
Of course, we could use an Automation application (calling the plug-in) to do all the jobs above, when we are sleeping.
Following is the source code. It self-explains the functionalities.
// namespaces of Navisworks
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
using Autodesk.Navisworks.Api.DocumentParts;
using ComBridge = Autodesk.Navisworks.Api.ComApi.ComApiBridge;
using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;
//namespaces of Amazon cloud
using Amazon.S3;
using Amazon.S3.Model;
[PluginAttribute("NW_API_Cloud_Mobile",
//4 character Developer ID or GUID
"ADSK",
//The tooltip for the item in the ribbon
ToolTip = "demo of Navisworks and C & M",
//Display name for the Plugin in the Ribbon
DisplayName = "Autodesk C&M")]
public class Class1 : AddInPlugin
{
// execute the plug-in command
public override int Execute(params string[] parameters)
{
// export the saved viewpoints to images
dumpSavedVP();
// upload images to cloud
uploadToCloud();
return 0;
}
#region "produce images"
//locations to export the images
static string _snapshos_location = "c:\\temp\\";
// array of names of the saved viewpoints
List<string> _fileArray = new List<string>();
// export the saved viewpoints to images
private void dumpSavedVP()
{
// get the active document
Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
// get the state of COM
ComApi.InwOpState10 oState =
ComBridge.State;
// get the IO plugin for image
ComApi.InwOaPropertyVec options =
oState.GetIOPluginOptions("lcodpimage");
// configure the option "export.image.format"
//to export png
foreach (ComApi.InwOaProperty opt in
options.Properties())
{
if (opt.name == "export.image.format")
opt.value = "lcodpexpng";
}
// swtich to the saved viewpoint
foreach (SavedViewpoint oSVP in
oDoc.SavedViewpoints.ToSavedItemCollection())
{
string svpName = oSVP.DisplayName;
// set the current viewpoint oDoc.SavedViewpoints.CurrentSavedViewpoint = oSVP;
// the image name
string tempFileName =
_snapshos_location + svpName + ".png";
// delete the existing image if there is.
if (System.IO.File.Exists(tempFileName))
System.IO.File.Delete(tempFileName);
try
{
//export the viewpoint to the image
oState.DriveIOPlugin("lcodpimage",
tempFileName, options);
System.Drawing.Bitmap oBitmap =
new System.Drawing.Bitmap(tempFileName);
System.IO.MemoryStream ImageStream =
new System.IO.MemoryStream();
oBitmap.Save(ImageStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
// add the image name to the list for use
//of uploading to cloud
_fileArray.Add(svpName);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
#endregion
#region "upload images"
//Amazon client object
static AmazonS3 _s3Client;
// Your Access Key To Cloud
static string accessKey = "Your Access Key To Cloud";
//Your Secrete Key To Cloud
static string secreteKey = "Your Secrete Key To Cloud";
//the name of the bucket we will upload image to
static string bucketName = "nw_demo_test";
//the name of folder to store the images of each model
static string folderName = "";
// upload images to cloud
private void uploadToCloud()
{
// connect to the cloud
_s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(
accessKey, secreteKey);
Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
// get the model file name
folderName =
System.IO.Path.
GetFileNameWithoutExtension(oDoc.FileName) + "/";
//Create bucket and folder
if (!createBucketAndFolder())
{
return;
}
try
{
// connect to the service to upload by S3
using (ListBucketsResponse response =
_s3Client.ListBuckets())
{
foreach(string oEachImage in
_fileArray)
{
// new a PutObjectRequest
PutObjectRequest por_file =
new PutObjectRequest();
//put into this bucket
por_file.WithBucketName
(bucketName);
//the key:
// "/" means the folder
por_file.WithKey(folderName +
oEachImage + ".png");
// the image to upload
por_file.WithFilePath(
_snapshos_location +
oEachImage + ".png");
// upload method
_s3Client.PutObject(por_file);
}
}
}
catch (AmazonS3Exception amazonS3Exception)
{
MessageBox.Show(amazonS3Exception.ToString());
}
}
//create the bucket and folder
private bool createBucketAndFolder()
{
try
{
// connect to the service to upload by S3
using (ListBucketsResponse response =
_s3Client.ListBuckets())
{
bool hasBucket = false;
//check if the bucket exists
////note: case sensitive
foreach (S3Bucket bucket in
response.Buckets)
{
if (bucket.BucketName ==
bucketName)
{
hasBucket = true; ;
break;
}
}
if (!hasBucket)
{
// create the bucket
PutBucketRequest oBucketRequest =
new PutBucketRequest();
oBucketRequest.BucketName = bucketName;
_s3Client.PutBucket(oBucketRequest);
}
//check if folder exists
if( ! S3ObjectExists(bucketName,folderName))
{
// a folder is an item with the name
//ended by "/"
PutObjectRequest por_folder =
new PutObjectRequest();
por_folder.WithBucketName(bucketName);
por_folder.WithKey(folderName);
por_folder.WithContentBody(string.Empty);
// create the item >> folder
_s3Client.PutObject(por_folder);
}
return true;
}
return false;
}
catch (AmazonS3Exception amazonS3Exception)
{
MessageBox.Show(amazonS3Exception.ToString());
return false;
}
}
// refer to this post
// http://www.billrowell.com/2010/12/21/determine-if-amazon-s3-object-exists-with-asp-net-sdk/
// check if an S3 item exists
public bool S3ObjectExists(string bucket, string key)
{
GetObjectRequest request =
new GetObjectRequest();
request.BucketName = bucket;
request.Key = key;
try
{
// try if the S3Response
S3Response response =
_s3Client.GetObject(request);
if (response.ResponseStream != null)
{
// the item exists
return true;
}
}
catch (AmazonS3Exception)
{
return false;
}
catch (Exception)
{
return false;
}
return false;
}
#endregion
}
Open a Navisworks file, run the plug-in. The images of each saved viewpoint will be uploaded to the cloud.
In the next post, we will see the section of creating Android project.