/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">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 Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { Autodesk.Revit.UI.UIApplication application = commandData.Application; m_docment = application.ActiveUIDocument.Document; try { // user should select one slab firstly. if (application.ActiveUIDocument.Selection.Elements.Size == 0) { TaskDialog.Show("Revit", "Please select one slab firstly.", TaskDialogCommonButtons.Ok); return(Autodesk.Revit.UI.Result.Cancelled); } // get the selected slab and show its span direction ElementSet elementSet = application.ActiveUIDocument.Selection.Elements; ElementSetIterator elemIter = elementSet.ForwardIterator(); elemIter.Reset(); while (elemIter.MoveNext()) { Floor floor = elemIter.Current as Floor; if (floor != null) { GetSpanDirectionAndSymobls(floor); } } } catch (Exception ex) { message = ex.ToString(); return(Autodesk.Revit.UI.Result.Failed); } return(Autodesk.Revit.UI.Result.Succeeded); }
/// <summary> /// Initializes a new instance of TagBeamData. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command</param> public TagBeamData(ExternalCommandData commandData) { //Get beams selected m_revitDoc = commandData.Application.ActiveUIDocument; m_docCreator = m_revitDoc.Document.Create; m_view = m_revitDoc.Document.ActiveView; ElementSet elementSet = new ElementSet(); foreach (ElementId elementId in m_revitDoc.Selection.GetElementIds()) { elementSet.Insert(m_revitDoc.Document.GetElement(elementId)); } ElementSetIterator itor = elementSet.ForwardIterator(); while (itor.MoveNext()) { FamilyInstance familyInstance = itor.Current as FamilyInstance; if ((familyInstance != null) && (familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Beam)) { m_beamList.Add(familyInstance); } } if (m_beamList.Count < 1) { throw new ApplicationException("there is no beam selected"); } //Get the family symbols of tag in this document. FilteredElementCollector collector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document); IList <Element> elements = collector.OfClass(typeof(Family)).ToElements(); foreach (Family family in elements) { if (family != null && family.GetFamilySymbolIds() != null) { List <FamilySymbol> ffs = new List <FamilySymbol>(); foreach (ElementId elementId in family.GetFamilySymbolIds()) { ffs.Add((FamilySymbol)(commandData.Application.ActiveUIDocument.Document.GetElement(elementId))); } foreach (FamilySymbol tagSymbol in ffs) { try { if (tagSymbol != null) { switch (tagSymbol.Category.Name) { case "Structural Framing Tags": m_categoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; case "Material Tags": m_materialTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; case "Multi-Category Tags": m_multiCategoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; default: continue; } } } catch (Exception) { continue; } } } } }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="cmdData">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="msg">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="eleSet">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 Autodesk.Revit.UI.Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet eleSet) { Autodesk.Revit.UI.Result res = Autodesk.Revit.UI.Result.Succeeded; Transaction trans = new Transaction(cmdData.Application.ActiveUIDocument.Document, "Revit.SDK.Samples.MoveLinear"); trans.Start(); try { System.Collections.IEnumerator iter; Autodesk.Revit.UI.Selection.Selection sel; sel = cmdData.Application.ActiveUIDocument.Selection; ElementSet elemSet; elemSet = new ElementSet(); foreach (ElementId elementId in sel.GetElementIds()) { elemSet.Insert(cmdData.Application.ActiveUIDocument.Document.GetElement(elementId)); } //Check whether user has selected only one element if (0 == elemSet.Size) { TaskDialog.Show("MoveLinear", "Please select an element"); trans.Commit(); return(res); } if (1 < elemSet.Size) { TaskDialog.Show("MoveLinear", "Please select only one element"); trans.Commit(); return(res); } iter = elemSet.ForwardIterator(); iter.MoveNext(); Autodesk.Revit.DB.Element element; element = (Autodesk.Revit.DB.Element)iter.Current; if (element != null) { Autodesk.Revit.DB.LocationCurve lineLoc; lineLoc = element.Location as LocationCurve; if (null == lineLoc) { TaskDialog.Show("MoveLinear", "Please select an element which based on a Line"); trans.Commit(); return(res); } Autodesk.Revit.DB.Line line; //get start point via "get_EndPoint(0)" Autodesk.Revit.DB.XYZ newStart = new XYZ( lineLoc.Curve.GetEndPoint(0).X + 100, lineLoc.Curve.GetEndPoint(0).Y, lineLoc.Curve.GetEndPoint(0).Z); //get end point via "get_EndPoint(1)" Autodesk.Revit.DB.XYZ newEnd = new XYZ( lineLoc.Curve.GetEndPoint(1).X, lineLoc.Curve.GetEndPoint(1).Y + 100, lineLoc.Curve.GetEndPoint(1).Z); //get a new line and use it to move current element //with property "Autodesk.Revit.DB.LocationCurve.Curve" line = Line.CreateBound(newStart, newEnd); lineLoc.Curve = line; } } catch (Exception ex) { TaskDialog.Show("MoveLinear", ex.Message); res = Autodesk.Revit.UI.Result.Failed; } finally { } trans.Commit(); return(res); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">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 Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // set out default result to failure. Autodesk.Revit.UI.Result retRes = Autodesk.Revit.UI.Result.Failed; Autodesk.Revit.UI.UIApplication app = commandData.Application; // get the elements selected // The current selection can be retrieved from the active // document via the selection object ElementSet seletion = new ElementSet(); foreach (ElementId elementId in app.ActiveUIDocument.Selection.GetElementIds()) { seletion.Insert(app.ActiveUIDocument.Document.GetElement(elementId)); } // we need to make sure that only one element is selected. if (seletion.Size == 1) { // we need to get the first and only element in the selection. Do this by getting // an iterator. MoveNext and then get the current element. ElementSetIterator it = seletion.ForwardIterator(); it.MoveNext(); Element element = it.Current as Element; // Next we need to iterate through the parameters of the element, // as we iterating, we will store the strings that are to be displayed // for the parameters in a string list "parameterItems" List <string> parameterItems = new List <string>(); ParameterSet parameters = element.Parameters; foreach (Parameter param in parameters) { if (param == null) { continue; } // We will make a string that has the following format, // name type value // create a StringBuilder object to store the string of one parameter // using the character '\t' to delimit parameter name, type and value StringBuilder sb = new StringBuilder(); // the name of the parameter can be found from its definition. sb.AppendFormat("{0}\t", param.Definition.Name); // Is the parameter shared? sb.AppendFormat(param.IsShared.ToString() + "\t"); // Revit parameters can be one of 5 different internal storage types: // double, int, string, Autodesk.Revit.DB.ElementId and None. // if it is double then use AsDouble to get the double value // then int AsInteger, string AsString, None AsStringValue. // Switch based on the storage type switch (param.StorageType) { case Autodesk.Revit.DB.StorageType.Double: // append the type and value sb.AppendFormat("double\t{0}", param.AsDouble()); break; case Autodesk.Revit.DB.StorageType.ElementId: // for element ids, we will try and retrieve the element from the // document if it can be found we will display its name. sb.Append("Element\t"); // using ActiveDocument.GetElement(the element id) to // retrieve the element from the active document Autodesk.Revit.DB.ElementId elemId = new ElementId(param.AsElementId().IntegerValue); Element elem = app.ActiveUIDocument.Document.GetElement(elemId); // if there is an element then display its name, // otherwise display the fact that it is not set sb.Append(elem != null ? elem.Name : "Not set"); break; case Autodesk.Revit.DB.StorageType.Integer: // append the type and value sb.AppendFormat("int\t{0}", param.AsInteger()); break; case Autodesk.Revit.DB.StorageType.String: // append the type and value sb.AppendFormat("string\t{0}", param.AsString()); break; case Autodesk.Revit.DB.StorageType.None: // append the type and value sb.AppendFormat("none\t{0}", param.AsValueString()); break; default: break; } // add the completed line to the string list parameterItems.Add(sb.ToString()); } // Create our dialog, passing it the parameters array for display. PropertiesForm propertiesForm = new PropertiesForm(parameterItems.ToArray()); propertiesForm.StartPosition = FormStartPosition.CenterParent; propertiesForm.ShowDialog(); retRes = Autodesk.Revit.UI.Result.Succeeded; } else { message = "Please select only one element"; } return(retRes); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">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 Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Autodesk.Revit.UI.Result res = Autodesk.Revit.UI.Result.Succeeded; try { UIDocument activeDoc = commandData.Application.ActiveUIDocument; string str; Material materialElement = null; ElementSet selection = new ElementSet(); foreach (ElementId elementId in activeDoc.Selection.GetElementIds()) { selection.Insert(activeDoc.Document.GetElement(elementId)); } if (selection.Size != 1) { message = "Please select only one element."; res = Autodesk.Revit.UI.Result.Failed; return(res); } System.Collections.IEnumerator iter; ElementSet es = new ElementSet(); foreach (ElementId elementId in activeDoc.Selection.GetElementIds()) { es.Insert(activeDoc.Document.GetElement(elementId)); } iter = es.ForwardIterator(); iter.MoveNext(); // we need verify the selected element is a family instance FamilyInstance famIns = iter.Current as FamilyInstance; if (famIns == null) { TaskDialog.Show("Revit", "Not a type of FamilyInsance!"); res = Autodesk.Revit.UI.Result.Failed; return(res); } // we need select a column instance foreach (Parameter p in famIns.Parameters) { string parName = p.Definition.Name; // The "Beam Material" and "Column Material" family parameters have been replaced // by the built-in parameter "Structural Material". //if (parName == "Column Material" || parName == "Beam Material") if (parName == "Structural Material") { Autodesk.Revit.DB.ElementId elemId = p.AsElementId(); materialElement = activeDoc.Document.GetElement(elemId) as Material; break; } } if (materialElement == null) { TaskDialog.Show("Revit", "Not a column!"); res = Autodesk.Revit.UI.Result.Failed; return(res); } // the PHY_MATERIAL_PARAM_TYPE built in parameter contains a number // that represents the type of material Parameter materialType = materialElement.get_Parameter(BuiltInParameter.PHY_MATERIAL_PARAM_TYPE); str = "Material type: " + (materialType.AsInteger() == 0 ? "Generic" : (materialType.AsInteger() == 1 ? "Concrete" : "Steel")) + "\r\n"; // A material type of more than 0 : 0 = Generic, 1 = Concrete, 2 = Steel if (materialType.AsInteger() > 0) { // Common to all types // Young's Modulus double[] youngsModulus = new double[3]; youngsModulus[0] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_YOUNG_MOD1).AsDouble(); youngsModulus[1] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_YOUNG_MOD2).AsDouble(); youngsModulus[2] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_YOUNG_MOD3).AsDouble(); str = str + "Young's modulus: " + youngsModulus[0].ToString() + "," + youngsModulus[1].ToString() + "," + youngsModulus[2].ToString() + "\r\n"; // Poisson Modulus double[] PoissonRatio = new double[3]; PoissonRatio[0] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_POISSON_MOD1).AsDouble(); PoissonRatio[1] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_POISSON_MOD2).AsDouble(); PoissonRatio[2] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_POISSON_MOD3).AsDouble(); str = str + "Poisson modulus: " + PoissonRatio[0].ToString() + "," + PoissonRatio[1].ToString() + "," + PoissonRatio[2].ToString() + "\r\n"; // Shear Modulus double[] shearModulus = new double[3]; shearModulus[0] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_SHEAR_MOD1).AsDouble(); shearModulus[1] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_SHEAR_MOD2).AsDouble(); shearModulus[2] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_SHEAR_MOD3).AsDouble(); str = str + "Shear modulus: " + shearModulus[0].ToString() + "," + shearModulus[1].ToString() + "," + shearModulus[2].ToString() + "\r\n"; // Thermal Expansion Coefficient double[] thermalExpCoeff = new double[3]; thermalExpCoeff[0] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_EXP_COEFF1).AsDouble(); thermalExpCoeff[1] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_EXP_COEFF2).AsDouble(); thermalExpCoeff[2] = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_EXP_COEFF3).AsDouble(); str = str + "Thermal expansion coefficient: " + thermalExpCoeff[0].ToString() + "," + thermalExpCoeff[1].ToString() + "," + thermalExpCoeff[2].ToString() + "\r\n"; // Unit Weight double unitWeight; unitWeight = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_UNIT_WEIGHT).AsDouble(); str = str + "Unit weight: " + unitWeight.ToString() + "\r\n"; // Damping Ratio double dampingRatio; dampingRatio = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_DAMPING_RATIO).AsDouble(); str = str + "Damping ratio: " + dampingRatio.ToString() + "\r\n"; // Behavior 0 = Isotropic, 1 = Orthotropic int behaviour; behaviour = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_BEHAVIOR).AsInteger(); str = str + "Behavior: " + behaviour.ToString() + "\r\n"; // Concrete Only if (materialType.AsInteger() == 1) { // Concrete Compression double concreteCompression; concreteCompression = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_CONCRETE_COMPRESSION).AsDouble(); str = str + "Concrete compression: " + concreteCompression.ToString() + "\r\n"; // Lightweight double lightWeight; lightWeight = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_LIGHT_WEIGHT).AsDouble(); str = str + "Lightweight: " + lightWeight.ToString() + "\r\n"; // Shear Strength Reduction double shearStrengthReduction; shearStrengthReduction = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_SHEAR_STRENGTH_REDUCTION).AsDouble(); str = str + "Shear strength reduction: " + shearStrengthReduction.ToString() + "\r\n"; } // Steel only else if (materialType.AsInteger() == 2) { // Minimum Yield Stress double minimumYieldStress; minimumYieldStress = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_MINIMUM_YIELD_STRESS).AsDouble(); str = str + "Minimum yield stress: " + minimumYieldStress.ToString() + "\r\n"; // Minimum Tensile Strength double minimumTensileStrength; minimumTensileStrength = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_MINIMUM_TENSILE_STRENGTH).AsDouble(); str = str + "Minimum tensile strength: " + minimumTensileStrength.ToString() + "\r\n"; // Reduction Factor double reductionFactor; reductionFactor = materialElement.get_Parameter( BuiltInParameter.PHY_MATERIAL_PARAM_REDUCTION_FACTOR).AsDouble(); str = str + "Reduction factor: " + reductionFactor.ToString() + "\r\n"; } // end of if/else materialType.Integer == 1 } // end if materialType.Integer > 0 TaskDialog.Show("Physical materials", str); } catch (Exception ex) { TaskDialog.Show("PhysicalProp", ex.Message); res = Autodesk.Revit.UI.Result.Failed; } finally { } return(res); } // end command
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // set out default result to failure. Autodesk.Revit.UI.Result retRes = Autodesk.Revit.UI.Result.Succeeded; Autodesk.Revit.UI.UIApplication app = commandData.Application; // get the elements selected // The current selection can be retrieved from the active // document via the selection object ElementSet seletion = new ElementSet(); foreach (ElementId elementId in app.ActiveUIDocument.Selection.GetElementIds()) { seletion.Insert(app.ActiveUIDocument.Document.GetElement(elementId)); } List <string> parametergroup = new List <string>(); List <ParamTree> All_Elem = new List <ParamTree>(); int n = seletion.Size; //迭代器 ElementSetIterator it = seletion.ForwardIterator(); //遍历所有选中的元素; for (int i = 0; i < n; i++) { //每次迭代下一个元素; it.MoveNext(); Element element = it.Current as Element; ParameterSet parameters = element.Parameters;//获取单个元素的全部属性; ParameterSet parameters2 = GetParamSet(element); foreach (Parameter param in parameters) { if (param == null) { continue; //如果参数没有,继续下一个 } parametergroup = GetParameterGroup(parametergroup, param); } foreach (Parameter param in parameters2) { if (param == null) { continue; //如果参数没有,继续下一个 } parametergroup = GetParameterGroup(parametergroup, param); } ParamTree tree = MakeParamTree(element, parametergroup); All_Elem.Add(tree); } CreateJsonFile(CreateJson(All_Elem)); retRes = Autodesk.Revit.UI.Result.Succeeded; return(retRes); }