Dimension-revit二次开发标注-墙体的标注

namespace DimensionDemo
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        [Obsolete]
        public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
            Document doc = uiDoc.Document;
            Autodesk.Revit.DB.View view = doc.ActiveView;
            ViewType viewType = view.ViewType;
             Reference reference = uiDoc.Selection.PickObject(ObjectType.Element, "请选择墙体");
            WallIn wallIn = new WallIn();
            Line line = wallIn.getLine(reference, doc);
            ReferenceArray SS= wallIn.getReference(reference, doc);
            Transaction transaction = new Transaction(doc, "VVVV");
            transaction.Start();
             Dimension dimension = doc.Create.NewDimension(doc.ActiveView, line, SS);
            ElementId cc = dimension.GetTypeId();
            Element element = doc.GetElement(cc);
            DimensionType dimensionType = element as DimensionType;
            Parameter parameter= dimensionType.LookupParameter("颜色");
            //这里的颜色是我随便自己给的。默认是黑色的。
            Color color = new Color((byte)125, (byte)125, (byte)125);
            int bb = toColor(color);
            parameter.Set(bb);
            //OverrideGraphicSettings ogs = new OverrideGraphicSettings();
           
            //ogs.SetProjectionLineColor(color);
            //Autodesk.Revit.DB.View view2 = doc.ActiveView;
            //view.SetElementOverrides(dimension.Id, ogs);           
            transaction.Commit();

            return Result.Succeeded;
        }
   //这个我也不是很懂,是一个行业大佬给我的,我就直接用了,但是可以修改族类型参数中的颜色
        public static int  toColor(Color color)
        {
            int rgb = 0;
            rgb += color.Red * (int)Math.Pow(256, 0);
            rgb += color.Green * (int)Math.Pow(256, 0);
            rgb += color.Blue * (int)Math.Pow(256, 0);
            return rgb;
        }
    
    }
}





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

namespace DimensionDemo
{
  public    class WallIn
    {
        public  Line  getLine(Reference reference,Document doc)
        {
            Line line=null ;
            Element element = doc.GetElement(reference);
            Wall wall = element as Wall;
            if (wall!=null)
            {
                Location location = wall.Location;
                LocationCurve locationCurve = location as LocationCurve;
                if (locationCurve!=null)
                {
                    XYZ p1 = locationCurve.Curve.GetEndPoint(0);
                    XYZ p2 = locationCurve.Curve.GetEndPoint(1);
                    XYZ wallXL = p1.Subtract(p2);//获得墙体的向量
                    XYZ wallunitXL = wallXL.Normalize();//获得墙体向量的单位向量
                    XYZ shituXL = doc.ActiveView.ViewDirection;//获得当前视图的法向量,也就是(0,0,1)因为这个试图现阶段是平面的
//根据三维图形的几何属性,两个向量的乘积是得到的向量是垂直于这个面的法向量
  //根据这个思路,墙体的向量 和视图的法向量,得到的是垂直于墙体的法向量,因此
                    XYZ wallnorXL = shituXL.CrossProduct(wallXL);//得到垂直于墙体的法向量
                     XYZ wallnorUnitXL=wallnorXL.Normalize();//得到垂直于墙体的法向量的单位向量
                    XYZ wallnorUnitxl3 = wallnorUnitXL.Multiply(3);//获得三倍距离稍微远点,距离墙体的距离,这个数据越大,标注距离墙体越远。
                    p1 = p1.Add(wallnorUnitxl3);
                    p2 = p2.Add(wallnorUnitxl3);
                    line = Line.CreateBound(p1, p2);                
                }
            }
            return line;
        }


        public ReferenceArray getReference(Reference reference,Document doc)
        {
            ReferenceArray arrRefs = new ReferenceArray();

            Element element = doc.GetElement(reference);
            Wall wall = element as Wall;
            LocationCurve locationCurve = wall.Location as LocationCurve;
            XYZ p1 = locationCurve.Curve.GetEndPoint(0);
            XYZ p2 = locationCurve.Curve.GetEndPoint(1);
            XYZ wallXL = p1.Subtract(p2);//获得墙体的向量
            XYZ shituXL = doc.ActiveView.ViewDirection;
            XYZ wallnorXL = shituXL.CrossProduct(wallXL);
            Options option = new Options();
            option.ComputeReferences = true;
            GeometryElement geometryelements = doc.GetElement(reference).get_Geometry(option);
            foreach (GeometryObject geoObject in geometryelements)
            {
                Solid solid = geoObject as Solid;
                if (solid!=null)
                {
                    FaceArray faces = solid.Faces;
                    foreach (PlanarFace item in faces)
                    {
                       XYZ xYZ= item.FaceNormal;
                        XYZ c = xYZ.CrossProduct(wallXL);
                        if (c.IsZeroLength())
                        {
                            arrRefs.Append(item.Reference);
                        }
                    }
                   
                }

            }
            if (arrRefs.Size != 2)
            {
                string  CC = arrRefs.Size.ToString();
                Autodesk.Revit.UI.TaskDialog.Show(CC, "Couldn’t find enough reference for creating dimension");
              
            }

            return arrRefs;
        }
    }
}

在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页