二次开发笔记_第1页
二次开发笔记_第2页
二次开发笔记_第3页
二次开发笔记_第4页
二次开发笔记_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Revit2016 二次开发笔记1.选中图元1.1事先选择某类图元(Wall) revit = commandData.Application;ElementSet collection = new ElementSet();ElementSet selWall = new ElementSet();foreach (ElementId elementId in revit.ActiveUIDocument.Selection.GetElementIds()collection.Insert(revit.ActiveUIDocument.Document.GetElement(elementI

2、d); /选中的原件foreach (Element ele in sel)Wall wall = ele as Wall; /如果为墙if (wall !=null)SelWal.Insert(ele);1.2.通过交互方式用矩形框选择图元所用方法:PickElementsByRectangle()using System.Collections.Generic; /声明引用命名空间,IlistUIDocument uidoc = commandData.Application.ActiveUIDocument;ElementSet collector = new ElementSet();

3、IList<Element> elemList = uidoc.Selection.PickElementsByRectangle();/矩形框的应用string info = "所选图元:"foreach (Element elem in elemList)collector.Insert(elem);info += "nt" + elem.ToString();TaskDialog.Show("Choose Items", info);return Result.Succeeded;1.2.2用点击选择图元所用方法:P

4、ickObject(ObjectType.Element)using Autodesk.Revit.UI.Selection; /声明引用命名空间,ObjectTypeUIDocument uidoc = commandData.Application.ActiveUIDocument;Reference hasPickOne = uidoc.Selection.PickObject(ObjectType.Element);Element elem = uidoc.Document.GetElement(hasPickOne.ElementId);string info = "所选图

5、元:"info += "nt" + elem.GetType().ToString();TaskDialog.Show("Revit", info);还存在其它用法:Selection.PickObject (ObjectType, String) /string 为提示PickObjects (ObjectType) /允许选择多个元素1.2.3选择及过滤UIDocument uidoc = commandData.Application.ActiveUIDocument;ISelectionFilter selFilter = new Ma

6、ssSelectionFilter();IList<Element> eList = uidoc.Selection.PickElementsByRectangle(selFilter) as IList<Element>string info = "选择了以下的墙:"foreach(Element elem in eList)info += "nt" + elem.Name;TaskDialog.Show("Revit", info);return Result.Succeeded;public class

7、MassSelectionFilter : ISelectionFilterpublic bool AllowElement(Element elem)return elem is Wall; /选择了墙public bool AllowReference(Reference reference, XYZ position)throw new NotImplementedException();/ 第二个条件2.加载族并创建实例UIApplication m_application;UIDocument m_document;m_application = commandData.Applic

8、ation;m_document = m_application.ActiveUIDocument;2.1.加载族1) m_document .Document.LoadFamilySymbol (FullPath, symbolName, out loadedfamilySymbol);2) m_document .Document.LoadFamily (FullPath, out loadedfamily);第一种用来加载族类型,第二种用来加载族,返回的loadfamily和loadfamilysymbol 可以随后用来创建族实例2.2创建族实例创建族实例将经过:1)选择一个平面,建立一

9、个工作平面2)选择一个点放置族实例2.1选择平面Reference faceRef = m_document.Selection.PickObject(ObjectType.Face, new PlanarFaceFilter(m_document.Document), "Please pick a planar face to set the work plane. ESC for cancel.");GeometryObject geoObject = m_document.Document.GetElement(faceRef).GetGeometryObjectFr

10、omReference(faceRef); PlanarFace planarFace = geoObject as PlanarFace;/判断选择为平面的类public class PlanarFaceFilter : ISelectionFilter / Revit document. Document m_doc = null; / Constructor the filter and initialize the document. public PlanarFaceFilter(Document doc) m_doc = doc; / Allow all the element t

11、o be selected public bool AllowElement(Element element) return true; / Allow planar face reference to be selectedpublic bool AllowReference(Reference refer, XYZ point)GeometryObject geoObject = m_doc.GetElement(refer).GetGeometryObjectFromReference(refer);return geoObject != null && geoObjec

12、t is PlanarFace;2.2创建工作平面SketchPlane faceSketchPlane = CreateSketchPlane(planarFace.FaceNormal, planarFace.Origin);if (faceSketchPlane != null)/ 对模型进行操作必须新建 Transaction 事件Transaction changeSketchPlane = new Transaction(m_document.Document, "Change Sketch Plane.");changeSketchPlane.Start();

