EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 567|回复: 1
打印 上一主题 下一主题

编程从一个项目中提取族到另一个项目中

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-15 11:37:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

已经完成的项目一般包含丰富的族,用户希望从一个项目中提取族到新项目中。通过Revit2014可以直接从一个项目复制族到另一个项目。

请看下面的代码把指定族里面的类型都复制到另一个项目中。代码很简洁,没有包含错误处理。只是示意拷贝族类型的办法。

  1. <p><font color="#333333" face="Arial"><span style="line-height: 26px;">[csharp] view plaincopy</span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System.Collections.Generic;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System.Text;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using System.Windows.Forms;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using  Autodesk.Revit .DB;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit.UI;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit .ApplicationServices;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit.Attributes ;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">using Autodesk.Revit.UI.Selection;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  [TransactionAttribute(TransactionMode.Manual)]  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">public class RevitCommand : IExternalCommand  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">{  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">    public Result Execute(ExternalCommandData commandData,   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      ref string messages, ElementSet elements)  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">    {  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      UIApplication app = commandData.Application;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      Document doc = app.ActiveUIDocument.Document;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      Selection sel = app.ActiveUIDocument.Selection;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      Reference ref1 = sel.PickObject(ObjectType.Element,   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        "pick a family instance");  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      FamilyInstance inst = doc.GetElement(ref1) as FamilyInstance;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      if(inst == null)  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      {  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        messages = "No family instance was picked";  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        return Result.Failed;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      }  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      FamilySymbol sym = inst.Symbol;        </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      Document targetDoc = null;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      foreach (Document doc1 in app.Application.Documents)  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      {  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        if (doc.Title != doc1.Title)  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        {  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">          targetDoc = doc1;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">          break;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        }  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      }  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      IList<ElementId> ids = new List<ElementId>();  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      foreach (FamilySymbol symbol in sym.Family.Symbols)  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      {  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        ids.Add(symbol.Id);  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      }  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      Transaction targetTrans = new Transaction(targetDoc);  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      targetTrans.Start("copyFamily");  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      ElementTransformUtils.CopyElements(doc, ids, targetDoc, null,   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">        new CopyPasteOptions());  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      targetTrans.Commit();  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">   </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">      return Result.Succeeded ;  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">    }  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">  </span></font></p><p><font color="#333333" face="Arial"><span style="line-height: 26px;">}  </span></font></p>
复制代码
转载请复制以下信息:
原文链接: http://blog.csdn.net/joexiongjin/article/details/9010845
作者:  叶雄进 ,

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-24 15:49

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表