不需要Inventor操作iProperties

原文地址: http://modthemachine.typepad.com/my_weblog/2010/03/iproperties-without-inventor-apprentice.html

大家知道,通常安装了Inventor得到的I叫做Inventor API ,拥有全部API功能。而如果只安装Inventor View (免费的),则可以得到Apprentice(学徒服务器)API,它是Inventor API的子集。对于iProperties,它具备了和Inventor API相同的功能,即包括读和写。因此,在很多情况下,你可以选用学徒服务器,这样就不用购买安装Inventor了。


Continuing with the iProperty theme of some recent posts I want to talk about the ability to work with iProperties without using Inventor. You can do this using a component known as Apprentice. Apprentice doesn’t have a user-interface but is a component use can only use through programming. Apprentice is installed as part of Inventor and is also installed as part of Inventor View (www.autodesk.com/inventorview).  This means if Inventor or Inventor View is installed on a machine you’ll have access to the Apprentice component and any Apprentice dependent program will work.  The fact that it’s available with Inventor View is interesting since Inventor View is free.

这里提到本文是基于前面几篇文章,但介绍如何不安装Inventor就可操作iProperties,即学徒服务器。它是个组件,可以在你的程序使用。它作为Inventor View(www.autodesk.com/inventorview).的一部分安装。

What is Apprentice? 
Apprentice is a programming component that provides access to Inventor files.  Using Apprentice you can open Inventor files, query information from them, and in a few cases, edit data.  Apprentice supports a small subset of the functionality that Inventor supports and is primarily read-only with just a few exceptions.  The primary things it provides access to are:

何为学徒服务器?它是用来访问Inventor功能的组件。你可以打开文档,查询信息,某些对象还可以编辑。它是Inventor API的一个子集。主要由以下内容:

  • Geometry of solids and surfaces (B-Rep)
  • Assembly structure
  • File references
  • Bill Of Material
  • Printing
  • Attributes
  • iProperties

You’ll notice that there are a lot of things missing.  Apprentice is designed to be smaller and faster than Inventor and achieves this by providing having less code and operating on smaller portions of a document.  Even things that are exposed in Apprentice will often have limitations that aren’t present in Inventor.  For example, let’s say you have an extrude feature that you’ve added an attribute to in Inventor.  Using Apprentice, you can open that part and you can query for all attributes and get the attribute set associated with the feature.  However when you try and get the feature from the attribute set it’s not available because Apprentice doesn’t support features.  Apprentice is good at what it does but does have limitations you need to be aware of before attempting to use it in a project.

显然,子集就意味着它比Inventor API要少得多,但快得多。但也有很多限制。例如,可以通过学徒服务器打开零件文档,查询所有属性attributes 或某个特征的属性。但无法通过属性返回对应的特征。所以,需要注意,学徒服务器功能是很有限的。你得根据需求做选用。

Using Apprentice 
Apprentice uses much of the same code and components as Inventor.  Because of this there are conflicts if you try running both of them within the same process.  Remember that Apprentice always runs within the process of another program.

This means:

  • You can’t use Apprentice with Inventor’s VBA.
  • You can’t use Apprentice in an Add-In.

基本上,学徒服务器的用法和Inventor API是类似的。但不要试图在Inventor API 插件程序进程中(add-in dll)或Inventor自己的VBA环境中使用它。另外,学徒服务器总是进程外的。

Using VBA within another application, for example Excel, is ok since Apprentice will be running within the Excel process and not Inventor’s process.  Another common use of Apprentice is within standalone exe programs where Apprentice will be running within the exe process.  The example that part of this post is a standalone exe.

当然,在其它程序例如Excel的VBA中,可以使用,因为Excel的进程必然和Inventor的进程不同,另外,通常人们用得比较多的方式是独立的EXE程序。

Another limitation of Apprentice is that it can’t migrate files.  Migration occurs when you open a file that is older than the current version of Inventor.  For example, if I have an Inventor 2009 file on disk and open it with Inventor 2010 the data is migrated to the current version of Inventor.  Inventor is smart and only migrates what’s necessary based on what you’re currently doing with the file.  However, if you save the file, the entire file must be migrated to the current version before it is saved.  Apprentice can open and query older files but nothing can be changed and saved because that would require migration.  You can use the NeedsMigrating property to determine if a file can be edited and saved.  This is demonstrated in the sample at the bottom of this post.

还有一点是学徒服务器无法移植文件。它可以打开老版本的文件,但无法修改和保存。有个标志变量NeedsMigrating 可以用来判断。假设碰到这样的文件,又需要适当修改的话,需要先用Inventor产品打开移植,另存为新版本的文件。

Connecting to Apprentice 
Here’s an example of how to use Apprentice within a VB Express Windows forms application.  To enable the use of Apprentice in your program you need to add a reference to the Inventor Object Library.  The Inventor Object Library is used for both Inventor and Apprentice even though Apprentice only supports a small subset of the entire library.  You can add the reference using the Project | Add Reference… command.  In the example below I’ve select the version 14.0.0 (Inventor 2010) interop.

如何写个学徒服务器的程序?首先要引用。从2009开始,学徒服务器是集成在Inventor API之中。所以,和你写Inventor API程序一样,先得添加Autodesk.Inventor.Interop.  如下图,选择了2010的interop。

AddReference

Below is the minimum code needed to load Apprentice.  This declares and creates a new ApprenticeServerComponent object.  The ApprenticeServerComponent object is similar to the Inventor Application object.  It is the top-level object for Apprentice.