13、 m_document.Document.ActiveView.SketchPlane = faceSketchPlane; m_document.Document.ActiveView.ShowActiveWorkPlane(); changeSketchPlane.Commit();/创建工作平面的子方法internal SketchPlane CreateSketchPlane(Autodesk.Revit.DB.XYZ normal, Autodesk.Revit.DB.XYZ origin)/ First create a Geometry.Plane which need in N

14、ewSketchPlane() method Plane geometryPlane = m_application.Application.Create.NewPlane(normal, origin);/ Then create a sketch plane using the Geometry Plane Transaction createSketchPlane = new Transaction(m_document.Document, "Create a sketch plane."); createSketchPlane.Start(); SketchPlan

15、e plane = SketchPlane.Create(m_document.Document, geometryPlane); createSketchPlane.Commit(); return plane;2.3 选择一个点并创建实例/ Pick point from current work plane with snaps.ObjectSnapTypes snapType = ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections| ObjectSnapTypes.Mid

16、points | ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid;XYZ point = m_document.Selection.PickPoint(snapType, "Please pick a point to place component.");Transaction LoadAndCreat = new Transaction(m_document.Document, "Load a Family.");LoadAndCreat.Start(); / 对模型进行操作必须新建 T

17、ransaction 事件/加载族并创建实例FamilySymbol loadedfamilySymbol = null;string directory = "C:UsersAdministratorDesktopRevitProgramFamily"string symbolName = "测斜管"trym_document.Document.LoadFamilySymbol(string.Format("01.rfa",directory,symbolName ) , symbolName , out loadedfamilyS

18、ymbol);catch (Exception e)message = e.Message;return Result.Failed;if (null = loadedfamilySymbol)TaskDialog.Show("Revit", "Can't load the prepared rfa.");return Result.Failed;/创建族实例CreatePointFamilyInstance(point, loadedfamilySymbol, StructuralType.NonStructural);LoadAndCreat

19、.Commit(); /子事件结束/创建族实例的方法public bool CreatePointFamilyInstance(XYZ location,FamilySymbol symbol,StructuralType structuralType)if (!symbol.IsActive)symbol.Activate();FamilyInstance instance = m_document.Document.Create.NewFamilyInstance(location, symbol, structuralType);List<ElementId> instanc

20、eId = new List<ElementId>();instanceId.Add(instance.Id);m_document.Selection.SetElementIds(instanceId); /已经在选择集里return true;3.用过滤器统计传感器3.1通过族类型名称查找族实例/用到了慢过滤器:FamilyInstanceFilterUIDocument uidoc = commandData.Application.ActiveUIDocument;Document doc = uidoc.Document;FilteredElementCollector

