Esempio n. 1
0
        /// <summary>
        /// The function set value to rotation of the beams and braces
        /// and rotate columns.
        /// </summary>
        public void RotateElement()
        {
            Transaction transaction = new Transaction(m_revit.ActiveUIDocument.Document, "RotateElement");

            transaction.Start();
            try
            {
                ElementSet selection = new ElementSet();
                foreach (ElementId elementId in m_revit.ActiveUIDocument.Selection.GetElementIds())
                {
                    selection.Insert(m_revit.ActiveUIDocument.Document.GetElement(elementId));
                }
                foreach (Autodesk.Revit.DB.Element e in selection)
                {
                    FamilyInstance familyComponent = e as FamilyInstance;
                    if (familyComponent == null)
                    {
                        //is not a familyInstance
                        continue;
                    }
                    // if be familyInstance,judge the types of familyInstance
                    if (StructuralType.Beam == familyComponent.StructuralType ||
                        StructuralType.Brace == familyComponent.StructuralType)
                    {
                        // selection is a beam or Brace
                        ParameterSetIterator paraIterator = familyComponent.Parameters.ForwardIterator();
                        paraIterator.Reset();

                        while (paraIterator.MoveNext())
                        {
                            object    para            = paraIterator.Current;
                            Parameter objectAttribute = para as Parameter;
                            //set generic property named "Cross-Section Rotation"
                            if (objectAttribute.Definition.Name.Equals(AngleDefinitionName))
                            {
                                Double originDegree = objectAttribute.AsDouble();
                                double rotateDegree = m_receiveRotationTextBox * Math.PI / 180;
                                if (!m_isAbsoluteChecked)
                                {
                                    // absolute rotation
                                    rotateDegree += originDegree;
                                }
                                objectAttribute.Set(rotateDegree);
                                // relative rotation
                            }
                        }
                    }
                    else if (StructuralType.Column == familyComponent.StructuralType)
                    {
                        // rotate a column
                        Autodesk.Revit.DB.Location columnLocation = familyComponent.Location;
                        // get the location object
                        LocationPoint         pointLocation = columnLocation as LocationPoint;
                        Autodesk.Revit.DB.XYZ insertPoint   = pointLocation.Point;
                        // get the location point
                        double temp = pointLocation.Rotation;
                        //existing rotation
                        Autodesk.Revit.DB.XYZ directionPoint = new Autodesk.Revit.DB.XYZ(0, 0, 1);
                        // define the vector of axis
                        Line   rotateAxis   = Line.CreateUnbound(insertPoint, directionPoint);
                        double rotateDegree = m_receiveRotationTextBox * Math.PI / 180;
                        // rotate column by rotate method
                        if (m_isAbsoluteChecked)
                        {
                            rotateDegree -= temp;
                        }
                        bool rotateResult = pointLocation.Rotate(rotateAxis, rotateDegree);
                        if (rotateResult == false)
                        {
                            TaskDialog.Show("Revit", "Rotate Failed.");
                        }
                    }
                }

                transaction.Commit();
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Revit", "Rotate failed! " + ex.Message);
                transaction.RollBack();
            }
        }
        /// <summary>
        /// Run this sample
        /// </summary>
        public void Run()
        {
            RotateFramingObjectsForm displayForm = new RotateFramingObjectsForm(this);
            ElementSet selection = new ElementSet();
            ICollection <ElementId> sel;

            foreach (ElementId elementid in m_doc.Selection.GetElementIds())
            {
                selection.Insert(m_doc.Document.GetElement(elementid));
            }

            bool isSingle            = true;              //selection is single object
            bool isAllFamilyInstance = true;              //all is not familyInstance

            // There must be beams, braces or columns selected
            if (selection.IsEmpty)
            {
                // nothing selected
                MessageBox.Show("Please select FamilyInstance.(such as column)", "RotateFramingObjects");
                return;
            }
            else if (1 != selection.Size)
            {
                isSingle = false;
                try
                {
                    if (DialogResult.OK != displayForm.ShowDialog())
                    {
                        return;
                    }
                }
                catch (Exception)
                {
                    return;
                }
                //	return IExternalCommand.Result.Succeeded;
                // more that one object selected
            }

            // if the selection are familyInstance, try to get their existing rotation
            foreach (Autodesk.Revit.DB.Element e in selection)
            {
                FamilyInstance familyComponent = e as FamilyInstance;
                if (familyComponent != null)
                {
                    if (Autodesk.Revit.DB.Structure.StructuralType.Beam == familyComponent.StructuralType ||
                        Autodesk.Revit.DB.Structure.StructuralType.Brace == familyComponent.StructuralType)
                    {
                        // selection is a beam or brace
                        string returnValue = this.FindParameter("Angle", familyComponent);
                        displayForm.rotationTextBox.Text = returnValue.ToString();
                    }
                    else if (Autodesk.Revit.DB.Structure.StructuralType.Column == familyComponent.StructuralType)
                    {
                        // selection is a column
                        Autodesk.Revit.DB.Location      columnLocation = familyComponent.Location;
                        Autodesk.Revit.DB.LocationPoint pointLocation  = columnLocation as Autodesk.Revit.DB.LocationPoint;
                        double temp   = pointLocation.Rotation;
                        string output = (Math.Round(temp * 180 / (Math.PI), 3)).ToString();
                        displayForm.rotationTextBox.Text = output;
                    }
                    else
                    {
                        // other familyInstance can not be rotated
                        MessageBox.Show("Can not deal with it.", "RotateFramingObjects");
                        sel = m_doc.Selection.GetElementIds();
                        sel.Add(familyComponent.Id);
                        m_doc.Selection.SetElementIds(sel);
                        return;
                    }
                }
                else
                {
                    if (isSingle)
                    {
                        MessageBox.Show("It is a Non-FamilyInstance.", "RotateFramingObjects");
                        sel = m_doc.Selection.GetElementIds();
                        sel.Add(e.Id);
                        m_doc.Selection.SetElementIds(sel);
                        return;
                    }
                    // there is some objects is not familyInstance
                    //MessageBox.Show("There is Non-FamilyInstance.", "RotateFramingObjects");
                    sel = m_doc.Selection.GetElementIds();
                    sel.Add(e.Id);
                    m_doc.Selection.SetElementIds(sel);
                    isAllFamilyInstance = false;
                }
            }


            if (isSingle)
            {
                try
                {
                    if (DialogResult.OK != displayForm.ShowDialog())
                    {
                        return;
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }

            if (isAllFamilyInstance)
            {
                return;
            }
            else
            {
                //output error information
                return;
            }
        }
Esempio n. 3
0
        public static List <IndependentTag> TagElements(this View view, ElementId elementId_TagType, IEnumerable <ElementId> elementIds, bool addLeader = false, TagOrientation tagOrientation = TagOrientation.Horizontal, bool allowDuplicates = false)
        {
            if (view == null || elementId_TagType == null || elementId_TagType == ElementId.InvalidElementId)
            {
                return(null);
            }

            Document document = view.Document;

            FamilySymbol familySymbol = document.GetElement(elementId_TagType) as FamilySymbol;

            if (familySymbol == null)
            {
                return(null);
            }

            BuiltInCategory builtInCategory_Tag = (BuiltInCategory)familySymbol.Category.Id.IntegerValue;

            IEnumerable <ElementId> elementIds_View = new FilteredElementCollector(document, view.Id).ToElementIds();

            if (elementIds_View == null)
            {
                return(null);
            }

            List <IndependentTag> result = new List <IndependentTag>();

            if (elementIds_View.Count() == 0)
            {
                return(result);
            }

            foreach (ElementId elementId in elementIds_View)
            {
                if (elementId == null || elementId == ElementId.InvalidElementId)
                {
                    continue;
                }

                if (!elementIds.Contains(elementId))
                {
                    continue;
                }

                Element element = document.GetElement(elementId);
                if (element == null)
                {
                    continue;
                }

                if (!allowDuplicates)
                {
#if Revit2017
                    IList <ElementId> elementIds_Tags = null;
#else
                    IList <ElementId> elementIds_Tags = element.GetDependentElements(new LogicalAndFilter(new ElementClassFilter(typeof(IndependentTag)), new ElementOwnerViewFilter(view.Id)));
#endif

                    if (elementIds_Tags != null && elementIds_Tags.Count != 0)
                    {
                        ElementId elementId_Tag = elementIds_Tags.ToList().Find(x => document.GetElement(x).GetTypeId() == elementId_TagType);
                        if (elementId_Tag != null)
                        {
                            continue;
                        }
                    }
                }

                if (!builtInCategory_Tag.IsValidTagCategory((BuiltInCategory)element.Category.Id.IntegerValue))
                {
                    continue;
                }

                Autodesk.Revit.DB.Location location = element.Location;
                if (location == null)
                {
                    continue;
                }

                XYZ xyz = null;
                if (location is LocationCurve)
                {
                    LocationCurve locationCurve = (LocationCurve)location;
                    Curve         curve         = locationCurve.Curve;
                    if (curve == null)
                    {
                        continue;
                    }

                    XYZ xyz_1 = curve.GetEndPoint(0);
                    XYZ xyz_2 = curve.GetEndPoint(1);
                    xyz = new XYZ((xyz_1.X + xyz_2.X) / 2, (xyz_1.Y + xyz_2.Y) / 2, (xyz_1.Z + xyz_2.Z) / 2);
                }
                else if (location is LocationPoint)
                {
                    xyz = ((LocationPoint)location).Point;
                }

                if (xyz == null)
                {
                    continue;
                }

#if Revit2017
                IndependentTag independentTag = document.Create.NewTag(view, element, addLeader, TagMode.TM_ADDBY_CATEGORY, tagOrientation, xyz);
                independentTag?.ChangeTypeId(elementId_TagType);
#elif Revit2018
                Reference      reference      = new Reference(element);
                IndependentTag independentTag = IndependentTag.Create(document, view.Id, reference, addLeader, TagMode.TM_ADDBY_CATEGORY, tagOrientation, xyz);
                independentTag?.ChangeTypeId(elementId_TagType);
#else
                Reference      reference      = new Reference(element);
                IndependentTag independentTag = IndependentTag.Create(document, elementId_TagType, view.Id, reference, addLeader, tagOrientation, xyz);
#endif

                if (independentTag != null)
                {
                    result.Add(independentTag);
                }
            }

            return(result);
        }
        /// <summary>
        /// The function set value to rotation of the beams and braces
        /// and rotate columns.
        /// </summary>
        public void RotateElement()
        {
            ElementSet selection = new ElementSet();

            foreach (ElementId elementid in m_doc.Selection.GetElementIds())
            {
                selection.Insert(m_doc.Document.GetElement(elementid));
            }
            foreach (Autodesk.Revit.DB.Element e in selection)
            {
                FamilyInstance familyComponent = e as FamilyInstance;
                if (familyComponent == null)
                {
                    //is not a familyInstance
                    continue;
                }
                // if be familyInstance,judge the types of familyInstance
                if (Autodesk.Revit.DB.Structure.StructuralType.Beam == familyComponent.StructuralType ||
                    Autodesk.Revit.DB.Structure.StructuralType.Brace == familyComponent.StructuralType)
                {
                    // selection is a beam or Brace
                    ParameterSetIterator j = familyComponent.Parameters.ForwardIterator();
                    j.Reset();

                    bool jMoreAttribute = j.MoveNext();
                    while (jMoreAttribute)
                    {
                        object    a = j.Current;
                        Parameter objectAttribute = a as Parameter;
                        //set generic property named ¡°Angle¡±
                        int p = objectAttribute.Definition.Name.CompareTo("Angle");
                        if (0 == p)
                        {
                            Double temp         = objectAttribute.AsDouble();
                            double rotateDegree = m_receiveRotationTextBox * Math.PI / 180;
                            if (!m_isAbsoluteChecked)
                            {
                                // absolute rotation
                                rotateDegree += temp;
                            }
                            objectAttribute.Set(rotateDegree);
                            // relative rotation
                        }

                        jMoreAttribute = j.MoveNext();
                    }
                }
                else if (Autodesk.Revit.DB.Structure.StructuralType.Column == familyComponent.StructuralType)
                {
                    // rotate a column
                    Autodesk.Revit.DB.Location columnLocation = familyComponent.Location;
                    // get the location object
                    Autodesk.Revit.DB.LocationPoint pointLocation = columnLocation as Autodesk.Revit.DB.LocationPoint;
                    Autodesk.Revit.DB.XYZ           insertPoint   = pointLocation.Point;
                    // get the location point
                    double temp = pointLocation.Rotation;
                    //existing rotation
                    XYZ directionPoint = m_doc.Document.Application.Create.NewXYZ(0, 0, 1);
                    // define the vector of axis
                    Autodesk.Revit.DB.Line rotateAxis = Line.CreateBound(insertPoint, directionPoint);
                    double rotateDegree = m_receiveRotationTextBox * Math.PI / 180;
                    // rotate column by rotate method
                    if (m_isAbsoluteChecked)
                    {
                        rotateDegree -= temp;
                    }
                    bool rotateResult = pointLocation.Rotate(rotateAxis, rotateDegree);
                    if (rotateResult == false)
                    {
                        MessageBox.Show("Rotate Failed.");
                    }
                }
            }
        }
Esempio n. 5
0
        public static List <SpaceTag> TagSpaces(this View view, ElementId elementId_SpaceTagType, bool allowDuplicates = false)
        {
            if (view == null || elementId_SpaceTagType == null || elementId_SpaceTagType == ElementId.InvalidElementId)
            {
                return(null);
            }

            Document document = view.Document;

            SpaceTagType spaceTagType = document.GetElement(elementId_SpaceTagType) as SpaceTagType;

            if (spaceTagType == null)
            {
                return(null);
            }

            IEnumerable <Space> spaces = new FilteredElementCollector(document, view.Id).OfCategory(BuiltInCategory.OST_MEPSpaces).Cast <Space>();

            if (spaces == null)
            {
                return(null);
            }

            List <SpaceTag> result = new List <SpaceTag>();

            if (spaces.Count() == 0)
            {
                return(result);
            }

            foreach (Space space in spaces)
            {
                if (space == null || !space.IsValidObject)
                {
                    continue;
                }

                if (!allowDuplicates)
                {
#if Revit2017
                    IList <ElementId> elementIds = null;
#else
                    IList <ElementId> elementIds = space.GetDependentElements(new LogicalAndFilter(new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaceTags), new ElementOwnerViewFilter(view.Id)));
#endif
                    if (elementIds != null)
                    {
                        ElementId elementId_Temp = elementIds.ToList().Find(x => document.GetElement(x)?.GetTypeId() == elementId_SpaceTagType);
                        if (elementId_Temp != null)
                        {
                            continue;
                        }
                    }
                }

                Autodesk.Revit.DB.Location location = space.Location;
                if (location == null)
                {
                    continue;
                }

                XYZ xyz = null;
                if (location is LocationPoint)
                {
                    xyz = ((LocationPoint)location).Point;
                }

                if (xyz == null)
                {
                    continue;
                }

                SpaceTag spaceTag = document.Create.NewSpaceTag(space, new UV(xyz.X, xyz.Y), view);
                if (spaceTag == null)
                {
                    continue;
                }

                if (spaceTag.GetTypeId() != elementId_SpaceTagType)
                {
                    spaceTag.SpaceTagType = spaceTagType;
                }

                result.Add(spaceTag);
            }

            return(result);
        }