Revit二次开发点滴记录

  1. 线的方向问题

XYZ xyz=Line.Direction;此属性返回的是normalized之后的向量。示例如下

 Line l1 = Line.CreateBound(new XYZ(0,5,0),new XYZ(0,0,0));
 Line l2 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 5, 0));
 Line l3 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(5, 5, 0));
 XYZ x1 = l1.Direction;//输出(0.000,-1.000,0.000)
 XYZ x2 = l2.Direction;//输出(0.000,1.000,0.000)
 XYZ x3 = l3.Direction;//输出(0.707,0.707,0.000)

2. 模型更新

如果要在一个事务中执行创建楼板、开洞两个操作命令,在创建完楼板后要执行一次doc.regenerate();再执行开洞命令,最后提交事务。

FamilyInstance instance = doc.Create.NewFamilyInstance(new XYZ(15, 20, 0), familySymbol, StructuralType.NonStructural);
FamilyInstance instance2 = doc.Create.NewFamilyInstance(new XYZ(25, 30, 0), familySymbol, StructuralType.NonStructural);
// faster to create multiple instances without calling Regenerate after each one

LocationPoint point = instance.Location as LocationPoint;
// this data is incorrect because the new geometry has not yet been regenerated

doc.Regenerate();
point = instance.Location as LocationPoint;
// now it is correct

3.找到与选定元素相交的实例图元

Reference reference = uidoc.Selection.PickObject(ObjectType.Element, "Select element that will be checked for intersection with all family instances");
Element element = doc.GetElement(reference);
GeometryElement geomElement = element.get_Geometry(new Options());
Solid solid = null;
foreach (GeometryObject geomObj in geomElement)
{
    solid = geomObj as Solid;
    if (solid != null) break;
}

FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilyInstance));
collector.WherePasses(new ElementIntersectsSolidFilter(solid)); // Apply intersection filter to find matches

TaskDialog.Show("Revit", collector.Count() + " family instances intersect with the selected element (" + element.Category.Name + " id:" + element.Id.ToString() + ")");

 4.选择

选择一个元素(Minimum PickObject)

Reference r = _uiDoc.Selection.PickObject(ObjectType.Element, "Select one element");           
Element e = _uiDoc.Document.GetElement(r);

 选择多个元素(Minimum PickObjects )

            IList<Reference> refs = _uiDoc.Selection.PickObjects(ObjectType.Element, "Select multiple elemens");            
            IList<Element> elems = new List<Element>();
            foreach (Reference r in refs)
            {
                elems.Add(_uiDoc.Document.GetElement(r));
            }

矩形选择(Minimum PickElementByRectangle ) 

IList<Element> elems = _uiDoc.Selection.PickElementsByRectangle("Select by rectangle");

选择点(Minimum PickPoint) 

XYZ pt = _uiDoc.Selection.PickPoint("Pick a point");

选择面 

Reference r = _uiDoc.Selection.PickObject(ObjectType.Face, "Select a face");
Element e = _uiDoc.Document.GetElement(r);            
Face oFace = e.GetGeometryObjectFromReference(r) as Face;

选择边

Reference r = _uiDoc.Selection.PickObject(ObjectType.Edge, "Select an edge");
Element e = _uiDoc.Document.GetElement(r);            
Edge oEdge = e.GetGeometryObjectFromReference(r) as Edge;

选择元素上某点

Reference r = _uiDoc.Selection.PickObject(ObjectType.PointOnElement,"Select a point on element");
XYZ pt = r.GlobalPoint;

 5.平面过滤器

class SelectionFilterPlanarFace : ISelectionFilter
    {
        Document _doc;

        public SelectionFilterPlanarFace(Document doc)
        {
            _doc = doc;
        }

        public bool AllowElement(Element e)
        {
            return true;
        }

        public bool AllowReference(Reference r, XYZ position)
        {         
            ElementId id = r.ElementId;           
            Element e = _doc.GetElement(id);
            if (e.GetGeometryObjectFromReference(r) is PlanarFace)
            {
                // Do additional checking here if needed

                return true;
            }
            return false;
        }
    }

平面选择

 public void PickPlanarFace()
        {            
            Document doc = _uiDoc.Document;
            SelectionFilterPlanarFace selFilterPlanarFace = new SelectionFilterPlanarFace(doc);
            Reference r = _uiDoc.Selection.PickObject(ObjectType.Face, selFilterPlanarFace, "Select a planar face");
            Element e = doc.GetElement(r);            
            Face oFace = e.GetGeometryObjectFromReference(r) as Face; 
            string msg = (null == oFace)
              ? "No face picked."
              : "You picked a face on element " + e.Id.ToString();
            TaskDialog.Show("PickPlanarFace", msg);
        }

去除一个字符串中的特定字符

 public string MakeFileName(string name)
        {
            string invalid = "\\/:*?\"<>|";
            foreach (char ch in invalid.ToCharArray())
            {
                name = name.Replace(ch.ToString(),"");
            }
            return name;
        }

ICollection转List 

FilteredElementCollector coll = new FilteredElementCollector(doc);
List<ElementId> floor = new List<ElementId>();
floor.AddRange(coll.OfClass(typeof(Floor)).ToElementIds());

猜你喜欢

转载自blog.csdn.net/weixin_42479664/article/details/82562686