我们需要什么
阶段1:Inventor插件
如果您对此部分有疑问,或者只是想跳过Inventor插件的创建,请参阅以下zip文件,其中包含我们插件的源代码和二进制文件:InventorThumbnailAddin.zip。
编辑:现在有一个Visual Studio项目模板,您可以用来引导Inventor插件解决方案。
安装了Inventor SDK后,打开Visual Studio并创建一个新的Autodesk Inventor外接程序 项目。我们将其称为InventorThumbnailAddin:

在项目Automation.cs中创建一个新文件,并使用该文件 通过以下实现来定义Automation类:
using System;using System.Runtime.InteropServices;using Inventor;namespace InventorThumbnailAddin{
[ComVisible(true)]public class Automation
{
InventorServer m_server;
public Automation(InventorServer server)
{
m_server = server;
}
public void Run(Document document)
{
string documentFolder = System.IO.Path.GetDirectoryName(document.FullFileName);string imageFilename = System.IO.Path.Combine(documentFolder, "thumbnail.bmp");if (document.DocumentType == DocumentTypeEnum.kPartDocumentObject)
{
Camera camera = m_server.TransientObjects.CreateCamera();
camera.SceneObject = (document as PartDocument).ComponentDefinition;
camera.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation;
camera.ApplyWithoutTransition();
camera.SaveAsBitmap(imageFilename, 256, 256, Type.Missing, Type.Missing);
}
}
public void RunWithArguments(Document document, NameValueMap args)
{
Run(document);
}
}}显示代码
在 StandardAddInServer.cs 文件(应该已经由Visual Studio项目模板创建的文件)中,更改StandardAddInServer 类的实现(但保留为您创建的GUID):
using System;using System.Runtime.InteropServices;using Inventor;namespace InventorThumbnailAddin{
[GuidAttribute("YOUR GUID")]public class StandardAddInServer : Inventor.ApplicationAddInServer
{
private InventorServer m_server;private Automation m_automation;
public StandardAddInServer()
{
}
#region ApplicationAddInServer Members
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
m_server = addInSiteObject.InventorServer;
m_automation = new Automation(m_server);
}
public void Deactivate()
{
Marshal.ReleaseComObject(m_server);
m_server = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
public void ExecuteCommand(int commandID)
{
}
public dynamic Automation
{
get
{
return m_automation;
}
}
#endregion
}}显示代码
接下来,找到Inventor加载项文件(Autodesk.InventorThumbnailAddin.Inventor.addin),并再次使用您自己的GUID将其内容替换为以下XML:
{YOUR GUID}{YOUR GUID}Inventor Thumbnail AddinAddin for generating thumbnail images from Inventor part files.InventorThumbnailAddin.dll
最后,打开项目的属性,导航到Build Events,然后将Post-build 命令替换为以下内容:
call "%VS140COMNTOOLS%vsvars32" x86mt.exe -manifest "$(ProjectDir)InventorThumbnailAddin.X.manifest" -outputresource:"$(TargetPath)"XCopy "$(ProjectDir)Autodesk.InventorThumbnailAddin.Inventor.addin" "$(TargetDir)" /y

