/// <summary> /// add Para's instances to m_parasList, finally return it /// </summary> /// <returns></returns> public SortableBindingList <Para> CreateParas() { try { ParameterSetIterator iterator = m_element.Parameters.ForwardIterator(); Parameter parameter; iterator.Reset(); //use Iterator to loop, new a Para for each parameter and //add it to m_parameter; if failed return null for (; iterator.MoveNext();) { parameter = iterator.Current as Parameter; if (null != parameter) { m_parasList.Add(new Para(parameter)); } } } catch (Exception e) { string errorText = "Create Paras failed: " + e.Message; TaskDialog.Show("Revit", errorText); return(null); } return(m_parasList); }
/// <summary> /// Method to retrieve parameters from element according to storage type /// </summary> /// <param name="doc"></param> /// <param name="category"></param> /// <returns></returns> public static Parameter[] GetParametersOfCategoryByStorageType(Document doc, BuiltInCategory category) { List <Parameter> parameters = new List <Parameter>(); Element element = new FilteredElementCollector(doc).OfCategory(category).WhereElementIsNotElementType().FirstElement(); if (element != null) { ParameterSet parameterSet = element.Parameters; ParameterSetIterator paramIt = parameterSet.ForwardIterator(); paramIt.Reset(); while (paramIt.MoveNext()) { Parameter param = paramIt.Current as Parameter; if (param.StorageType == StorageType.String && param.IsReadOnly == false) { parameters.Add(param); } } return(parameters.ToArray()); } else { return(null); } }
/// <summary> /// Gets the parameter by name from an element. /// </summary> /// <param name="element"> /// The element. /// </param> /// <param name="propertyName"> /// The property name. /// </param> /// <returns> /// The Parameter. /// </returns> static Parameter getParameterFromName(Element element, string propertyName) { ParameterSet parameterIds = element.Parameters; if (parameterIds.Size == 0) { return(null); } IList <Parameter> parameters = new List <Parameter>(); IList <Definition> paramDefinitions = new List <Definition>(); // We will do two passes. In the first pass, we will look at parameters in the IFC group. // In the second pass, we will look at all other groups. ParameterSetIterator parameterIt = parameterIds.ForwardIterator(); while (parameterIt.MoveNext()) { Parameter parameter = parameterIt.Current as Parameter; Definition paramDefinition = parameter.Definition; if (paramDefinition == null) { continue; } if (paramDefinition.ParameterGroup != BuiltInParameterGroup.PG_IFC) { parameters.Add(parameter); paramDefinitions.Add(paramDefinition); continue; } if (NamingUtil.IsEqualIgnoringCaseAndSpaces(paramDefinition.Name, propertyName)) { return(parameter); } } int size = paramDefinitions.Count; for (int ii = 0; ii < size; ii++) { if (NamingUtil.IsEqualIgnoringCaseAndSpaces(paramDefinitions[ii].Name, propertyName)) { return(parameters[ii]); } } return(null); }
public static string get_ParameterByString(Element e, string lookup) { string aString = ""; bool found = false; ParameterSetIterator i = e.Parameters.ForwardIterator(); i.Reset(); bool iMoreAttribute = i.MoveNext(); while (iMoreAttribute) { object o = i.Current; Parameter familyAttribute = o as Parameter; //find the parameter whose name is same to the given parameter name Autodesk.Revit.DB.StorageType st = familyAttribute.StorageType; switch (st) { //get the storage type case StorageType.String: if (familyAttribute.Definition.Name == lookup) { //make conversion between degrees and radians aString = familyAttribute.AsString(); found = true; } break; } if (found) { break; } else { iMoreAttribute = i.MoveNext(); } } return(aString); }
/// <summary> /// Method to retrieve instance parameters of an instance /// </summary> /// <param name="element"></param> /// <returns></returns> public static Parameter[] GetParametersOfInstance(Element element) { List <Parameter> parameters = new List <Parameter>(); ParameterSet parameterSet = element.Parameters; ParameterSetIterator paramIt = parameterSet.ForwardIterator(); paramIt.Reset(); while (paramIt.MoveNext()) { Parameter param = paramIt.Current as Parameter; if (param.StorageType == StorageType.String && param.IsReadOnly == false) { parameters.Add(param); } } return(parameters.ToArray()); }
/// <summary> /// Cache the parameters for an element, allowing quick access later. /// </summary> /// <param name="id">The element id.</param> static private void CacheParametersForElement(ElementId id) { if (id == ElementId.InvalidElementId) { return; } if (m_NonIFCParameters.ContainsKey(id)) { return; } IDictionary <BuiltInParameterGroup, ParameterElementCache> nonIFCParameters = new SortedDictionary <BuiltInParameterGroup, ParameterElementCache>(); ParameterElementCache ifcParameters = new ParameterElementCache(); m_NonIFCParameters[id] = nonIFCParameters; m_IFCParameters[id] = ifcParameters; Element element = ExporterCacheManager.Document.GetElement(id); if (element == null) { return; } ParameterSet parameterIds = element.Parameters; if (parameterIds.Size == 0) { return; } // We will do two passes. In the first pass, we will look at parameters in the IFC group. // In the second pass, we will look at all other groups. ParameterSetIterator parameterIt = parameterIds.ForwardIterator(); while (parameterIt.MoveNext()) { Parameter parameter = parameterIt.Current as Parameter; if (parameter == null) { continue; } if (IsDuplicateParameter(parameter)) { continue; } Definition paramDefinition = parameter.Definition; if (paramDefinition == null) { continue; } // Don't cache parameters that aren't visible to the user. InternalDefinition internalDefinition = paramDefinition as InternalDefinition; if (internalDefinition != null && internalDefinition.Visible == false) { continue; } if (string.IsNullOrWhiteSpace(paramDefinition.Name)) { continue; } string cleanPropertyName = NamingUtil.RemoveSpaces(paramDefinition.Name); BuiltInParameterGroup groupId = paramDefinition.ParameterGroup; if (groupId != BuiltInParameterGroup.PG_IFC) { ParameterElementCache cacheForGroup = null; if (!nonIFCParameters.TryGetValue(groupId, out cacheForGroup)) { cacheForGroup = new ParameterElementCache(); nonIFCParameters[groupId] = cacheForGroup; } cacheForGroup.ParameterCache[cleanPropertyName] = parameter; } else { ifcParameters.ParameterCache[cleanPropertyName] = parameter; } } }
/// <summary> /// get the parameter value according given parameter name /// </summary> public string FindParameter(string parameterName, FamilyInstance familyInstanceName) { ParameterSetIterator i = familyInstanceName.Parameters.ForwardIterator(); i.Reset(); string valueOfParameter = null; bool iMoreAttribute = i.MoveNext(); while (iMoreAttribute) { bool isFound = false; object o = i.Current; Parameter familyAttribute = o as Parameter; if (familyAttribute.Definition.Name == parameterName) { //find the parameter whose name is same to the given parameter name Autodesk.Revit.DB.StorageType st = familyAttribute.StorageType; switch (st) { //get the storage type case StorageType.Double: if (parameterName.Equals(AngleDefinitionName)) { //make conversion between degrees and radians Double temp = familyAttribute.AsDouble(); valueOfParameter = Math.Round(temp * 180 / (Math.PI), 3).ToString(); } else { valueOfParameter = familyAttribute.AsDouble().ToString(); } break; case StorageType.ElementId: //get Autodesk.Revit.DB.ElementId as string valueOfParameter = familyAttribute.AsElementId().ToString(); break; case StorageType.Integer: //get Integer as string valueOfParameter = familyAttribute.AsInteger().ToString(); break; case StorageType.String: //get string valueOfParameter = familyAttribute.AsString(); break; case StorageType.None: valueOfParameter = familyAttribute.AsValueString(); break; default: break; } isFound = true; } if (isFound) { break; } iMoreAttribute = i.MoveNext(); } //return the value. return(valueOfParameter); }
/// <summary> /// The function set value to rotation of the beams and braces /// and rotate columns. /// </summary> public void RotateElement() { Transaction transaction = new Transaction(m_revit.ActiveUIDocument.Document, "RotateElement"); transaction.Start(); try { ElementSet selection = new ElementSet(); foreach (ElementId elementId in m_revit.ActiveUIDocument.Selection.GetElementIds()) { selection.Insert(m_revit.ActiveUIDocument.Document.GetElement(elementId)); } foreach (Autodesk.Revit.DB.Element e in selection) { FamilyInstance familyComponent = e as FamilyInstance; if (familyComponent == null) { //is not a familyInstance continue; } // if be familyInstance,judge the types of familyInstance if (StructuralType.Beam == familyComponent.StructuralType || StructuralType.Brace == familyComponent.StructuralType) { // selection is a beam or Brace ParameterSetIterator paraIterator = familyComponent.Parameters.ForwardIterator(); paraIterator.Reset(); while (paraIterator.MoveNext()) { object para = paraIterator.Current; Parameter objectAttribute = para as Parameter; //set generic property named "Cross-Section Rotation" if (objectAttribute.Definition.Name.Equals(AngleDefinitionName)) { Double originDegree = objectAttribute.AsDouble(); double rotateDegree = m_receiveRotationTextBox * Math.PI / 180; if (!m_isAbsoluteChecked) { // absolute rotation rotateDegree += originDegree; } objectAttribute.Set(rotateDegree); // relative rotation } } } else if (StructuralType.Column == familyComponent.StructuralType) { // rotate a column Autodesk.Revit.DB.Location columnLocation = familyComponent.Location; // get the location object LocationPoint pointLocation = columnLocation as LocationPoint; Autodesk.Revit.DB.XYZ insertPoint = pointLocation.Point; // get the location point double temp = pointLocation.Rotation; //existing rotation Autodesk.Revit.DB.XYZ directionPoint = new Autodesk.Revit.DB.XYZ(0, 0, 1); // define the vector of axis Line rotateAxis = Line.CreateUnbound(insertPoint, directionPoint); double rotateDegree = m_receiveRotationTextBox * Math.PI / 180; // rotate column by rotate method if (m_isAbsoluteChecked) { rotateDegree -= temp; } bool rotateResult = pointLocation.Rotate(rotateAxis, rotateDegree); if (rotateResult == false) { TaskDialog.Show("Revit", "Rotate Failed."); } } } transaction.Commit(); } catch (Exception ex) { TaskDialog.Show("Revit", "Rotate failed! " + ex.Message); transaction.RollBack(); } }
/// <summary> /// Cache the parameters for an element, allowing quick access later. /// </summary> /// <param name="id">The element id.</param> static private void CacheParametersForElement(ElementId id) { if (id == ElementId.InvalidElementId) { return; } if (m_NonIFCParameters.ContainsKey(id)) { return; } IDictionary <BuiltInParameterGroup, ParameterElementCache> nonIFCParameters = new SortedDictionary <BuiltInParameterGroup, ParameterElementCache>(); ParameterElementCache ifcParameters = new ParameterElementCache(); m_NonIFCParameters[id] = nonIFCParameters; m_IFCParameters[id] = ifcParameters; Element element = ExporterCacheManager.Document.GetElement(id); if (element == null) { return; } ParameterSet parameterIds = element.Parameters; if (parameterIds.Size == 0) { return; } // We will do two passes. In the first pass, we will look at parameters in the IFC group. // In the second pass, we will look at all other groups. ParameterSetIterator parameterIt = parameterIds.ForwardIterator(); while (parameterIt.MoveNext()) { Parameter parameter = parameterIt.Current as Parameter; if (parameter == null) { continue; } if (IsDuplicateParameter(parameter)) { continue; } Definition paramDefinition = parameter.Definition; if (paramDefinition == null) { continue; } // Don't cache parameters that aren't visible to the user. InternalDefinition internalDefinition = paramDefinition as InternalDefinition; if (internalDefinition != null && internalDefinition.Visible == false) { continue; } if (string.IsNullOrWhiteSpace(paramDefinition.Name)) { continue; } string cleanPropertyName = NamingUtil.RemoveSpaces(paramDefinition.Name); BuiltInParameterGroup groupId = paramDefinition.ParameterGroup; ParameterElementCache cacheForGroup = null; if (groupId != BuiltInParameterGroup.PG_IFC) { if (!nonIFCParameters.TryGetValue(groupId, out cacheForGroup)) { cacheForGroup = new ParameterElementCache(); nonIFCParameters[groupId] = cacheForGroup; } } else { cacheForGroup = ifcParameters; } if (cacheForGroup != null) { // We may have situations (due to bugs) where a parameter with the same name appears multiple times. // In this case, we will preserve the first parameter with a value. // Note that this can still cause inconsistent behavior in the case where multiple parameters with the same // name have values, and we should warn about that when we start logging. if (!cacheForGroup.ParameterCache.ContainsKey(cleanPropertyName) || !cacheForGroup.ParameterCache[cleanPropertyName].HasValue) { cacheForGroup.ParameterCache[cleanPropertyName] = parameter; } } } }
/// <summary> /// The function set value to rotation of the beams and braces /// and rotate columns. /// </summary> public void RotateElement() { ElementSet selection = new ElementSet(); foreach (ElementId elementid in m_doc.Selection.GetElementIds()) { selection.Insert(m_doc.Document.GetElement(elementid)); } foreach (Autodesk.Revit.DB.Element e in selection) { FamilyInstance familyComponent = e as FamilyInstance; if (familyComponent == null) { //is not a familyInstance continue; } // if be familyInstance,judge the types of familyInstance if (Autodesk.Revit.DB.Structure.StructuralType.Beam == familyComponent.StructuralType || Autodesk.Revit.DB.Structure.StructuralType.Brace == familyComponent.StructuralType) { // selection is a beam or Brace ParameterSetIterator j = familyComponent.Parameters.ForwardIterator(); j.Reset(); bool jMoreAttribute = j.MoveNext(); while (jMoreAttribute) { object a = j.Current; Parameter objectAttribute = a as Parameter; //set generic property named ¡°Angle¡± int p = objectAttribute.Definition.Name.CompareTo("Angle"); if (0 == p) { Double temp = objectAttribute.AsDouble(); double rotateDegree = m_receiveRotationTextBox * Math.PI / 180; if (!m_isAbsoluteChecked) { // absolute rotation rotateDegree += temp; } objectAttribute.Set(rotateDegree); // relative rotation } jMoreAttribute = j.MoveNext(); } } else if (Autodesk.Revit.DB.Structure.StructuralType.Column == familyComponent.StructuralType) { // rotate a column Autodesk.Revit.DB.Location columnLocation = familyComponent.Location; // get the location object Autodesk.Revit.DB.LocationPoint pointLocation = columnLocation as Autodesk.Revit.DB.LocationPoint; Autodesk.Revit.DB.XYZ insertPoint = pointLocation.Point; // get the location point double temp = pointLocation.Rotation; //existing rotation XYZ directionPoint = m_doc.Document.Application.Create.NewXYZ(0, 0, 1); // define the vector of axis Autodesk.Revit.DB.Line rotateAxis = Line.CreateBound(insertPoint, directionPoint); double rotateDegree = m_receiveRotationTextBox * Math.PI / 180; // rotate column by rotate method if (m_isAbsoluteChecked) { rotateDegree -= temp; } bool rotateResult = pointLocation.Rotate(rotateAxis, rotateDegree); if (rotateResult == false) { MessageBox.Show("Rotate Failed."); } } } }
/// <summary> /// /// </summary> /// <param name="elem"></param> void ParamFactory(Element elem, DataRow row) { foreach (KeyValuePair <string, string> usrParam in EAFileData.MapDict) { if (usrParam.Key == "Count" || usrParam.Key == "Name" || usrParam.Key == "") { continue; } bool match = false; ParameterSetIterator parSetItor = elem.Parameters.ForwardIterator(); while (parSetItor.MoveNext()) { Parameter elemParam = parSetItor.Current as Parameter; if (usrParam.Key == elemParam.Definition.Name) // if it already exists we need to assign it { match = true; // need to test storage type NOTE: may not need to do this... switch (elemParam.StorageType) { case StorageType.ElementId: // don't ever want to assign elementId break; case StorageType.Double: elemParam.SetValueString(row[usrParam.Value].ToString()); break; case StorageType.String: elemParam.Set(row[usrParam.Value].ToString()); break; case StorageType.Integer: if (elemParam.Definition.ParameterType != ParameterType.YesNo) { elemParam.SetValueString(row[usrParam.Value].ToString()); } break; default: break; } } } if (!match) // if it does NOT exist we need to create it { Category roomCat = EADocumentData.Doc.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms); CategorySet catSet = new CategorySet(); catSet.Insert(roomCat); EASharedParamData.CreateProjParamFromExistSharedParam(usrParam.Key, catSet, BuiltInParameterGroup.PG_IDENTITY_DATA, true ); // now that we've made it, we need to set it ParameterSetIterator parSetRevItor = elem.Parameters.ReverseIterator(); while (parSetRevItor.MoveNext()) { Parameter elemParam = parSetRevItor.Current as Parameter; if (usrParam.Key == elemParam.Definition.Name) // if it already exists we need to assign it { switch (elemParam.StorageType) { case StorageType.ElementId: // don't ever want to assign elementId break; case StorageType.Double: elemParam.SetValueString(row[usrParam.Value].ToString()); break; case StorageType.String: elemParam.Set(row[usrParam.Value].ToString()); break; case StorageType.Integer: if (elemParam.Definition.ParameterType != ParameterType.YesNo) { elemParam.SetValueString(row[usrParam.Value].ToString()); } break; default: break; } break; } } } } }