/// <summary> /// Load a family from xml /// </summary> /// <param name="familyXElement"></param> /// <param name="findFolderUtility"></param> private void ParseFamilyFromXml(XElement familyXElement, FindFolderUtility findFolderUtility) { XAttribute xafilename = familyXElement.Attribute(XName.Get("filename")); string familyPath = xafilename.Value; if (!System.IO.File.Exists(familyPath)) { string filename = System.IO.Path.GetFileName(familyPath); familyPath = findFolderUtility.FindFileFolder(filename); if (!System.IO.File.Exists(familyPath)) { throw new RoutingPreferenceDataException("Cannot find family file: " + xafilename.Value); } } if (string.Compare(System.IO.Path.GetExtension(familyPath), ".rfa", true) != 0) { throw new RoutingPreferenceDataException(familyPath + " is not a family file."); } try { if (!m_document.LoadFamily(familyPath)) { return; //returns false if already loaded. } } catch (System.Exception ex) { throw new RoutingPreferenceDataException("Cannot load family: " + xafilename.Value + ": " + ex.ToString()); } }
/// <summary> /// Create an instance of the helper class /// </summary> public FindFolderUtility(Autodesk.Revit.ApplicationServices.Application application) { m_application = application; m_basePath = GetFamilyBasePath(); List <string> extraFamilyPaths = FindFolderUtility.GetAdditionalFamilyPaths(); m_Familyfiles = new Dictionary <string, string>(); GetAllFiles(m_basePath, m_Familyfiles); //Get additional .rfa files in user-defined paths specified in familypaths.xml foreach (string extraPath in extraFamilyPaths) { GetAllFiles(extraPath, m_Familyfiles); } }
/// <summary> /// Reads pipe fitting family, segment, size, schedule, and routing preference data from a document and summarizes it in Xml. /// </summary> /// <returns>An XDocument containing an Xml summary of routing preference information</returns> public XDocument CreateXmlFromAllPipingPolicies(ref bool pathsNotFound) { //To export the full path name of all .rfa family files, use the FindFolderUtility class. FindFolderUtility findFolderUtility = new FindFolderUtility(m_document.Application); XDocument routingPreferenceBuilderDoc = new XDocument(); XElement xroot = new XElement(XName.Get("RoutingPreferenceBuilder")); FormatOptions formatOptionPipeSize = m_document.GetUnits().GetFormatOptions(UnitType.UT_PipeSize); string unitStringPipeSize = formatOptionPipeSize.DisplayUnits.ToString(); xroot.Add(new XAttribute(XName.Get("pipeSizeUnits"), unitStringPipeSize)); FormatOptions formatOptionRoughness = m_document.GetUnits().GetFormatOptions(UnitType.UT_Piping_Roughness); string unitStringRoughness = formatOptionRoughness.DisplayUnits.ToString(); xroot.Add(new XAttribute(XName.Get("pipeRoughnessUnits"), unitStringRoughness)); foreach (FamilySymbol familySymbol in this.m_fittings) { xroot.Add(CreateXmlFromFamily(familySymbol, findFolderUtility, ref pathsNotFound)); } foreach (PipeType pipeType in m_pipeTypes) { xroot.Add(CreateXmlFromPipeType(pipeType)); } foreach (PipeScheduleType pipeScheduleType in m_pipeSchedules) { xroot.Add(CreateXmlFromPipeScheduleType(pipeScheduleType)); } foreach (PipeSegment pipeSegment in m_segments) { xroot.Add(CreateXmlFromPipeSegment(pipeSegment)); } foreach (PipeType pipeType in m_pipeTypes) { xroot.Add(CreateXmlFromRoutingPreferenceManager(pipeType.RoutingPreferenceManager)); } routingPreferenceBuilderDoc.Add(xroot); return(routingPreferenceBuilderDoc); }
/// <summary> /// Create xml from a family /// </summary> /// <param name="pipeFitting"></param> /// <param name="findFolderUtility"></param> /// <param name="pathNotFound"></param> /// <returns></returns> private static XElement CreateXmlFromFamily(FamilySymbol pipeFitting, FindFolderUtility findFolderUtility, ref bool pathNotFound) { //Try to find the path of the .rfa file. string path = findFolderUtility.FindFileFolder(pipeFitting.Family.Name + ".rfa"); string pathToWrite; if (path == "") { pathNotFound = true; pathToWrite = pipeFitting.Family.Name + ".rfa"; } else { pathToWrite = path; } XElement xFamilySymbol = new XElement(XName.Get("Family")); xFamilySymbol.Add(new XAttribute(XName.Get("filename"), pathToWrite)); return(xFamilySymbol); }
/// <summary> /// Reads data from an Xml source and loads pipe fitting families, creates segments, sizes, schedules, and routing preference rules from the xml data. /// </summary> /// <param name="xDoc">The Xml data source to read from</param> public void ParseAllPipingPoliciesFromXml(XDocument xDoc) { if (m_pipeTypes.Count() == 0) { throw new RoutingPreferenceDataException("No pipe pipes defined in this project. At least one must be defined."); } FormatOptions formatOptionPipeSize = m_document.GetUnits().GetFormatOptions(UnitType.UT_PipeSize); string docPipeSizeUnit = formatOptionPipeSize.DisplayUnits.ToString(); string xmlPipeSizeUnit = xDoc.Root.Attribute("pipeSizeUnits").Value; if (docPipeSizeUnit != xmlPipeSizeUnit) { throw new RoutingPreferenceDataException("Units from XML do not match current pipe size units."); } FormatOptions formatOptionRoughness = m_document.GetUnits().GetFormatOptions(UnitType.UT_Piping_Roughness); string docRoughnessUnit = formatOptionRoughness.DisplayUnits.ToString(); string xmlRoughnessUnit = xDoc.Root.Attribute("pipeRoughnessUnits").Value; if (docRoughnessUnit != xmlRoughnessUnit) { throw new RoutingPreferenceDataException("Units from XML do not match current pipe roughness units."); } Transaction loadFamilies = new Transaction(m_document, "Load Families"); loadFamilies.Start(); IEnumerable <XElement> families = xDoc.Root.Elements("Family"); FindFolderUtility findFolderUtility = new FindFolderUtility(m_document.Application); foreach (XElement xfamily in families) { try { ParseFamilyFromXml(xfamily, findFolderUtility); //Load families. } catch (Exception ex) { loadFamilies.RollBack(); throw ex; } } loadFamilies.Commit(); Transaction addPipeTypes = new Transaction(m_document, "Add PipeTypes"); addPipeTypes.Start(); IEnumerable <XElement> pipeTypes = xDoc.Root.Elements("PipeType"); foreach (XElement xpipeType in pipeTypes) { try { ParsePipeTypeFromXml(xpipeType); //Define new pipe types. } catch (Exception ex) { addPipeTypes.RollBack(); throw ex; } } addPipeTypes.Commit(); Transaction addPipeSchedules = new Transaction(m_document, "Add Pipe Schedule Types"); addPipeSchedules.Start(); IEnumerable <XElement> pipeScheduleTypes = xDoc.Root.Elements("PipeScheduleType"); foreach (XElement xpipeScheduleType in pipeScheduleTypes) { try { ParsePipeScheduleTypeFromXml(xpipeScheduleType); //Define new pipe schedule types. } catch (Exception ex) { addPipeSchedules.RollBack(); throw ex; } } addPipeSchedules.Commit(); //The code above have added some new pipe types, schedules, or fittings, so update the lists of all of these. UpdatePipeTypesList(); UpdatePipeTypeSchedulesList(); UpdateFittingsList(); Transaction addPipeSegments = new Transaction(m_document, "Add Pipe Segments"); addPipeSchedules.Start(); IEnumerable <XElement> pipeSegments = xDoc.Root.Elements("PipeSegment"); //Define new segments. foreach (XElement xpipeSegment in pipeSegments) { try { ParsePipeSegmentFromXML(xpipeSegment); } catch (Exception ex) { addPipeSchedules.RollBack(); throw ex; } } addPipeSchedules.Commit(); UpdateSegmentsList(); //More segments may have been created, so update the segment list. //Now that all of the various types that routing preferences use have been created or loaded, add all the routing preferences. Transaction addRoutingPreferences = new Transaction(m_document, "Add Routing Preferences"); addRoutingPreferences.Start(); IEnumerable <XElement> routingPreferenceManagers = xDoc.Root.Elements("RoutingPreferenceManager"); foreach (XElement xroutingPreferenceManager in routingPreferenceManagers) { try { ParseRoutingPreferenceManagerFromXML(xroutingPreferenceManager); } catch (Exception ex) { addRoutingPreferences.RollBack(); throw ex; } } addRoutingPreferences.Commit(); }