public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素, 本次按只能选择柱考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element, "选择个柱吧"); //通过引用取到选中的元素 Element element = doc.GetElement(pickedEleReference); FamilyInstance column = doc.GetElement(pickedEleReference) as FamilyInstance; string info = "提示如下:"; info += "\n\t" + "1 族类别ELEM_CATEGORY_PARAM_MT" + element.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM_MT).AsValueString(); TaskDialog.Show("提示", info); if (null != column) { Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisX, XYZ.Zero); if (ElementTransformUtils.CanMirrorElement(doc, column.Id)) { ElementTransformUtils.MirrorElement(doc, column.Id, plane); } TaskDialog.Show("提示", "成功"); } else { TaskDialog.Show("提示", "失败"); } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //重要的属性: category类别, location, levelId标高, group组, id, uniqueId唯一id //重要的方法: get.materials, //通过点选,选择一个元素 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用,选取到选中的元素. (群里大佬指点, 用reference是正常方法) Element elem = doc.GetElement(pickedEleReference); FamilyInstance familyInstance = elem as FamilyInstance; string family = familyInstance.Category.Name; string familySymbol = familyInstance.Name; string info = "选择的元素属性如下"; info += "\n\t" + "族名称是:" + family; info += "\n\t" + "族类型名称是:" + familySymbol; TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Element elem = doc.GetElement(pickedEleReference); ParameterSet parameters = elem.Parameters; string info = "属性如下:"; foreach (Parameter para in parameters) { if (para.Definition.Name == "长度" && para.StorageType == StorageType.Double) { string length = para.AsValueString(); info += length; } } TaskDialog.Show("提示", info, TaskDialogCommonButtons.Close); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); string info = "length = "; Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Element elem = doc.GetElement(pickedEleReference); Wall wall = elem as Wall; Parameter parameterLength = wall.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH); if (parameterLength != null && parameterLength.StorageType == StorageType.Double) { double length = parameterLength.AsDouble(); info += "\n\t" + length.ToString(); } TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素, 本次按只能选择墙考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Wall wall = doc.GetElement(pickedEleReference) as Wall; if (null != wall) { LocationCurve wallLine = wall.Location as LocationCurve; XYZ pointOne = XYZ.Zero; XYZ pointTwo = new XYZ(200, 200, 0); //定义线 Line newWallLine = Line.CreateBound(pointOne, pointTwo); //把墙的位置线换成新的线 wallLine.Curve = newWallLine; } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素, 本次按只能选择墙考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Wall wall = doc.GetElement(pickedEleReference) as Wall; string info = "信息如下"; info += "\n\t" + "1 ELEM_TYPE_PARAM (族名称):" + wall.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsValueString(); info += "\n\t" + "2 长度:" + wall.LookupParameter("长度").AsValueString(); TaskDialog.Show("提示", info); //当新添加的族类型族中已经有的时候, 该程序不能正常运行. WallType wallType = wall.WallType; ElementType duplicatedWallType = wallType.Duplicate(wallType.Name + "duplicated7"); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //获取当前选择的id var collection = sel.GetElementIds(); if (0 == collection.Count) { //如果执行该例子之前没有选择任何元素, 则会弹出提示 TaskDialog.Show("工具集提示", "您为选择任何元素"); } else { string info = "您选择的元素为:"; foreach (var elem in collection) { info += "\n\t" + elem.GetType().ToString(); } //显示选择的元素 TaskDialog.Show("工具集提示", info); } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //1 创建收集器 FilteredElementCollector filteredElements = new FilteredElementCollector(doc); //2 创建一个过滤器 ElementClassFilter classFilter = new ElementClassFilter(typeof(Wall)); //接着调用收集器的WherePasses函数对元素进行过滤 filteredElements.WherePasses(classFilter); string info = "所选元素为: "; foreach (var wall in filteredElements) { info += "\n\t" + wall.GetTypeId().ToString(); } TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); List <ElementId> elementsToDelete = new List <ElementId>(); //uidoc.Selection.PickElementsByRectangle() 是一个让用户用鼠标画矩形框选择的 方法 string info = "删除的元素如下:"; foreach (var element in uidoc.Selection.PickElementsByRectangle()) { elementsToDelete.Add(element.Id); info += "\n\t" + element.Id; } ICollection <ElementId> deletedElements = doc.Delete(elementsToDelete); TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素, 本次按只能选择柱考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Element element = doc.GetElement(pickedEleReference); FamilyInstance column = element as FamilyInstance; if (null != column) { LocationPoint columnPoint = column.Location as LocationPoint; XYZ newLocation = new XYZ(100, 100, 0); columnPoint.Point = newLocation; } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //删除因素的练习 //点选指定执行的元素, 本次按只能选择柱考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element, "选择个元素吧"); //通过引用取到选中的元素 Element element = doc.GetElement(pickedEleReference); string info = "成功删除了: "; info += element.Id.ToString(); ICollection <ElementId> deleterElementIds = doc.Delete(element.Id); TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //获取一堵墙,并创建一条和墙位置垂直的旋转轴,然后对这堵墙进行逆时针60°的旋转 //点选指定执行的元素, 本次按只能选择柱考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element, "选择个墙吧"); //通过引用取到选中的元素 Wall wall = doc.GetElement(pickedEleReference) as Wall; LocationCurve wallLine = wall.Location as LocationCurve; XYZ point1 = wallLine.Curve.GetEndPoint(0); XYZ point2 = new XYZ(point1.X, point1.Y, 30); Line axis = Line.CreateBound(point1, point2); ElementTransformUtils.RotateElement(doc, wall.Id, axis, Math.PI / (180 / 60)); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); //通过ID选择元素. //不用写在事务里, 为什么? int targetID = 417275; var elementId = new ElementId(targetID); sel.SetElementIds(new List <ElementId>() { elementId }); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //点选指定执行的元素 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用取到选中的元素 Element elem = doc.GetElement(pickedEleReference); string info = "所选元素为: "; info += "\n\t" + elem.GetType().ToString(); TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //获取revit文档的创建句柄 Autodesk.Revit.Creation.Document creater = doc.Create; //创建一根柱子: 使用给点的位置(坐标原点),柱子的类型和标高(高度为0) XYZ origin = new XYZ(0, 0, 0); //获得level Level level = null; if (acview.ViewType == ViewType.FloorPlan) { level = acview.GenLevel; } if (level == null) { MessageBox.Show("未找到楼层"); return(Result.Cancelled); } //获取symbol var symbol = doc.TCollector <FamilySymbol>() .Where(m => m.Category.Id.IntegerValue == (int)BuiltInCategory.OST_StructuralColumns).First(); if (symbol == null) { MessageBox.Show("未找到柱子的族符号"); return(Result.Cancelled); } //创建柱子 FamilyInstance colum = creater.NewFamilyInstance(origin, symbol, level, Autodesk.Revit.DB.Structure.StructuralType.Column); // 把柱子移动到新的位置 XYZ newPlace = new XYZ(100, 100, 100); ElementTransformUtils.MoveElement(doc, colum.Id, newPlace); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); //重要的属性: category类别, location, levelId标高, group组, id, uniqueId唯一id //重要的方法: get.materials, //通过点选,选择一个元素 Reference pickedEleReference = sel.PickObject(ObjectType.Element); //通过引用,选取到选中的元素. (群里大佬指点, 用reference是正常方法) Element elem = doc.GetElement(pickedEleReference); if (elem == null) { TaskDialog.Show("提示", "啥也没有,重选"); } var categoryName = elem.Category.Name; Location location = elem.Location; var levelId = elem.LevelId; var group = elem.GroupId; var id = elem.Id; var uniqueId = elem.UniqueId; string info = "选择的元素属性如下"; info += "\n\t" + "category:" + categoryName; info += "\n\t" + "location:" + location; info += "\n\t" + "levelId:" + levelId; info += "\n\t" + "group:" + group; info += "\n\t" + "id:" + id; info += "\n\t" + "uniqueId:" + uniqueId; TaskDialog.Show("提示", info); ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acview = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); ///通过LocationCurve或LocationPoint旋转元素 //点选指定执行的元素, 本次按只能选择柱考虑 Reference pickedEleReference = sel.PickObject(ObjectType.Element, "选择个墙或柱吧"); //通过引用取到选中的元素 Element element = doc.GetElement(pickedEleReference); string info = "提示如下:"; info += "\n\t" + "1 族类别ELEM_CATEGORY_PARAM_MT" + element.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM_MT).AsValueString(); info += "\n\t" + "2 族类别ELEM_CATEGORY_PARAM" + element.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM).AsValueString(); TaskDialog.Show("提示", info); XYZ point1 = XYZ.Zero; XYZ point2 = XYZ.Zero; //当选择的对象是墙时:通过元素的位置来旋转元素 if (element.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM_MT).AsValueString() == "墙") { Wall wall = doc.GetElement(pickedEleReference) as Wall; LocationCurve curve = wall.Location as LocationCurve; Curve line = curve.Curve; point1 = line.GetEndPoint(0); point2 = new XYZ(point1.X, point1.Y, point1.Z + 10); Line axis = Line.CreateBound(point1, point2); curve.Rotate(axis, Math.PI / (180 / 30)); } //当选择的对象是柱时:通过元素的位置点来旋转元素 else if (element.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM_MT).AsValueString() == "柱") { LocationPoint point = element.Location as LocationPoint; if (null != point) { point1 = point.Point; point2 = new XYZ(point1.X, point1.Y, point1.Z + 10); Line axis = Line.CreateBound(point1, point2); point.Rotate(axis, Math.PI / (180 / 30)); } } //当选择的对象是不是墙也不是柱时, 提示干不了. else { TaskDialog.Show("提示", "哥,既不是墙,也不是柱,我没法弄啊"); } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; View acView = uidoc.ActiveView; UIView acuiview = uidoc.ActiveUiview(); Transaction ts = new Transaction(doc, "******"); try { ts.Start(); ///在文档中,找名字为"0762x2032"的门类型, 如果没有找到,则加载一个名称为"M_单嵌板4.rfa"的族文件,这样就得到一个族,然后从族中获取名为0762x2032的门类型. /// 然后在文档中找到一个直线形的墙,计算墙的重点位置,在此处插入类型为 0762x2032的门. /// string doorTypeName = "750 x 2000mm"; FamilySymbol doorType = null; //在文档中找到名字为 "0762 x 2032"的门类型 ElementFilter doorCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors); ElementFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol)); LogicalAndFilter andFilter = new LogicalAndFilter(doorCategoryFilter, familySymbolFilter); FilteredElementCollector doorSymbols = new FilteredElementCollector(doc); doorSymbols = doorSymbols.WherePasses(andFilter); bool symbolFound = false; foreach (FamilySymbol element in doorSymbols) { if (element.Name == doorTypeName) { symbolFound = true; doorType = element; break; } } //如果没有找到, 就加载一个族文件 if (!symbolFound) { string file = @"C:\ProgramData\Autodesk\RVT 2019\Libraries\China\建筑\门\普通门\平开门\单扇"; Family family; bool loadSuccess = doc.LoadFamily(file, out family); if (loadSuccess) { foreach (var doorTypeId in family.GetValidTypes()) { doorType = doc.GetElement(doorTypeId) as FamilySymbol; if (doorType != null) { if (doorType.Name == doorTypeName) { break; } } } } else { TaskDialog.Show("load family failed", "could not load family file"); } } //使用组类型创建门 if (doorType != null) { //首先找到线性的墙 ElementFilter wallFilter = new ElementClassFilter(typeof(Wall)); FilteredElementCollector filteredElements = new FilteredElementCollector(doc); filteredElements = filteredElements.WherePasses(wallFilter); Wall wall = null; Line line = null; foreach (Wall element in filteredElements) { LocationCurve locationCurve = element.Location as LocationCurve; if (locationCurve != null) { line = locationCurve.Curve as Line; if (line != null) { wall = element; break; } } } //在墙的中心创建一个门 if (wall != null) { XYZ midPoint = (line.GetEndPoint(0) + line.GetEndPoint(1)) / 2; Level wallLevel = doc.GetElement(wall.LevelId) as Level; //创建门: 传入标高参数 FamilyInstance door = doc.Create.NewFamilyInstance(midPoint, doorType, wall, wallLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); TaskDialog.Show("成功", door.Id.ToString()); Trace.WriteLine("door created" + door.Id.ToString()); } else { TaskDialog.Show("失败", "元素不存在,没找到合适的墙"); } } else { TaskDialog.Show("失败提示", "没有找到族类型" + doorTypeName); } ts.Commit(); } catch (Exception) { if (ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } return(Result.Succeeded); }