//public QLFabricationPartResolve(FabricationPart _fabricationPart, Field queryFieldForParameters) //{ // id = _fabricationPart.Id.ToString(); // name = _fabricationPart.Name; // if (queryFieldForParameters != null) // { // var returnElementsObject = new ConcurrentBag<QLParameter>(); // var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForParameters, "nameFilter"); // var _doc = ResolverEntry.Doc; // ParameterSet objectList = _fabricationPart.Parameters; // //Parallel.ForEach(objectList, x => // foreach (Parameter x in objectList) // { // if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Definition.Name)) // { // returnElementsObject.Add(new QLParameter() // { // id = x.Id.ToString(), // name = x.Definition.Name, // value = x.AsValueString() == null ? x.AsString() : x.AsValueString(), // userModifiable = x.UserModifiable, // isReadOnly = x.IsReadOnly // }); // } // } // qlParameters = returnElementsObject.OrderBy(x => x.name).ToList(); // } //} public QLFabricationPartResolve(FabricationPart _fabricationPart, object aFieldOrContext) { id = _fabricationPart.Id.ToString(); name = _fabricationPart.Name; var queryFieldForParameters = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlParameters"); if (queryFieldForParameters != null) { var returnElementsObject = new ConcurrentBag <QLParameter>(); var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForParameters, "nameFilter"); var _doc = ResolverEntry.Doc; ParameterSet objectList = _fabricationPart.Parameters; //Parallel.ForEach(objectList, x => foreach (Parameter x in objectList) { if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Definition.Name)) { returnElementsObject.Add(new QLParameter() { id = x.Id.ToString(), name = x.Definition.Name, value = x.AsValueString() == null ? x.AsString() : x.AsValueString(), userModifiable = x.UserModifiable, isReadOnly = x.IsReadOnly }); } } qlParameters = returnElementsObject.OrderBy(x => x.name).ToList(); } }
private Connector GetValidConnectorToStretchAndFitFrom(Document doc, ElementId elementId) { // must be a fabrication part FabricationPart part = doc.GetElement(elementId) as FabricationPart; if (part == null) { return(null); } // must not be a straight, hanger or tap if (part.IsAStraight() || part.IsATap() || part.IsAHanger()) { return(null); } // part must be connected at one end and have one unoccupied connector int numUnused = part.ConnectorManager.UnusedConnectors.Size; int numConns = part.ConnectorManager.Connectors.Size; if (numConns - numUnused != 1) { return(null); } foreach (Connector conn in part.ConnectorManager.UnusedConnectors) { // return the first unoccupied connector return(conn); } return(null); }
/// <summary> /// Checks if the part is a coupling. /// The CID's (the fabrication part item customer Id) that are recognized internally as couplings are: /// CID 522, 1112 - Round Ductwork /// CID 1522 - Oval Ductwork /// CID 4522 - Rectangular Ductwork /// CID 2522 - Pipe Work /// CID 3522 - Electrical /// </summary> /// <param name="fabPart">The part to check.</param> /// <returns>True if the part is a coupling.</returns> private bool IsACoupling(FabricationPart fabPart) { if (fabPart != null) { int CID = fabPart.ItemCustomId; if (CID == 522 || CID == 1522 || CID == 2522 || CID == 3522 || CID == 1112) { return(true); } } return(false); }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { Document doc = commandData.Application.ActiveUIDocument.Document; // check user selection UIDocument uidoc = commandData.Application.ActiveUIDocument; ICollection <ElementId> collection = uidoc.Selection.GetElementIds(); if (collection.Count > 0) { ISet <ElementId> selIds = new HashSet <ElementId>(); foreach (ElementId id in collection) { selIds.Add(id); } using (Transaction tr = new Transaction(doc, "Optimize Straights")) { tr.Start(); // optimize lengths method will take a set of elements and any fabrication straight parts // within this set that have been optimized will be returned. ISet <ElementId> affectedPartIds = FabricationPart.OptimizeLengths(doc, selIds); if (affectedPartIds.Count == 0) { message = "No fabrication straight parts were optimized."; return(Result.Cancelled); } doc.Regenerate(); tr.Commit(); } return(Result.Succeeded); } else { // inform user they need to select at least one element message = "Please select at least one element."; } return(Result.Failed); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
private Connector GetValidConnectorToStretchAndFitTo(Document doc, ElementId elementId) { // connect to another fabrication part - will work also with families. FabricationPart part = doc.GetElement(elementId) as FabricationPart; if (part == null) { return(null); } // must not be a fabrication part hanger if (part.IsAHanger()) { return(null); } foreach (Connector conn in part.ConnectorManager.UnusedConnectors) { // return the first unoccupied connector return(conn); } return(null); }
private void Stream( ArrayList data, FabricationPart fabPart ) { data.Add( new Snoop.Data.ClassSeparator( typeof( FabricationPart ) ) ); data.Add( new Snoop.Data.Object( "Connector Manager", fabPart.ConnectorManager ) ); data.Add( new Snoop.Data.Object( "Domain Type", fabPart.DomainType ) ); data.Add( new Snoop.Data.Object( "Hosted Info", fabPart.GetHostedInfo() ) ); data.Add( new Snoop.Data.Object( "Rod Info", fabPart.GetRodInfo() ) ); data.Add( new Snoop.Data.Int( "Insulation Specification", fabPart.InsulationSpecification ) ); data.Add( new Snoop.Data.Bool( "Is a Hanger", fabPart.IsAHanger() ) ); data.Add( new Snoop.Data.Bool( "Is a Straight", fabPart.IsAStraight() ) ); data.Add( new Snoop.Data.Bool( "Is a Tap", fabPart.IsATap() ) ); data.Add( new Snoop.Data.Double( "Level Offset", fabPart.LevelOffset ) ); data.Add( new Snoop.Data.Int( "Material", fabPart.Material ) ); data.Add( new Snoop.Data.Xyz( "Origin", fabPart.Origin ) ); data.Add( new Snoop.Data.String( "Product Code", fabPart.ProductCode ) ); data.Add( new Snoop.Data.Int( "Specification", fabPart.Specification ) ); }
/// <summary> /// Checks if the part is fabrication pipework. /// </summary> /// <param name="fabPart">The part to check.</param> /// <returns>True if the part is fabrication pipework.</returns> private bool IsAPipe(FabricationPart fabPart) { return(fabPart != null && (fabPart.Category.Id.IntegerValue == (int)BuiltInCategory.OST_FabricationPipework)); }
private void extract_mep_data(Document rvtDoc) { try { FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(rvtDoc); if (config == null) { LogTrace("GetFabricationConfiguration failed"); return; } GetFabricationConnectors(config); FilteredElementCollector m_Collector = new FilteredElementCollector(rvtDoc); m_Collector.OfClass(typeof(FabricationPart)); IList <Element> fps = m_Collector.ToElements(); string outputFullPath = System.IO.Directory.GetCurrentDirectory() + "\\" + OUTPUT_FILE; FileStream fs = new FileStream(outputFullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); foreach (Element el in fps) { FabricationPart fp = el as FabricationPart; LogTrace("Reading data of one FabricationPart: " + fp.Name); sw.WriteLine("Name: " + fp.Name); foreach (Parameter para in el.Parameters) { sw.WriteLine(" " + GetParameterInformation(para, rvtDoc)); } sw.WriteLine(" ProductName: " + fp.ProductName); sw.WriteLine(" ServiceName: " + fp.ServiceName); sw.WriteLine(" Service Type: " + config.GetServiceTypeName(fp.ServiceType)); sw.WriteLine(" SpoolName: " + fp.SpoolName); sw.WriteLine(" Alias: " + fp.Alias); sw.WriteLine(" CID: " + fp.ItemCustomId.ToString()); sw.WriteLine(" Domain Type: " + fp.DomainType.ToString()); if (fp.IsAHanger()) { string rodKitName = " None"; var rodKit = fp.HangerRodKit; if (rodKit > 0) { rodKitName = config.GetAncillaryGroupName(fp.HangerRodKit) + ": " + config.GetAncillaryName(fp.HangerRodKit); } sw.WriteLine(" Hanger Rod Kit: " + rodKitName); } var insSpec = config.GetInsulationSpecificationGroup(fp.InsulationSpecification) + ": " + config.GetInsulationSpecificationName(fp.InsulationSpecification); sw.WriteLine(" Insulation Specification: " + insSpec); sw.WriteLine(" Has No Connections: " + fp.HasNoConnections().ToString()); sw.WriteLine(" Item Number: " + fp.ItemNumber); var material = config.GetMaterialGroup(fp.Material) + ": " + config.GetMaterialName(fp.Material); sw.WriteLine(" Material: " + material); sw.WriteLine(" Part Guid: " + fp.PartGuid.ToString()); sw.WriteLine(" Part Status: " + config.GetPartStatusDescription(fp.PartStatus)); sw.WriteLine(" Product Code: " + fp.ProductCode); var spec = config.GetSpecificationGroup(fp.Specification) + ": " + config.GetSpecificationName(fp.Specification); sw.WriteLine(" Specification: " + spec); sw.WriteLine(" Dimensions from Fabrication Dimension Definition:"); foreach (FabricationDimensionDefinition dmdef in fp.GetDimensions()) { sw.WriteLine(" " + dmdef.Name + ":" + fp.GetDimensionValue(dmdef).ToString()); } int index = 1; sw.WriteLine(" Connectors:"); foreach (Connector con in fp.ConnectorManager.Connectors) { sw.WriteLine(" C" + index.ToString()); FabricationConnectorInfo fci = con.GetFabricationConnectorInfo(); sw.WriteLine(" Connector Info:" + m_connGroups[fci.BodyConnectorId] + " " + m_connNames[fci.BodyConnectorId]); sw.WriteLine(" IsBodyConnectorLocked:" + fci.IsBodyConnectorLocked.ToString()); sw.WriteLine(" Shape:" + con.Shape.ToString()); try { sw.WriteLine(" Radius:" + con.Radius.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" PressureDrop:" + con.PressureDrop.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" Width:" + con.Width.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" Height:" + con.Height.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" PipeSystemType:" + con.PipeSystemType.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" VelocityPressure:" + con.VelocityPressure.ToString()); } catch (Exception ex) { } try { sw.WriteLine(" DuctSystemType:" + con.DuctSystemType.ToString()); } catch (Exception ex) { } index++; } } LogTrace("Saving file..."); sw.Close(); fs.Close(); } catch (Exception ex) { LogTrace("Unexpected Exception..." + ex.ToString()); } }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { // check user selection var uidoc = commandData.Application.ActiveUIDocument; var doc = uidoc.Document; ISet <ElementId> parts = null; using (Transaction tr = new Transaction(doc, "Optimise Preselection")) { tr.Start(); ICollection <ElementId> selElems = uidoc.Selection.GetElementIds(); if (selElems.Count > 0) { parts = new HashSet <ElementId>(selElems); } tr.Commit(); } if (parts == null) { MessageBox.Show("Select parts to export."); return(Result.Failed); } var callingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var saveAsDlg = new FileSaveDialog("CSV Files (*.csv)|*.csv"); saveAsDlg.InitialFileName = callingFolder + "\\geomExport"; saveAsDlg.Title = "Save Part Geometry As"; var result = saveAsDlg.Show(); if (result == ItemSelectionDialogResult.Canceled) { return(Result.Cancelled); } string filename = ModelPathUtils.ConvertModelPathToUserVisiblePath(saveAsDlg.GetSelectedModelPath()); string ext = Path.GetExtension(filename); filename = Path.GetFileNameWithoutExtension(filename); int partcount = 1, exported = 0; foreach (ElementId eid in parts) { // get all rods and kist with rods FabricationPart part = doc.GetElement(eid) as FabricationPart; if (part != null) { Options options = new Options(); options.DetailLevel = ViewDetailLevel.Coarse; IList <Mesh> main = getMeshes(part.get_Geometry(options)); IList <Mesh> ins = getMeshes(part.GetInsulationLiningGeometry()); int mlp = 0; foreach (Mesh mesh in main) { String file = String.Concat(filename, partcount.ToString(), "-main-", (++mlp).ToString(), ext); if (exportMesh(file, mesh)) { exported++; } } int ilp = 0; foreach (Mesh mesh in ins) { String file = String.Concat(filename, partcount.ToString(), "-ins-", (++ilp).ToString(), ext); if (exportMesh(file, mesh)) { exported++; } } } partcount++; } String res = (exported > 0) ? "Export was successful" : "Nothing was exported"; String manywritten = String.Format("{0} Parts were exported", exported); TaskDialog td = new TaskDialog("Export Part Mesh Geometry") { MainIcon = TaskDialogIcon.TaskDialogIconInformation, TitleAutoPrefix = false, MainInstruction = res, MainContent = manywritten, AllowCancellation = false, CommonButtons = TaskDialogCommonButtons.Ok }; td.Show(); return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
/// <summary> /// Checks if the part is fabrication pipework. /// </summary> /// <param name="fabPart">The part to check.</param> /// <returns>True if the part is fabrication pipework.</returns> private bool IsAPipe(FabricationPart fabPart) { return(fabPart != null && (fabPart.Category.BuiltInCategory == BuiltInCategory.OST_FabricationPipework)); }
/// <summary> /// Convenience method to set fabrication part's dimension value, specified by the dimension name. /// </summary> /// <param name="part"> /// The fabrication part. /// </param> /// <param name="dimName"> /// The name of the fabrication dimension. /// </param> /// <param name="dimValue"> /// The value of the fabrication dimension to set to. /// </param> /// <returns> /// Returns the fabrication dimension value for the fabrication part, as specified by the dimension name. /// </returns> bool SetDimValue(FabricationPart part, string dimName, double dimValue) { IList<FabricationDimensionDefinition> dims = part.GetDimensions(); FabricationDimensionDefinition dim = null; foreach (FabricationDimensionDefinition def in dims) { if (def.Name.Equals(dimName)) { dim = def; break; } } if (dim == null) return false; part.SetDimensionValue(dim, dimValue); m_doc.Regenerate(); return true; }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { Document doc = commandData.Application.ActiveUIDocument.Document; // check user selection UIDocument uidoc = commandData.Application.ActiveUIDocument; ICollection <ElementId> collection = uidoc.Selection.GetElementIds(); if (collection.Count > 0) { List <ElementId> selIds = new List <ElementId>(); foreach (ElementId id in collection) { selIds.Add(id); } if (selIds.Count != 2) { message = "Select a fabrication part to stretch and fit from and an element to connect to."; return(Result.Cancelled); } Connector connFrom = GetValidConnectorToStretchAndFitFrom(doc, selIds.ElementAt(0)); Connector connTo = GetValidConnectorToStretchAndFitTo(doc, selIds.ElementAt(1)); FabricationPartRouteEnd toEnd = FabricationPartRouteEnd.CreateFromConnector(connTo); if (connFrom == null || connTo == null) { message = "Invalid fabrication parts to stretch and fit"; return(Result.Cancelled); } using (Transaction tr = new Transaction(doc, "Stretch and Fit")) { tr.Start(); ISet <ElementId> newPartIds; FabricationPartFitResult result = FabricationPart.StretchAndFit(doc, connFrom, toEnd, out newPartIds); if (result != FabricationPartFitResult.Success) { message = result.ToString(); return(Result.Failed); } doc.Regenerate(); tr.Commit(); } return(Result.Succeeded); } else { // inform user they need to select at least one element message = "Select a fabrication part to stretch and fit from and an element to connect to."; } return(Result.Failed); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { // check user selection var uidoc = commandData.Application.ActiveUIDocument; var doc = uidoc.Document; var elementIds = new HashSet <ElementId>(); uidoc.Selection.GetElementIds().ToList().ForEach(x => elementIds.Add(x)); var hasFabricationParts = false; foreach (var elementId in elementIds) { var part = doc.GetElement(elementId) as FabricationPart; if (part != null) { hasFabricationParts = true; break; } } if (hasFabricationParts == false) { message = "Select at least one fabrication part"; return(Result.Failed); } var callingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var saveAsDlg = new FileSaveDialog("MAJ Files (*.maj)|*.maj"); saveAsDlg.InitialFileName = callingFolder + "\\majExport"; saveAsDlg.Title = "Export To MAJ"; var result = saveAsDlg.Show(); if (result == ItemSelectionDialogResult.Canceled) { return(Result.Cancelled); } string filename = ModelPathUtils.ConvertModelPathToUserVisiblePath(saveAsDlg.GetSelectedModelPath()); ISet <ElementId> exported = FabricationPart.SaveAsFabricationJob(doc, elementIds, filename, new FabricationSaveJobOptions(true)); if (exported.Count > 0) { TaskDialog td = new TaskDialog("Export to MAJ") { MainIcon = TaskDialogIcon.TaskDialogIconInformation, TitleAutoPrefix = false, MainInstruction = string.Concat("Export to MAJ was successful - ", exported.Count.ToString(), " Parts written"), MainContent = filename, AllowCancellation = false, CommonButtons = TaskDialogCommonButtons.Ok }; td.Show(); } return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { Document doc = commandData.Application.ActiveUIDocument.Document; UIDocument uidoc = commandData.Application.ActiveUIDocument; FabricationPart fabPart = null; FabricationConfiguration config = null; try { // check for a load fabrication config config = FabricationConfiguration.GetFabricationConfiguration(doc); if (config == null) { message = "No fabrication configuration loaded."; return(Result.Failed); } // pick a fabrication part Reference refObj = uidoc.Selection.PickObject(ObjectType.Element, "Pick a fabrication part to start."); fabPart = doc.GetElement(refObj) as FabricationPart; } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); } if (fabPart == null) { message = "The selected element is not a fabrication part."; return(Result.Failed); } else { // get ancillary data from selected part and report to user IList <FabricationAncillaryUsage> ancillaries = fabPart.GetPartAncillaryUsage(); List <string> ancillaryDescriptions = new List <string>(); // create list of ancillary descriptions using the Ancillary UseageType and Name foreach (var ancillaryUsage in ancillaries) { FabricationAncillaryType ancilType = ancillaryUsage.Type; FabricationAncillaryUsageType usageType = ancillaryUsage.UsageType; ancillaryDescriptions.Add($"{ancilType.ToString()}: {usageType.ToString()} - " + $"{config.GetAncillaryName(ancillaryUsage.AncillaryId)}"); } string results = string.Empty; // group and quantify if (ancillaryDescriptions.Count > 0) { ancillaryDescriptions.Sort(); StringBuilder resultsBuilder = new StringBuilder(); string currentAncillary = string.Empty; foreach (var ancillaryName in ancillaryDescriptions) { if (ancillaryName != currentAncillary) { resultsBuilder.AppendLine($"{ancillaryName} x {ancillaryDescriptions.Count(x => x == ancillaryName)}"); currentAncillary = ancillaryName; } } results = resultsBuilder.ToString(); } TaskDialog td = new TaskDialog("Ancillaries") { MainIcon = TaskDialogIcon.TaskDialogIconInformation, TitleAutoPrefix = false, MainInstruction = ancillaryDescriptions.Count > 0 ? $"{ancillaryDescriptions.Count} ancillaries found on selected part" : $"No ancillaries found on selected part", MainContent = results }; td.Show(); } return(Result.Succeeded); }
/// <summary> /// Report the custom data. /// </summary> /// <param name="doc"></param> /// <param name="uiDoc"></param> /// <param name="setNewValues"></param> /// <param name="message"></param> /// <returns></returns> public static Result ReportCustomData(Document doc, UIDocument uiDoc, bool setNewValues, ref string message) { FabricationPart fabPart = null; FabricationConfiguration config = null; try { // check for a load fabrication config config = FabricationConfiguration.GetFabricationConfiguration(doc); if (config == null) { message = "No fabrication configuration loaded."; return(Result.Failed); } // pick a fabrication part Reference refObj = uiDoc.Selection.PickObject(ObjectType.Element, "Pick a fabrication part to start."); fabPart = doc.GetElement(refObj) as FabricationPart; } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); } if (fabPart == null) { message = "The selected element is not a fabrication part."; return(Result.Failed); } else { // get custom data from loaded fabrication config IList <int> customDataIds = config.GetAllPartCustomData(); int customDataCount = customDataIds.Count; string results = string.Empty; // report custom data info if (customDataCount > 0) { StringBuilder resultsBuilder = new StringBuilder(); resultsBuilder.AppendLine($"Fabrication config contains {customDataCount} custom data entries {Environment.NewLine}"); foreach (var customDataId in customDataIds) { FabricationCustomDataType customDataType = config.GetPartCustomDataType(customDataId); string customDataName = config.GetPartCustomDataName(customDataId); resultsBuilder.AppendLine($"Type: {customDataType.ToString()} Name: {customDataName}"); // check custom data exists on selected part if (fabPart.HasCustomData(customDataId)) { string fabPartCurrentValue = string.Empty; string fabPartNewValue = string.Empty; switch (customDataType) { case FabricationCustomDataType.Text: fabPartCurrentValue = $"\"{fabPart.GetPartCustomDataText(customDataId)}\""; if (setNewValues) { string installDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); fabPart.SetPartCustomDataText(customDataId, installDateTime); fabPartNewValue = installDateTime; } break; case FabricationCustomDataType.Integer: fabPartCurrentValue = fabPart.GetPartCustomDataInteger(customDataId).ToString(); if (setNewValues) { int installHours = new Random().Next(1, 10); fabPart.SetPartCustomDataInteger(customDataId, installHours); fabPartNewValue = installHours.ToString(); } break; case FabricationCustomDataType.Real: fabPartCurrentValue = $"{fabPart.GetPartCustomDataReal(customDataId):0.##}"; if (setNewValues) { double installCost = new Random().NextDouble() * new Random().Next(100, 1000); fabPart.SetPartCustomDataReal(customDataId, installCost); fabPartNewValue = $"{installCost:0.##}"; } break; } resultsBuilder.AppendLine("Current custom data entry value = " + $"{fabPartCurrentValue} {Environment.NewLine}"); if (setNewValues) { resultsBuilder.AppendLine("New custom data entry value = " + $"{fabPartNewValue} {Environment.NewLine}"); } } else { resultsBuilder.AppendLine($"Custom data entry is not set on the part {Environment.NewLine}"); } } results = resultsBuilder.ToString(); } TaskDialog td = new TaskDialog("Custom Data") { MainIcon = TaskDialogIcon.TaskDialogIconInformation, TitleAutoPrefix = false, MainInstruction = $"{customDataCount} custom data entries found in the loaded fabrication config", MainContent = results }; td.Show(); } return(Result.Succeeded); }
/// <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 virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { var uiDoc = commandData.Application.ActiveUIDocument; var doc = uiDoc.Document; FilteredElementCollector cl = new FilteredElementCollector(doc); cl.OfClass(typeof(Level)); IList <Element> levels = cl.ToElements(); Level levelOne = null; foreach (Level level in levels) { if (level != null && level.Name.Equals("Level 1")) { levelOne = level; break; } } if (levelOne == null) { return(Result.Failed); } using (var config = FabricationConfiguration.GetFabricationConfiguration(doc)) { if (config == null) { message = "No fabrication configuration in use"; return(Result.Failed); } using (var configInfo = config.GetFabricationConfigurationInfo()) { using (var source = FabricationConfigurationInfo.FindSourceFabricationConfiguration(configInfo)) { if (source == null) { message = "Source fabrication configuration not found"; return(Result.Failed); } using (var trans = new Autodesk.Revit.DB.Transaction(doc, "Load And Place Next Item File")) { trans.Start(); // reload the configuration config.ReloadConfiguration(); // get the item folders var itemFolders = config.GetItemFolders(); // get the next unloaded item file from the item folders structure var nextFile = GetNextUnloadedItemFile(itemFolders); if (nextFile == null) { message = "Could not locate the next unloaded item file"; return(Result.Failed); } var itemFilesToLoad = new List <FabricationItemFile>(); itemFilesToLoad.Add(nextFile); // load the item file into the config var failedItems = config.LoadItemFiles(itemFilesToLoad); if (failedItems != null && failedItems.Count > 0) { message = "Could not load the item file: " + nextFile.Identifier; return(Result.Failed); } // create a part from the item file using (var part = FabricationPart.Create(doc, nextFile, levelOne.Id)) { doc.Regenerate(); var selectedElements = new List <ElementId>() { part.Id }; uiDoc.Selection.SetElementIds(selectedElements); uiDoc.ShowElements(selectedElements); trans.Commit(); } } } } } return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
/// <summary> /// Convenience method to get fabrication part's dimension value, specified by the dimension name. /// </summary> /// <param name="part"> /// The fabrication part to be queried. /// </param> /// <param name="dimName"> /// The name of the fabrication dimension. /// </param> /// <returns> /// Returns the fabrication dimension value for the fabrication part, as specified by the dimension name. /// </returns> double GetDimValue(FabricationPart part, string dimName) { double value = 0; if (part != null) { IList<FabricationDimensionDefinition> dims = part.GetDimensions(); foreach (FabricationDimensionDefinition def in dims) { if (def.Name.Equals(dimName)) { value = part.GetDimensionValue(def); break; } } } return value; }