Beispiel #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 Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            Autodesk.Revit.UI.UIApplication application = commandData.Application;
            m_docment = application.ActiveUIDocument.Document;
            try
            {
                // user should select one slab firstly.
                if (application.ActiveUIDocument.Selection.Elements.Size == 0)
                {
                    TaskDialog.Show("Revit", "Please select one slab firstly.", TaskDialogCommonButtons.Ok);
                    return(Autodesk.Revit.UI.Result.Cancelled);
                }

                // get the selected slab and show its span direction
                ElementSet         elementSet = application.ActiveUIDocument.Selection.Elements;
                ElementSetIterator elemIter   = elementSet.ForwardIterator();
                elemIter.Reset();
                while (elemIter.MoveNext())
                {
                    Floor floor = elemIter.Current as Floor;
                    if (floor != null)
                    {
                        GetSpanDirectionAndSymobls(floor);
                    }
                }
            }
            catch (Exception ex)
            {
                message = ex.ToString();
                return(Autodesk.Revit.UI.Result.Failed);
            }
            return(Autodesk.Revit.UI.Result.Succeeded);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="attribsColl"></param>
        private void UpdateFamily(Element elem, XmlAttributeCollection attribsColl)
        {
            string familyName;
            string familySymbolString;

            try
            {
                familyName         = attribsColl["Family"].Value;
                familySymbolString = attribsColl["Type"].Value;
            }
            catch (Exception e)
            {
                MessageBox.Show(" The column headers should not be edited", "Import Error");
                throw e;
            }

            FamilyInstance famInst = elem as FamilyInstance;

            if (famInst != null)
            {
                ElementSet elemSet;
                if (famInst.Symbol.Family.Name != familyName)
                {
                    elemSet = GetElementsOfType(typeof(Family));

                    ElementSetIterator elemSetIter = elemSet.ForwardIterator();
                    while (elemSetIter.MoveNext())
                    {
                        Family family = elemSetIter.Current as Family;
                        if (family.Name == familyName)
                        {
                            UpdateFamilySymbol(family, famInst, familySymbolString);
                            break;
                        }
                    }
                }
                else
                {
                    if (famInst.Symbol.Name != familySymbolString)
                    {
                        UpdateFamilySymbol(famInst.Symbol.Family, famInst, familySymbolString);
                    }
                }
            }
        }
