revit二次開發(fā)筆記_第1頁
revit二次開發(fā)筆記_第2頁
revit二次開發(fā)筆記_第3頁
revit二次開發(fā)筆記_第4頁
revit二次開發(fā)筆記_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Revit2016二次開發(fā)筆記1 .選中圖元1.1 事先選擇奧類圖元(Wall)Autodesk.Revit.Ul.UIApplication revit = commandData.Application;ElementSet collection = new ElementSet();ElementSet selWall = new ElementSet();foreach (ElementId elementId in revit.ActiveUIDocument.Selection.GetElementIds() collection.Insert(revit.ActiveUIDocu

2、ment.Document.GetElement(elementId); / 選中的原件 foreach (Element ele in sel) Wall wall = ele as Wall; / 如果為墻 if (wall !=null) SelWal.Insert(ele);1.2 .通過交互方式1.2.1 用矩形框選擇圖元所用方法:PickElementsByRectangle()using System.Collections.Generic; / 聲明引用命名空間,IlistUIDocument uidoc = commandData.Application.ActiveUIDo

3、cument;ElementSet collector = new ElementSet();IList<Element> elemList = uidoc.Selection.PickElementsByRectangle();/ 矩形框的應(yīng)用 string info ="所選圖元:"foreach (Element elem in elemList) collector.Insert(elem);info += "nt" + elem.ToString();TaskDialog.Show("Choose Items",

4、 info); return Result.Succeeded;1.2.2 用點擊選擇圖元所用方法:PickObject(ObjectType.Element)using Autodesk.Revit.Ul.Selection; / 聲明引用命名空間, ObjectTypeUIDocument uidoc = commandData.Application.ActiveUIDocument;Reference hasPickOne = uidoc.Selection.PickObject(ObjectType.Element);Element elem = uidoc.Document.Get

5、Element(hasPickOne.ElementId);string info ="所選圖元:"info += "nt" + elem.GetType().ToString();TaskDialog.Show("Revit", info);還存在其它用法:Selection.PickObject (ObjectType, String) /string 為提示PickObjects (ObjectType) / 允許選擇多個元素1.2.3 選擇及過濾UIDocument uidoc = commandData.Applicatio

6、n.ActiveUIDocument;ISelectionFilter selFilter = new MassSelectionFilter();IList<Element> eList = uidoc.Selection.PickElementsByRectangle(selFilter) as IList<Element>string info ="選擇了以下的墻:"foreach(Element elem in eList) info += "nt" + elem.Name;TaskDialog.Show("Re

7、vit", info);return Result.Succeeded;public class MassSelectionFilter : ISelectionFilter public bool AllowElement(Element elem) return elem is Wall;/ 選擇了 墻public bool AllowReference(Reference reference, XYZ position)throw new NotImplementedException(); /第二個條件2 .加載族并創(chuàng)建實例UlApplication m_applicatio

8、n;UIDocument m_document;m_application = commandData.Application;m_document = m_application.ActiveUIDocument;2.1 .加載族1) m_document .Document.LoadFamilySymbol ( FullPath, symbolName, out loadedfamilySymbol);2) m_document .Document.LoadFamily ( FullPath, out loadedfamily);第一種用來加載族類型,第二種用來加載族,返回的 loadfa

9、mily和loadfamilysymbol可以隨后用來創(chuàng)建族實例2.2 創(chuàng)建族實例創(chuàng)建族實例將經(jīng)過:1)選擇一個平面,建立一個工作平面2)選擇一個點放置族實例ObjectType .Face, new2.3 選擇平面Reference faceRef = m_document.Selection.PickObject(PlanarFaceFilter (m_document.Document), "Please pick a planar face to set the work plane. ESC for cancel.");GeometryObject geoObje

10、ct =m_document.Document.GetElement(faceRef).GetGeometryObjectFromReference(faceRef);PlanarFace planarFace = geoObject as PlanarFace ;/判斷選擇為平面的類public class PlanarFaceFilter : ISelectionFilter / Revit document.Document m_doc = null ;/ Constructor the filter and initialize the document. public PlanarF

