用API实现在位输入标题栏内容

本文介绍了一个简化Inventor工程图标题栏填写过程的方法。通过使用自定义对话框,用户可以直接在类似标题栏的界面上输入信息,实现‘所见即所得’的效果。

上一篇文章,我们创建了自己的对话框进行iProperties的输入。本篇文章将介绍一个不错的例子,是Brian编写的,用来实现工程图标题栏内容的输入。原文地址:

http://modthemachine.typepad.com/my_weblog/iproperties/

When writing the previous post about a custom dialog for iProperty entry I had forgotten about another example I had put together a couple of years ago.  This is similar but I think is better at demonstrating how useful this concept can be.  In this case there’s a custom dialog that looks like a title block.  This significantly simplifies filling out the title block.  Instead of the user having to search through the iProperties dialog and remember which property corresponds to which field on the title block they just fill out the title block directly.  For this example, one of the fields is also a prompted text entry to demonstrate that you’re not limited to only using iProperties.  This sample doesn’t demonstrate it but it could also use drop-down fields with lists like that shown in the previous post.

Inventor工程图里标题栏里的内容由标题栏定义设置,其中有三种文字:
普通静态文字:即定义里是什么内容,标题栏显示的就是什么
提示文字: 当标题栏插入时,会有一个字串提示该是什么方面的内容,由用户自己输入最终的显示内容
关联文字:定义时该文字和某个文档iProperties关联,当标题栏插入时,自动找到对应iProperties的内容

总之,没有一个直观的标题栏编写方式。

When the program is run, this is what the user sees.  It’s a VBA dialog that was created by doing a screen shot of the title block and inserting that as an image on the dialog.  The various text controls were added on top of the image.

Brian用VBA写了个小例子,根据某种工程图标题栏样式,创建出一个在位编辑的对话框,和在图纸看到的对应,如下图

CustomTitleBlock

One the dialog is filled out and the “Apply” button is pressed it sets the corresponding iProperties and the prompted text and then the title block automatically updates to reflect those changes.  Here’s the result.

用户可以在这里,直接进行输入,点击Apply后,标题栏就显示出输入的内容,达到“所见即所得”。

CustomTitleBockResults

Here’s a zip file containing a sample drawing that has the custom iProperties and the prompted text field the sample program expects and it contains a VBA project that contains the source code.  If you want to implement this within your company I would recommend writing this as an add-in rather than VBA, like the previous postdemonstrates.

这个例子的源代码可在这里下载zip file ,其中包括了测试的工程图和自定义的iProperties。因为标题栏样式是多样的,你需要根据具体的样式,让对话框的样式自动的进行匹配。另外,最好写成插件,而不是用VBA。

### 通过 Revit API 实现批量修改在位模型的方法 Revit 提供了强大的 API 接口,允许开发者通过编程方式对项目中的图元进行操作,包括批量修改在位模型。由于在位模型是直接在项目中创建的族实例,因此可以通过访问其所属的 `FamilyInstance` 类型,并对其参数、几何形状或属性进行批量更新。 #### 获取在位模型 在位模型通常具有特定的族类型标识,可以通过筛选 `FamilyInstance` 来识别它们。使用 `FilteredElementCollector` 可以遍历项目中的所有构件,并筛选出属于在位模型的图元: ```csharp var collector = new FilteredElementCollector(doc); var inPlaceInstances = collector.OfClass(typeof(FamilyInstance)) .Cast<FamilyInstance>() .Where(fi => fi.Symbol == null && fi.IsInPlace) .ToList(); ``` 上述代码通过判断 `Symbol == null` 和 `IsInPlace == true` 来获取所有在位模型实例[^1]。 #### 修改在位模型参数 如果在位模型定义了共享参数或实例参数,可以通过 `Set` 方法进行批量赋值: ```csharp foreach (var instance in inPlaceInstances) { var param = instance.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS); if (param != null && param.IsReadOnly == false) { param.Set("Updated via API"); } } ``` 该代码示例展示了如何批量修改“注释”字段的内。 #### 修改在位模型几何形状(高级) 若需要修改几何形状,通常需要进入族编辑器环境,但通过 API 无法直接编辑族的几何体。可以考虑将部分逻辑封装为事务处理,并结合嵌套族来实现动态调整: ```csharp using (Transaction trans = new Transaction(doc, "Modify In-Place Geometry")) { trans.Start(); foreach (var instance in inPlaceInstances) { // 假设嵌套了一个可载入族作为子部件 var nestedFamily = instance.GetSubComponentIds().Select(id => doc.GetElement(id)).OfType<FamilyInstance>().FirstOrDefault(); if (nestedFamily != null) { var heightParam = nestedFamily.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM); if (heightParam != null && !heightParam.IsReadOnly) { heightParam.Set(UnitUtils.Convert(3000, UnitTypeId.Millimeters, UnitTypeId.Meters)); } } } trans.Commit(); } ``` 此方法适用于包含嵌套族的在位模型,通过修改嵌套族的参数间接影响整体结构。 #### 批量重命名与分类管理 在位模型可以被赋予统一的类别或名称,以便于后续筛选和管理: ```csharp int index = 1; foreach (var instance in inPlaceInstances) { instance.Name = $"Custom_InPlace_{index++}"; } ``` 该操作可以在项目后期维护中提升查找效率。 --- ### 注意事项 - **事务控制**:所有对文档的修改都必须包裹在 `Transaction` 中,确保数据一致性。 - **权限检查**:某些参数可能为只读状态,修改前应进行判断。 - **性能优化**:大批量操作时建议分批处理,避免内存溢出或界面卡顿。 - **版本兼性**:不同版本的 Revit API 对 `IsInPlace` 属性的支持略有差异,需参考官方文档确认可用性。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值