【Revit 二次开发】族与族之间的标注基本方法 (可批量)

本文介绍Revit二次开发中如何从族实例提取参照面,通过判断筛选合适的面作为标注参数,实现自动创建尺寸标注。文章深入探讨了通过C#编程,利用Autodesk.Revit API操作族实例,获取其参照面并进行尺寸标注的详细过程。
该文章已生成可运行项目,

Revit二次开发 ,关键在于从族实例内把族的参照面提取出来,判断筛选合格的面,作为标注的参数。

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace SEPD.BuidingElectricity.test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    class DimesionOpration : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document document = commandData.Application.ActiveUIDocument.Document;
            Selection selection = commandData.Application.ActiveUIDocument.Selection;
            Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;

            Autodesk.Revit.DB.View view = document.ActiveView;
            int hashcode = 0;
            ReferenceArray referenceArray = new ReferenceArray();
            //Pick one object from Revit.
            List<Wall> mWalls = new List<Wall>();
            List<FamilyInstance> familyInstances = new List<FamilyInstance>();

            IList<Reference> instanceReferences = new List<Reference>();
            IList<Reference> hasPickOne = selection.PickObjects(ObjectType.Element);
            foreach (Reference reference in hasPickOne)
            {
                familyInstances.Add((document.GetElement(reference)) as FamilyInstance);//转换选中的族们
            }
            bool isHorizen = isDimensionHorizen(familyInstances);//判断设备位置横竖大致情况
            foreach (FamilyInstance familyInstance in familyInstances)
            {
                double rotation = (familyInstance.Location as LocationPoint).Rotation;
                bool phare = Math.Round(rotation / (0.5 * Math.PI) % 2, 4) == 0;//是否180旋转  即 与原有族各参照线平行  平行则true
                //MessageBox.Show(isHorizen.ToString() + "\t" + phare.ToString());
                IList<Reference> refs = isHorizen ^ phare == false ? familyInstance.GetReferences(FamilyInstanceReferenceType.CenterLeftRight)
                    : familyInstance.GetReferences(FamilyInstanceReferenceType.CenterFrontBack);//将实例中特殊的参照平台拿出来

                IList<Reference> refLRs = familyInstance.GetReferences(FamilyInstanceReferenceType.CenterLeftRight);
                IList<Reference> reFBRs = familyInstance.GetReferences(FamilyInstanceReferenceType.CenterFrontBack);

             

                // IList<Reference> refs = familyInstance.GetReferences(FamilyInstanceReferenceType.CenterLeftRight);//将实例中特殊的参照平台拿出来
                instanceReferences.Add(refs.Count == 0 ? null : refs[0]); //将获得的中线放入参照平台面内

                referenceArray.Append(refs.Count == 0 ? null : refs[0]);
            }

            //  AutoCreatDimension(document, selection, instanceReferences);  //该函数可用 但条件苛刻

            Element element = document.GetElement(hasPickOne.ElementAt(0));
            XYZ elementXyz = (element.Location as LocationPoint).Point;
            // Line line = (element.Location as LocationCurve).Curve as Line;

            //尺寸线定位
            double distanceNewLine = 2;
            Line line = Line.CreateBound(elementXyz, new XYZ(elementXyz.X + distanceNewLine, elementXyz.Y, elementXyz.Z));
            line = isHorizen == true ?
                  Line.CreateBound(elementXyz, new XYZ(elementXyz.X, elementXyz.Y + distanceNewLine, elementXyz.Z))
                : Line.CreateBound(elementXyz, new XYZ(elementXyz.X + distanceNewLine, elementXyz.Y, elementXyz.Z));
            XYZ selectionPoint = selection.PickPoint();
            selectionPoint = new XYZ(elementXyz.X + distanceNewLine, elementXyz.Y + 50, elementXyz.Z);
            selectionPoint = isHorizen == true ?
                    new XYZ(elementXyz.X + 50, elementXyz.Y + distanceNewLine, elementXyz.Z)
                : new XYZ(elementXyz.X + distanceNewLine, elementXyz.Y + 50, elementXyz.Z);
            XYZ projectPoint = line.Project(selectionPoint).XYZPoint;
            Line newLine = Line.CreateBound(selectionPoint, projectPoint);

            Transaction transaction = new Transaction(document, "添加标注");
            transaction.Start();
            //调用创建尺寸的方法创建
            Dimension autoDimension = document.Create.NewDimension(view, newLine, referenceArray);
            transaction.Commit();

            
            // AutoCreatDimension(document,selection, hasPickOne);  //该函数可用 但条件苛刻

            //throw new NotImplementedException();

            return Result.Succeeded;
        }
         
        /// <summary>
        /// 判断设备位置大致情况 是否为水平 还是垂直
        /// </summary>
        /// <param name="familyInstances"></param>
        /// <returns></returns>
        public bool isDimensionHorizen(List<FamilyInstance> familyInstances)
        {
            if (
                Math.Abs
                (
                   familyInstances.OrderBy(f => (f.Location as LocationPoint).Point.X).Select(f => (f.Location as LocationPoint).Point.X).First()
                 - familyInstances.OrderByDescending(f => (f.Location as LocationPoint).Point.X).Select(f => (f.Location as LocationPoint).Point.X).First()
                )
                >
                Math.Abs
                (
                      familyInstances.OrderBy(f => (f.Location as LocationPoint).Point.Y).Select(f => (f.Location as LocationPoint).Point.Y).First()
                    - familyInstances.OrderByDescending(f => (f.Location as LocationPoint).Point.Y).Select(f => (f.Location as LocationPoint).Point.Y).First()
                 )
                )
            {
                return true;
            }
            return false;
 
        }


    }
}

 

