翻模交互之CAD读取

参考方法

如何读取一条cad

public class Class1 : IExternalCommand
    {
    
    
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
    
    
            Document revitDoc = commandData.Application.ActiveUIDocument.Document;  //取得文档
            Application revitApp = commandData.Application.Application;             //取得应用程序
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;

            Selection sel = uiDoc.Selection;
            //Reference re = sel.PickObject(ObjectType.Element);
            Reference re = sel.PickObject(ObjectType.PointOnElement);

            ImportInstance dwg = revitDoc.GetElement(re) as ImportInstance;
            var geoObj = (dwg as Element).GetGeometryObjectFromReference(re);
            TaskDialog.Show("revit", geoObj.GetType().ToString());
            //Transform transf = null;
            XYZ p1 = null;
            XYZ p2 = null;



                    if (geoObj is Line)
                    {
    
    
                        Line l = geoObj as Line;
                        p1 = l.GetEndPoint(0);
                        p2 = l.GetEndPoint(1);
                        TaskDialog.Show("revit", p2.X.ToString());
                    }

            return Result.Succeeded;
        }
    }
}

如何使用Teigha获取CAD中文字信息

如何使用teigha

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CreatBridgeForRevit2018.ElementsSelect;
using CreatBridgeForRevit2018.Filter;
using Teigha.Runtime;
using Teigha.DatabaseServices;
using System.IO;
using System.Collections;
using Teigha.Geometry;

namespace CreatBridgeForRevit2018.ReadCAD
{
    
    
    class ReadCADUtils
    {
    
    
        /// <summary>
        /// 取得链接cad的路径
        /// </summary>
        /// <param name="cadLinkTypeID"></param>
        /// <param name="revitDoc"></param>
        /// <returns></returns>
        public string GetCADPath(ElementId cadLinkTypeID,Document revitDoc)
        {
    
    
            CADLinkType cadLinkType = revitDoc.GetElement(cadLinkTypeID) as CADLinkType;
            return ModelPathUtils.ConvertModelPathToUserVisiblePath(cadLinkType.GetExternalFileReference().GetAbsolutePath());
        }

