|  | 
 
| 思路:我想通过调用Revit的API。在主文档中的平面视图下选择一面链接文件中的墙,捕获到墙边界的面的Refernce。然后完成尺寸的标注。
 标注   
 问题:通过API获取到链接文档,然后获取到墙在链接文档中的Refernce后,无法引用这些Refernce在当前文档中实现标注。提示错误如下:
 
 错误   
 
 
 代码如下:
 
 求大神告诉我,我该怎样去引用墙的Refernce。才能完成这个标注呢?复制代码        [Transaction(TransactionMode.Manual)]
        [Regeneration(RegenerationOption.Manual)]
        public class 链接文件墙洞口标注 : IExternalCommand
        {
            public class WallSelectionFilter : ISelectionFilter
            {
                public RevitLinkInstance instance = null;
                public bool AllowElement(Element elem)
                {
                    instance = elem as RevitLinkInstance;
                    if (instance != null)
                    {
                        return true;
                    }
                    return false;
                }
                public bool AllowReference(Reference reference, XYZ position)
                {
                    if (instance == null)
                    {
                        return false;
                    }
                    else
                    {
                        Document linkdocument = instance.GetLinkDocument();
                        Element ent = linkdocument.GetElement(reference.LinkedElementId);
                        return ent is Wall;
                        return false;
                    }
                }
            }
            private void AddCurvesAndSolids(GeometryElement geomElem, CurveArray curves, List<Solid> solids)
            {
                foreach (GeometryObject geomObject in geomElem)
                {
                    Solid solid = geomObject as Solid;
                    if (null != solid)
                    {
                        solids.Add(solid);
                        continue;
                    }
                    //如果GeometryObject 是几何实例,则进行二次遍历  
                    GeometryInstance geomInst = geomObject as GeometryInstance;
                    if (null != geomInst)
                    {
                        AddCurvesAndSolids(geomInst.GetSymbolGeometry(geomInst.Transform), curves, solids);
                    }
                }
            }
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                #region 建立revit连接
                UIDocument uiDoc = commandData.Application.ActiveUIDocument;
                Document doc = uiDoc.Document;
                Autodesk.Revit.ApplicationServices.Application app = doc.Application;
                #endregion
 
                Selection sel = uiDoc.Selection;
                sel.PickElementsByRectangle();
                Reference ref1 = uiDoc.Selection.PickObject(ObjectType.LinkedElement, new WallSelectionFilter(), "Please pick a point on Wall face.");
                RevitLinkInstance linkInstance = doc.GetElement(ref1) as RevitLinkInstance;
                Document lincdoc = linkInstance.GetLinkDocument();
                Element ent = lincdoc.GetElement(ref1.LinkedElementId);
                Wall wallent = null;
                wallent = ent as Wall;
                Transaction trans = new Transaction(doc);
                trans.Start("sta");
                LocationCurve lc = wallent.Location as LocationCurve;
                Curve curve = lc.Curve;
                Line line1 = curve as Line;
                XYZ PTXL = line1.GetEndPoint(0) - line1.GetEndPoint(1);//获取墙的方向向量
                #region 获取lines
                ReferenceArray referenceArray = new ReferenceArray();
                Options opt = doc.Application.Create.NewGeometryOptions();
                opt.ComputeReferences = true;
                opt.View = doc.ActiveView;
                GeometryElement geo = wallent.get_Geometry(opt);
                CurveArray curvearrau = new CurveArray();
                List<Solid> Solids = new List<Solid>();
                AddCurvesAndSolids(geo, curvearrau, Solids);
                jisuanlei JISUAN = new jisuanlei();
                for (int i = 0; i < Solids.Count; i++)
                {
                    Solid solid = Solids[i];
                    FaceArray faces = solid.Faces;
                    foreach (Face fa in faces)
                    {
                        PlanarFace pf = fa as PlanarFace;
                        if (Math.Abs(pf.Normal.AngleTo(PTXL)) < 0.1)
                        {
                            referenceArray.Append(fa.Reference);
                        }
                        if (Math.Abs(pf.Normal.AngleTo(PTXL) - Math.PI) < 0.1)
                        {
                            referenceArray.Append(fa.Reference);
                        }
                    }
                }
                XYZ pt1 = (line1.GetEndPoint(0).X < line1.GetEndPoint(1).X ? line1.GetEndPoint(0) : line1.GetEndPoint(1));
                XYZ pt2 = (line1.GetEndPoint(0).X > line1.GetEndPoint(1).X ? line1.GetEndPoint(0) : line1.GetEndPoint(1));
                Line newLine2JD = Line.CreateBound(pt1, pt2);
                Dimension newDimensionJD = doc.Create.NewDimension(doc.ActiveView, newLine2JD, referenceArray);
                #endregion
                trans.Commit();
                return Result.Succeeded;
            }
        }
 
 | 
 |