本文章已经生成可运行项目
左旋(Left Rotation)是一种用于平衡二叉搜索树(如红黑树)的重要操作,其核心目的是通过调整节点的位置来维持树的平衡,从而保证树的操作(如查找、插入、删除)具有较好的时间复杂度。 ### 具体步骤 设要进行左旋操作的节点为 `x`,`x` 的右子节点为 `y`。 1. **处理 `y` 的左子节点**:将 `y` 的左子节点(若存在)挂接到 `x` 的右子节点位置。即 `x->right = y->left`。若 `y` 的左子节点不为空,还需更新其父节点为 `x`,即 `y->left->par = x`。 2. **更新 `y` 的父节点**:将 `y` 的父节点更新为 `x` 的父节点,即 `y->par = x->par`。 3. **更新 `x` 父节点的子节点指针**: - 若 `x` 是根节点,那么 `y` 成为新的根节点,即 `T->root = y`。 - 若 `x` 是其父节点的左子节点,那么将 `x` 父节点的左子节点指针指向 `y`,即 `x->par->left = y`。 - 若 `x` 是其父节点的右子节点,那么将 `x` 父节点的右子节点指针指向 `y`,即 `x->par->right = y`。 4. **更新 `y` 的左子节点和 `x` 的父节点**:将 `y` 的左子节点设为 `x`,即 `y->left = x`;同时将 `x` 的父节点设为 `y`,即 `x->par = y`。 ### 代码示例 ```python class Node: def __init__(self, key): self.key = key self.left = None self.right = None self.par = None class RBTree: def __init__(self): self.nil = Node(None) self.root = self.nil def left_rotate(self, x): y = x.right x.right = y.left if y.left != self.nil: y.left.par = x y.par = x.par if x.par == self.nil: self.root = y elif x == x.par.left: x.par.left = y else: x.par.right = y y.left = x x.par = y ``` ### 作用 在红黑树等自平衡二叉搜索树中,插入和删除操作可能会破坏树的平衡性质(如红黑树的颜色规则和高度平衡)。左旋操作可以右旋操作配合使用,通过调整节点的位置和颜色,恢复树的平衡,保证树的高度始终保持在 $O(\log n)$ 级别,从而使得树的各种操作(如查找、插入、删除)的时间复杂度都能保持在 $O(\log n)$。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值