revit在桥梁中的应用二次开发(1) - 族

更新时间:2023-11-23 22:43:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Revit在桥梁中的应用二次开发篇(1)——族

建模篇简单讲了怎么在revit软件中手动建桥梁模型,更多的基础操作的内容可以直接看revit在建筑中应用的相关教程,其实也是相通的,没必要多讲。

这次诚意满满,花了两天时间专门研究二次开发中族的应用。以下代码亲测可试,结合《AUTODESK REVIT二次开发基础教程》可以获得更好的效果。因为书本上都是一些代码片段,而且是基于2015版的,在2016版的revit上部分代码跑不了。我也尝试了很久,终于基本上搞通了族相关的语法。

基础知识直接看书,这里直接上代码。书里的代码片段可以想办法插到完整代码中运行。

1、找到族实例

//用id找到族实例

2、拿到族文档 //拿到族文档

Document familyDoc =

revitDoc.EditFamily(familyInstance.Symbol.Family);

if (!familyDoc.IsFamilyDocument) {

TaskDialog.Show(\错误3\, \不是族文档!\); } else {

TaskDialog.Show(\, \是族文档!\); }

3、修改族类型名称(完整代码) using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; using Autodesk.Revit.UI; using Autodesk.Revit.DB;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.ApplicationServices; namespace 修改族类型名称 {

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

public class Class1 : IExternalCommand {

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {

Document revitDoc =

commandData.Application.ActiveUIDocument.Document; //取得文档 Application revitApp =

commandData.Application.Application; //取得应用程序

Selection sel = commandData.Application.ActiveUIDocument.Selection; Reference ref1 = sel.PickObject(ObjectType.Element, \选择一个族实例\);

//得到当前选择的文档

Document doc = commandData.Application.ActiveUIDocument.Document; //族实例

FamilyInstance familyIns = doc.GetElement(ref1) as FamilyInstance; FamilySymbol symbol = familyIns.Symbol;

Transaction transaction = new Transaction(doc); //打开关闭子程序

transaction.Start(\改变族类型名称\); symbol.Name = \ + symbol.Name; transaction.Commit(); return Result.Succeeded; } } }

4、删除一个族参数

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

public class Class1 : IExternalCommand {

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {

Document revitDoc =

commandData.Application.ActiveUIDocument.Document; //取得文档 Application revitApp =

commandData.Application.Application; //取得应用程序 //选择一个族实例

Selection sel = commandData.Application.ActiveUIDocument.Selection; Reference ref1 = sel.PickObject(ObjectType.Element, \选择一个族实例\);

FamilyInstance familyIns = revitDoc.GetElement(ref1) as FamilyInstance;

FamilySymbol symbol = familyIns.Symbol; //得到族文档

Document familyDoc = revitDoc.EditFamily(symbol.Family);

//取得FamilyManager,注意这里只能从族文档中取得FamilyManager,然后修改参数

FamilyManager familyManager = familyDoc.FamilyManager;

IList faparms = familyManager.GetParameters();

TaskDialog.Show(\,faparms.First().Definition.Name); Transaction trans = new Transaction(familyDoc, \); trans.Start();

familyManager.RemoveParameter(faparms.First()); trans.Commit();

familyDoc.LoadFamily(revitDoc, new Opt()); return Result.Succeeded; } } }

//族载入时覆盖并修改族参数

public class Opt : IFamilyLoadOptions {

public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues) {

overwriteParameterValues = true; return true; }

public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues) {

source = FamilySource.Project; overwriteParameterValues = true; return true; } }

