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

萧闫子 发表于 2014-1-15 12:48:10

拷贝Revit中的对象的诀窍,顺论Revit中对象的拷贝

我以前有一篇文章讲到可以使用Duplicate() 方法在Revit中,如何编程创建新类型(如窗户或墙) 。 顺便说一句在以后Revit(> 2012)的类型对象复制中,慢慢从类型对象的类里面增加Create() 函数替代原来的Dupicate方法。
这个方法只是局限于类型对象。Revit的对象种类很多,比如视图中可见对象,不可见的对象。如何从这些对象中拷贝创建新对象呢?

在Revit2012, 提供了ElementTransformUtils类用于移动,镜像,旋转,复制对象。 从这几个方法看,好像这个类只能对可见的Revit对象或者是具有几何表达的对象进行操作。因为包含了镜像,移动。 所以比较容易理解其中的CopyElement()方法也只能对可见对象进行复制。
实际上这个CopyElement() 方法也可以对不可见的对象进行复制,比如下面的代码复制已有的视图的模板,view template。这个是定义视图的一些属性,可以在多个视图中共用其中的属性设置。
请看下面的代码:

view plaincopy
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

usingAutodesk.Revit .DB;
using Autodesk.Revit.UI;
using Autodesk.Revit .ApplicationServices;
using Autodesk.Revit.Attributes ;




public class RevitCommand : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {

      UIApplication app = commandData.Application;
      Document doc = app.ActiveUIDocument.Document;

      //get the plan view template

      FilteredElementCollector collector = new FilteredElementCollector(doc);
      collector.OfClass(typeof(ViewPlan));

      Autodesk.Revit.DB.View template = null;
      foreach (Element elem in collector)
      {
      Autodesk.Revit.DB.View view = elem as ViewPlan;
      if (view.IsTemplate)
      {
          template = view;
          break;
      }
      }
      
      Transaction trans = new Transaction(doc, "ExComm");
      ICollection<ElementId> cols = null;
      try
      {
      trans.Start();

      //29485
      XYZ vector = new XYZ(0, 0, 1);
      cols = ElementTransformUtils.CopyElement(doc, template.Id, vector);

      trans.Commit();
      }
      catch(Exception ex)      
      {
      MessageBox.Show(ex.Message,"test");
      }

      MessageBox.Show(cols.Count.ToString());

      return Result.Succeeded ;
    }
}
但是这篇文章并没有完全测试所有的对象的拷贝,请使用时测试下是否可行。如果碰到不可行的对象,请在本文追加说明。这样大家都知道了。
作者:叶雄进文章来源:http://blog.csdn.net/joexiongjin/article/category/782739


乖乖仔 发表于 2014-2-20 14:11:17

(*^__^*) 嘻嘻……

页: [1]
查看完整版本: 拷贝Revit中的对象的诀窍,顺论Revit中对象的拷贝