1. 程式人生 > >Revit二次開發點滴記錄

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());