这些将确保我们的插件DLL已正确签名,并带有相应的插件文件。
现在,您应该能够构建项目,并在bin / Debug 子文件夹中生成几个文件。我们将在设置设计自动化管道时使用它们。
阶段2:设计自动化流程
在转到各个设计自动化API之前,让我们讨论该服务使用的一些基本概念。总结官方文档,有四种主要类型的对象:引擎,应用程序捆绑包,活动和工作项:
引擎 是指将处理您的任务的实际应用程序,例如Revit或Inventor,
应用程序捆绑包 表示用于使用可用引擎之一执行特定任务的文件(通常是插件二进制文件)的集合,
活动 基本上是一个任务模板,用于定义输入,输出的类型以及 将对其进行处理的应用程序包,
最后,工作项 是具有特定输入和输出(通常是要从中下载或上传到文件的URL)的任务的实例
有一件非常重要的 事情要理解:每个应用程序包,活动或工作项 对象可以具有多个版本,并且为了正确地引用特定的对象,您必须创建一个称为别名的东西,该别名 是引用特定对象的自定义字符串。对象的版本。有点像git标签。例如,当您创建一个名为GenerateThumbnail的活动的第二版时,会有一个端点(我们将在稍后使用),您可以调用它来获得别名-假设prod -指向此版本。然后,为了引用此特定活动,您将使用活动名称,后跟加号和别名,例如 GenerateThumbnail + prod。此外,在某些情况下,您会看到ID带有井号和点的前缀,例如YhryNMLor4R1maFhY4zER8unpISoP5E4.GenerateThumbnail + prod- 该字符串将您完全标识为您的对象。
现在,我们了解了概念,就可以开始设置设计自动化管道了!
获取访问令牌
我们将对所有Forge请求使用两足式身份验证令牌。请遵循本分步指南 ,以获取有关如何获取此类信息的更多详细信息。大多数设计自动化API仅需要一个OAuth范围:code:all,但是在本文中,我们可能还需要使用数据管理API创建存储桶和对象,因此请确保还包括范围 bucket:create,bucket:read,data:create,data:read和data:write。
curl -X POST \
https://developer.api.autodesk.com/authentication/v1/authenticate \
-H 'Content-Type: application/x-www-form-urlencoded' \-d 'client_id=&client_secret=&grant_type=client_credentials&scope=bucket%3Acreate%20bucket%3Aread%20data%3Acreate%20data%3Aread%20data%3Awrite%20code%3Aall'
每当您 在本文的其余部分中看到Authorization:Bearer标头时,请使用响应中的令牌 。
创建一个应用程序包
首先,让我们将Inventor插件二进制文件打包到bundle中。捆绑软件基本上是一个压缩文件夹,其中包含您的二进制文件和一个 描述文件夹内容的PackageContents.xml文件。在我们的示例中,我们将从Inventor插件版本中获取输出文件,并将其压缩在以下文件夹结构中:

请注意,PackageContents.xml 文件在InventorThumbnailAddin.bundle 文件夹下,而不在Contents 文件夹下。
在PackageContents.xml 文件中,我们描述了我们的插件(包括GUID)和插件清单的相对路径。(可选)您还可以在插件后面包含有关作者/公司的其他信息。
准备好我们的InventorThumbnailAddin.bundle.zip文件后,让我们创建一个新的应用程序包(例如 ThumbnailBundle )和第一个版本的新别名。
curl -X POST \
https://developer.api.autodesk.com/da/us-east/v3/appbundles \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{
"id": "ThumbnailBundle",
"description": "Inventor plugin for generating thumbnails from IPT files.",
"engine": "Autodesk.Inventor+23"
}'
响应将是一个JSON,其结构与此类似:
{
"uploadParameters": {
"endpointURL": "https://dasprod-store.s3.amazonaws.com",
"formData": {
"key": "...",
"content-type": "...",
"policy": "...",
"success_action_status": "200",
"success_action_redirect": "",
"x-amz-signature": "...",
"x-amz-credential": "...",
"x-amz-algorithm": "...",
"x-amz-date": "...",
"x-amz-server-side-encryption": "...",
"x-amz-security-token": "..."
}},
"engine": "Autodesk.Inventor+23",
"description": "Inventor plugin for generating thumbnails from IPT files.",
"version": 1,
"id": ".ThumbnailBundle"}显示代码
uploadParameters 是我们需要将应用程序包上传到Design Automation服务可以找到它的位置的参数。做一个POST请求所提供的端点URL,包括所有的论据,表单数据,一个额外的文件 归档,你提供你的包zip文件。
curl -X POST \
https://dasprod-store.s3.amazonaws.com \
-H 'content-type: multipart/form-data' \
-F key=... \
-F policy=... \
-F content-type=... \
-F success_action_status=200 \
-F success_action_redirect= \
-F x-amz-signature=... \
-F x-amz-credential=... \
-F x-amz-algorithm=... \
-F x-amz-date=... \
-F x-amz-server-side-encryption=... \
-F x-amz-security-token=... \
-F file=@/path/to/your/zipfile
如果您不想从命令行上传二进制文件,则可以将curl命令导入邮递员,根据您的uploadParameters填写所有字段,将文件 参数的类型切换为File,然后打开要上传的zip文件。

