判断链接文件偏移量创建空间。

start
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public  class cmd : IExternalCommand
{
     ///   <summary>
    
///  根据链接文件名称找到对应链接路径,模糊匹配,待改进
    
///   </summary>
    
///   <param name="listPath"></param>
    
///   <param name="strKey"></param>
    
///   <returns></returns>
     private  string GetPath(List< string> listPath,  string strKey)
    {
         foreach ( string strPath  in listPath)
        {
             if (strPath.Contains(strKey))
                 return strPath;
        }
         return  "";
    }
     public Result Execute(ExternalCommandData cmdData,  ref  string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        UIApplication uiApp = cmdData.Application;
        Document doc = uiDoc.Document;
        Selection selection = uiDoc.Selection;

        Transaction ts =  new Transaction(doc,  " www ");
        ts.Start();

        FilteredElementCollector collector =  new FilteredElementCollector(doc); //
        collector.OfClass( typeof(Instance)).OfCategory(BuiltInCategory.OST_RvtLinks); // RevitLinkType
        List< string> listPath = GetLinkFilePaths(doc);
         foreach (Element elDoc  in collector)
        {
            Instance ins = elDoc  as Instance;
             if (ins !=  null)
            {
                Transform transForm = ins.GetTransform();
                 string strKey = elDoc.Name.Substring( 0, elDoc.Name.IndexOf( " .rvt ")); // 找到链接文件名称
                Document docLink = uiApp.Application.OpenDocumentFile(GetPath(listPath, strKey));
                FilteredElementCollector collectorLink =  new FilteredElementCollector(docLink);
                collectorLink.OfCategory(BuiltInCategory.OST_Rooms);
                 foreach (Element el  in collectorLink)
                {
                    Room room = el  as Room;
                    LocationPoint roomPoint = room.Location  as LocationPoint;
                    UV uv =  new UV();
                     if (room.Location !=  null)
                    {
                        uv =  new UV(roomPoint.Point.X + transForm.Origin.X, roomPoint.Point.Y + transForm.Origin.Y);
                    }
                    doc.Create.NewSpace(room.Level, uv);
                }
            }
        }
        ts.Commit();

         return Result.Succeeded;
    }
     ///   <summary>
    
///  取得链接文件路径
    
///   </summary>
    
///   <param name="doc"></param>
    
///   <returns></returns>
     public List< string> GetLinkFilePaths(Document doc)
    {
        List< string> listPath =  new List< string>();
         foreach (ElementId elId  in ExternalFileUtils.GetAllExternalFileReferences(doc))
        {
             if (doc.get_Element(elId).IsExternalFileReference())
            {
                ExternalFileReference fileRef = doc.get_Element(elId).GetExternalFileReference();
                 if ( " RevitLink " == fileRef.ExternalFileReferenceType.ToString())
                    listPath.Add(ModelPathUtils.ConvertModelPathToUserVisiblePath(fileRef.GetAbsolutePath()));
            }
        }
         return listPath;
    }
}
url: http://greatverve.cnblogs.com/p/revit-create-space.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值