这个例子主要介绍的 RoutingPreferenceTools 布管系统工具。
有三个命令:
把布管系统配置从文件中读取出来,或者写入到文件中,使用的格式都是 XML。具体由 RoutingPreferenceBuilder
来完成。
写入到文件中,builder.CreateXmlFromAllPipingPolicies
:
RoutingPreferenceBuilder builder = new RoutingPreferenceBuilder(commandData.Application.ActiveUIDocument.Document);
bool pathsNotFound = false;
XDocument routingPreferenceBuilderDoc = builder.CreateXmlFromAllPipingPolicies(ref pathsNotFound);
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create(sfd.FileName, xmlWriterSettings);
routingPreferenceBuilderDoc.WriteTo(writer);
writer.Flush();
writer.Close();
CreateXmlFromAllPipingPolicies
这个方法,把当前文件中所有相关的布管系统配置转化成 XML 格式,用到的方法:
CreateXmlFromFamily
、CreateXmlFromPipeType
、CreateXmlFromPipeScheduleType
、CreateXmlFromPipeSegment
、CreateXmlFromRoutingPreferenceManager
、createXmlFromRoutingPreferenceRule
。
从XML文件中读取,这是写入的一个逆过程,builder.ParseAllPipingPoliciesFromXml
:
StreamReader reader = new StreamReader(ofd.FileName);
XDocument routingPreferenceBuilderDoc = XDocument.Load(new XmlTextReader(reader));
reader.Close();
RoutingPreferenceBuilder builder = new RoutingPreferenceBuilder(commandData.Application.ActiveUIDocument.Document);
builder.ParseAllPipingPoliciesFromXml(routingPreferenceBuilderDoc);
ParseAllPipingPoliciesFromXml
这个方法,和 CreateXmlFromAllPipingPolicies
对应,用到的方法:
ParseFamilyFromXml
、ParsePipeTypeFromXml
、ParsePipeScheduleTypeFromXml
、ParsePipeSegmentFromXML
、ParseRoutingPreferenceManagerFromXML
、ParseMEPSizeFromXml
、ParseRoutingPreferenceRuleFromXML
。
读取和写入的不同在于,读取的时候会涉及到一部分创建的工作。
例如,
创建 RoutingPreferenceRule
:
// 对例子做了简化
RoutingPreferenceRule rule = new RoutingPreferenceRule(partId, description);
PrimarySizeCriterion sizeCriterion;
double min, max;
min = Convert.ConvertValueToFeet(min, m_document);
max = Convert.ConvertValueToFeet(max, m_document);
sizeCriterion = new PrimarySizeCriterion(min, max);
rule.AddCriterion(sizeCriterion);
设置RoutingPreferenceManager
:
ElementId pipeTypeId = GetPipeTypeByName(xaPipeTypeName.Value);
PipeType pipeType = m_document.GetElement(pipeTypeId) as PipeType;
RoutingPreferenceManager routingPreferenceManager = pipeType.RoutingPreferenceManager;
routingPreferenceManager.PreferredJunctionType = preferredJunctionType;
foreach (XNode xRule in routingPreferenceManagerXElement.Nodes())
{
if (xRule is XElement)
{
RoutingPreferenceRuleGroupType groupType;
RoutingPreferenceRule rule = ParseRoutingPreferenceRuleFromXML(xRule as XElement, out groupType);
routingPreferenceManager.AddRule(groupType, rule);
}
}
SDK 自带的例子是2012年,在Revit 2021 里打开会导致死机,可能也是这台计算机的个别现象。
这个命令的主逻辑不复杂,是读取和做判断,涉及到的内容和上面两个命令相似。详见:Revit 2021 SDK\Samples\RoutingPreferenceTools\CS\RoutingPreferenceAnalysis\Analyzer.cs
关键的类:
RoutingPreferenceManager
、RoutingConditions
、RoutingPreferenceRule
。