EaBIM

标题: 编程从一个项目中提取族到另一个项目中 [打印本页]

作者: 萧闫子    时间: 2014-1-15 11:37
标题: 编程从一个项目中提取族到另一个项目中

已经完成的项目一般包含丰富的族,用户希望从一个项目中提取族到新项目中。通过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
作者:  叶雄进 ,


作者: 乖乖仔    时间: 2014-2-20 14:21
路过!!! 帮顶……




欢迎光临 EaBIM (https://eabim.net/) Powered by Discuz! X3.2