接着新建ApprenticeServerComponent,它是使用学徒服务器的入口。还记得我们如何编写Inventor API程序么,首先需要获得InventorApplication。而ApprenticeServerComponent就类似于这种顶层入口对象。

Dim apprentice As New Inventor.ApprenticeServerComponent

It’s also common to declare the variable and then create apprentice separately, like shown below.  I do this in the sample program where the variable is declared as a member variable so I can reuse it.

Dim apprentice As Inventor.ApprenticeServerComponent 
apprentice = New Inventor.ApprenticeServerComponent

当然,也可以分两行进行这个新建工作。

When you create the ApprenticeServerComponent object the Apprentice dll’s are loaded into your process and you can access the apprentice functionality through the properties and methods of the object.

Once you have an instance of the ApprenticeServerComponent, the next step is to open an Inventor document.  You do this using the Open method of the ApprenticeServerComponent object, as shown below.

现在,就可以使用ApprenticeServerComponent打开一个文件了。如下面的代码。是不是有点眼熟?和我们用Inventor API文档打开类似。但学徒服务器没有界面功能,因此谈不上文档可见不可见。因此就是打开而已。

' Open a file using Apprentice. 
Dim apprenticeDoc As Inventor.ApprenticeServerDocument 
apprenticeDoc = apprentice.Open(Filename)

Apprentice supports the ApprenticeServerDocument and ApprenticeServerDrawingDocument types.  The ApprenticeServerDocument object can represent all document types.  the ApprenticeServerDrawingDocument can be used to access some drawing specific functionality.

ApprenticeServerDocument 表示所有类型的文件,而ApprenticeServerDrawingDocument 单独对工程图文档操作。

Using iProperties With Apprentice 
Once you have the document, accessing iProperties is the same in Apprentice as it is in Inventor.  The example below gets the summary information property set and changes the value of the “Company” property.  The previous posts on using the API with iProperties also apply to Apprentice.

以下是如何访问和修改iProperties。和我们以前学习到的很类似。也是特性集合,特性集,特性。

' Get the "Summary Information" property set.  
Dim summaryPropSet As Inventor.PropertySet  
summaryPropSet = apprenticeDoc.PropertySets.Item( _  
                           "Inventor Document Summary Information")  

' Get the "Company" property.  
Dim companyProp As Inventor.Property  
companyProp = summaryPropSet.Item("Company")

' Change the value of the iProperty.  
companyProp.Value = "Widgets R Us"

Saving Changes With Apprentice 
When you use Inventor to change iProperties, you save the changes by saving the document.  In Apprentice there’s a much more efficient way of saving iProperty changes.  Instead of saving the entire document it’s possible to just save the iProperties.  You do this using the FlushToFile method of the PropertySet object, as illustrated below.

如果你准备保存修改iProperties的结果,则注意不能直接save。需要先调用一句PropertySets.FlushToFile ,通知修改写入文件。

' Save the iProperty changes.  
apprenticeDoc.PropertySets.FlushToFile()

Example Program 
Here’s an example program written using Visual Basic 2008 Express.  It demonstrates the functionality discussed above.  It’s a simple program that gets all of the part files in a specified directory, changes an iProperty value and saves it.  Even though it’s simple it does demonstrates the functionality needed to access iProperties using Apprentice.  You’ll need to modify it to fit your specific requirements.

这里还提供了一个Visual Basic 2008 Express的工程样例example program。包括了前面提到的内容,工大家参考。


inventor学徒服务器Apprentice inventor学徒服务器Apprentice全文共12页,当前为第1页。 Agenda Apprentice Definition Apprentice Functionalities Guidelines Apprentice vs. Inventor: Differences Saving Files with Apprentice inventor学徒服务器Apprentice全文共12页,当前为第2页。 Apprentice Inventor Data Inventor Application Client App Add-In VBA Apprentice Add-In Client App inventor学徒服务器Apprentice全文共12页,当前为第3页。 Apprentice Server Apprentice is an ActiveX component. Runs In-process with the client using it. Apprentice Server provides a subset of the Inventor API. Can be used standalone (doesn't require Inventor). Provided at no cost (installed as part of Inventor View). Apprentice can be very efficient for certain tasks in comparison of connecting to Inventor out-of-process. inventor学徒服务器Apprentice全文共12页,当前为第4页。 Apprentice Functionalities Provides read-only access to: Assembly structure B-Rep Drawing sheets and views (limited access) iParts iAssemblies BOM Provides read / write access to iProperties, attributes, and . inventor学徒服务器Apprentice全文共12页,当前为第5页。 Apprentice - Guidelines Should NOT be used in-process to Inventor. Not in an Add-In (exe type of Add-In is ok). Not through Inventor's VBA. Prior to Inventor 11, Apprentice was the only way to edit . (Design Assistant is build on top of Apprentice.) Prior to 2008, the Apprentice functionalities were provided in its own type library. Now Autodesk Inventor type library contains all of the Apprentice functionality Add a reference to Inventor.Interop for an Apprentice project. inventor学徒服务器Apprentice全文共12页,当前为第6页。 Apprentice vs. Inventor: differences Instantiated using "new ApprenticeServerComponent" No Documents collection in Apprentice Document object: ApprenticeServerDocument (*.ipt &*.iam) ApprenticeServerDrawingDocument (*.idw) Apprentice cannot save a a previous version, i.e. that is not migrated. You will need to migrate the to save it with Apprentice: To migrate a file, open it with Inventor, save it and close it. inventor学徒服务器Apprentice全文共12页,当前为第7页。 Appren
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值