Beispiel #3
0
        public static Element SelectSingleElement(
            UIDocument uidoc,
            string description)
        {
            if (ViewType.Internal == uidoc.ActiveView.ViewType)
            {
                TaskDialog.Show("Error",
                                "Cannot pick element in this view: "
                                + uidoc.ActiveView.Name);

                return(null);
            }

#if _2010
            sel.Elements.Clear();
            Element e = null;
            sel.StatusbarTip = "Please select " + description;
            if (sel.PickOne())
            {
                ElementSetIterator elemSetItr
                    = sel.Elements.ForwardIterator();
                elemSetItr.MoveNext();
                e = elemSetItr.Current as Element;
            }
            return(e);
#endif // _2010

            try
            {
                Reference r = uidoc.Selection.PickObject(
                    ObjectType.Element,
                    "Please select " + description);

                // 'Autodesk.Revit.DB.Reference.Element' is
                // obsolete: Property will be removed. Use
                // Document.GetElement(Reference) instead.
                //return null == r ? null : r.Element; // 2011

                return(uidoc.Document.GetElement(r)); // 2012
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(null);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Reads the current selection of Revit.
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <returns>True if reading succeed; otherwise, false.</returns>
        private bool ReadSelectedElementFromRevit(out string errorMessage)
        {
            errorMessage = string.Empty;

            if (ThisExtension.Revit.ActiveUIDocument.Selection.Elements.Size == 0)
            {
                errorMessage = "The selection is empty";
                return(false);
            }

            if (ThisExtension.Revit.ActiveUIDocument.Selection.Elements.Size > 1)
            {
                errorMessage = "The selection contains more than one element";
                return(false);
            }

            ElementSetIterator iterator = ThisExtension.Revit.ActiveUIDocument.Selection.Elements.ForwardIterator();

            iterator.Reset();
            iterator.MoveNext();
            FamilyInstance familyInstance = iterator.Current as FamilyInstance;

            if (familyInstance == null)
            {
                errorMessage = "Selected element is not a FamilyInstance";
                return(false);
            }

            if (familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Beam ||
                familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Column ||
                familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Brace)
            {
                ThisMainExtension.Data.SelectedElement = familyInstance;
                FillPropertiesOfSelectedElement();
                FillGeometryOfSelectedElement();
                return(true);
            }
            else
            {
                errorMessage = "Selected element is not a beam nor a column";
                return(false);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of TagBeamData.
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application
        /// which contains data related to the command</param>
        public TagBeamData(ExternalCommandData commandData)
        {
            //Get beams selected
            m_revitDoc   = commandData.Application.ActiveUIDocument;
            m_docCreator = m_revitDoc.Document.Create;
            m_view       = m_revitDoc.Document.ActiveView;

            ElementSet elementSet = new ElementSet();

            foreach (ElementId elementId in m_revitDoc.Selection.GetElementIds())
            {
                elementSet.Insert(m_revitDoc.Document.GetElement(elementId));
            }
            ElementSetIterator itor = elementSet.ForwardIterator();

            while (itor.MoveNext())
            {
                FamilyInstance familyInstance = itor.Current as FamilyInstance;
                if ((familyInstance != null) && (familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Beam))
                {
                    m_beamList.Add(familyInstance);
                }
            }
            if (m_beamList.Count < 1)
            {
                throw new ApplicationException("there is no beam selected");
            }

            //Get the family symbols of tag in this document.
            FilteredElementCollector collector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
            IList <Element>          elements  = collector.OfClass(typeof(Family)).ToElements();

            foreach (Family family in elements)
            {
                if (family != null && family.GetFamilySymbolIds() != null)
                {
                    List <FamilySymbol> ffs = new List <FamilySymbol>();
                    foreach (ElementId elementId in family.GetFamilySymbolIds())
                    {
                        ffs.Add((FamilySymbol)(commandData.Application.ActiveUIDocument.Document.GetElement(elementId)));
                    }
                    foreach (FamilySymbol tagSymbol in ffs)
                    {
                        try
                        {
                            if (tagSymbol != null)
                            {
                                switch (tagSymbol.Category.Name)
                                {
                                case "Structural Framing Tags":
                                    m_categoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
                                    continue;

                                case "Material Tags":
                                    m_materialTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
                                    continue;

                                case "Multi-Category Tags":
                                    m_multiCategoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
                                    continue;

                                default:
                                    continue;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
            }
        }
Beispiel #6
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 Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message,
                                                ElementSet elements)
        {
            // set out default result to failure.
            Autodesk.Revit.UI.Result retRes = Autodesk.Revit.UI.Result.Failed;

            Autodesk.Revit.UI.UIApplication app = commandData.Application;

            // get the elements selected
            // The current selection can be retrieved from the active
            // document via the selection object
            ElementSet seletion = new ElementSet();

            foreach (ElementId elementId in app.ActiveUIDocument.Selection.GetElementIds())
            {
                seletion.Insert(app.ActiveUIDocument.Document.GetElement(elementId));
            }

            // we need to make sure that only one element is selected.
            if (seletion.Size == 1)
            {
                // we need to get the first and only element in the selection. Do this by getting
                // an iterator. MoveNext and then get the current element.
                ElementSetIterator it = seletion.ForwardIterator();
                it.MoveNext();
                Element element = it.Current as Element;

                // Next we need to iterate through the parameters of the element,
                // as we iterating, we will store the strings that are to be displayed
                // for the parameters in a string list "parameterItems"
                List <string> parameterItems = new List <string>();
                ParameterSet  parameters     = element.Parameters;
                foreach (Parameter param in parameters)
                {
                    if (param == null)
                    {
                        continue;
                    }

                    // We will make a string that has the following format,
                    // name type value
                    // create a StringBuilder object to store the string of one parameter
                    // using the character '\t' to delimit parameter name, type and value
                    StringBuilder sb = new StringBuilder();

                    // the name of the parameter can be found from its definition.
                    sb.AppendFormat("{0}\t", param.Definition.Name);

                    // Is the parameter shared?
                    sb.AppendFormat(param.IsShared.ToString() + "\t");

                    // Revit parameters can be one of 5 different internal storage types:
                    // double, int, string, Autodesk.Revit.DB.ElementId and None.
                    // if it is double then use AsDouble to get the double value
                    // then int AsInteger, string AsString, None AsStringValue.
                    // Switch based on the storage type
                    switch (param.StorageType)
                    {
                    case Autodesk.Revit.DB.StorageType.Double:
                        // append the type and value
                        sb.AppendFormat("double\t{0}", param.AsDouble());
                        break;

                    case Autodesk.Revit.DB.StorageType.ElementId:
                        // for element ids, we will try and retrieve the element from the
                        // document if it can be found we will display its name.
                        sb.Append("Element\t");

                        // using ActiveDocument.GetElement(the element id) to
                        // retrieve the element from the active document
                        Autodesk.Revit.DB.ElementId elemId = new ElementId(param.AsElementId().IntegerValue);
                        Element elem = app.ActiveUIDocument.Document.GetElement(elemId);

                        // if there is an element then display its name,
                        // otherwise display the fact that it is not set
                        sb.Append(elem != null ? elem.Name : "Not set");
                        break;

                    case Autodesk.Revit.DB.StorageType.Integer:
                        // append the type and value
                        sb.AppendFormat("int\t{0}", param.AsInteger());
                        break;

                    case Autodesk.Revit.DB.StorageType.String:
                        // append the type and value
                        sb.AppendFormat("string\t{0}", param.AsString());
                        break;

                    case Autodesk.Revit.DB.StorageType.None:
                        // append the type and value
                        sb.AppendFormat("none\t{0}", param.AsValueString());
                        break;

                    default:
                        break;
                    }


                    // add the completed line to the string list
                    parameterItems.Add(sb.ToString());
                }

                // Create our dialog, passing it the parameters array for display.
                PropertiesForm propertiesForm = new PropertiesForm(parameterItems.ToArray());
                propertiesForm.StartPosition = FormStartPosition.CenterParent;
                propertiesForm.ShowDialog();
                retRes = Autodesk.Revit.UI.Result.Succeeded;
            }
            else
            {
                message = "Please select only one element";
            }
            return(retRes);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app   = commandData.Application;
            UIDocument    uidoc = app.ActiveUIDocument;
            Document      doc   = uidoc.Document;

            Selection sel    = uidoc.Selection;
            Element   column = null;

            if (1 == sel.GetElementIds().Count)
            {
                foreach (Element e in sel.Elements)
                {
                    column = e;
                }
                if (!IsColumn(column))
                {
                    column = null;
                }
            }

            if (null == column)
            {
#if _2010
                sel.Elements.Clear();
                sel.StatusbarTip = "Please select a column";
                if (sel.PickOne())
                {
                    ElementSetIterator i
                        = sel.Elements.ForwardIterator();
                    i.MoveNext();
                    column = i.Current as Element;
                }
#endif // _2010

                Reference r = uidoc.Selection.PickObject(ObjectType.Element,
                                                         "Please select a column");

                if (null != r)
                {
                    // 'Autodesk.Revit.DB.Reference.Element' is
                    // obsolete: Property will be removed. Use
                    // Document.GetElement(Reference) instead.
                    //column = r.Element; // 2011

                    column = doc.GetElement(r); // 2012

                    if (!IsColumn(column))
                    {
                        message = "Please select a single column instance";
                    }
                }
            }

            if (null != column)
            {
                Options          opt = app.Application.Create.NewGeometryOptions();
                GeometryElement  geo = column.get_Geometry(opt);
                GeometryInstance i   = null;

                //GeometryObjectArray objects = geo.Objects; // 2012
                //foreach( GeometryObject obj in objects ) // 2012

                foreach (GeometryObject obj in geo) // 2013
                {
                    i = obj as GeometryInstance;
                    if (null != i)
                    {
                        break;
                    }
                }
                if (null == i)
                {
                    message = "Unable to obtain geometry instance";
                }
                else
                {
                    bool isCylindrical = false;
                    geo = i.SymbolGeometry;

                    //objects = geo.Objects; // 2012
                    //foreach( GeometryObject obj in objects ) // 2012

                    foreach (GeometryObject obj in geo)
                    {
                        Solid solid = obj as Solid;
                        if (null != solid)
                        {
                            foreach (Face face in solid.Faces)
                            {
                                if (face is CylindricalFace)
                                {
                                    isCylindrical = true;
                                    break;
                                }
                            }
                        }
                    }
                    message = string.Format(
                        "Selected column instance is{0} cylindrical",
                        (isCylindrical ? "" : " NOT"));
                }
            }
            return(Result.Failed);
        }
Beispiel #8
0
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message,
                                                ElementSet elements)
        {
            // set out default result to failure.
            Autodesk.Revit.UI.Result        retRes = Autodesk.Revit.UI.Result.Succeeded;
            Autodesk.Revit.UI.UIApplication app    = commandData.Application;

            // get the elements selected
            // The current selection can be retrieved from the active
            // document via the selection object
            ElementSet seletion = new ElementSet();

            foreach (ElementId elementId in app.ActiveUIDocument.Selection.GetElementIds())
            {
                seletion.Insert(app.ActiveUIDocument.Document.GetElement(elementId));
            }

            List <string>    parametergroup = new List <string>();
            List <ParamTree> All_Elem       = new List <ParamTree>();
            int n = seletion.Size;

            //迭代器
            ElementSetIterator it = seletion.ForwardIterator();

            //遍历所有选中的元素;
            for (int i = 0; i < n; i++)
            {
                //每次迭代下一个元素;
                it.MoveNext();
                Element element = it.Current as Element;

                ParameterSet parameters  = element.Parameters;//获取单个元素的全部属性;
                ParameterSet parameters2 = GetParamSet(element);



                foreach (Parameter param in parameters)
                {
                    if (param == null)
                    {
                        continue;                //如果参数没有,继续下一个
                    }
                    parametergroup = GetParameterGroup(parametergroup, param);
                }

                foreach (Parameter param in parameters2)
                {
                    if (param == null)
                    {
                        continue;                //如果参数没有,继续下一个
                    }
                    parametergroup = GetParameterGroup(parametergroup, param);
                }
                ParamTree tree = MakeParamTree(element, parametergroup);
                All_Elem.Add(tree);
            }


            CreateJsonFile(CreateJson(All_Elem));
            retRes = Autodesk.Revit.UI.Result.Succeeded;
            return(retRes);
        }