Exemplo n.º 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)
        {
            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);
        }
        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());
            }
        }