        /// <summary>
        /// 取得CAD的文字信息
        /// </summary>
        /// <param name="dwgFile"></param>
        /// <returns></returns>
        public List<CADTextModel> GetCADTextInfo(string dwgFile)
        {
    
    
            List<CADTextModel> listCADModels = new List<CADTextModel>();            
            using (new Services())
            {
    
    
                using (Database database = new Database(false, false))
                {
    
    
                    database.ReadDwgFile(dwgFile, FileShare.Read, true, "");
                    using (var trans = database.TransactionManager.StartTransaction())
                    {
    
    
                        using (BlockTable table = (BlockTable)database.BlockTableId.GetObject(OpenMode.ForRead))
                        {
    
    
                            using (SymbolTableEnumerator enumerator = table.GetEnumerator())
                            {
    
    
                                StringBuilder sb = new StringBuilder();
                                while (enumerator.MoveNext())
                                {
    
    
                                    using (BlockTableRecord record = (BlockTableRecord)enumerator.Current.GetObject(OpenMode.ForRead))
                                    {
    
    

                                        foreach (ObjectId id in record)
                                        {
    
    
                                            Entity entity = (Entity)id.GetObject(OpenMode.ForRead, false, false);                                            
                                            CADTextModel model = new CADTextModel();
                                            switch (entity.GetRXClass().Name)
                                            {
    
                                                    
                                                case "AcDbText":
                                                    Teigha.DatabaseServices.DBText text = (Teigha.DatabaseServices.DBText)entity;
                                                    model.Location = ConverCADPointToRevitPoint(text.Position);
                                                    model.Text = text.TextString;
                                                    model.Angel = text.Rotation;
                                                    model.Layer = text.Layer;
                                                    listCADModels.Add(model);
                                                    break;
                                                case "AcDbMText":
                                                    Teigha.DatabaseServices.MText mText = (Teigha.DatabaseServices.MText)entity;
                                                    model.Location = ConverCADPointToRevitPoint(mText.Location);
                                                    model.Text = mText.Text;
                                                    model.Angel = mText.Rotation;
                                                    model.Layer = mText.Layer;
                                                    listCADModels.Add(model);
                                                    break;
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
            return listCADModels;
        }

        /// <summary>
        /// 取得cad的图层名称
        /// </summary>
        /// <param name="dwgFile"></param>
        /// <returns></returns>
        public IList<string> GetLayerName(string dwgFile)
        {
    
    
            IList<string> cadLayerNames = new List<string>();
            using (new Services())
            {
    
    
                using (Database database = new Database(false, false))
                {
    
    
                    database.ReadDwgFile(dwgFile, FileShare.Read, true, "");
                    using (var trans = database.TransactionManager.StartTransaction())
                    {
    
    
                        using (LayerTable lt = (LayerTable)trans.GetObject(database.LayerTableId, OpenMode.ForRead))
                        {
    
    
                            foreach (ObjectId id in lt)
                            {
    
    
                                LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead);
                                cadLayerNames.Add(ltr.Name);
                            }
                        }
                        trans.Commit();
                    }
                }
            }                    
            return cadLayerNames;
        }

        /// <summary>
        /// 取得CAD里的线,包括直线、多段线、圆曲线
        /// </summary>
        /// <param name="dwgFile"></param>
        /// <returns></returns>
        public List<CADGeometryModel> GetCADCurves(string dwgFile)
        {
    
    
            List<CADGeometryModel> listCADModels = new List<CADGeometryModel>();            
            using (new Services())
            {
    
    
                using (Database database = new Database(false, false))
                {
    
    
                    database.ReadDwgFile(dwgFile, FileShare.Read, true, "");
                    using (var trans = database.TransactionManager.StartTransaction())
                    {
    
    
                        using (BlockTable table = (BlockTable)database.BlockTableId.GetObject(OpenMode.ForRead))
                        {
    
    
                            using (SymbolTableEnumerator enumerator = table.GetEnumerator())
                            {
    
    
                                StringBuilder sb = new StringBuilder();
                                while (enumerator.MoveNext())
                                {
    
    
                                    using (BlockTableRecord record = (BlockTableRecord)enumerator.Current.GetObject(OpenMode.ForRead))
                                    {
    
    

                                        foreach (ObjectId id in record)
                                        {
    
    
                                            Entity entity = (Entity)id.GetObject(OpenMode.ForRead, false, false);
                                            CADGeometryModel geoModel = new CADGeometryModel();
                                            switch (entity.GetRXClass().Name)
                                            {
    
    
                                                case "AcDbPolyline":
                                                    Teigha.DatabaseServices.Polyline pl = (Teigha.DatabaseServices.Polyline)entity;
                                                    IList<XYZ> listPoints = new List<XYZ>();
                                                    for (int i = 0; i < pl.NumberOfVertices; i++)
                                                    {
    
    
                                                        listPoints.Add(new XYZ(MillimetersToUnits(pl.GetPoint2dAt(i).X), MillimetersToUnits(pl.GetPoint2dAt(i).Y), 0));
                                                    }
                                                    PolyLine polyLine = PolyLine.Create(listPoints);
                                                    listCADModels.Add(new CADGeometryModel() {
    
    CadPolyLine= polyLine,LayerName=pl.Layer });
                                                    break;
                                                case "AcDbLine":
                                                    Teigha.DatabaseServices.Line line = (Teigha.DatabaseServices.Line)entity;
                                                    Autodesk.Revit.DB.Line revitLine = Autodesk.Revit.DB.Line.CreateBound(ConverCADPointToRevitPoint(line.StartPoint), ConverCADPointToRevitPoint(line.EndPoint));
                                                    listCADModels.Add(new CADGeometryModel() {
    
    CadCurve=revitLine as Autodesk.Revit.DB.Curve ,LayerName=line.Layer});
                                                    break;
                                                case "AcDbArc":
                                                    Teigha.DatabaseServices.Arc arc = (Teigha.DatabaseServices.Arc)entity;
                                                    double enda, stara;
                                                    if (arc.StartAngle> arc.EndAngle)
                                                    {
    
    
                                                        enda = arc.StartAngle;
                                                        stara = arc.EndAngle;
                                                    }
                                                    else
                                                    {
    
    
                                                        enda = arc.EndAngle;
                                                        stara = arc.StartAngle;
                                                    }
                                                    Autodesk.Revit.DB.Arc revitArc = Autodesk.Revit.DB.Arc.Create(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(new XYZ( arc.Normal.X, arc.Normal.Y, arc.Normal.Z),
                                                        ConverCADPointToRevitPoint(arc.Center)), MillimetersToUnits(arc.Radius), stara, enda);
                                                    listCADModels.Add(new CADGeometryModel() {
    
     CadCurve = revitArc as Autodesk.Revit.DB.Curve, LayerName = arc.Layer });
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
            return listCADModels;
        }

        /// <summary>
        /// 毫米转化成英寸
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private double MillimetersToUnits(double value)
        {
    
    
            return UnitUtils.ConvertToInternalUnits(value, DisplayUnitType.DUT_MILLIMETERS);
        }

        /// <summary>
        /// 将CAD里的点转化为Revit里的点
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        private XYZ ConverCADPointToRevitPoint(Point3d point)
        {
    
    
            return new XYZ(MillimetersToUnits(point.X), MillimetersToUnits(point.Y), MillimetersToUnits(point.Z));
        }
    }
}

属性类补充

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

namespace ReadCADWPF
{
    
    
  public  class CADTextModel
    {
    
    
        public CADTextModel()
        {
    
    
            Location = null;
            Text = "";
            Angel = 0;
            Layer = "";
        }
        public string  Text {
    
     get; set; }
        public XYZ Location {
    
     get; set; }
        public  Double Angel {
    
     get; set; }

        public string Layer {
    
     get; set; }

    }
}

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

namespace ReadCADWPF
{
    
    
  public  class CADGeometryModel
    {
    
    
        public PolyLine CadPolyLine {
    
     get; set; }
        public string LayerName {
    
     get; set; }
        public Curve CadCurve {
    
     get; set; }
    }
}

启动项补充

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

namespace ReadCADWPF
{
    
    
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    public class StartDymo : IExternalCommand
    {
    
    
 
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
    
    
           
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            Reference refer = uidoc.Selection.PickObject(ObjectType.PointOnElement);
            ElementId elementId = doc.GetElement(refer).GetTypeId();
            ReadCADUtils readCADUtils = new ReadCADUtils();
   string file=readCADUtils.GetCADPath(elementId, doc);
            List<CADTextModel> cADTexts = readCADUtils.GetCADTextInfo(file);

            MainWindow mainWindow = new MainWindow(cADTexts);
            mainWindow.ShowDialog();

            return Result.Succeeded;
        }
    }
}

常见问题报错,错误编码e313或e312

方法解释

此插件与addim交互时出现错误,仅调试一次会正常显示,之后便会报错,所以需要重复启动revit才能实现对应的方法,但是集合成为Ribbon时,不会出现这个错误,可以循环使用,只是调试时会比较麻烦,但是正常使用时一切正常
e313问题的报错问题是因为Teigha的类库TD_Mgd_4.02_11.dll,在运行时,自动生成的部分不足以满足程序运行,需要手动导入其他剩余部分。

Revit API翻模基本原理

APII翻模基础原理

读取并绘制线

猜你喜欢

转载自blog.csdn.net/waiting233/article/details/118573464
CAD