您的位置:首页 > 其它

用API能否修改Revit链接模型

2014-08-13 15:40 609 查看

Revit使用协同的方式有工作集模式和链接模型的模式. 在工作集模式,协同和参照很容易. 链接模型可以参照但是修改起来比较麻烦.
一直由一个疑问,可否通过编写程序的方式来修改模型里的链接文档.

不试不知道,通过一个小例子测试可行性. SharpDevelop代码如下;

public void CreateElementInLinkModel()
		{
			Selection sel = this.ActiveUIDocument.Selection;
			Document doc = this.ActiveUIDocument.Document;		
			Reference ref1 = sel.PickObject(ObjectType.Element,"please pick  a linked model");
			RevitLinkInstance linkedInstance = doc.GetElement(ref1) as RevitLinkInstance;
			
			ElementId typeId = linkedInstance.GetTypeId();
		    RevitLinkType linkType = doc.GetElement(typeId) as RevitLinkType;
		    string docName = linkType.Name;
		
		    //find its document.
		    Document linkDoc = null;
		    foreach (Document d in doc.Application.Documents)
		    {
		      if (d.Title == docName)
		      {
		        linkDoc = d;
		        break;
		      }
		    }
		    
		    Transaction trans = new Transaction(linkDoc);
		    trans.Start("createALine");
		    
		    Line l = this.Application.Create.NewLineBound(new XYZ(0,0,0),new XYZ(1000,1000,0));
		    
		    //
		    FilteredElementCollector collector = new FilteredElementCollector(linkDoc);
		    collector.OfClass(typeof(ViewPlan));
		    ViewPlan vp = collector.FirstElement() as ViewPlan;
		    
		    
		    linkDoc.Create.NewModelCurve(l,vp.SketchPlane);
		    
		    trans.Commit();
		    linkDoc.Save();

		
		}


运行后,提示
Autodesk.Revit.Exceptions.ArgumentException: Document is a linked file. Transactions can only be used in primary documents (projects or families.)
Parameter name: document

意思是对于非主要文档,我们无法启动一个事务. 所以无法直接修改一个连接模型对象.

后来又尝试先卸载连接模型. 然后在从后台打开这个链接模型的rvt文件. 修改完该模型后,再次加载该连接文件. 实验成功. 成功绘制了一条线. 并可以加载. 就是稍有点闪烁.

public void EditLinkModelLoadBackGround()
		{
			Selection sel = this.ActiveUIDocument.Selection;
			Document doc = this.ActiveUIDocument.Document;		
			Reference ref1 = sel.PickObject(ObjectType.Element,"please pick  a linked model");
			RevitLinkInstance linkedInstance = doc.GetElement(ref1) as RevitLinkInstance;
			
			ElementId typeId = linkedInstance.GetTypeId();
		    RevitLinkType linkType = doc.GetElement(typeId) as RevitLinkType;
		    string docName = linkType.Name;
		
		    //find its document.
		    Document linkDoc = null;
		    foreach (Document d in doc.Application.Documents)
		    {
		      if (d.Title == docName)
		      {
		        linkDoc = d;
		        break;
		      }
		    }
		    
		    string linkDocFullName = linkDoc.PathName;
		    linkType.Unload(new myoverload());
		    
		    
		    Document backGroundDoc = this.Application.OpenDocumentFile( linkDocFullName);
		     
		    Transaction trans = new Transaction(backGroundDoc);
		    trans.Start("createALine");
		    
		    Line l = this.Application.Create.NewLineBound(new XYZ(0,0,0),new XYZ(1000,100,0));
		    
		    //
		    FilteredElementCollector collector = new FilteredElementCollector(backGroundDoc);
		    collector.OfClass(typeof(ViewPlan));
		    ViewPlan vp = collector.FirstElement() as ViewPlan;
		    
		    
		    backGroundDoc.Create.NewModelCurve(l,vp.SketchPlane);
		    
		    trans.Commit();
		    backGroundDoc.Save();
		    backGroundDoc.Close(false);

		    linkType.Load();
		
		}


这篇文章比较有价值, 可能能解决大家在编程中的很多需求.

作者: 叶雄进--橄榄山BIM软件
转载请注明作者和来源
原文地址: http://blog.csdn.net/joexiongjin/article/details/38537729
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: