Пример #1
0
        /// <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;

                // get the user selection
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                ICollection <ElementId> collection = uidoc.Selection.GetElementIds();

                if (collection.Count > 0)
                {
                    // DesignToFabrication needs an ISet<ElementId>
                    ISet <ElementId> selIds = new HashSet <ElementId>();
                    foreach (ElementId id in collection)
                    {
                        selIds.Add(id);
                    }

                    using (Transaction tr = new Transaction(doc, "Convert To Fabrication Parts"))
                    {
                        tr.Start();

                        FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(doc);

                        // get all loaded fabrication services and attempt to convert the design elements
                        // to the first loaded service
                        IList <FabricationService> allLoadedServices = config.GetAllLoadedServices();

                        DesignToFabricationConverter       converter = new DesignToFabricationConverter(doc);
                        DesignToFabricationConverterResult result    = converter.Convert(selIds, allLoadedServices[0].ServiceId);

                        if (result != DesignToFabricationConverterResult.Success)
                        {
                            message = "There was a problem with the conversion.";
                            return(Result.Failed);
                        }

                        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);
            }
        }
Пример #2
0
        /// <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;

                using (Transaction tr = new Transaction(doc, "Set button and group exclusions"))
                {
                    tr.Start();

                    FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(doc);

                    if (config == null)
                    {
                        message = "No fabrication configuration loaded.";
                        return(Result.Failed);
                    }

                    // get all loaded fabrication services
                    IList <FabricationService> allLoadedServices = config.GetAllLoadedServices();
                    // get the "ADSK - HVAC:Supply Air" service
                    string             serviceName     = "ADSK - HVAC: Supply Air";
                    FabricationService selectedService = allLoadedServices.FirstOrDefault(x => x.Name == serviceName);

                    if (selectedService == null)
                    {
                        message = $"Could not find fabrication service {serviceName}";
                        return(Result.Failed);
                    }

                    string rectangularGroupName = "Rectangular";
                    string roundGroupName       = "Round Bought Out";
                    string excludeButtonName    = "Square Bend";

                    int rectangularGroupIndex = -1;
                    int roundGroupIndex       = -1;

                    // find Rectangular and Round groups in service
                    for (int i = 0; i < selectedService.GroupCount; i++)
                    {
                        if (selectedService.GetGroupName(i) == rectangularGroupName)
                        {
                            rectangularGroupIndex = i;
                        }

                        if (selectedService.GetGroupName(i) == roundGroupName)
                        {
                            roundGroupIndex = i;
                        }

                        if (rectangularGroupIndex > -1 && roundGroupIndex > -1)
                        {
                            break;
                        }
                    }

                    if (rectangularGroupIndex > -1)
                    {
                        // exclude square bend in Rectangular group
                        for (int i = 0; i < selectedService.GetButtonCount(rectangularGroupIndex); i++)
                        {
                            if (selectedService.GetButton(rectangularGroupIndex, i).Name == excludeButtonName)
                            {
                                selectedService.OverrideServiceButtonExclusion(rectangularGroupIndex, i, true);
                                break;
                            }
                        }
                    }
                    else
                    {
                        message = $"Unable to locate {excludeButtonName} button to exclude.";
                        return(Result.Failed);
                    }

                    // exclude entire Round Bought Out service group
                    if (roundGroupIndex > -1)
                    {
                        selectedService.SetServiceGroupExclusions(new List <int>()
                        {
                            roundGroupIndex
                        });
                    }
                    else
                    {
                        message = $"Unable to locate {roundGroupName} service group to exclude.";
                        return(Result.Failed);
                    }

                    tr.Commit();

                    TaskDialog td = new TaskDialog("Button and Group Exclsuions")
                    {
                        MainIcon        = TaskDialogIcon.TaskDialogIconInformation,
                        TitleAutoPrefix = false,
                        MainInstruction = "Operation Successful",
                        MainContent     = $"Excluded {excludeButtonName} button from {serviceName} {rectangularGroupName} Group {Environment.NewLine}"
                                          + $"Excluded {roundGroupName} Group from {serviceName}"
                    };

                    td.Show();
                }


                return(Result.Succeeded);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }
