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

 

根据元素类型、名称查找元素

Element findElement(Document _rvtDoc, Type targetType, string targetName)
    {
        // get the elements of the given type
        //
        FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
        collector.WherePasses(new ElementClassFilter(targetType));

        // parse the collection for the given name
        // using LINQ query here. 
        // 
        var targetElems = from element in collector where element.Name.Equals(targetName) select element;
        List<Element> elems = targetElems.ToList<Element>();

        if (elems.Count > 0)
        {  // we should have only one with the given name. 
            return elems[0];
        }

        // cannot find it.
        return null;
    }

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值