4、遍历族参数

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; using Autodesk.Revit.UI; using Autodesk.Revit.DB;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.ApplicationServices; namespace 遍历族参数 {

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

public class Class1 : IExternalCommand {

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {

Document revitDoc =

commandData.Application.ActiveUIDocument.Document; //取得文档

UIApplication uiApp = commandData.Application; Application revitApp =

commandData.Application.Application; //取得应用程序

//取得选择的元素

Selection sel = uiApp.ActiveUIDocument.Selection;

Reference ref1 = sel.PickObject(ObjectType.Element, \family instance\);

Document doc = uiApp.ActiveUIDocument.Document; Element elem = doc.GetElement(ref1);

//遍历元素的参数

string strParamInfo = null;

foreach (Parameter param in elem.Parameters) {

if (param.AsValueString() != null)

strParamInfo += param.Definition.Name + \is:\ + param.AsValueString() + \; else

strParamInfo += param.Definition.Name + \is:\ + param.AsString() + \; }

TaskDialog.Show(\, strParamInfo); return Result.Succeeded; } } }

5、获得指定族参数并修改参数,这里以窗户的底高度为例。这里用两种方法找到指定参数,尤其是用名称找到指定参数尤其要注意,旧版本的get_Parameter()已经不能用了 using System.Text;

using System.Threading.Tasks; using Autodesk.Revit.UI; using Autodesk.Revit.DB;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.ApplicationServices; namespace 获得指定参数并修改参数 {

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

public class Class1 : IExternalCommand {

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {

Document revitDoc =

commandData.Application.ActiveUIDocument.Document; //取得文档

UIApplication uiApp = commandData.Application; Application revitApp =

commandData.Application.Application; //取得应用程序 //取得选择的元素

Selection sel = uiApp.ActiveUIDocument.Selection;

Reference ref1 = sel.PickObject(ObjectType.Element, \选择一个窗族\); Element elem = revitDoc.GetElement(ref1); //获得族实例

FamilyInstance familyInstance = elem as FamilyInstance; //遍历元素的参数

string strParamInfo = null;

////方法一:使用parameterId来找到参数 //Parameter param =

familyInstance.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM); //strParamInfo += param.Definition.Name + \param.AsValueString() + \

//方法二:使用名称来找到参数,因为revit2016为了避免得到重复的名字,只能用GetParameters得到一个集合,如果你只用唯一的名字,那只用取list[0]就可以了

IList list = familyInstance.GetParameters(\底高度\); Parameter param = list[0];

strParamInfo += param.Definition.Name + \ + param.AsValueString() + \; //方法三:见下图

//修改参数的值

Transaction transaction = new Transaction(revitDoc); //打开子程序,修改参数,然后关闭子程序 transaction.Start(\改变族类型名称\);

param.Set(10); //注意,这里单位是英寸,10英寸等于3048mm transaction.Commit();

TaskDialog.Show(\, strParamInfo); return Result.Succeeded; } } }

//接以上方法三:

6、毫米转到英寸

7、几个有用的关于revit二次开发族的相关链接

a、http://wenku.it168.com/d_001413025.shtml Revit二次开发之“修改已经加载的族或系统族的参数”

b、http://blog.csdn.net/joexiongjin/article/details/9011073

程如何修改Revit族类型名称

c、http://www.cnblogs.com/greatverve/archive/2011/04/01/revit-param.html Revit 二次开发之“参数”

d、http://blog.csdn.net/joexiongjin/article/details/17220183 详

谈用API读取Revit对象的参数值

e、http://www.cnblogs.com/greatverve/archive/2011/08/11/modify-family-param.html Revit二次开发之“通过新建族来修改族参数”

6、毫米转到英寸

7、几个有用的关于revit二次开发族的相关链接

a、http://wenku.it168.com/d_001413025.shtml Revit二次开发之“修改已经加载的族或系统族的参数”

b、http://blog.csdn.net/joexiongjin/article/details/9011073

程如何修改Revit族类型名称

c、http://www.cnblogs.com/greatverve/archive/2011/04/01/revit-param.html Revit 二次开发之“参数”

d、http://blog.csdn.net/joexiongjin/article/details/17220183 详

谈用API读取Revit对象的参数值

e、http://www.cnblogs.com/greatverve/archive/2011/08/11/modify-family-param.html Revit二次开发之“通过新建族来修改族参数”

本文来源:https://www.bwwdw.com/article/3s9t.html

Top