Пример #3
0
        /// <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;

                // get the user selection
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                ICollection <ElementId> collection = uidoc.Selection.GetElementIds();

                if (collection.Count > 0)
                {
                    // FabricationNetworkChangeService needs an ISet<ElementId>
                    ISet <ElementId> selIds = new HashSet <ElementId>();
                    foreach (ElementId id in collection)
                    {
                        selIds.Add(id);
                    }

                    using (Transaction tr = new Transaction(doc, "Appply Change Service and Size of Fabrication Parts"))
                    {
                        tr.Start();

                        FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(doc);

                        // Get all loaded fabrication services
                        IList <FabricationService> allLoadedServices = config.GetAllLoadedServices();

                        FabricationNetworkChangeService applychange = new FabricationNetworkChangeService(doc);

                        // Set the selection of element identifiers to be changed
                        applychange.SetSelection(selIds);
                        // Set the service to the second service in the list (ductwork exhaust service)
                        applychange.SetServiceId(allLoadedServices[1].ServiceId);
                        // Set the group to the second in the list (round)
                        applychange.SetGroupId(1);

                        // Get the sizes of all the straights that was in the selection of elements that was added to FabricationNetworkChangeService
                        ISet <Autodesk.Revit.DB.Fabrication.FabricationPartSizeMap> sizeMappings = applychange.GetMapOfAllSizesForStraights();
                        foreach (Autodesk.Revit.DB.Fabrication.FabricationPartSizeMap sizemapping in sizeMappings)
                        {
                            if (sizemapping != null)
                            {
                                // Testing round so ignoring the depth and adding 6" to the current size so all straights will be updated to a new size
                                var widthDia = sizemapping.WidthDiameter + 0.5;
                                sizemapping.MappedWidthDiameter = widthDia;
                            }
                        }
                        applychange.SetMapOfSizesForStraights(sizeMappings);

                        // Get the in-line element type identiers
                        var inlineRevIds = new HashSet <Autodesk.Revit.DB.ElementId>();
                        ISet <Autodesk.Revit.DB.ElementId> inlineIds = applychange.GetInLinePartTypes();
                        for (var ii = inlineIds.Count() - 1; ii > -1; ii--)
                        {
                            var elemId = inlineIds.ElementAt(ii);
                            if (elemId != null)
                            {
                                inlineRevIds.Add(elemId);
                            }
                        }
                        // Set the in-line element type identiers by swapping them out by reversing the order to keep it simple but still exercise the code
                        IDictionary <ElementId, ElementId> swapinlineIds = new Dictionary <ElementId, ElementId>();
                        for (var ii = inlineIds.Count() - 1; ii > -1; ii--)
                        {
                            var elemId      = inlineIds.ElementAt(ii);
                            var elemIdother = inlineRevIds.ElementAt(ii);
                            if ((elemId != null) && (elemId != null))
                            {
                                swapinlineIds.Add(elemId, elemIdother);
                            }
                        }
                        applychange.SetMapOfInLinePartTypes(swapinlineIds);

                        // Apply the changes
                        FabricationNetworkChangeServiceResult result = applychange.ApplyChange();

                        if (result != FabricationNetworkChangeServiceResult.Success)
                        {
                            message = "There was a problem with the apply change.";
                            return(Result.Failed);
                        }

                        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);
            }
        }