最后,为了使我们的新应用程序包在后续步骤中可寻址,我们需要创建一个别名-我们 将其称为prod-其第一个版本:
curl -X POST \
https://developer.api.autodesk.com/da/us-east/v3/appbundles/ThumbnailBundle/aliases \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{
"id": "prod",
"version": 1
}'
定义活动
接下来,我们定义一个活动GenerateThumbnail ,它将使用ThumbnailBundle,在输入中期望一个IPT文件,在输出中生成一个BMP文件:
curl -X POST \
https://developer.api.autodesk.com/da/us-east/v3/activities \
-H 'Content-Type: application/json' \-d '{
"commandLine": [
"$(engine.path)\\InventorCoreConsole.exe /i $(args[PartFile].path) /al $(appbundles[ThumbnailBundle].path)"
],
"parameters": {
"PartFile": {
"verb": "get",
"description": "IPT file or ZIP with assembly to process"
},
"OutputBmp": {
"zip": false,
"ondemand": false,
"optional": true,
"verb": "put",
"description": "Generated thumbnail",
"localName": "thumbnail.bmp"
}
},
"engine": "Autodesk.Inventor+23",
"appbundles": [".ThumbnailBundle+prod"],
"description": "Generate thumbnails for IPT files (Inventor 2019).",
"id": "GenerateThumbnail"
}'显示代码
让我们看一下JSON的不同部分:
我们给我们的活动一个ID 和描述
我们指定的列表appbundles 它使用(不要忘了替换 与创建您的应用程序软件包时,您收到的哈希值),而发动机 将运行它们
我们定义了一个名为PartFile的输入参数
我们将一个名为OutputBmp的输出参数定义 为一个名为thumbnail.bmp的文件,该文件 可以(可选)由活动生成
最后,我们指定一个CommandLine ,它将使用我们的参数运行实际的引擎可执行文件
与应用程序包一样,我们必须创建一个别名,该别名将指向活动的第一个版本:
curl -X POST \
https://developer.api.autodesk.com/da/us-east/v3/activities/GenerateThumbnail/aliases \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{
"id": "prod",
"version": 1
}'
执行工作项
在运行GenerateThumbnail 活动之前,我们需要准备输入和输出URL。您可以使用任何支持签名URL的存储解决方案 进行预授权的下载和上传。在我们的示例中,我们将利用数据管理API中的POST / oss / v2 / buckets /:bucketKey / objects /:objectKey / signed 端点,创建一个具有 对现有IPT文件的读取访问权限的签名URL,以及一个具有读写 访问权限的签名URL 到可以将缩略图上传到的位置。
curl -X POST \'https://developer.api.autodesk.com/oss/v2/buckets//objects//signed?access=read' \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{}'
curl -X POST \'https://developer.api.autodesk.com/oss/v2/buckets//objects//signed?access=readwrite' \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{}'
在准备好URL的情况下,让我们创建工作项:
curl -X POST \
https://developer.api.autodesk.com/da/us-east/v3/workitems \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \-d '{
"activityId": ".GenerateThumbnail+prod",
"arguments": {
"PartFile": {
"url": "",
"zip": false
},
"OutputBmp": {
"url": "",
"verb": "put"
}
}
}'
响应应类似于以下内容:
{
"status": "pending",
"stats": {
"timeQueued": "..."
},
"id": ""}
使用您自己的工作项目ID,然后可以查询其状态:
curl -X GET \https://developer.api.autodesk.com/da/us-east/v3/workitems/ \-H 'Authorization: Bearer '
如果任务成功完成,您应该看到类似以下的响应:
{
"status": "success",
"reportUrl": "...",
"stats": {
"timeQueued": "...",
"timeDownloadStarted": "...",
"timeInstructionsStarted": "...",
"timeInstructionsEnded": "...",
"timeUploadEnded": "..."
},
"id": ""}
如果有任何问题, reportUrl 字段将链接到带有其他信息的文本文件。
就是这样。您创建的第二个预签名URL(“ <您的上传文件url>”)现在应指向带有输入Inventor零件文件缩略图的位图文件。