21、collector = new FilteredElementCollector(doc);collector = collector.OfClass(typeof(FamilySymbol);var query = from element in collector where element.Name ="测斜管" /查找名为“测斜管”的族实例 select element; /linq 查询List<Element> famSyms = query.ToList<Element>();ElementId symbolId = famSyms0.

22、Id;/创建过滤器并找到该族类型对应的所有族实例collector = new FilteredElementCollector(doc);FamilyInstanceFilter filter = new FamilyInstanceFilter(doc, symbolId);IList<Element> founds = collector.WherePasses(filter).ToElements();foreach(FamilyInstance inst in founds)TaskDialog.Show("Revit", string.Format(

23、"FamilyInstance:t0nFamilySybmol Id:t1nName:t2", inst.Id.IntegerValue,inst.Symbol.Id.IntegerValue,inst.Symbol.Name);/新建对话框,选择是否删除查找到的族实例TaskDialog mainDialog = new TaskDialog("删除");mainDialog.MainContent = "单击"Ok"删除传感器!"mainDialog.CommonButtons = TaskDialogComm

24、onButtons.Ok | TaskDialogCommonButtons.Cancel;if (TaskDialogResult.Ok = mainDialog.Show()/对元件进行编辑都必须新建事件来解决using (Transaction delTrans = new Transaction(doc, "delete the element!") delTrans.Start(); doc.Delete(founds0.Id); delTrans.Commit(); 3.2 通过族名查找族类型/用到了快过滤器:FamilySymbolFilterUIDocume

25、nt uidoc = commandData.Application.ActiveUIDocument;Document doc = uidoc.Document;FilteredElementCollector collector = new FilteredElementCollector(doc);collector = collector.OfClass(typeof(Family);var query = from element in collector where element.Name = "测斜管" select element; /Linq查询List

26、<Element> fam= query.ToList<Element>();ElementId famId = fam0.Id;/创建过滤器并找到该族对应的所有族类型collector = new FilteredElementCollector(doc);FamilySymbolFilter filter = new FamilySymbolFilter(famId);/收集族类型名称IList<Element> founds = collector.WherePasses(filter).ToElements();foreach (FamilySymb

27、ol syms in founds)TaskDialog.Show("Revit", string.Format("FamilySymbol:t0nFamily Id:t1nName:t2",syms.Id.IntegerValue, syms.Family.Id.IntegerValue, syms.Family.Name);/收集族类型IDICollection<ElementId> found= collector.WherePasses(filter).ToElementIds();foreach (ElementId symsId

28、in found)TaskDialog.Show("Revit", string.Format("FamilySymbol Id:t0nName:t1", symsId.IntegerValue, symsId.ToString();4.实例的隔离与颜色改变4.1 实例的颜色改变UIDocument uidoc = commandData.Application.ActiveUIDocument;Document doc = uidoc.Document;/过滤填充图案FilteredElementCollector fillPatternFilter

29、= new FilteredElementCollector(doc);fillPatternFilter.OfClass(typeof(FillPatternElement);/获取实体填充FillPatternElement fp = fillPatternFilter.First(m => (m as FillPatternElement).GetFillPattern().IsSolidFill) as FillPatternElement;Reference hasPickOne = uidoc.Selection.PickObject(ObjectType.Element);

30、if (hasPickOne != null)Transaction trans = new Transaction(doc, "trans"); trans.Start();View v = doc.ActiveView;OverrideGraphicSettings ogs = v.GetElementOverrides(hasPickOne.ElementId);Color oldColor = ogs.ProjectionFillColor; /实例的现在颜色ElementId oldEleId = ogs.ProjectionFillPatternId; /现在的

31、fillPatternid/设置 投影/表面 ->填充图案->填充图案ogs.SetProjectionFillPatternId(fp.Id);/设置 投影/表面 ->填充图案->颜色ogs.SetProjectionFillColor(new Color(255, 0, 0);/应用到视图v.SetElementOverrides(hasPickOne.ElementId, ogs);trans.Commit();return Result.Succeeded;4.2 实例的隔离与恢复显示 单独构建:1. var ui = comman

32、dData.Application.ActiveUIDocument;  2.            var doc = ui.Document;  3.   4.            var select = ui.Selection; 

33、0;5.            var refe = select.PickObject(ObjectType.Element);  6.   7.            using (Transaction trans = new Transaction(d

34、oc)  8.              9.                trans.Start("isloate");  10.           &#

35、160;    ui.ActiveView.IsolateElementTemporary(refe.ElementId);  11.                trans.Commit();  12.              构建组

36、:1. var ui = commandData.Application.ActiveUIDocument;  2.            var doc = ui.Document;  3.            var select = ui.S

37、election;  4.            var refe = select.PickObject(ObjectType.Element);  5.            /using (Transaction trans = new Transact

38、ion(doc)  6.            /  7.            /    trans.Start("isloate");  8.          &#

39、160; /    ui.ActiveView.IsolateElementTemporary(refe.ElementId);  9.            /    trans.Commit();  10.            / &

40、#160;11.            var ele = doc.GetElement(refe);  12.            if (ele is Group)  13.         &

41、#160;    14.                var g = ele as Group;  15.                var ids = 

42、g.GetMemberIds();  16.                using (Transaction trans = new Transaction(doc)  17.                

43、;  18.                    trans.Start("isloate");  19.                    ui.Act

44、iveView.IsolateElementsTemporary(ids);  20.                    trans.Commit();  21.   /输出图片22.              &#

45、160;     ImageExportOptions ieop = new ImageExportOptions()  23.                      24.         &#

46、160;              ExportRange = ExportRange.CurrentView,  25.                        FilePath&#

47、160;= "g:tt.bmp",  26.                        FitDirection = FitDirectionType.Horizontal,  27.        

48、60;               HLRandWFViewsFileType = ImageFileType.BMP,  28.                        ImageResolution = ImageResolution.DPI_600,  29.                     

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论