Пример #4
0
        /// <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;

                // get the user selection
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                ICollection <ElementId> collection = uidoc.Selection.GetElementIds();

                if (collection.Count > 0)
                {
                    // DesignToFabrication needs an ISet<ElementId>
                    ISet <ElementId> selIds = new HashSet <ElementId>();
                    foreach (ElementId id in collection)
                    {
                        selIds.Add(id);
                    }

                    // Set the in-line element type identiers to be swapped during the conversion replacing the family with a similar fabrication part
                    IDictionary <ElementId, ElementId> convertInLineIds = new Dictionary <ElementId, ElementId>();

                    // Get all family symbols
                    FilteredElementCollector familyFinder = new FilteredElementCollector(doc);
                    var families = familyFinder.OfClass(typeof(FamilySymbol)).ToElements().ToList();

                    // Get the family symbol for the damper we are going to convert
                    String fsName = "Fire Damper - Rectangular - Simple";

                    // The found family symbol
                    FamilySymbol fsDamper = null;
                    foreach (FamilySymbol family in families)
                    {
                        if (family.FamilyName == fsName)
                        {
                            fsDamper = family;
                            break;
                        }
                    }

                    // If the damper was found try to find the matching fabrication part type
                    if (fsDamper != null)
                    {
                        // Get the element type identifier for the family symbol
                        var elemFamSymId = fsDamper.Id;

                        // Get all fabrication part types
                        FilteredElementCollector fabPartTypeFinder = new FilteredElementCollector(doc);
                        var fabPartTypes = fabPartTypeFinder.OfClass(typeof(FabricationPartType)).ToElements().ToList();

                        // Get the fabrication part type for the damper we are going to convert to
                        String fptName = "Rect FD - Flange";

                        // The found fabrication part type
                        FabricationPartType fptDamper = null;
                        foreach (FabricationPartType partType in fabPartTypes)
                        {
                            if (partType.FamilyName == fptName)
                            {
                                fptDamper = partType;
                                break;
                            }
                        }

                        // The damper was found create the mapping in between the family symbol and the matching fabrication part type
                        if (fptDamper != null)
                        {
                            // Get the element type identifier for the fabricaion part type
                            var elemFabPartTypeId = fptDamper.Id;

                            // Create the mapping for the family to the fabrication part
                            convertInLineIds.Add(elemFamSymId, elemFabPartTypeId);
                        }
                    }

                    using (Transaction tr = new Transaction(doc, "Convert To Fabrication Parts"))
                    {
                        tr.Start();

                        FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(doc);

                        // get all loaded fabrication services and attempt to convert the design elements
                        // to the first loaded service
                        IList <FabricationService> allLoadedServices = config.GetAllLoadedServices();

                        DesignToFabricationConverter converter = new DesignToFabricationConverter(doc);

                        // If there is a mapping defined attempt to add it to the converter
                        if (convertInLineIds.Count() > 0)
                        {
                            // Set the mappings
                            DesignToFabricationMappingResult mappingResult = converter.SetMapForFamilySymbolToFabricationPartType(convertInLineIds);

                            if (mappingResult != DesignToFabricationMappingResult.Success)
                            {
                                if (mappingResult != DesignToFabricationMappingResult.Undefined)
                                {
                                    message = "There was a problem with the conversion. The map contained no entries.";
                                }
                                else if (mappingResult != DesignToFabricationMappingResult.InvalidFamilySymbol)
                                {
                                    message = "There was a problem with the conversion. There was an invalid Family symbol identifier or an identifier that did not exist in the mappings.";
                                }
                                else if (mappingResult != DesignToFabricationMappingResult.InvalidFabricationPartType)
                                {
                                    message = "There was a problem with the conversion. There was an invalid Fabrication part type identifier or an identifier that did not exist in the mappings.";
                                }
                                else if (mappingResult != DesignToFabricationMappingResult.UnsupportedFamilySymbol)
                                {
                                    message = "There was a problem with the conversion. Unsupported Family symbol it is expected to be either valve, strainer, damper, smoke detector, end cap, or other in line component.";
                                }
                                else if (mappingResult != DesignToFabricationMappingResult.UnsupportedFabricationPartType)
                                {
                                    message = "There was a problem with the conversion. Unsupported Fabrication part type. It is expected to be either valve, strainer, damper, smoke detector, end cap, or other in line component.";
                                }
                                return(Result.Failed);
                            }
                        }

                        DesignToFabricationConverterResult result = converter.Convert(selIds, allLoadedServices[0].ServiceId);

                        if (result != DesignToFabricationConverterResult.Success)
                        {
                            message = "There was a problem with the conversion.";
                            return(Result.Failed);
                        }

                        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);
            }
        }
Пример #5
0
        /// <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;

                // get the user selection
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                ICollection <ElementId> collection = uidoc.Selection.GetElementIds();

                if (collection.Count > 0)
                {
                    // FabricationNetworkChangeService needs an ISet<ElementId>
                    ISet <ElementId> selIds = new HashSet <ElementId>();
                    foreach (ElementId id in collection)
                    {
                        selIds.Add(id);
                    }

                    using (Transaction tr = new Transaction(doc, "Change Size of Fabrication Parts"))
                    {
                        tr.Start();

                        FabricationConfiguration config = FabricationConfiguration.GetFabricationConfiguration(doc);

                        // Get all loaded fabrication services
                        IList <FabricationService> allLoadedServices = config.GetAllLoadedServices();

                        // Create a map of sizes to swap the current sizes to a new size
                        var sizeMappings = new HashSet <Autodesk.Revit.DB.Fabrication.FabricationPartSizeMap>();
                        var mapping      = new Autodesk.Revit.DB.Fabrication.FabricationPartSizeMap("12x12", 1.0, 1.0, false, ConnectorProfileType.Rectangular, allLoadedServices[0].ServiceId, 0);
                        mapping.MappedWidthDiameter = 1.5;
                        mapping.MappedDepth         = 1.5;
                        sizeMappings.Add(mapping);
                        var mapping1 = new Autodesk.Revit.DB.Fabrication.FabricationPartSizeMap("18x18", 1.5, 1.5, false, ConnectorProfileType.Rectangular, allLoadedServices[0].ServiceId, 0);
                        mapping1.MappedWidthDiameter = 2.0;
                        mapping1.MappedDepth         = 2.0;
                        sizeMappings.Add(mapping1);

                        FabricationNetworkChangeService changesize = new FabricationNetworkChangeService(doc);

                        // Change the size of the fabrication parts in the selection to the new sizes
                        FabricationNetworkChangeServiceResult result = changesize.ChangeSize(selIds, sizeMappings);

                        if (result != FabricationNetworkChangeServiceResult.Success)
                        {
                            // Get the collection of element identifiers for parts that had errors posted against them
                            ICollection <ElementId> errorIds = changesize.GetElementsThatFailed();
                            if (errorIds.Count > 0)
                            {
                                message = "There was a problem with the change size.";
                                return(Result.Failed);
                            }
                        }

                        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);
            }
        }