/// <summary> /// The top level command. /// </summary> /// <param name="revit">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { m_app = revit.Application.Application; m_doc = revit.Application.ActiveUIDocument.Document; // Find a 3D view to use for the ray tracing operation FilteredElementCollector collector = new FilteredElementCollector(m_doc); Func <View3D, bool> isNotTemplate = v3 => !(v3.IsTemplate); m_view3D = collector.OfClass(typeof(View3D)).Cast <View3D>().First <View3D>(isNotTemplate); Autodesk.Revit.UI.Selection.Selection selection = revit.Application.ActiveUIDocument.Selection; // If skylight is selected, process it. m_skylight = null; if (selection.GetElementIds().Count == 1) { foreach (ElementId eId in selection.GetElementIds()) { Element e = revit.Application.ActiveUIDocument.Document.GetElement(eId); if (e is FamilyInstance) { FamilyInstance instance = e as FamilyInstance; bool isWindow = (instance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Windows); bool isHostedByRoof = (instance.Host.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Roofs); if (isWindow && isHostedByRoof) { m_skylight = instance; } } } } if (m_skylight == null) { message = "This tool requires exactly one skylight to be selected."; return(Result.Cancelled); } // Find the floor to use for the measurement (hardcoded) ElementId id = new ElementId(150314); m_floor = m_doc.GetElement(id) as Floor; // Calculate the height Line line = CalculateLineAboveFloor(); // Create a model curve to show the distance Plane plane = m_app.Create.NewPlane(new XYZ(1, 0, 0), line.GetEndPoint(0)); SketchPlane sketchPlane = SketchPlane.Create(m_doc, plane); ModelCurve curve = m_doc.Create.NewModelCurve(line, sketchPlane); // Show a message with the length value TaskDialog.Show("Distance", "Distance to floor: " + String.Format("{0:f2}", line.Length)); return(Result.Succeeded); }
static void CheckSelectionChange() { Autodesk.Revit.UI.Selection.Selection selection = _uiApplication.ActiveUIDocument.Selection; ElementId IdSelection; ICollection <ElementId> selectedIds = selection.GetElementIds(); int PreviousId = 0; if (selectedIds.Count == 0) { IdSelection = null; PreviousId = 0; } else { IdSelection = selectedIds.ElementAt(selectedIds.Count - 1); PreviousId = IdSelection.IntegerValue; } int i = 0, CurrentId = 0; bool IsSecond = true;; while (true) { Thread.Sleep(500); if (IsSecond && RaisePeriodicEvent) { _PeriodicEvent.Raise(); } IsSecond = !IsSecond; selection = _uiApplication.ActiveUIDocument.Selection; selectedIds = selection.GetElementIds(); if (selectedIds.Count == 0) { IdSelection = null; CurrentId = 0; } else { IdSelection = selectedIds.ElementAt(selectedIds.Count - 1); CurrentId = IdSelection.IntegerValue; } if (CurrentId != PreviousId) { if (RaiseOnSelectionEvent) { _SelectionChangedEvent.Raise(); } PreviousId = CurrentId; i++; } } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { AllocConsole();//開啟主控台 Console.WriteLine("針對特定的ID及參數名稱插入參數值.."); UIDocument uidoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.UI.Selection.Selection selElement = uidoc.Selection; Autodesk.Revit.DB.Document doc = uidoc.Document; //Revit 2017一定要用Document來抓Element,不再有SelElementSet類別 Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("238494", "紀乃文到此一遊,北面牆"); dic.Add("238695", "紀乃文到此一遊,東面牆"); dic.Add("238855", "紀乃文到此一遊,西面牆"); dic.Add("238993", "紀乃文到此一遊,南面牆"); Transaction trans2 = new Transaction(doc); trans2.Start("設定參數");//裡面這個字串不能省不然會報錯 foreach (ElementId eleId in selElement.GetElementIds()) { Element ele = doc.GetElement(eleId); Console.WriteLine(eleId.ToString() + "\t" + ele.Name); this.setMark(dic, eleId, doc); } trans2.Commit(); Console.Read(); FreeConsole(); //和Revit配合時,一定要用程式碼將主控台關掉,如果直接把主控台視窗按掉的話,會連Revit一起關掉 return(Result.Succeeded); }
public static extern void FreeConsole();//關閉主控台 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { AllocConsole();//開啟主控台 Console.WriteLine("以下列印出所有選取元素的ID.."); UIDocument uidoc = commandData.Application.ActiveUIDocument; //UIDocument代表「現在正開啟的模型」 Autodesk.Revit.UI.Selection.Selection selElement = uidoc.Selection; //Selection代表「使用者用滑鼠左鍵按住拖曳所選取的元件」 Autodesk.Revit.DB.Document doc = uidoc.Document; //Document類別可以用來存放模型的全部或是一部份,從uidoc存取Document屬性, //就會把整個模型放進Document類別當中 foreach (ElementId eleId in selElement.GetElementIds()) { Element ele = doc.GetElement(eleId); //以ElementId在document當中找到對應的Element元件 //Document docSingle = ele.Document; //單一的元素也有Document屬性,但在這裡我們還不會用上 Console.WriteLine(eleId.ToString() + "\t" + ele.UniqueId + "\t" + ele.Name); } Console.Read(); //如果我們想要在螢幕上看到主控台的執行成果,要用Console.Read()叫它等一下,否則它會「一閃即逝」 FreeConsole(); //和Revit配合時,一定要用程式碼將主控台關掉,如果直接把主控台視窗按掉的話,會連Revit一起關掉 return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { try { UIDocument uidoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.UI.Selection.Selection selection = uidoc.Selection; var collectionId = selection.GetElementIds(); if (0 == collectionId.Count) { TaskDialog.Show("Revit", "You haven't selected any elements."); } else { string info = "Ids of selected elements in the document are: "; foreach (var id in collectionId) { info += "\n\t" + id.IntegerValue; } TaskDialog.Show("Revit", info); } } catch (Exception e) { message = e.Message; return(Autodesk.Revit.UI.Result.Failed); } return(Autodesk.Revit.UI.Result.Succeeded); }
private void button1_Click(object sender, EventArgs e) { Autodesk.Revit.UI.Selection.Selection selElement = uidoc.Selection; ICollection <ElementId> elementIds = selElement.GetElementIds(); foreach (ElementId elementId in elementIds) { Element elem = doc.GetElement(elementId); BoundingBoxXYZ bbx = elem.get_BoundingBox(null); //從元件取得「邊界長方體」(包覆元件邊界的長方體,如果元件本身是長方體,就會完全包覆) //OutLine是一條線,此處等於直接拿包覆長方體的對角線來定義它 Outline outline = new Outline(bbx.Min, bbx.Max);//Min及Max各是一個點,都能存取XYZ座標 BoundingBoxIntersectsFilter filterIntersection = new BoundingBoxIntersectsFilter(outline); //這個過濾器會取得「所有和這個BoundingBox相交的BoundingBox,並且傳回其所代表之元件」 IList <Element> Intersects = new FilteredElementCollector(doc).WherePasses(filterIntersection).WhereElementIsNotElementType().ToElements(); StringBuilder sb = new StringBuilder("這個元件是" + elem.Category.Name + ":" + elem.Id + ":" + elem.Name + ","); sb.AppendLine("和它相鄰的元件有:"); foreach (Element eleFiltered in Intersects) { try //這邊需要用try包起來,因為我發現有些Element似乎是沒有Category.Name的,這會發生空參考錯誤 { sb.AppendLine(eleFiltered.Category.Name + ":" + eleFiltered.Id + ":" + eleFiltered.Name); //印出相鄰元件資訊 } catch (Exception ex) { } } sb.AppendLine("======="); MessageBox.Show(sb.ToString()); } //end foreach } //end fun
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.UI.Selection.Selection selElement = uidoc.Selection; Autodesk.Revit.DB.Document doc = uidoc.Document; foreach (ElementId eleId in selElement.GetElementIds()) { Element ele = doc.GetElement(eleId); TaskDialog.Show("印出選取元件的id", eleId.ToString() + "\t" + ele.Name); } return(Result.Succeeded); }
private void button1_Click(object sender, EventArgs e) { Transaction trans = new Transaction(doc); trans.Start("自動貼材質"); //從Selection當中取得ElementId集合,再取得Element,這些和之前學過的都一樣 foreach (ElementId eleId in selElement.GetElementIds()) { Element ele = doc.GetElement(eleId); //下一行很重要!從下拉式選單裡再分離出使用者所選的材質Id ElementId eid = new ElementId(Convert.ToInt32(this.comboBox1.Text.Split('\t')[0])); try { this.PaintElementFace(ele, eid); //然後試著塗色,為什麼要「試」,因為有一些元件像門窗是不能貼材質的,不知它們會不會拋出例外 } catch (Exception ex) { } //但是我們面對例外就讓它do nothing就好 } trans.Commit(); //最後認列交易 }//end fun
/// <summary> /// This example set values to the parameter we created in the previous unit /// </summary> /// <param name="commandData"></param> /// <param name="message"></param> /// <param name="elements"></param> /// <returns></returns> public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.UI.Selection.Selection selElement = uidoc.Selection; Autodesk.Revit.DB.Document doc = uidoc.Document; //this is the first time to use transaction object, becasue we want to modfify the BIM model Transaction trans2 = new Transaction(doc); trans2.Start("set values for parameters"); //you must give a name to the transaction, cannot be ignored int k = 1; foreach (ElementId eleId in selElement.GetElementIds()) { Element ele = doc.GetElement(eleId); this.setMark(k, eleId, doc); k++; } trans2.Commit(); MessageBox.Show("set values to parameters successfully!"); return(Result.Succeeded); }