|
一个实用性很强的问题,和大家分享一下我的实现方式。
首先声明实现基于Revit 2011。程序需要在一个打开的项目文档中再打开另外一个族文档,并且将对族进行读写操作。在两篇Jeremy的博客里分别找到了部分答案,组合之后最终解决了问题。
1. 在文档中打开另一个文档
需要注意的是 Autodesk.Revit.ApplicationServices.Application.OpenDocumentFile(filePath) 无法满足要求。该方法只是在内存中打开文档,并不会创建新文档对应的视图,当然就更谈不上切换到新文档视图了。可行的方法是纯C#桌面编程:System.Diagnostics.Process.Start(filePath)。该方法无论在Add-In之内还是之外都有效。
2. 在文档中创建新文档,并将用户界面切换到新文档
有些小复杂,但是思路依然可以在Jeremy博客中找到,核心操作有四步:
1) 创建新文档对象(在内存中)
Autodesk.Revit.DB.Document dbDoc = Autodesk.Revit.ApplicationServices.Application.NewFamilyDocument(templateFilePath)
2) 创建新文档的专属文档视图 UIDocument
Autodesk.Revit.UI.UIDocument uiDoc = new Autodesk.Revit.UI.UIDocument(dbDoc);
3) 在新文档希望的视图中创建一个元素
Jeremy博客里提到的是创建一根线(BoundLine),但是这种元素在我的族模板中无法创建。所以我创建了一个参考平面(ReferencePlane,记为 referencePlaneInNewDoc)。虽然没有经过全面测试,不过个人认为参考平面应该适用于绝大多数(也可能全部)族模板。
4. 调用 UIDocument.ShowElement(referencePlaneInNewDoc) 将用户界面切换到新文档中参考平面所在视图
这个方法有两个效果:切换用户界面到新文档;切换到新文档的指定视图
以下是我的代码(抱歉,有点儿乱)
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">[csharp] view plaincopy</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">string templatePath = RevitDocument.GetTemplatePath(transElem.RevitTemplate); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Document modelDBDoc = m_serviceApp.NewFamilyDocument(templatePath); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">FilteredElementCollector collector = new FilteredElementCollector(modelDBDoc); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">IList<Element> views = collector.OfClass(typeof(Autodesk.Revit.DB.View)).ToElements(); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Autodesk.Revit.DB.View frontView = null; </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">foreach (Element viewElem in views) </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{ </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> if (viewElem.Name == "Front") </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> { </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> frontView = viewElem as Autodesk.Revit.DB.View; </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> break; </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> } </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">} </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Transaction transModel = new Transaction(modelDBDoc, "构件"); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">transModel.Start(); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Autodesk.Revit.UI.UIDocument modelUIDoc = new Autodesk.Revit.UI.UIDocument(modelDBDoc); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">XYZ bubbleEnd = new XYZ(0, -1, 0); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">XYZ freeEnd = new XYZ(0, 1, 0); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">XYZ cutVec = new XYZ(0, 0, 1); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ReferencePlane refPlane = modelDBDoc.FamilyCreate.NewReferencePlane(bubbleEnd, freeEnd, cutVec, frontView); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">try </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{ </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> modelUIDoc.ShowElements(refPlane); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">} </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">catch (Exception ex) </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{ </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> System.Diagnostics.Debug.WriteLine(ex); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">} </span>
复制代码 如果希望在新文档完成操作之后再将用户界面切换回原始文档,同样可以利用 UIDocument.ShowElement()。方法是切换步骤1)之前选取原始文档中的某个元素(记为 elementInOrigDoc)并保存,然后在完成了对新文档的操作之后,调用 UIDocument.ShowElement(elementInOrigDoc)。
原文链接:http://blog.csdn.net/flower4wine/article/details/6838917
|
|