11、aceFilter( Documentdoc)m_doc = doc;Ill Allow all the element to be selected public bool AllowElement( Element element) ( return true ;)Ill Allow planar face reference to be selected public bool AllowReference( Reference refer, XYZpoint) (GeometryObject geoObject =m_doc.GetElement(refer).GetGeometryO

12、bjectFromReference(refer); return geoObject != null && geoObject is PlanarFace ; ) )2.4 創(chuàng)建工作平面SketchPlane faceSketchPlane = CreateSketchPlane(planarFace.FaceNormal, planarFace.Origin); if (faceSketchPlane != null ) (II對模型進行操作必須新建 Transaction 事件Transaction changeSketchPlane = new Transaction

13、(m_document.Document, "Change Sketch Plane.");changeSketchPlane.Start();m_document.Document.ActiveView.SketchPlane = faceSketchPlane;m_document.Document.ActiveView.ShowActiveWorkPlane();changeSketchPlane.Commit();)II創(chuàng)建工作平面的子方法internal SketchPlane CreateSketchPlane(Autodesk.Revit.DB. XYZnor

14、mal, Autodesk.Revit.DB. XYZorigin) (II First create a Geometry.Plane which need in NewSketchPlane() method Plane geometryPlane = m_application.Application.Create.NewPlane(normal, origin);II Then create a sketch plane using the Geometry PlaneTransaction createSketchPlane = new Transaction (m_document

15、.Document, "Create a sketch plane." );createSketchPlane.Start();SketchPlane plane = SketchPlane .Create(m_document.Document, geometryPlane); createSketchPlane.Commit();return plane;2.5 選擇一個點并創(chuàng)建實例/ Pick point from current work plane with snaps.ObjectSnapTypes snapType = ObjectSnapTypes .Cen

16、ters | ObjectSnapTypes .Endpoints | ObjectSnapTypes .Intersections ObjectSnapTypes .Midpoints | ObjectSnapTypes .Nearest | ObjectSnapTypes .WorkPlaneGrid;XYZpoint = m_document.Selection.PickPoint(snapType, "Please pick a point to place component.");Transaction LoadAndCreat = new Transactio

17、n (m_document.Document, "Load a Family." ); LoadAndCreat.Start(); /對模型進行操作必須新建 Transaction 事件 /加載族并創(chuàng)建實例 FamilySymbol loadedfamilySymbol = null ; string directory = "C:UsersAdministratorDesktopRevitProgramFamily" ;string symbolName ="測斜管";try ( m_document.Document.LoadFa

18、milySymbol( string .Format( "01.rfa" ,directory,symbolName ),symbolName ,out loadedfamilySymbol);catch ( Exception e) message = e.Message; return Result .Failed;if ( null = loadedfamilySymbol) TaskDialog .Show("Revit" , "Can't load the prepared rfa." ); return Resul

19、t .Failed;/創(chuàng)建族實例CreatePointFamilyInstance(point, loadedfamilySymbol,StructuralType .NonStructural);LoadAndCreat.Commit(); / 子事件結(jié)束 /創(chuàng)建族實例的方法 public bool CreatePointFamilyInstance( XYZlocation, FamilySymbol symbol, StructuralType structuralType) if (!symbol.IsActive) symbol.Activate();FamilyInstance i

20、nstance = m_document.Document.Create.NewFamilyInstance(location, symbol, structuralType);List <ElementId > instanceld = new List <ElementId >();instanceld.Add(instance.Id);/已經(jīng)在選擇集里m_document.Selection.SetElementIds(instanceId); return true ;3 .用過濾器統(tǒng)計傳感器3.1 通過族類型名稱查找族實例/ 用到了慢過濾器:FamilyIns

21、tanceFilterUIDocument uidoc = commandData.Application.ActiveUIDocument;Documentdoc = uidoc.Document;FilteredElementCollector collector = new FilteredElementCollector (doc); collector = collector.OfClass( typeof (FamilySymbol ); var query = from element in collectorwhere element.Name ="測斜管"

22、/查找名為“測斜管”的族實例select element; /linq 查詢List <Element > famSyms = query.ToList< Element >();ElementId symbolId = famSyms0.Id; /創(chuàng)建過濾器并找到該族類型對應(yīng)的所有族實例 collector = new FilteredElementCollector (doc);FamilyInstanceFilter filter = new FamilyInstanceFilter (doc, symbolId);IList <Element > f

23、ounds = collector.WherePasses(filter).ToElements(); foreach ( FamilyInstance inst in founds) TaskDialog .Show( "Revit" , string .Format( "FamilyInstance:t0nFamilySybmol Id:t1nName:t2”,inst.Id.IntegerValue,inst.Symbol.Id.IntegerValue,inst.Symbol.Name);/新建對話框,選擇是否刪除查找到的族實例TaskDialog mai

24、nDialog = new TaskDialog ("刪除"); mainDialog.MainContent ="單擊'"Ok"刪除傳感器!"mainDialog.CommonButtons = TaskDialogCommonButtons .Ok | TaskDialogCommonButtons .Cancel; if ( TaskDialogResult .Ok = mainDialog.Show()/對元件進行編輯都必須新建事件來解決using ( Transaction delTrans = new Transa

25、ction (doc, "delete the element!" ) delTrans.Start();doc.Delete(founds0.Id);delTrans.Commit();3.2 通過族名查找族類型/ 用到了快過濾器:FamilySymbolFilterUIDocument uidoc = commandData.Application.ActiveUIDocument;Documentdoc = uidoc.Document;FilteredElementCollector collector = new FilteredElementCollector

26、(doc); collector = collector.OfClass( typeof (Family ); var query = from element in collectorwhere element.Name ="測余4管" select element; /Linq 查詢ListElementfam= query.ToList< Element>();ElementId famId = fam0.Id;/創(chuàng)建過濾器并找到該族對應(yīng)的所有族類型collector = new FilteredElementCollector (doc);FamilyS

27、ymbolFilter filter = new FamilySymbolFilter (famId);收集族類型名稱IList <Element> founds = collector.WherePasses(filter).ToElements();foreach ( FamilySymbol syms in founds) (TaskDialog .Show("Revit" , string .Format( "FamilySymbol:t0nFamilyId:t1nName:t2”,syms.Id.IntegerValue, syms.Fami

28、ly.Id.IntegerValue, syms.Family.Name);/收集族類型IDICollection <ElementId > found= collector.WherePasses(filter).ToElementIds(); foreach ( ElementId symsId in found) TaskDialog .Show("Revit" , string .Format( "FamilySymbol Id:t0nName:t1" symsId.IntegerValue, symsId.ToString();4

29、.實例的隔離與顏色改變4.1 實例的顏色改變UIDocument uidoc = commandData.Application.ActiveUIDocument;Documentdoc = uidoc.Document;/過濾填充圖案(doc);FilteredElementCollector fillPatternFilter = new FilteredElementCollector fillPatternFilter.OfClass( typeof (FillPatternElement ); /獲取實體填充FillPatternElement fp = fillPatternFil

30、ter.First(m => (masFillPatternElement ).GetFillPattern().IsSolidFill)as FillPatternElement;Reference hasPickOne = uidoc.Selection.PickObject(ObjectType .Element);if (hasPickOne != null ) Transaction trans = new Transaction (doc, "trans");trans.Start();View v = doc.ActiveView;OverrideGra

31、phicSettings ogs = v.GetElementOverrides(hasPickOne.Elementld);Color oldColor = ogs.ProjectionFillColor;/ 實例的現(xiàn)在顏色Elementld oldEleld = ogs.ProjectionFillPatternld;/ 現(xiàn)在的 fillPatternid/設(shè)置 投影/表面-> 填充圖案->填充圖案ogs.SetProjectionFillPatternld(fp.Id);/設(shè)置 投影/表面-> 填充圖案-> 顏色ogs.SetProjectionFillColor

32、( new Color (255, 0, 0);/應(yīng)用到視圖v.SetElementOverrides(hasPickOne.Elementld, ogs); trans.Commit();)return Result .Succeeded;4.2 實例的隔離與恢復(fù)顯示 單獨構(gòu)建:1. var ui = commandData.Application.ActiveUlDocument;2. var doc = ui.Document;3.4. var select = ui.Selection;5. var refe = select.PickObject(ObjectType.Element);6.7. using (Transaction trans =new Transaction(doc)8. (9. trans.Start("isloate" );10. ui.ActiveView.lsolateElementTemporary(refe.Elementld);11. trans.Commit();12. 構(gòu)建組:1. var ui = commandData.Application.ActiveUlDocument;2. var doc = ui.Document;3. var select = ui.Selection;4. var ref

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論