EaBIM
标题: Parameter of Revit API – 50: Parameter Converter – Parameter to SharedParamete [打印本页]
作者: 萧闫子 时间: 2014-1-9 11:43
标题: Parameter of Revit API – 50: Parameter Converter – Parameter to SharedParamete
We talked about programmatically converting parameters from one to another manually before. Now let’s see how Parameter Converter of RevitAddinWizard can assist us to hit the target with just a few mouse clicks. The Parameter Converter can be found from either Revit Addin Coder sub-menu under the Tools or from Revit Addin Coder toolbar.
When the menu item or the tool button is clicked, the Parameter Converter window will show up:
If the Parameter (to SharedParameter) Converter is selected from the check list box and a class is chosen from the combo box, a Parameter to SharedParameter Converter will be created and added into the class automatically. The entire converter code and necessary namespaces and class declarations have been appended below:- <div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "></div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">
- #region Namespaces</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using System;
- using System.Text;
- using System.Linq;
- using System.Xml;
- using System.Reflection;
- using System.ComponentModel;
- using System.Collections;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.Windows.Forms;
- using System.IO;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using Autodesk.Revit.ApplicationServices;
- using Autodesk.Revit.Attributes;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Events;
- using Autodesk.Revit.DB.Architecture;
- using Autodesk.Revit.DB.Structure;
- using Autodesk.Revit.DB.Mechanical;
- using Autodesk.Revit.DB.Electrical;
- using Autodesk.Revit.DB.Plumbing;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using Autodesk.Revit.UI;
- using Autodesk.Revit.UI.Selection;
- using Autodesk.Revit.UI.Events;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using Autodesk.Revit.Collections;
- using Autodesk.Revit.Exceptions;
- using Autodesk.Revit.Utility;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
- using RvtDocument = Autodesk.Revit.DB.Document;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">
- namespace RevitAddinCS1
- {
- class ParameterConverter
- {
- public class RawParameterInfo
- {
- public string Name { get; set; }
- public string ID { get; set; }
- public string Value { get; set; }
- public BuiltInParameterGroup Group { get; set; }
- public ParameterType Type { get; set; }
- public StorageType Storage { get; set; }
- public string Unit { get; set; }
- public bool Shared { get; set; }
- public bool ReadOnly { get; set; }
- }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> public static string RawGetDUTString(Parameter p)
- {
- string unitType = string.Empty;
- try { unitType = p.DisplayUnitType.ToString(); }
- catch { }
-
- return unitType;
- }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> public static List<RawParameterInfo> RawGetParametersInfo(Element e)
- {
- List<RawParameterInfo> paramList =
- (from Parameter p in e.Parameters
- select new RawParameterInfo
- {
- Name = p.Definition.Name,
- ID = p.IsShared ? p.GUID.ToString() : (p.Definition as InternalDefinition).BuiltInParameter.ToString(),
- Value = p.AsValueString(),
- Group = p.Definition.ParameterGroup,
- Type = p.Definition.ParameterType,
- Storage = p.StorageType,
- Unit = RawGetDUTString(p),
- Shared = p.IsShared,
- ReadOnly = p.IsReadOnly,
-
- }).ToList();
- return paramList;
- }
-
- public static string RawParametersInfoToCSVString<T>(List<T> infoList, ref string title)
- {
- StringBuilder sb = new StringBuilder();
- PropertyInfo[] propInfoArrary = typeof(T).GetProperties();
- foreach (PropertyInfo pi in propInfoArrary)
- {
- title += pi.Name + ",";
- }
- title = title.Remove(title.Length - 1);
-
- foreach (T info in infoList)
- {
- foreach (PropertyInfo pi in propInfoArrary)
- {
- object obj = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, info, null);
- IList list = obj as IList;
- if (list != null)
- {
- string str = string.Empty;
- foreach (object e in list)
- {
- str += e.ToString() + ";";
- }
- str = str.Remove(str.Length - 1);
-
- sb.Append(str + ",");
- }
- else
- {
- sb.Append((obj == null ? string.Empty : obj.ToString()) + ",");
- }
- }
- sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine);
- }
-
- return sb.ToString();
- }
-
-
- public static List<T> RawConvertSetToList<T>(IEnumerable set)
- {
- List<T> list = (from T p in set select p).ToList<T>();
- return list;
- }
-
-
- public static Definition RawCreateSharedParameter(RvtApplication app, string name, string group, ParameterType type, bool visible)
- {
- DefinitionFile defFile = app.OpenSharedParameterFile();
- if (defFile == null) throw new Exception("No SharedParameter File!");
-
- DefinitionGroup dg = RawConvertSetToList<DefinitionGroup>(defFile.Groups).FirstOrDefault(g => g.Name == group);
- if (dg == null) dg = defFile.Groups.Create(group);
-
- Definition def = RawConvertSetToList<Definition>(dg.Definitions).FirstOrDefault(d => d.Name == name);
- if (def != null) return def; //dg.Definitions.Erase(def); //ReadOnly Exception!!
- def = dg.Definitions.Create(name, type, visible);
-
- return def;
- }
-
- public static void ConvertParameterToSharedParameter(Element e, string name, bool convertSharedOnly, bool visibilityOverride)
- {
- List<RawParameterInfo> pList = RawGetParametersInfo(e);
- List<RawParameterInfo> pListOfInterest = pList.FindAll(p => p.Name == name);
- foreach (RawParameterInfo info in pListOfInterest)
- {
- if (info.Type == ParameterType.Invalid)
- {
- info.Type = ParameterType.Text; //It's weird that a valid Parameter can have an invalid ParameterType! We set it as the most popular Text type in this case. Otherwise, the DefinitionGroup.Definitions.Create() would fail!
- }
-
- if (convertSharedOnly)
- {
- if (info.Shared)
- {
- RawCreateSharedParameter(e.Document.Application, name, "For_" + e.Category.Name, info.Type, visibilityOverride);
- }
- }
- else
- {
- RawCreateSharedParameter(e.Document.Application, name, "For_" + e.Category.Name, info.Type, visibilityOverride);
- }
- }
- }
-
- public static void ConvertAllParametersToSharedParameters(Element e, bool convertSharedOnly, bool visibilityOverride)
- {
- List<RawParameterInfo> pList = RawGetParametersInfo(e);
-
- foreach (RawParameterInfo info in pList)
- {
- if (info.Type == ParameterType.Invalid)
- {
- info.Type = ParameterType.Text;
- }
-
- if (convertSharedOnly)
- {
- if (info.Shared)
- {
- RawCreateSharedParameter(e.Document.Application, info.Name, "For_" + e.Category.Name, info.Type, visibilityOverride);
- }
- }
- else
- {
- RawCreateSharedParameter(e.Document.Application, info.Name, "For_" + e.Category.Name, info.Type, visibilityOverride);
- }
- }
- }
- }
- }</div>
复制代码We would not create test command to extercise these parameter converter code here. In case needed, please refer to earlier posts for ideas and code examples.
As mentioned, Parameter Converter of RevitAddinCodercan create all these code automatically in a second.
作者: 黑超BB 时间: 2014-2-20 15:11
路过!!!
帮顶……
作者: 熊猫 时间: 2014-2-25 10:28
(*^__^*) 嘻嘻……
作者: ben7 时间: 2014-2-25 10:33
路过!!!
不发表意见……
作者: 波罗友 时间: 2014-3-4 13:59
顶!!!!!!!!!!
作者: 爬爬``PA 时间: 2014-3-10 10:47
顶起来…………
作者: 宇航员 时间: 2014-3-10 16:20
顶!!!!!!!!!!!!
作者: 猫猫girl 时间: 2014-3-10 16:23
顶!!!!!!!!!!!!
作者: OK佬 时间: 2014-3-10 16:32
谢谢老师…
作者: 悠悠筱荷 时间: 2014-3-10 16:33
谢谢老师…
作者: 教父 时间: 2014-3-10 16:48
顶!!!!!!!!!!
作者: 鹦鹉仔 时间: 2014-3-10 16:49
顶起来…………
作者: 筱雅 时间: 2014-3-10 16:55
顶!!!!!!!!!!!!
作者: 猫猫girl 时间: 2014-3-10 17:15
谢谢老师…
作者: 雁田佬 时间: 2014-3-10 17:20
谢谢BIM大神…
作者: 看看侃侃 时间: 2014-3-11 10:29
非常感谢!!
作者: zj1345 时间: 2014-3-11 10:31
顶!!!!!!!!!!!!
作者: 順順 时间: 2014-3-11 10:34
谢谢老师…
作者: 毫半字 时间: 2014-3-11 10:37
非常感谢!!
作者: 严英华 时间: 2014-3-13 11:59
顶起来…………
作者: 风吹枫落 时间: 2014-3-13 12:04
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: 慕容柔晴 时间: 2014-3-13 12:12
顶起来…………
作者: 静儿 时间: 2014-3-13 12:14
顶......
楼下跟上.....
作者: 泰安oim 时间: 2014-3-13 12:27
(*^__^*) 嘻嘻……
作者: 爬爬``PA 时间: 2014-3-13 12:29
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: dgpeihua 时间: 2014-3-14 10:57
路过!!!
帮顶……
作者: levin 时间: 2014-3-14 11:02
路过!!!
帮顶……
作者: 宇航员 时间: 2014-3-18 11:00
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: zpklfruV 时间: 2014-3-18 11:05
顶......
楼下跟上.....
作者: 冰雨 时间: 2014-3-20 10:52
顶......
楼下跟上.....
作者: 极HONDA速 时间: 2014-3-20 10:57
(*^__^*) 嘻嘻……
作者: zpklfruV 时间: 2014-3-26 15:50
(*^__^*) 嘻嘻……
作者: 欧宝 时间: 2014-3-28 09:46
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: 风浪子 时间: 2014-3-28 09:50
路过!!!
帮顶……
作者: 拉登 时间: 2014-3-28 09:52
顶!!!!!!!!!!
作者: 茶神idg 时间: 2014-3-28 09:57
路过!!!
不发表意见……
作者: dglei88 时间: 2014-3-31 16:34
顶起来…………
作者: 等你回来 时间: 2014-3-31 16:37
顶......
楼下跟上.....
作者: 野风 时间: 2014-4-2 16:03
顶!!!!!!!!!!
作者: 慕容柔晴 时间: 2014-4-8 10:28
(*^__^*) 嘻嘻……
作者: 飞天舞 时间: 2014-4-8 10:34
路过!!!
帮顶……
作者: 老朽 时间: 2014-4-8 10:35
(*^__^*) 嘻嘻……
作者: 车晶晶 时间: 2014-4-10 15:48
路过!!!
帮顶……
作者: 老朽 时间: 2014-4-10 15:50
路过!!!
不发表意见……
作者: lw7511 时间: 2014-4-10 15:55
顶......
楼下跟上.....
作者: We晕晕 时间: 2014-4-23 10:42
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: 慕容柔晴 时间: 2014-5-14 14:04
路过!!!
不发表意见……
作者: 看看侃侃 时间: 2014-5-14 14:06
顶!!!!!!!!!!!!!!!!!!!!!!!!!
作者: audigy 时间: 2014-5-14 14:07
顶......
楼下跟上.....
作者: 鹦鹉仔 时间: 2014-5-14 14:14
路过!!!
帮顶……
作者: 卡巴kala 时间: 2014-6-12 09:38
路过!!!
不发表意见……
作者: ben7 时间: 2014-6-12 09:41
(*^__^*) 嘻嘻……
作者: 江枫 时间: 2014-6-12 09:42
路过!!!
不发表意见……
作者: 苦田辛君 时间: 2014-6-17 14:59
路过!!!
帮顶……
欢迎光临 EaBIM (https://eabim.net/) |
Powered by Discuz! X3.2 |