不需要Inventor操作iProperties

使用学徒服务器操作iProperties
本文介绍如何在不安装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。包括了前面提到的内容,工大家参考。


Agenda Apprentice Definition Apprentice Functionalities Guidelines Apprentice vs. Inventor: Differences Saving Files with Apprentice 第1页/共11页 inventor学徒服务器全文共11页,当前为第1页。 Apprentice Inventor Data Inventor Application Client App Add-In VBA Apprentice Add-In Client App 第2页/共11页 inventor学徒服务器全文共11页,当前为第2页。 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. 第3页/共11页 inventor学徒服务器全文共11页,当前为第3页。 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 file references. 第4页/共11页 inventor学徒服务器全文共11页,当前为第4页。 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 file references. (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. 第5页/共11页 inventor学徒服务器全文共11页,当前为第5页。 Apprentice vs. Inventor: differences Instantiated using "new ApprenticeServerComponent" No Documents collection in Apprentice Document object: ApprenticeServerDocument (*.ipt &*.iam) ApprenticeServerDrawingDocument (*.idw) Apprentice cannot save a file from a previous version, i.e. that is not migrated. You will need to migrate the file prior to save it with Apprentice: To migrate a file, open it with Inventor, save it and close it. 第6页/共11页 inventor学徒服务器全文共11页,当前为第6页。 Apprentice - Example Pri
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
<think>我们正在处理一个关于AutodeskInventor的问题:用户希望同时修改文件名和零件名。根据引用内容,特别是引用[3]提到在工程图环境中零件代号可以同步关联工程图名称,我们可以推断Inventor有同步更新名称的机制。但是,用户的问题是关于同时修改文件名(即磁盘上的文件名称)和零件名(即Inventor内部属性中的零件名称)。引用[3]中的VB代码示例展示了如何设置零件代号(PartNumber)并同步到工程图名称,但这里用户需要的是同时修改文件名和零件名。分析:1.在Inventor中,文件名是存储在文件系统中的,而零件名(或零件代号)是存储在Inventor文件内部的属性。2.通常,修改文件名需要在文件系统中进行重命名操作,而修改零件名需要在Inventor内部通过属性设置。3.引用[3]提到了一种同步:将零件代号与工程图名称同步。但是,这并直接涉及文件名。然而,Inventor提供了一种机制:当你在Inventor中通过“另存为”重命名文件时,可以选择更新内部链接和引用。但是,这并会自动修改零件名(即属性中的名称)。用户要求“同时操作”,即希望一步操作同时修改两个名称。这需要结合Inventor的API或者特定的操作流程。根据引用[2]和[3],我们可以知道Inventor有API可以操作零件属性。引用[3]的VB代码:oPart.PartNumber ="MyPartNumber"oPart.UpdateNameSynchronously这里UpdateNameSynchronously方法用于同步更新名称(可能是工程图名称与零件代号同步)。但是,修改文件名(磁盘上的名称)需要调用另存为操作。因此,我们可以通过以下步骤实现:步骤1:在Inventor中打开文件。步骤2:修改零件属性(如零件代号、零件名称等)。步骤3:使用另存为操作,将文件保存为新的文件名,并且在保存时选择更新内部链接(以确保所有引用更新)。步骤4:删除旧文件(可选)。但是,用户希望同时修改,即在一个操作中完成。我们可以考虑使用Inventor的iLogic规则(引用[5])或者API脚本来实现自动化。解决方案:1.使用iLogic规则(无需编程):在Inventor中,可以创建iLogic规则来修改零件属性,然后调用另存为。但是,iLogic规则本身直接支持删除原文件,所以可能需要额外的脚本。2.使用API编写外部程序:通过Inventor API,我们可以打开文件,修改属性,然后另存为新文件名,最后关闭文件并删除原文件。然而,在Inventor中,直接重命名文件(在文件系统中)是推荐的,因为Inventor的文件可能被其他文件引用,直接重命名会导致引用丢失。因此,正确的做法是通过Inventor的“另存为”来重命名。同时修改零件名(这里可能指零件代号PartNumber或描述性名称)和文件名的步骤(手动):1.打开Inventor文件。2.在“管理”选项卡中,找到“iLogic”面板,点击“iLogic浏览器”打开。3.在iLogic浏览器中,可以创建新规则,使用以下代码(示例):'设置新的零件代号(PartNumber)和描述(Description)等属性iProperties.Value("Project","PartNumber")= "新的零件代号"iProperties.Value("Project", "Description")= "新的零件名称"'然后执行另存为操作DimnewFileNameAs StringnewFileName= "新的文件名"'注意:这里需要包含路径,且扩展名变(如.ipt)'调用Inventor的另存为ThisDoc.Document.SaveAs(newFileName,False)'False表示保留旧文件的引用(即断开与旧文件的链接,使用新文件)'注意:保存后,当前打开的文件已经变为新文件,旧文件仍然存在。'如果需要删除旧文件,可以使用文件操作(但需谨慎,因为可能被其他文件引用)' System.IO.File.Delete("旧文件完整路径")4.运行此规则,将同时修改内部属性和文件名。但是,上述规则中,删除旧文件需要知道旧文件的路径(ThisDoc.Path可以获取当前文档的路径,但执行SaveAs后当前文档路径已经改变)。因此,删除旧文件的操作需要在SaveAs之前保存旧路径,并在SaveAs后删除。但注意:如果旧文件被其他装配体引用,删除后会导致装配体丢失引用。因此,删除操作需要用户确认。另一种更安全的方法:删除旧文件,而是通过Inventor的打包功能(PackandGo)来重命名并更新引用。打包功能可以复制文件并重命名,同时更新所有引用。但是,打包功能通常用于整个项目,而是单个文件。考虑到用户可能只需要修改当前文件,我们可以使用以下手动步骤(使用脚本):步骤1:打开Inventor文件。步骤2:修改零件属性(通过iProperties对话框)。步骤3:使用“文件”->“另存为”,保存为新的文件名,在“另存为”对话框中,确保勾选“保存时更新所有引用”(这样,如果当前文件被其他装配体引用,装配体将引用新文件)。步骤4:关闭当前文件,删除旧文件(如果确定没有其他引用)。但是,用户要求同时操作,所以使用iLogic规则可以一步完成。根据引用[5],有关于iLogic的参考资料,我们可以参考创建iLogic规则。因此,回答如下:</think>### Autodesk Inventor 中同时修改文件名和零件名的方法 在 Autodesk Inventor 中,**文件名**(磁盘存储名称)和**零件名**(内部属性名称)是两个独立属性。通过以下方法可实现同步修改: --- ### 方法 1:通过 Inventor API 脚本(推荐) 引用[3]展示了通过 API 同步零件代号与工程图名称的 VB 代码原理,可扩展为同时修改文件名和零件名: ```vb ' 打开当前文档 Dim oDoc As PartDocument = ThisApplication.ActiveDocument Dim oPart As PartComponentDefinition = oDoc.ComponentDefinition ' 设置新零件名 oPart.PartNumber = "新零件名" ' 修改内部零件代号 oDoc.PropertySets("Design Tracking Properties").Item("Description").Value = "新描述" ' 修改描述属性 ' 同步修改文件名 Dim oldPath As String = oDoc.FullFileName Dim newPath As String = Path.Combine(Path.GetDirectoryName(oldPath), "新文件名.ipt") oDoc.SaveAs(newPath, True) ' True=更新所有引用[^3] ' 关闭旧文件并删除(可选) oDoc.Close(False) System.IO.File.Delete(oldPath) ``` **操作步骤**: 1. 在 Inventor 中打开需修改的零件 2. 按 `F12` 打开 VBA 编辑器 3. 粘贴上述代码,修改 `新零件名` 和 `新文件名` 4. 运行宏(F5) --- ### 方法 2:手动操作流程 1. **修改零件属性**: - 右键浏览器中的零件 → `iProperties` - 在 `项目` 选项卡修改 **零件代号**(PartNumber) - 在 `描述` 字段修改显示名称 - 点击 `应用` 保存属性[^3][^4] 2. **同步修改文件名**: - `文件` → `另存为` - 输入新文件名 → 勾选 **保存时更新所有引用** - 点击 `保存`(原文件保留,新文件同步属性)[^1] 3. **清理旧文件**(可选): - 关闭 Inventor → 在资源管理器中删除旧文件 - 打开引用此零件的装配体 → 通过 `替换` 功能关联新文件 --- ### 关键注意事项 1. **引用同步**: - 必须勾选 `保存时更新所有引用`,否则装配体会丢失零件关联[^1] - 通过 API 的 `SaveAs(..., True)` 参数可自动处理引用[^3] 2. **属性优先级**: - **零件代号**(PartNumber):用于 BOM 表和装配引用 - **描述**(Description):工程图中的显示名称 - 两者需同时修改以确保一致性[^3][^4] 3. **文件位置**: - 修改前确认文件在 `Content Center` 目录(默认 `D:\...\Inventor Content Center Files`),否则需先移出[^4] --- ### 自动化建议 对于频繁操作,建议: 1. 创建 **iLogic 规则**(参考引用[5]): - 添加表单输入框接收新名称 - 自动执行属性修改和文件另存 2. 使用 **Design Automation API**: - 批量处理云端文件重命名与属性更新 > **提示**:修改后使用 `项目` → `更新项目` 刷新所有装配引用[^1][^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值